int check_builts(char *cmd, char **arg, char **env) { int ret; ret = 0; if (my_strcmp("setenv", arg[0]) == 0) { if (arg == NULL || arg[0] == NULL || arg[1] == NULL || arg[2] == NULL) printf("Usage : setenv [NAME] [VALUE] \n"); else my_setenv(env, arg[1], arg[2]); ++ret; } if (my_strcmp("unsetenv", arg[0]) == 0) { if (arg && arg[0] && arg[1]) my_unsetenv(env, arg[1]); else printf("Usage : unsetenv [NAME] \n"); save_my_env(env); ++ret; } ret += check_next(cmd, arg, env); if (ret == 0) printf("%s : Command not found\n", arg[0]); return (ret); }
/* ** brief: we will try to find the good command ** @env: our env list ** @arr: contain the command and values ** return: 1 if we found our cmd but it's not cd, 0 if the cmd is not found ** and 2 if we have change the dir and 43 if exit */ static int _my_builtin_fun(char **arr, t_list *env) { char *pwd; if (my_strcmp(arr[0], "exit", 0)) return (my_exit(env, arr)); else if (my_strcmp(arr[0], "env", 0)) my_env(env, arr); else if (my_strcmp(arr[0], "cd", 0)) return (my_cd(arr) + 1); else if (my_strcmp(arr[0], "pwd", 0)) { pwd = my_find_element(env, "PWD"); write(1, pwd, my_strlen(pwd)); write(1, "\n", 1); return (1); } else if (my_strcmp(arr[0], "setenv", 0)) my_setenv(env, arr); else if (my_strcmp(arr[0], "unsetenv", 0)) my_unsetenv(env, arr); else return (0); return (1); }
int cond(char **tab, char **path) { int i; i = 0; if (my_strcmp(tab[0], "env") == 0) { my_env(); return (1); } if (my_strcmp(tab[0], "setenv") == 0) { if (tab[1] == NULL) return (my_env()); else if (tab[2] != NULL) return (my_setenv(tab[1], tab[2])); } if (my_strcmp(tab[0], "unsetenv") == 0 && tab[1] != NULL) return (my_unsetenv(tab[1])); if (my_strcmp(tab[0], "exit") == 0) { free(path); exit(EXIT_SUCCESS); } return (0); }
int is_exist(t_tab *tab, t_btree **tree) { if (tab->env != NULL) if (my_get_special_env(tab->env, my_strdup((*tree)->right->stock[1])) != NULL) { my_unsetenv(tab, tree); return (0); } return (0); }
int main (int argc, char *argv[]) { char name[] = "GREET"; char value[] = "bonjour"; assert(getenv(name) == NULL); assert(my_setenv(NULL, value, 0) == -1); assert(my_setenv(name, value, 0) == 0); assert(strcmp(getenv(name), "bonjour") == 0); value[0] = 'B'; value[1] = 'O'; assert(strcmp(getenv(name), "bonjour") == 0); assert(my_setenv(name, "guten tag", 0) == 0); assert(strcmp(getenv(name), "bonjour") == 0); assert(my_setenv(name, "guten tag", 1) == 0); assert(strcmp(getenv(name), "guten tag") == 0); assert(my_unsetenv(NULL) == -1); assert(my_unsetenv(name) == 0); assert(my_setenv("lorem", "ipsum", 0) == 0); assert(getenv(name) == NULL); assert(strcmp(getenv("lorem"), "ipsum") == 0); exit(EXIT_SUCCESS); }
int check_builtins(t_main *list, t_main **env) { if (strcmp(list->str, "cd") == 0) my_cd(list, *env); else if (strcmp(list->str, "env") == 0) show_env(*env); else if (strcmp(list->str, "setenv") == 0) my_setenv(list, env); else if (strcmp(list->str, "unsetenv") == 0) my_unsetenv(list, env); else if (strcmp(list->str, "exit") == 0) my_exit(list); else if (strcmp(list->str, "echo") == 0) my_echo(list); else if (strcmp(list->str, "..") == 0) my_cd(manage_double_dots(&list), *env); else return (0); return (1); }
int my_elem(char **path2, char **str2, char **env) { t_par *par; if (!(par = malloc(sizeof(par)))) return (1); par->a = 0; par->i = 0; par->acs = -1; par->elem = malloc(8096 * sizeof(char)); if (my_exit2(str2) == 2) return (2); if (found_str2(str2, env) == 0) return (0); if (my_setenv(str2, env) == 0) return (0); if (my_unsetenv(str2, env) == 0) return (0); if (my_executable(str2, env) == 0) return (0); my_elem2(path2, str2, env, par); return (3); }