|
@@ -4,6 +4,7 @@
|
|
|
*
|
|
|
* Created on 9 novembre 2018
|
|
|
*/
|
|
|
+#define _POSIX_C_SOURCE 200112L
|
|
|
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
@@ -14,6 +15,7 @@
|
|
|
#include "mysh.h"
|
|
|
#include "execute.h"
|
|
|
#include "command.h"
|
|
|
+#include "ipc.h"
|
|
|
|
|
|
/* --- Extern --- */
|
|
|
extern Error error;
|
|
@@ -24,6 +26,8 @@ char* cmdlist[] = {
|
|
|
"cd",
|
|
|
"exit",
|
|
|
"status",
|
|
|
+ "setenv",
|
|
|
+ "unsetenv",
|
|
|
NULL
|
|
|
};
|
|
|
boolean exitsh = false;
|
|
@@ -125,6 +129,11 @@ int launch_internal_command(Command* cmd) {
|
|
|
result = status(cmd->argc, cmd->argv);
|
|
|
return result;
|
|
|
}
|
|
|
+ //setenv
|
|
|
+ else if (strncmp(cmd->name, cmdlist[3], length) == 0) {
|
|
|
+ result = set_env(cmd->argc, cmd->argv);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
//Aucune commande
|
|
|
else {
|
|
|
return SHELL_FAIL;
|
|
@@ -174,4 +183,50 @@ int status(int argc, char** argv){
|
|
|
printf("%d terminé avec comme code de retour %d\n", last, status_cmd);
|
|
|
}
|
|
|
return EXIT_SUCCESS;
|
|
|
+}
|
|
|
+
|
|
|
+int set_env(int argc, char** argv){
|
|
|
+ char* str, * key, * val;
|
|
|
+ int length, pos = 0;
|
|
|
+ //Verif les arguments
|
|
|
+ if(argc < 2){
|
|
|
+ error.print("too few arguments : 1 required, 0 given\n");
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ if(argc > 2){
|
|
|
+ error.print("too many arguments : 1 required, %d given\n", argc - 1);
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ str = argv[1];
|
|
|
+ length = strlen(str);
|
|
|
+ //Verif que chaine est correcte
|
|
|
+ for(int i = 0; i < length; i++){
|
|
|
+ if(str[i] == '='){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ pos++;
|
|
|
+ }
|
|
|
+ if(pos >= length - 1){
|
|
|
+ error.print("Argument invalide : clef=valeur attendu, %s donnée\n", str);
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ //Decoupe la chaine
|
|
|
+ key = malloc(sizeof(char) * (pos + 1));
|
|
|
+ memset(key, 0, pos + 1);
|
|
|
+ strncpy(key, str, pos);
|
|
|
+ val = str + pos + 1;
|
|
|
+ //Ajoute la chaine en shm
|
|
|
+ if(!add_shm_data(str)){
|
|
|
+ error.print("Erreur interne impossible d'ajouter la variables\n");
|
|
|
+ free(key);
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ //Ajoute sur le système
|
|
|
+ if(setenv(key, val, true) == ERR){
|
|
|
+ addperror("Impossible d'ajouter la variable d'environnement");
|
|
|
+ error.print("Erreur interne impossible d'ajouter la variables\n");
|
|
|
+ free(key);
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ return EXIT_SUCCESS;
|
|
|
}
|