int particular_cases(t_link *link, int type) { char *tmp; if (type == 0) { if ((tmp = get_var_value(link, "HOME")) != NULL) if (tmp[0] != '\0') update_pwd(link, tmp, tmp); return (42); } if (type == 1) { if ((tmp = get_var_value(link, "OLDPWD")) != NULL) update_pwd(link, tmp, tmp); return (42); } if (type == 2) { if ((tmp = my_strdup("/")) != NULL) update_pwd(link, tmp, tmp); return (42); } return (42); }
void my_cd(t_mysh *mysh) { int ret; char *pwd; pwd = my_strdup("OLDPWD="); pwd = my_strcat_free(pwd, getcwd(NULL, 1000)); /* getcwd a free*/ ret = 0; if (comp_str(mysh->tab_com[0], "cd") == 1) { if (mysh->tab_com[1] && mysh->tab_com[1][0] == '-') ret = chdir(src_env(mysh->env, "OLDPWD")); else if (!mysh->tab_com[1]) ret = chdir(src_env(mysh->env, "HOME")); else ret = chdir(mysh->tab_com[1]); if (ret == -1) { my_error("cd: no such file or directory: "); my_error(mysh->tab_com[1]); my_error("\n"); } else update_pwd(mysh, pwd); } free (pwd); }
int builtin_cd(t_link *link, char **cmd) { char **tab_two; char *pwd; int i; i = 0; if (!cmd[1] || (cmd[1] && !cmd[1][0])) return (particular_cases(link, 0)); if (!my_strcmp(cmd[1], "-")) return (particular_cases(link, 1)); if (!my_strcmp(cmd[1], "/")) return (particular_cases(link, 2)); tab_two = my_explode(cmd[1], '/'); pwd = get_pwd(); while (tab_two[i]) { if (!my_strcmp("..", tab_two[i])) pwd = downgrade_pwd(pwd); else pwd = upgrade_pwd(link, pwd, tab_two[i], i); i++; } update_pwd(link, pwd, cmd[1]); return (TRUE); }