|
@@ -71,17 +71,17 @@ void remove_pid(pid_list* pl, pid_node* pn) {
|
|
|
if (pn->prev == NULL && pn->next == NULL) {
|
|
|
pl->first = NULL;
|
|
|
pl->last = NULL;
|
|
|
- }
|
|
|
+ }
|
|
|
//Si 1er et non seul
|
|
|
else if (pn->prev == NULL && pn->next != NULL) {
|
|
|
pn->next->prev = NULL;
|
|
|
pl->first = pn->next;
|
|
|
- }
|
|
|
+ }
|
|
|
//Si dernier
|
|
|
else if (pn->next == NULL) {
|
|
|
pn->prev->next = NULL;
|
|
|
pl->last = pn->prev;
|
|
|
- }
|
|
|
+ }
|
|
|
//Sinon si il est au milieu
|
|
|
else {
|
|
|
pn->prev->next = pn->next;
|
|
@@ -118,22 +118,27 @@ int launch_internal_command(Command* cmd) {
|
|
|
if (strncmp(cmd->name, cmdlist[0], length) == 0) {
|
|
|
result = cd(cmd->argc, cmd->argv);
|
|
|
return result;
|
|
|
- }
|
|
|
+ }
|
|
|
//exit
|
|
|
else if (strncmp(cmd->name, cmdlist[1], length) == 0) {
|
|
|
result = exit_sh(cmd->argc, cmd->argv);
|
|
|
return result;
|
|
|
- }
|
|
|
+ }
|
|
|
//status
|
|
|
else if (strncmp(cmd->name, cmdlist[2], length) == 0) {
|
|
|
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;
|
|
|
- }
|
|
|
+ }
|
|
|
+ //unsetenv
|
|
|
+ else if (strncmp(cmd->name, cmdlist[4], length) == 0) {
|
|
|
+ result = unset_env(cmd->argc, cmd->argv);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
//Aucune commande
|
|
|
else {
|
|
|
return SHELL_FAIL;
|
|
@@ -153,7 +158,7 @@ int cd(int argc, char** argv) {
|
|
|
addperror("Erreur chdir()");
|
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
//Sinon on va dans le dossier indiqué par l'utilisateur
|
|
|
else {
|
|
|
if (chdir(argv[1]) == ERR) {
|
|
@@ -166,18 +171,18 @@ int cd(int argc, char** argv) {
|
|
|
//show_current_dir("current working directory is: ", "\n");
|
|
|
}
|
|
|
|
|
|
-int exit_sh(int argc, char** argv){
|
|
|
+int exit_sh(int argc, char** argv) {
|
|
|
exitsh = true;
|
|
|
return EXIT_SUCCESS;
|
|
|
}
|
|
|
|
|
|
-int status(int argc, char** argv){
|
|
|
- if(!(last != -1 && status_cmd != -1)){
|
|
|
+int status(int argc, char** argv) {
|
|
|
+ if (!(last != -1 && status_cmd != -1)) {
|
|
|
error.print("Aucune commande executée\n");
|
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
|
//En fonction du status
|
|
|
- if(result_cmd == SHELL_FAIL){
|
|
|
+ if (result_cmd == SHELL_FAIL) {
|
|
|
printf("%d terminé anormalement\n", last);
|
|
|
} else {
|
|
|
printf("%d terminé avec comme code de retour %d\n", last, status_cmd);
|
|
@@ -185,48 +190,72 @@ int status(int argc, char** argv){
|
|
|
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;
|
|
|
+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 variable\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 variable\n");
|
|
|
+ free(key);
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ return EXIT_SUCCESS;
|
|
|
+}
|
|
|
+
|
|
|
+int unset_env(int argc, char** argv) {
|
|
|
+ //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;
|
|
|
+ }
|
|
|
+ //Supprime de la shm
|
|
|
+ if (!remove_shm_data(argv[1])) {
|
|
|
+ error.print("Erreur interne impossible de supprimer la variable\n");
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ //Supprime du systeme
|
|
|
+ if (unsetenv(argv[1]) == ERR) {
|
|
|
+ addperror("Impossible de supprimer la variable d'environnement");
|
|
|
+ error.print("Erreur interne impossible de supprimer la variable\n");
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+ return EXIT_SUCCESS;
|
|
|
}
|