int cd(t_shell *shell, t_tree *tree) { char *pwd; char *path; char *home; if (cd_needed(shell, tree, &pwd, &home) == EXIT_FAILURE) return (EXIT_FAILURE); cd_get_path(tree->argv, &path, pwd, home); if (!path) { if (!(path = get_env(shell->env, "OLDPWD"))) { ft_putmsg(CD_OLDPWD, NULL); return (EXIT_FAILURE); } ft_putendl(path); } if (chdir(path) == -1) return (cd_error(&path, tree->argv[1])); else end_cd(shell, &path, &pwd); ft_del(&path, &pwd, &home); return (EXIT_SUCCESS); }
static t_error_msg_id isvalid(\ char *path, char **arg_v) { struct stat s; if (access(path, F_OK)) return (cd_error(Cdnofound, path, EXIT_SUCCESS)); else if (access(path, R_OK)) return (cd_error(Cdnoright, path, EXIT_SUCCESS)); else if (stat(path, &s)) return (cd_error(Cdstat, path, EXIT_SUCCESS)); if (!IS(S_IFDIR, s.st_mode)) return (cd_error(Cdnodir, path, EXIT_SUCCESS)); if (arg_v[0] && arg_v[1]) return (cd_error(Cdtoomany, NULL, EXIT_SUCCESS)); return (1); }
int ft_cd(char **tab) { if (!tab[1]) { ft_putendl(find_value_envir(g_env, "HOME")); tab[1] = find_value_envir(g_env, "HOME"); if (chdir(tab[1]) == -1) return (cd_error(tab[1])); change_pwd(1); } else if (ft_strcmp("-", tab[1]) == 0) { chdir(find_value_envir(g_env, "OLDPWD")); ft_putendl(find_value_envir(g_env, "OLDPWD")); change_pwd(0); } else if (chdir(tab[1]) == -1) return (cd_error(tab[1])); else change_pwd(0); return (0); }
static void build_cd2(t_data *d, char **av) { if (chdir(av[1]) == -1) cd_error(av[1]); else { if (ft_strncmp(av[1], "..", 2) == 0 && (av[1][2] == '/' || av[1][2] == '\0')) cd_double_dot(d, av[1]); else cd_change_pwd(d, av[1]); } }
int hd_reset() { /****************** HD software reset sequence ******************* ControlRegister (0x3F6)=(0000 1RE0); R=reset, E=0:enable interrupt Strobe R bit from HI to LO; with delay time in between: Write 0000 1100 to ControlReg; delay(); Write 0000 1000 to ControlReg; wait for notBUSY & no error *****************************************************************/ out_byte(0x3F6, 0x0C); delay(); out_byte(0x3F6, 0x08); delay(); if (hd_busy() || cd_error()) { printf("HD reset error\n"); return(BAD); } return 0; // return 0 means OK }
void cd_only(t_data *d, char *name) { char *home; char *pwd; home = ft_getenv("HOME", d->cenv); pwd = ft_getenv("PWD", d->cenv); if (chdir(home) == -1) cd_error("Home directory"); else { ft_setenv(d, "OLDPWD", pwd); ft_setenv(d, "PWD", home); } if (home != NULL) ft_memdel((void **)&home); if (pwd != NULL) ft_memdel((void **)&pwd); (void)name; }
static int cd_write_in_pwd(char **args, char ***env) { char *pwd; char *tmp; pwd = cd_check(args, env); if (chdir(pwd) != -1) { tmp = pwd; if (args[0][0] == '-' && ft_strlen(ft_strrchr(args[0], 'P')) == 1) { pwd = ft_getcwd(tmp, *env); free(tmp); } ft_change_pwds(pwd, env); free(pwd); return (0); } return (cd_error(pwd, args[1])); }
static int cd_search_in_pwd(char **args, char ***env) { char *pwd; char *tmp; pwd = ft_getenv("PWD", *env); if ((tmp = ft_strstr(pwd, args[1])) == NULL) { ft_strerror(ft_strjoin("42sh: cd: string not in pwd: ", args[1])); return (1); } pwd = cd_change_in_pwd(pwd, tmp, args); if (chdir(pwd) != -1) { ft_change_pwds(pwd, env); ft_putendl(pwd); free(pwd); return (0); } return (cd_error(pwd, pwd)); }
int my_cd(t_env *env, char **array) { t_env *tmp; tmp = env->next; if (array[1]) { if (array[1][0] == '/') empty_pwd(env); if (array[1][0] == '-') { print_oldpwd(env); return (my_oldpwd(env)); } if (opendir(array[1]) == NULL) return (cd_error(array[1])); chdir(array[1]); env = my_swap_old(env); env = my_change_pwd(env, array[1], 0); return (0); } env = my_cd_thereturn(env, tmp, array[0] + 2); return (0); }