void my_cd2(t_lnv *env, char *path, char *npath, char *pwd) { if (env == NULL) return; pwd = get_value(env, "PWD"); if (path == NULL) npath = get_value(env, "HOME"); else if (pwd == NULL) npath = my_strdup(path); else if (!my_strcmp("-", path) && get_value(env, "OLDPWD") != NULL) { npath = get_value(env, "OLDPWD"); set_value(env, "OLDPWD", pwd); } else { npath = ""; set_oldpwd(env, &pwd, &path, &npath); } if (npath) { set_value(env, "PWD", npath); chdir(npath); } gbgc_free(NULL, npath); }
t_cmd *cd(t_cmd *cmd) { char *home; char buffer[8192]; int ok; set_buffer(buffer, &ok, cmd); if ((size_wordtab(cmd->tabx)) > 1) if ((my_strcmp(cmd->tabx[1], "-")) == 0) cmd = cd_oldpwd(cmd); else ok = chdir(cmd->tabx[1]); else { if ((home = get_info("HOME", cmd->lenv)) == NULL) { cmd->retour = 1; my_fdputstr("bash: cd: HOME not set\n", cmd->fdout); return (cmd); } ok = chdir(home); } if (ok < 0) error_cd(cmd->tabx, home, cmd); else if (ok >= 0 && cmd->retour == 0) set_oldpwd(cmd, buffer); return (cmd); }
static void handle_cd_to(t_state *state, char *cd_to) { struct stat statt; if (!is_exist_cmd(cd_to)) print_no_such_file_or_dir(state, "cd", cd_to); else if (stat(cd_to, &statt) && !S_ISDIR(statt.st_mode)) print_not_a_directory(state, "cd", cd_to); else if (!is_exec_cmd(cd_to)) print_permission_denied(state, "cd", cd_to); else { set_oldpwd(state); set_pwd(state, cd_to); } }