static int s_in_path_env(char **aexe) { char **list_path; char *tmp; size_t i; int ret; list_path = ft_strsplit(sh_getenv(sgt_sh()->env, "PATH"), ':'); if (list_path == NULL) return (0); ret = 0; i = 0; tmp = NULL; while (list_path[i]) { ft_asprintf(&tmp, "%s/%s", list_path[i], *aexe); if (s_check_if_access(tmp)) { ft_asprintf(aexe, "%s", tmp); ret = 1; break ; } ++i; } ft_strtable_clear(&list_path); ft_strdel(&tmp); return (ret); }
int path_finding(t_tree *current, char **all_path, char *name) { if (sgt_sh()->env == NULL || !current->args || !current->args[0]) return (g_return); if (current->args[0][0] != '.' && current->args[0][0] != '/') { all_path = ft_strsplit(sh_getenv(sgt_sh()->env, "PATH"), ':'); if (all_path == NULL) return (g_return); if ((name = get_executable_path(all_path, current->args[0], 0)) \ && (g_return = 1)) current->full_path = name; ft_strtable_clear(&all_path); } else if ((current->args[0][0] == '.' || current->args[0][0] == '/') && ft_strcmp(current->args[0], "..")) { if (check_access(current->args[0]) != 0 && (g_return = 1)) { if (current->args[0]) current->full_path = ft_strdup(current->args[0]); current->args = update_name(current->args, 0, 0); } } if (!ft_strcmp(current->args[0], "..")) return (-2); return (g_return); }
static int lgetenv(lua_State *L) { const char *s = sh_getenv(luaL_checkstring(L, 1)); if (s) lua_pushstring(L, s); else lua_pushnil(L); return 1; }
int cd_to_env_var(char *var) { char *path; if ((path = sh_getenv(var)) != NULL) { return change_dir(path); } else shell_warn("cd: %s not set.\n", var); return -1; }
int do_chdir(char *path) { char *pwd; if (chdir(path) != 0) { shell_warn("cd: chdir() failed.\n"); return -1; } if ((pwd = sh_getenv("PWD")) != NULL) { sh_setenv("OLDPWD", pwd); } else sh_unsetenv("OLDPWD"); set_pwd(); return 0; }
void replace_tilde(t_lexer *node, char **env, int pos) { char *home_ptr; char *new_cmd; home_ptr = sh_getenv("HOME", env); new_cmd = ft_strnew(ft_strlen(node->str) - 1 + ft_strlen(home_ptr)); if (new_cmd == NULL) ft_exit(PROG, 1); ft_strncat(new_cmd, node->str, (pos)); ft_strcat(new_cmd, home_ptr); ft_strcat(new_cmd, node->str + (pos + 1)); free(node->str); free(home_ptr); node->str = new_cmd; }
void sh_and_die(char **argv, char **env, int start) { char *cmd; int shlvl; shlvl = 0; cmd = NULL; sgt_sh()->env = sh_cpyenv(env); if ((shlvl = ft_atoi(sh_getenv(sgt_sh()->env, "SHLVL"))) >= 0) sh_setenvsep(&sgt_sh()->env, "SHLVL", ft_itoa((shlvl - 1))); cmd = get_cmd(argv, start); if (cmd && cmd[0]) { s_processor(cmd); ft_strdel(&cmd); } }
void open_config_file() { char path[4096]; char *home_path; if ((home_path = sh_getenv("HOME")) != NULL) { snprintf(path, sizeof(path), "%s/%s", home_path, SHAFT_CONFIG_FILE); if ((sh.read_cmd_fd = open(path, O_RDONLY|O_CREAT, 0644)) != -1) { read_cmd_loop(); close(sh.read_cmd_fd); sh.read_cmd_fd = 0; } else shell_warn("Could not open configuraton file: '%s'.\n", path); } else shell_warn("No '$HOME' environment variable, \ could not load configuration file.\n"); }
void init_prompt() { char *user; char *host; char prompt[255]; char c; uid_t uid; c = ((uid = geteuid()) == 0 ? '#' : '$' ); if ((user = get_username()) != NULL) { if ((host = sh_getenv("HOSTNAME")) != NULL) { snprintf(prompt, sizeof(prompt), "%s@%s%c ", user, host, c); } else snprintf(prompt, sizeof(prompt), "%s%c ", user, c); } else snprintf(prompt, sizeof(prompt), "%c ", c); sh.prompt = strdup(prompt); }
int main(int c, const char *v[]) { register int rflag = ttyflg; /* initialise storage allocation */ blokinit(); stdsigs(); setbrk(BRKINCR); addblok((POS) 0); /* set names from userenv */ sh_getenv(); /* look for restricted */ /* if(c>0 && any('r', *v) ) { rflag=0 ;} */ /* look for options */ dolc = options(c, v); if (dolc < 2) flags |= stdflg; if ((flags & stdflg) == 0) dolc--; dolv = v + c - dolc; dolc--; /* return here for shell file execution */ setjmp(subshell); /* number of positional parameters */ assnum(&dolladr, dolc); cmdadr = (char *)dolv[0]; /* set pidname */ assnum(&pidadr, getpid()); /* set up temp file names */ settmp(); /* default ifs */ dfault(&ifsnod, sptbnl); if ((beenhere++) == FALSE) { /* ? profile */ if (*cmdadr == '-' && (input = pathopen(nullstr, profile)) >= 0) { exfile(rflag); flags &= ~ttyflg; ; } if (rflag == 0) { flags |= rshflg; } /* open input file if specified */ if (comdiv) { estabf(comdiv); input = -1; } else { input = ((flags & stdflg) ? 0 : chkopen(cmdadr)); comdiv--; ; } // } else { *execargs=(char *)dolv; /* for `ps' cmd */ ; } exfile(0); done(); }