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); }
void ft_init(void) { char buf[1024]; char histo_path[1024] = {0}; ioctl(1, TIOCGWINSZ, &g_ws); signal(SIGWINCH, ft_resize); g_env.alias_lst = NULL; g_env.quote_wait = 0; signal(SIGINT, SIG_IGN); g_env.pid_list = NULL; g_env.cut = NULL; g_env.histo = NULL; ft_get_alias_list(); if (tgetent(buf, ft_getenv(g_env.env, "TERM")) == -1) exit(EXIT_FAILURE); tcgetattr(0, &g_env.term); g_env.term.c_lflag &= ~(ICANON | ECHO | ISIG); g_env.term.c_cc[VMIN] = 1; g_env.term.c_cc[VTIME] = 0; tcsetattr(0, 0, &g_env.term); ft_strcpy(histo_path, ft_getenv(g_env.env, "HOME")); ft_strcat(histo_path, "/.42sh_histo"); g_env.histo_fd = open(histo_path, O_CREAT | O_RDWR, 0777); ft_get_history(); }
char *replace_env(char **env, char *cmd, int lret) { int size; char *str; size = replace_size_env(env, cmd, lret); size += replace_size_tilde(cmd, ft_getenv(env, "HOME")); str = ft_strnew(size + 1); size = 0; while (*cmd) { cmd = skip_quotes(cmd, &size); if (*cmd == '$') size += sub_replace_env(env, &cmd, &str[size], lret); else if (*cmd == '~') { size += replace_tilde(ft_getenv(env, "HOME"), &str[size]); cmd++; } else { str[size++] = *cmd; cmd++; } } str[size] = 0; return (str); }
char *cd_cleanpath(char *path) { t_dlist *lst; t_dlist *clean; char cpath[2048]; char *pref; if (!ft_strcmp("-", path)) { if (!(path = ft_getenv("OLDPWD"))) cd_exit("OLDPWD needed in the environment"); return (ft_strdup(path)); } else if (*path != '/') { if (!(pref = ft_getenv("PWD"))) cd_exit("PWD needed in the environment"); ft_sprintf(cpath, "%s/%s/", pref, path); } else ft_sprintf(cpath, "%s/", path); if (!(lst = makelst(cpath))) return (NULL); if (!(clean = ft_dlstmap(lst, cleanlst))) return (NULL); ft_dlstdel(&lst, NULL); clean = cleanlstparent(clean); return (convertlst(&clean)); }
static void ft_change_part2(char **args, char *env_pwd, t_data *st) { char *path; char *tmp; if (args[1] && ft_strcmp(args[1], "-") == 0) { path = ft_strjoin(env_pwd, ft_getenv(st->envtab, "OLDPWD")); ft_putendl(ft_getenv(st->envtab, "OLDPWD")); set_var_env(st, ft_strjoin("OLDPWD=", getcwd(NULL, 1024))); chdir(path); } else if (args[1] && args[1][0] == '~') { tmp = ft_getenv(st->envtab, "HOME"); if (tmp == NULL) path = "/"; else { path = ft_strjoin(ft_getenv(st->envtab, "HOME"), "/"); path = ft_strjoin(path, &(args[1][1])); } chdir(path); } }
static int check_right(char **path, char **args, t_data *st) { struct stat stats; if (stat(*path, &stats) == -1) { ft_putstr("cd: no such file or directory: "); ft_putendl(args[1]); *path = ft_getenv(st->envtab, "PWD"); } else if ((stats.st_mode & S_IRUSR) == 0) { ft_putstr("cd: permission denied: "); ft_putendl(args[1]); *path = ft_getenv(st->envtab, "PWD"); } else if ((stats.st_mode & S_IFDIR) == 0) { ft_putstr("cd: not a directory: "); ft_putendl(args[1]); *path = ft_getenv(st->envtab, "PWD"); } else return (0); return (1); }
int sh_cd(t_data *st) { char *env_pwd; char *path; int ret; ret = 0; env_pwd = ft_strjoin(ft_getenv(st->envtab, "PWD"), "/"); if (st->cmd[1] && (ft_strcmp(st->cmd[1], "-") == 0 || st->cmd[1][0] == '~')) ft_change_part2(st->cmd, env_pwd, st); else if (st->cmd[1]) { path = ft_strjoin(env_pwd, st->cmd[1]); ret = check_right(&path, st->cmd, st); set_var_env(st, ft_strjoin("OLDPWD=", getcwd(NULL, 1024))); path = (st->cmd[1][0] == '/' ? st->cmd[1] : path); chdir(path); } else { path = ft_getenv(st->envtab, "HOME"); set_var_env(st, ft_strjoin("OLDPWD=", getcwd(NULL, 1024))); chdir(path ? path : "/"); } free(env_pwd); set_var_env(st, ft_strjoin("PWD=", getcwd(NULL, 1024))); return (ret); }
void ft_change_pwds(char *pwd, char ***env) { char **tmp; if ((tmp = (char**)malloc(sizeof(char*) * 4)) != NULL) { tmp[0] = ft_strdup("setenv"); tmp[3] = NULL; if (ft_getenv("OLDPWD", *env)) { tmp[1] = ft_strdup("OLDPWD"); tmp[2] = ft_getenv("PWD", *env); ft_setenv(tmp, env); free(tmp[1]); } if (ft_getenv("PWD", *env)) { tmp[1] = ft_strdup("PWD"); tmp[2] = ft_strdup(pwd); ft_setenv(tmp, env); } ft_free(&tmp); free(tmp); } }
void prompt(void) { int i; char *logname; char *pwd; char *home; i = 0; logname = NULL; pwd = NULL; home = NULL; tputs(tgoto(tgetstr("ch", NULL), 0, 0), 1, tputs_putchar); ft_putstr("\033[40m"); if ((logname = ft_getenv("LOGNAME"))) ft_putstr(logname); ft_putstr("\033[m\033[44m\033[30m"); if ((pwd = ft_getenv("PWD")) && (home = ft_getenv("HOME")) && !ft_strncmp(pwd, home, ft_strlen(home))) { ft_putstr("~"); i = ft_strlen(home); } if (pwd) ft_putstr(pwd + i); print_error(logname, pwd, home); }
void ft_prompt(char **envp) { char *pwd; char *home; char *user; int len; if ((user = ft_getenv(envp, "USER"))) { ft_putstr(user); ft_putstr(":"); } pwd = ft_getenv(envp, "PWD"); home = ft_getenv(envp, "HOME"); if (pwd && home) { len = ft_strlen(home); if (ft_strnequ(pwd, home, len)) { ft_putstr("~"); ft_putstr(&pwd[len]); } else ft_putstr(pwd); } envp++; ft_putstr("> "); }
void manage_cd(t_group *grp) { struct stat s_buf; char *path; if (grp->cmd[1] == NULL) { path = ft_getenv(grp, "HOME") ? ft_getenv(grp, "HOME") : SDUP("HOME has been unset from environnement !"); } else if (grp->cmd[1][0] == '-' && grp->cmd[1][1] == false) { if ((path = ft_getenv(grp, "OLDPWD")) != NULL) { ft_putstr("> "); ft_putendl(path); } else path = SDUP("OLDPWD has been unset from environnement !"); } else path = SDUP(grp->cmd[1]); lstat(path, &s_buf); cderr_pwd(grp, path, s_buf); }
void ft_loop(t_env *env) { char *line; char **cmds; char **args; int status; status = 1; ft_getenv("USER", &env->username, &env); ft_printf("\033c"); while (status) { ft_printf("[%s]$>", env->username); get_next_line(0, &line); cmds = ft_splitcommands(line); while (*cmds) { args = ft_split(*cmds); status = ft_execute(args, &env); if (status == 0) break ; cmds++; } } free(env->username); }
int ft_change_pwd(char *path, t_cmd *cmd) { struct stat infos; int error; int ret; error = 0; ret = 0; ret = stat(path, &infos); if (access(path, F_OK) != 0) error = NOTEXIST; else if (!(S_IFDIR == (infos.st_mode & S_IFMT))) error = NOTDIR; else if (access(path, X_OK) != 0) error = NOTACCESS; if (ret == -1 && !error) error = UNDEF; if (error) ft_put_error(error, cmd->args[1]); else { ft_update_env(cmd->envp, "OLDPWD", ft_getenv(*cmd->envp, "PWD")); ft_update_env(cmd->envp, "PWD", path); error = chdir(path); } free(path); return (error != 0); }
int comp_right_arrow(int j, char *buff, t_data *st) { char *term_name; int tmp; term_name = ft_getenv(st->envtab, "TERM"); if (term_name == NULL) term_name = "xterm-256color"; tgetent(NULL, term_name); if (j == (int)ft_strlen(buff)) return (j); if ((j + 2) % (tgetnum("co") - 1) == 0) { tputs(tgetstr("do", NULL), 1, ft_outc); tmp = tgetnum("co"); while (tmp != 1) { tputs(tgetstr("le", NULL), 1, ft_outc); tmp--; } } else tputs(tgetstr("nd", NULL), 1, ft_outc); return (j + 1); }
static void ft_get_alias_list(void) { int i; int fd; char *tmp; char *path; tmp = ft_strjoin(ft_getenv(g_env.env, "HOME"), "/"); path = ft_strjoin(tmp, ".bashrc"); fd = open(path, O_RDONLY); free(tmp); tmp = NULL; while (ft_gnl(fd, &tmp) > 0) { i = 0; while (*(tmp + i) == ' ' || *(tmp + i) == '\t') i++; if (!ft_strncmp(tmp + i, "alias ", 6)) ft_add_node(tmp + i); } if (g_env.alias_lst) g_env.alias_lst = g_env.alias_lst->start; free(path); free(tmp); }
int completion_search_path(t_comp *comp) { t_env *env; t_env_list *path; char **paths; env = &get_shell_data()->env; if (!(path = ft_getenv(env->env_list, "PATH"))) return (0); if (!(paths = ft_strsplit(path->value, ':'))) return (MALLOC_FAIL); if (ft_tab_len(paths) > MAX_PATH_COMPONENTS) { ft_dprintf(STDERR_FILENO, "42sh: Max path components reached"); reset_buffer(&get_shell_data()->input); ft_free_string_tab(&paths); return (1); } if ((comp->matches = open_path_dirs(comp, paths)) == NULL && comp->nb_matches == -1) { ft_free_string_tab(&paths); return (MALLOC_FAIL); } ft_free_string_tab(&paths); return (0); }
int main(int ac, char **av, char **ep) { char *line; char *prompt; t_lxem *lex; prompt = (ac < 2 ? "" : av[1]); g_ftenviron = ep; while (42) { line = ft_readline(prompt, (RL_GECHO | RL_GHISTORY | RL_GSAVE | RL_GLOAD), ft_getenv(ep, "TERM")); if (line) { if (!ft_strcmp(line, "exit")) { ft_memdel((void **)&line); break ; } if ((lex = ft_lexer(line))) lx_del(&lex); ft_memdel((void **)&line); } } ft_atexit(EXIT_SUCCESS, av[0], "main: Success"); return (0); }
int search_paths(t_env *e) { t_search env; env.paths = ft_strsplit(ft_getenv("PATH", e), ':'); env.y = 0; while (env.y > -1 && env.paths[env.y] != 0) { env.dirp = opendir(env.paths[env.y]); while ((env.entry = readdir(env.dirp)) != NULL) { if (strcmp(env.entry->d_name, e->args[0]) == 0) { env.str = ft_strjoin(env.paths[env.y], "/"); run_exec(ft_strjoin(env.str, env.entry->d_name), e); free(env.str); env.str = NULL; env.y = -2; } } closedir(env.dirp); env.y++; } free_tab(env.paths); free(env.paths); env.paths = NULL; return (env.y); }
static int get_env_variable(t_expander *exp, char *s) { t_env_list *env_var; int len; t_bsh *const bsh = get_shell_data(); len = 0; while (ft_isalnum(*(s + len)) || *(s + len) == '_') len++; if (!len) return (append(exp)); if (!(s = ft_strndup(exp->tmp + 1, len))) return (MALLOC_FAIL); if ((env_var = ft_getenv(bsh->env.env_list, s))) { exp->buffer_len += ft_strlen(env_var->value); while (exp->buffer_len >= exp->buffer_size) if (realloc_exp_buffer(exp) == MALLOC_FAIL) { ft_strdel(&s); return (MALLOC_FAIL); } ft_strcat(exp->buffer, env_var->value); } ft_strdel(&s); exp->tmp += len; return (0); }
void ft_kill(int sig) { if (!sig) return ; if (g_env.in_exec) { kill(g_env.thread, SIGKILL); ft_printf("%$\n42sh: Killed > %s%$\n", INFOS_CLR , g_env.in_exec, C_RESET); g_env.in_exec = NULL; if (g_env.pid_list) ft_got_node_nbr(); } else { if (g_env.histo) { while (g_env.histo->next) g_env.histo = g_env.histo->next; } g_env.in_histo = 0; ft_printf("%$\n%s 42sh (%T)%s "\ , PROMPT_CLR, ft_getenv(g_env.env, "USER"), "%"); } }
char *cd_currentrep(void) { char *rep; if (!(rep = ft_getenv("HOME"))) cd_exit("HOME needed in the environment"); return (rep); }
void putprompt(void) { char *info; char *pwd; char *tmp; char host[1024]; ft_bzero(host, 1024); info = ft_getenv("USER"); ft_printf("\x1B[32m%s\033[0m:\x1B[34m", info); gethostname(host, 1023); pwd = ft_getenv("PWD"); info = get_last(pwd); tmp = get_host(host); ft_printf("%s\x1B[31m @ \x1B[33m%s\033[0m ~> ", tmp, info); ft_strdel(&info); ft_strdel(&tmp); }
static char *ft_get_cd_path(t_options *options, t_cmd *cmd, int i) { char *tmp; tmp = NULL; if (ft_optisset(options, '-') && (tmp = ft_getenv(*cmd->envp, "OLDPWD"))) return (ft_strdup(tmp)); else if (!cmd->args[i]) { if ((tmp = ft_getenv(*cmd->envp, "HOME")) && !ft_strequ(tmp, "")) ft_strtabadd(&cmd->args, ft_strdup(tmp)); else return (NULL); } if (cmd->args[i][0] == '/') return (ft_strdup(cmd->args[i])); else return (ft_check_in_cdpath(cmd->args[i], *cmd->envp)); }
static int build_cd_check_for_env(t_data *d) { char *home; char *pwd; char *oldpwd; home = ft_getenv("HOME", d->cenv); oldpwd = ft_getenv("OLDPWD", d->cenv); pwd = ft_getenv("PWD", d->cenv); if (oldpwd == NULL || pwd == NULL || home == NULL) { ft_putstr_fd("Cannot run `cd' without HOME or OLDPWD, PWD\n", 2); d->current->exedone = 1; free_vars(home, pwd, oldpwd); return (0); } free_vars(home, pwd, oldpwd); return (1); }
static char *ston_pwd(char *s) { static char *t = NULL; if (s != NULL) t = s; else if (t == NULL) t = ft_getenv("PWD"); return (t); }
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 void ft_check_var2(char **av) { if (!ft_getenv(g_env.env, "OLDPWD")) { ft_strcpy(av[1], "OLDPWD"); ft_strcpy(av[2], ft_getenv(g_env.env, "HOME")); ft_builtin(av); } if (!ft_getenv(g_env.env, "PWD")) { ft_strcpy(av[1], "PWD"); ft_strcpy(av[2], ft_getenv(g_env.env, "HOME")); ft_builtin(av); ft_strcpy(av[0], "cd"); ft_strcpy(av[1], ft_getenv(g_env.env, "PWD")); free(av[2]); av[2] = '\0'; ft_builtin(av); } }
void shlvl(t_group *grp) { char *shlvl; int lvl; lvl = ft_atoi(ft_getenv(grp, "SHLVL")); lvl++; shlvl = ft_itoa(lvl); shlvl = JOIN("SHLVL=", shlvl); insert_env(grp, shlvl); }
int main(int ac, char **av) { int n; int opt; char *oldrep; char *rep; char *tmp; char msg[2048]; ft_init(av[0]); opt = ft_getopt(ac, av, "LP"); if (!(n = ft_countarg(ac, av))) oldrep = cd_currentrep(); else oldrep = av[ac - n]; if (!(rep = cd_cleanpath(oldrep))) ft_exit(1, "Memory allocation fail"); if (CD_ISO(opt, CD_O_P) && CD_ISO(opt, CD_O_L)) return (cd_usage()); tmp = getcwd(NULL, 0); if (access(rep, F_OK)) { ft_sprintf(msg, "No such directory: %s", oldrep); ft_error(msg); return (-1); } if (chdir(rep)) { ft_sprintf(msg, "Permission denied: %s", oldrep); ft_error(msg); return (-1); } ft_setenv("OLDPWD=", ft_getenv("PWD")); if (CD_ISO(opt, CD_O_P)) ft_setenv("PWD=", tmp); else ft_setenv("PWD=", rep); free(tmp); ft_printf("%s\n", ft_getenv("PWD")); return (0); }
static void ft_check_var1(char **av) { if (!ft_getenv(g_env.env, "PATH")) { ft_strcpy(av[1], "PATH"); ft_strcpy(av[2], "/usr/local/bin:/usr/bin:/bin:/usr/"\ "sbin:/sbin:/opt/X11/bin:/usr/texbin"); ft_builtin(av); } if (!ft_getenv(g_env.env, "USER")) { ft_strcpy(av[1], "USER"); ft_strcpy(av[2],"unknow_user"); ft_builtin(av); } if (!ft_getenv(g_env.env, "HOME")) { ft_strcpy(av[1], "HOME"); ft_strcpy(av[2],"/"); ft_builtin(av); } }