char mysh_cd(t_arg *targ, UNUSED char *path) { int i; if ((i = cd_tiret(targ)) == 1) return (1); if (i != 2 && !access(targ->wtab[1], F_OK)) { if (!access(targ->wtab[1], R_OK)) { if ((i = find_env(targ->env, "PWD=")) < 0) if ((targ->env = add_env(targ->env, "PWD", '=', "/")) == NULL) return (1); if ((targ->pwd = get_flagss(targ->env,"PWD")) == NULL || ((i = find_env(targ->env, "PWD=")) < 0)) return (1); free(targ->env[i]); if ((targ->env[i] = concat_str("PWD", '=', targ->wtab[1])) == NULL || chdir(targ->wtab[1]) < 0) return (1); } else write(2, "Error while accessing path, no rights.\n", 40); } else if (i != 2) write(2, "No such file or directory.\n", 28); return (0); }
char cd_home(t_arg *targ) { int i; char *str3; if (targ->wtab[1] == NULL) { if ((i = find_env(targ->env, "PWD=")) < 0) if ((targ->env = add_env(targ->env, "PWD", '=', "/")) == NULL) return (1); if ((i = find_env(targ->env, "HOME=")) < 0) if ((targ->env = add_env(targ->env, "HOME", '=', "/")) == NULL) return (1); if ((targ->pwd = get_flagss(targ->env, "PWD")) == NULL || (i = find_env(targ->env, "PWD=")) < 0) return (1); if ((str3 = get_flagss(targ->env, "HOME")) == NULL || (targ->env[i] = concat_str("PWD", '=', str3)) == NULL || (chdir(str3) < 0)) return (1); free(str3); return (2); } return (0); }
void ft_cd(t_list *lst, char **av) { char *path; t_env *e; int show; path = NULL; show = 0; if (!av[1] && (e = find_env(lst, "HOME"))) path = ft_strdup(e->value); else if (!av[1]) return (err_cd_args()); else if (av[2] != NULL && !(path = ft_cd_replace(lst, av))) return ; else if (!av[2] && !ft_strcmp(av[1], "~") && (e = find_env(lst, "HOME"))) path = ft_strreplace("~", e->value, av[1], 0); else if (!av[2] && !ft_strcmp(av[1], "-") && (e = find_env(lst, "OLDPWD"))) { path = ft_strdup(e->value); show = 1; } else if (av[1][0] != '/' && !av[2] && (e = find_env(lst, "PWD"))) path = ft_strjoin(e->value, ft_strjoin("/", av[1])); else if ((av[1][0] == '/' && !av[2]) || (av[1] && !av[2])) path = ft_strdup(av[1]); ft_cd_do(lst, path, show); free(path); }
static void ft_cd_do(t_list *envlst, char *path, int show) { char *buf; int len; int t; buf = NULL; t = test_pwd(path); if (t == 1) { chdir(path); len = ft_strlen(path); buf = getcwd(buf, len); if (!buf) buf = clear_path(path); if (find_env(envlst, "PWD")) update_env(envlst, "OLDPWD", find_env(envlst, "PWD")->value); update_env(envlst, "PWD", buf); if (show) ft_putendl(remove_home(envlst, buf)); } else if (t == -1) err_cd_perm(remove_home(envlst, path)); else err_cd_no_file(remove_home(envlst, path)); }
int builtin_unsetenv(char **av) { int i; int j; int index; char **tab; i = 0; j = 0; if (!av[1]) return (-1); if ((index = find_env(av[1])) == -1) return (0); tab = (char **)ft_memalloc(sizeof(char *) * ft_tablen(g_handler.env)); if (!tab) return (-42); while (g_handler.env[i]) { if (index != i) tab[j++] = ft_strdup(g_handler.env[i]); i++; } ft_free_tab(&g_handler.env); g_handler.env = tab; return (0); }
/* If no specified variable exist, safe_set_env will create one, else it only modify the value */ static void _safe_set_env(const char *name, unsigned long long value, enum envtype Type, void* ptr, size_t size ) { struct env_struct *var; var = find_env(name); if (!var) { if (Type == kEnvPtr) { _set_env(name, 0, kEnvPtr,ptr,size); } else if (Type == kEnvValue) { _set_env(name, value, kEnvValue,0,0); } } else if (var->Type != Type) { return; } else { if (Type == kEnvValue) var->value = value; else if (Type == kEnvPtr) _re_set_env_copy(var,ptr,size); } return; }
int init_termios(t_term *term, char **env) { term->fd = STDIN_FILENO; if (!isatty(STDIN_FILENO)) return (-1); term->term_name = find_env(env, "TERM"); if (!term->term_name) term->term_name = "xterm-256color"; if (tgetent(NULL, term->term_name) < 1) { term->term_name = NULL; return (-1); } if ((tcgetattr(term->fd, &(term)->term) == -1) || (tcgetattr(term->fd, &(term)->term_bak) == -1)) { ft_putendl_fd("Termios failed to init", 3); return (-1); } term->term.c_cc[VMIN] = 1; term->term.c_cc[VTIME] = 0; term->term.c_lflag &= ~(ICANON | ECHO); ioctl(term->fd, TIOCGWINSZ, &(term)->ws); if (tcsetattr(term->fd, 0, &(term)->term) == -1) return (-1); return (0); }
/** * If the ENV is not exist, create it. * @see flash_write_env * * @param key ENV name * @param value ENV value * * @return result */ static FlashErrCode create_env(const char *key, const char *value) { FlashErrCode result = FLASH_NO_ERR; FLASH_ASSERT(key); FLASH_ASSERT(value); if (*key == NULL) { FLASH_INFO("Flash ENV name must be not empty!\n"); return FLASH_ENV_NAME_ERR; } if (strstr(key, "=")) { FLASH_INFO("Flash ENV name can't contain '='.\n"); return FLASH_ENV_NAME_ERR; } /* find ENV */ if (find_env(key)) { FLASH_INFO("The name of \"%s\" is already exist.\n", key); return FLASH_ENV_NAME_EXIST; } /* write ENV at the end of cache */ result = write_env(key, value); return result; }
void cupsdSetEnv(const char *name, /* I - Name of variable */ const char *value) /* I - Value of variable */ { int i; /* Index into environent array */ /* * If "value" is NULL, try getting value from current environment... */ if (!value) value = getenv(name); if (!value) return; /* * Do not allow dynamic linker variables when running as root... */ if (!RunUser && (!strncmp(name, "DYLD_", 5) || !strncmp(name, "LD_", 3))) return; /* * See if this variable has already been defined... */ if ((i = find_env(name)) < 0) { /* * Check for room... */ if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0]))) { cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdSetEnv: Too many environment variables set!"); return; } i = num_common_env; num_common_env ++; } /* * Set the new environment variable... */ cupsdSetStringf(common_env + i, "%s=%s", name, value); cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s", common_env[i]); }
void re_set_env(const char *name , unsigned long value) { struct env_struct *var; var = find_env(name); if (!var || (var->Type != kEnvValue)/* kEnvPtr currently unsupported */) { printf("Unable to reset environement value variable %s\n",name); return ; } var->value = value; return; }
void re_set_env_copy(const char *name , void* ptr,size_t size) { struct env_struct *var; var = find_env(name); if (!var|| (var->Type == kEnvPtr)) { printf("Unable to find environement variable %s\n",name); return ; } _re_set_env_copy(var , ptr, size); return; }
/** * Get an ENV value by key name. * * @param key ENV name * * @return value */ char *flash_get_env(const char *key) { uint32_t *env_cache_addr = NULL; char *value = NULL; /* find ENV */ env_cache_addr = find_env(key); if (env_cache_addr == NULL) { return NULL; } /* get value address */ value = strstr((char *) env_cache_addr, "="); if (value != NULL) { /* the equal sign next character is value */ value++; } return value; }
/** * Set an ENV. If it value is empty, delete it. * If not find it in ENV table, then create it. * * @param key ENV name * @param value ENV value * * @return result */ FlashErrCode flash_set_env(const char *key, const char *value) { FlashErrCode result = FLASH_NO_ERR; /* if ENV value is empty, delete it */ if (*value == NULL) { result = flash_del_env(key); } else { /* if find this ENV, then delete it and recreate it */ if (find_env(key)) { result = flash_del_env(key); } if (result == FLASH_NO_ERR) { result = create_env(key, value); } } return result; }
void * get_env_ptr(const char *name) { struct env_struct *var; var = find_env(name); if (!var) { printf("Unable to get environement ptr variable %s\n",name); return 0; } if (var->Type != kEnvPtr) { printf("Variable %s is not a ptr\n",name); return 0; } return var->ptr; }
unsigned long get_env_var(const char *name) { struct env_struct *var; var = find_env(name); if (!var) { printf("Unable to find environement variable %s\n",name); return 0; } if (var->Type != kEnvValue) { printf("Variable %s is not a value\n",name); return 0; } return var->value; }
/** * Delete an ENV in cache. * * @param key ENV name * * @return result */ FlashErrCode flash_del_env(const char *key){ FlashErrCode result = FLASH_NO_ERR; char *del_env_str = NULL; size_t del_env_length, remain_env_length; FLASH_ASSERT(key); if (*key == NULL) { FLASH_INFO("Flash ENV name must be not NULL!\n"); return FLASH_ENV_NAME_ERR; } if (strstr(key, "=")) { FLASH_INFO("Flash ENV name or value can't contain '='.\n"); return FLASH_ENV_NAME_ERR; } /* find ENV */ del_env_str = (char *) find_env(key); if (!del_env_str) { FLASH_INFO("Not find \"%s\" in ENV.\n", key); return FLASH_ENV_NAME_ERR; } del_env_length = strlen(del_env_str); /* '\0' also must be as ENV length */ del_env_length ++; /* the address must multiple of 4 */ if (del_env_length % 4 != 0) { del_env_length = (del_env_length / 4 + 1) * 4; } /* calculate remain ENV length */ remain_env_length = get_env_detail_size() - (((uint32_t) del_env_str + del_env_length) - ((uint32_t) env_cache + ENV_PARAM_PART_BYTE_SIZE)); /* remain ENV move forward */ memcpy(del_env_str, del_env_str + del_env_length, remain_env_length); /* reset ENV end address */ set_env_detail_end_addr(get_env_detail_end_addr() - del_env_length); return result; }
char cd_tiret(t_arg *targ) { char *tmp; int i; if (targ->wtab[1] != NULL && !my_strncmp(targ->wtab[1], "-", 1) && targ->pwd != NULL) { tmp = targ->pwd; if ((i = find_env(targ->env, "PWD=")) < 0) if ((targ->env = add_env(targ->env, "PWD", '=', "/")) == NULL) return (1); if ((targ->pwd = get_flagss(targ->env, "PWD")) == NULL) return (1); free(targ->env[i]); if ((targ->env[i] = concat_str("PWD", '=', tmp)) == NULL || chdir(tmp) < 0) return (1); return (2); } return (cd_home(targ)); }
void cupsdUpdateEnv(void) { /* * Set common variables... */ #define set_if_undefined(name,value) if (find_env(name) < 0) cupsdSetEnv(name,value) set_if_undefined("CUPS_CACHEDIR", CacheDir); set_if_undefined("CUPS_DATADIR", DataDir); set_if_undefined("CUPS_DOCROOT", DocumentRoot); set_if_undefined("CUPS_FONTPATH", FontPath); set_if_undefined("CUPS_REQUESTROOT", RequestRoot); set_if_undefined("CUPS_SERVERBIN", ServerBin); set_if_undefined("CUPS_SERVERROOT", ServerRoot); set_if_undefined("CUPS_STATEDIR", StateDir); set_if_undefined("DYLD_INSERT_LIBRARIES", NULL); set_if_undefined("DYLD_LIBRARY_PATH", NULL); set_if_undefined("HOME", TempDir); set_if_undefined("LD_ASSUME_KERNEL", NULL); set_if_undefined("LD_LIBRARY_PATH", NULL); set_if_undefined("LD_PRELOAD", NULL); set_if_undefined("NLSPATH", NULL); if (find_env("PATH") < 0) cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR ":/bin:/usr/bin", ServerBin); set_if_undefined("SERVER_ADMIN", ServerAdmin); set_if_undefined("SHLIB_PATH", NULL); set_if_undefined("SOFTWARE", CUPS_MINIMAL); set_if_undefined("TMPDIR", TempDir); set_if_undefined("TZ", NULL); set_if_undefined("USER", "root"); set_if_undefined("VG_ARGS", NULL); cupsdSetEnvf("CUPS_MAX_MESSAGE", "%d", CUPSD_SB_BUFFER_SIZE - 1); }
void open_oslibext(lua_State * L) { find_env(L); lua_getglobal(L, "os"); lua_pushcfunction(L, ex_sleep); lua_setfield(L, -2, "sleep"); lua_pushliteral(L, OS_PLATTYPE); lua_setfield(L, -2, "type"); lua_pushliteral(L, OS_PLATNAME); lua_setfield(L, -2, "name"); lua_pushcfunction(L, ex_uname); lua_setfield(L, -2, "uname"); #if (! defined (_WIN32)) && (! defined (__SUNOS__)) lua_pushcfunction(L, os_times); lua_setfield(L, -2, "times"); #endif #if ! defined (__SUNOS__) lua_pushcfunction(L, os_gettimeofday); lua_setfield(L, -2, "gettimeofday"); #endif lua_pushcfunction(L, os_setenv); lua_setfield(L, -2, "setenv"); lua_pushcfunction(L, os_exec); lua_setfield(L, -2, "exec"); lua_pushcfunction(L, os_spawn); lua_setfield(L, -2, "spawn"); lua_pushcfunction(L, os_execute); lua_setfield(L, -2, "execute"); lua_pushcfunction(L, os_tmpdir); lua_setfield(L, -2, "tmpdir"); lua_pop(L, 1); /* pop the table */ }
int evaluate(int exp_id) { if (DEBUG) { print(exp_id); indent += 1; } if (ATOM(exp_id)) { int found = find_env(exp_id); if (!NILP(found)) { expression[exp_id] = REMOVE_BIT(found); } } else { switch (expression[CAR(exp_id)]) { // car case 1: evaluate(CADR(exp_id)); if (ATOM(CADR(exp_id))) { error(LIST_EXPECTED); } expression[exp_id] = expression[CAADR(exp_id)]; break; // cdr case 2: evaluate(CADR(exp_id)); if (ATOM(CADR(exp_id))) { error(LIST_EXPECTED); } expression[exp_id] = expression[CDR(CADR(exp_id))]; break; // cons case 3: evaluate(CADR(exp_id)); evaluate(CADDR(exp_id)); expression[exp_id] = CONS(CADR(exp_id), CADDR(exp_id)); break; // quote case 4: expression[exp_id] = expression[CADR(exp_id)]; break; // eq case 5: evaluate(CADR(exp_id)); evaluate(CADDR(exp_id)); if (expression[CADR(exp_id)] == expression[CADDR(exp_id)]) { expression[exp_id] = L_T; } else { expression[exp_id] = L_NIL; } break; // atom case 6: evaluate(CADR(exp_id)); if (ATOM(CADR(exp_id))) { expression[exp_id] = L_T; } else { expression[exp_id] = L_NIL; } break; // cond case 7: evaluate_cond(CDR(exp_id)); expression[exp_id] = expression[CDR(exp_id)]; break; // print case 8: evaluate(CADR(exp_id)); print(CADR(exp_id)); expression[exp_id] = expression[CADR(exp_id)]; break; // apply case 12: { int callee = CADR(exp_id); int args = CDDR(exp_id); eval_args(args); before_call(); // if expression stack is not sufficient, // you can save and restore max id here if (expression[CAR(callee)] == L_LAMBDA) { int new_exp_id = move_exp(CADDR(callee)); update_environment(CADR(callee), args); evaluate(new_exp_id); expression[exp_id] = expression[new_exp_id]; } else if (expression[CAR(callee)] == L_LABEL) { int lambda_name = CADR(callee); int lambda = CADDR(callee); int new_exp_id = 0; if (ATOM(lambda_name)) { env[(call_depth << 8) + expression[lambda_name]] = SET_BIT(expression[lambda]); } else { error(INVALID_LABEL_NAME); } new_exp_id = move_exp(CADDR(lambda)); update_environment(CADR(lambda), args); evaluate(new_exp_id); expression[exp_id] = expression[new_exp_id]; } else { error(NOT_LAMBDA); } after_call(); } break; default: { int found = find_env(CAR(exp_id)); if (!NILP(found)) { int cdr = (REMOVE_BIT(found) << 16) >> 16; int new_exp_id = 0; int args = CDR(exp_id); eval_args(args); before_call(); new_exp_id = move_exp(CADR(cdr)); update_environment(CAR(cdr), args); evaluate(new_exp_id); expression[exp_id] = expression[new_exp_id]; after_call(); } else { print(exp_id); error(FUNCTION_NOT_FOUND); } } break; }