int change_dir_serv(t_serv *serv, char *cmd) { char **tab; char *tmp; tab = ft_strsplit(cmd, ' '); if (!tab[1]) cd_to_root(serv); else { tmp = ft_strjoinwsep(serv->pwd, tab[1], '/'); if (chdir(tmp) == -1) { free(tmp); send(serv->sock, CD_FAIL, ft_strlen(CD_FAIL), 0); return (0); } free(tmp); free(serv->pwd); serv->pwd = getcwd(NULL, 0); if (check_cd(serv)) send(serv->sock, CD_OK, ft_strlen(CD_OK), 0); } return (0); }
int cmd_cd(char **tab) { int size; char *pwd; pwd = ft_strnew(200); if ((size = ft_size_tab(tab)) > 3) return (ft_err(ERCDMULT, tab[1])); else if (size == 2) pwd = ft_strcpy(pwd, ft_getenv("HOME", g_env)); else { if (tab[1][0] == '~') pwd = ft_strcpy(pwd, ft_getenv("HOME", g_env)); else if (tab[1][0] == '-') pwd = ft_strcpy(pwd, ft_getenv("OLDPWD", g_env)); else if (tab[1][0] != '/') pwd = ft_strcpy(pwd, ft_getenv("PWD", g_env)); if (!ft_strlen((pwd = build_pwd(tab[1], pwd)))) pwd[0] = '/'; } if (check_cd(pwd) == 0) return (1); if (chdir(pwd) != -1) refresh_pwd(pwd); free(pwd); return (1); }
static int check_cd(char *pwd) { struct stat buf; char *tmp; int err; err = 0; if (ft_strchr((pwd + 1), '/')) { tmp = ft_strdup(pwd); tmp = ft_path_back(tmp, '/'); if (check_cd(tmp) == 0) { free(tmp); return (0); } free(tmp); } lstat(pwd, &buf); if (access(pwd, F_OK) == -1) err = ERCDNOTFOUND; else if (!(buf.st_mode & S_IFDIR)) err = ERCDNOTDIR; else if (access(pwd, R_OK) == -1) err = ERCDACCESS; return ((!err) ? 1 : ft_err(err, pwd)); }
int dir_with_slash(t_mysh *ptr) { int flag; if ((flag = check_cd(ptr, 0)) == 3) return (0); if (flag == 1) { if (my_getenv("PWD", ptr) == 0) return (0); free(ptr->previous); if ((ptr->previous = my_strdup(ptr->recup_getenv)) == 0) return (0); if (chdir(ptr->param[1]) == -1) return (0); modif_path(ptr, "PWD", ptr->param[1]); return (1); } return (0); }
int dir_with_slash(t_cd *ptr, char *option, char ***env) { int flag; if ((flag = check_cd(ptr, 0, env, option)) == 3) return (0); if (flag == 1) { if (my_recup_var_env("PWD", ptr, env) == 0) return (0); free(ptr->previous); if ((ptr->previous = my_strdup(ptr->recup_getenv)) == 0) return (0); if (chdir(option) == -1) return (0); modif_path(env, "PWD", option); return (1); } return (0); }
int dir_no_slash(t_mysh *ptr) { int flag; char *line; if ((flag = check_cd(ptr, 0)) == 3) return (0); if (flag == 1) { if (my_getenv("PWD", ptr) == 0) return (0); free(ptr->previous); if ((ptr->previous = my_strdup(ptr->recup_getenv)) == NULL) return (0); if ((line = my_strcat_three(ptr->recup_getenv, "/", ptr->param[1])) == 0) return (0); if (chdir(line) == -1) return (2); if (for_two_point(ptr, &line) == 0) return (0); return (1); } return (0); }
int dir_no_slash(t_cd *ptr, char ***env, char *option) { int flag; char *line; if ((flag = check_cd(ptr, 0, env, option)) == 3) return (0); if (flag == 1) { if (my_recup_var_env("PWD", ptr, env) == 0) return (0); free(ptr->previous); if ((ptr->previous = my_strdup(ptr->recup_getenv)) == NULL) return (0); if ((line = my_strcat_three(ptr->recup_getenv, "/", option)) == 0) return (0); if (chdir(line) == -1) return (2); if (for_two_point(ptr, &line, option, env) == 0) return (0); return (1); } return (0); }
// cmdline 파싱 // exit 체크 // cd 명령 수행 // execute_cmdgrp() 호출 void execute_cmdline(char* cmdline) { int i = 0; int count = 0; int cd_ok = 0; count = makelist(cmdline, ";", cmdgrps, MAX_CMD_GRP); for(i = 0; i < count; ++i) { // exit 명령 check_exit(cmdgrps[i]); // cd 명령 cd_ok = check_cd(cmdgrps[i]); if(cd_ok) { continue; } switch(fork()) { case -1: fatal("fork error"); break; case 0: // child process execute_cmdgrp(cmdgrps[i]); exit(0); default: // parent process wait(NULL); fflush(stdout); } } }