/*======================================================================= DateTime_time_to_string_local Caller must free string =======================================================================*/ char *DateTime_time_to_string_local (const DateTime *self, const char *tz, BOOL twelve_hour) { char *oldtz = NULL; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } struct tm *tm = localtime(&(self->priv->utime)); char s[20]; if (twelve_hour) { snprintf (s, sizeof (s), "%2d:%02d %s", tm->tm_hour <= 12 ? tm->tm_hour : tm->tm_hour - 12 , tm->tm_min, tm->tm_hour >= 12 ? "pm": "am"); } else //snprintf (s, sizeof (s), "%02d:%02d", tm->tm_hour, tm->tm_min); //snprintf (s, sizeof (s), "%02d:%02d %d/%d", tm->tm_hour, tm->tm_min, tm->tm_mday, tm->tm_mon + 1); snprintf (s, sizeof (s), "%02d:%02d", tm->tm_hour, tm->tm_min); if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } return strdup (s); }
/*======================================================================= DateTime_get_day_end Caller must free result Get 23:59:59 on the day in which this datetime falls =======================================================================*/ DateTime *DateTime_get_day_end (const DateTime *self, const char *tz) { char *oldtz = NULL; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } struct tm tm; memcpy (&tm, localtime (&self->priv->utime), sizeof (struct tm)); tm.tm_hour = 23; tm.tm_min = 59; tm.tm_sec = 59; time_t utime = mktime (&tm); if (tz) { if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } DateTime *dt = DateTime_new_utime (utime); return dt; }
void set_oldpwd_and_pwd(t_shell *shell) { int i; free(shell->buffer); if ((shell->buffer = malloc(my_strlen(shell->old_dir) + 20)) == NULL) exit(84); shell->buffer = my_strcpy(shell->buffer, "setenv "); shell->buffer = toolcpy(shell->buffer, "OLDPWD"); i = my_strlen(shell->buffer); shell->buffer[i] = ' '; shell->buffer[i + 1] = '\0'; shell->buffer = toolcpy(shell->buffer, shell->old_dir); my_setenv(shell); free(shell->buffer); if ((shell->buffer = malloc(my_strlen(shell->cd_move) + 20)) == NULL) exit(84); shell->buffer = my_strcpy(shell->buffer, "setenv "); shell->buffer = toolcpy(shell->buffer, "PWD"); i = my_strlen(shell->buffer); shell->buffer[i] = ' '; shell->buffer[i + 1] = '\0'; shell->buffer = toolcpy(shell->buffer, shell->cd_move); my_setenv(shell); }
/*======================================================================= DateTime_add_days Nightmare! We can't just add 24*3600*days seconds, because we might be crossing at DST boundary. We want the same time on the days a certan distance from the current day =======================================================================*/ void DateTime_add_days (DateTime *self, int days, const char *tz, BOOL utc) { struct tm tm; char *oldtz = NULL; if (utc) tz = "UTC0"; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } memcpy (&tm, localtime (&(self->priv->utime)), sizeof (struct tm)); // mktime seems to cope with mday values > 31 and < 0, by adjusting // the other fields to match. This even deals with DST. I am unsure // whether this behaviour can be relied up on all systems tm.tm_mday += days; tm.tm_isdst = -1; self->priv->utime = mktime (&tm); if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } }
/*======================================================================= DateTime_get_ymdhms =======================================================================*/ void DateTime_get_ymdhms (const DateTime *self, int *year, int *month, int *day, int *hours, int *minutes, int *seconds, const char *tz, BOOL utc) { struct tm tm; char *oldtz = NULL; if (utc) tz = "UTC0"; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } memcpy (&tm, localtime (&self->priv->utime), sizeof (struct tm)); *year = tm.tm_year + 1900; *month = tm.tm_mon + 1; *day = tm.tm_mday; *hours = tm.tm_hour; *minutes = tm.tm_min; *seconds = tm.tm_sec; if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } }
int gl_path(t_sh *sh, int i, glob_t *globuf) { int rg; if ((rg = glob(sh->av[i], GLOB_DOOFFS, NULL, globuf)) != 0) { my_setenv(&sh->env, "?", "1"); if (rg == GLOB_NOMATCH) printf("%s: No match.\n", sh->av[0]); else return (2); return (1); } while (sh->av[++i]) if (is_glob(sh->av[i])) if ((rg = glob(sh->av[i], GLOB_DOOFFS | GLOB_APPEND, NULL, globuf)) != 0) { my_setenv(&sh->env, "?", "1"); if (rg == GLOB_NOMATCH) printf("%s: No match.\n", sh->av[0]); else return (2); return (1); } return (0); }
/*======================================================================= DateTime_date_to_string_syslocal Caller must free string =======================================================================*/ char *DateTime_date_to_string_local (const DateTime *self, const char *tz) { char *oldtz = NULL; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } struct tm *tm = localtime(&(self->priv->utime)); char s[100]; strftime (s, sizeof (s), "%A %e %B %Y", tm); if (s[strlen(s) - 1] == 10) s[strlen(s) - 1] = 0; if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } return strdup (s); }
/*======================================================================= DateTime_set_time_hours_fracion =======================================================================*/ void DateTime_set_time_hours_fraction (DateTime *self, double hours) { struct tm tm; double h, m, s; memcpy (&tm, gmtime (&self->priv->utime), sizeof (struct tm)); h = floor (hours); m = floor ((hours - h) * 60); s = (hours - h - m / 60) * 3600; tm.tm_hour = h; tm.tm_min = m; tm.tm_sec = s; char *oldtz = NULL; char *tz = "UTC0"; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } self->priv->utime = mktime (&tm); if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } } }
int xchdir(char *path, t_sllist **myenv) { clear_path(path); if (chdir(path) == -1) { fprintf(stderr, "%s%s", path, ERR_ISNOTFOLD); return (EXIT_FAILURE); } else { my_setenv("OLDPWD", my_getenv("PWD", *myenv), myenv); my_setenv("PWD", path, myenv); } return (EXIT_SUCCESS); }
int check_builts(char *cmd, char **arg, char **env) { int ret; ret = 0; if (my_strcmp("setenv", arg[0]) == 0) { if (arg == NULL || arg[0] == NULL || arg[1] == NULL || arg[2] == NULL) printf("Usage : setenv [NAME] [VALUE] \n"); else my_setenv(env, arg[1], arg[2]); ++ret; } if (my_strcmp("unsetenv", arg[0]) == 0) { if (arg && arg[0] && arg[1]) my_unsetenv(env, arg[1]); else printf("Usage : unsetenv [NAME] \n"); save_my_env(env); ++ret; } ret += check_next(cmd, arg, env); if (ret == 0) printf("%s : Command not found\n", arg[0]); return (ret); }
/* ** brief: we will try to find the good command ** @env: our env list ** @arr: contain the command and values ** return: 1 if we found our cmd but it's not cd, 0 if the cmd is not found ** and 2 if we have change the dir and 43 if exit */ static int _my_builtin_fun(char **arr, t_list *env) { char *pwd; if (my_strcmp(arr[0], "exit", 0)) return (my_exit(env, arr)); else if (my_strcmp(arr[0], "env", 0)) my_env(env, arr); else if (my_strcmp(arr[0], "cd", 0)) return (my_cd(arr) + 1); else if (my_strcmp(arr[0], "pwd", 0)) { pwd = my_find_element(env, "PWD"); write(1, pwd, my_strlen(pwd)); write(1, "\n", 1); return (1); } else if (my_strcmp(arr[0], "setenv", 0)) my_setenv(env, arr); else if (my_strcmp(arr[0], "unsetenv", 0)) my_unsetenv(env, arr); else return (0); return (1); }
void dad(t_sh *sh, pid_t pid) { int status; char buff[5]; my_memset(buff, 0, 5); if (sh->actual->piper_read != NULL && sh->actual->fd[0] != 0) { close(sh->actual->piper_read->pipe[0]); close(sh->actual->piper_read->pipe[1]); } if (sh->actual->piper_write != NULL) waitpid(pid, &status, WNOHANG); else waitpid(pid, &status, 0); if (sh->actual->fd[0] != 0) close(sh->actual->fd[0]); if (sh->actual->fd[1] != 1) close(sh->actual->fd[1]); if (WEXITSTATUS(status) == 0) return_exec_success(sh); sprintf(buff, "%d", WEXITSTATUS(status)); my_setenv(&sh->env, "?", buff); handle_message(status); freetab(sh->av); }
t_uchar run_setenv(char ***env, char **argv) { char *tmp; if (count_args(argv) < 2) { my_printenv(*env, '\n'); return (0); } else if (count_args(argv) > 3 || !my_str_isalpha(argv[1]) || (!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0]))) { if (count_args(argv) > 3) my_dprintf(STDERR, "setenv: Too many arguments.\n"); else if ((!my_char_islower(argv[1][0]) && !my_char_islower(argv[1][0]))) my_dprintf(STDERR, "setenv: Variable name must begin with a letter.\n"); else if (!my_str_isalpha(argv[1])) my_dprintf(STDERR, ERALPH); return (1); } if ((tmp = malloc(sizeof(char) * (my_strlen(argv[1]) + 2))) == NULL) my_exit(EXIT_FAILURE, "ERROR: Out of memory! malloc() failed\n"); tmp = my_strncpy(tmp, argv[1], my_strlen(argv[1])); tmp = my_strncat(tmp, "=", 1); my_setenv(env, tmp, argv[2]); free(tmp); return (0); }
int cond(char **tab, char **path) { int i; i = 0; if (my_strcmp(tab[0], "env") == 0) { my_env(); return (1); } if (my_strcmp(tab[0], "setenv") == 0) { if (tab[1] == NULL) return (my_env()); else if (tab[2] != NULL) return (my_setenv(tab[1], tab[2])); } if (my_strcmp(tab[0], "unsetenv") == 0 && tab[1] != NULL) return (my_unsetenv(tab[1])); if (my_strcmp(tab[0], "exit") == 0) { free(path); exit(EXIT_SUCCESS); } return (0); }
/*======================================================================= DateTime_new_parse =======================================================================*/ DateTime *DateTime_new_parse (const char *str, Error **error, const char *tz, BOOL utc) { struct tm tm; time_t utime = 0; char *oldtz = NULL; if (DateTime_parse (str, "%e/%m/%Y %H:%M", &tm)) goto success; if (DateTime_parse (str, "%e/%m/%Y", &tm)) goto success; if (DateTime_parse (str, "%e/%m %M:M", &tm)) goto success; if (DateTime_parse (str, "%e/%m", &tm)) goto success; if (DateTime_parse (str, "%b %e %Y %H:%M", &tm)) goto success; if (DateTime_parse (str, "%b %e %H:%M", &tm)) goto success; if (DateTime_parse (str, "%b %e %Y", &tm)) goto success; if (DateTime_parse (str, "%b %e", &tm)) goto success; if (DateTime_parse (str, "%H:%M", &tm)) goto success; if (DateTime_parse (str, "%j#%Y", &tm)) goto success; // Failed *error = Error_new ("Can't parse date"); return NULL; success: if (utc) tz = "UTC0"; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } utime = mktime (&tm); if (tz) { ////if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } DateTime *dt = DateTime_new_utime (utime); return dt; }
int builtin_unsetenv(size_t argc, char **args) { if (argc < 1) { unsetenv_usage(); return (-1); } my_setenv(args[0], NULL); return (0); }
int builtin_cd(size_t argc, char **argv) { char *dir; char pwd[1024]; char oldpwd[1024]; getcwd(oldpwd, 1023); if (argc <= 0) dir = my_getenv("HOME"); else if (strcmp(argv[0], "-") == 0) dir = my_getenv("OLDPWD"); else dir = argv[0]; if (dir == NULL) return (-1); if (chdir(dir) != 0) my_perror(); my_setenv("OLDPWD", oldpwd); my_setenv("PWD", getcwd(pwd, 1023)); return (0); }
int AST_ProcessAssignment(AST_Assignment_t *assignment) { char *value; if (assignment->value) { value = assignment->value->str; } else { value = ""; } my_setenv(assignment->var->str, value, true); return 0; }
int build_env(char **env, t_sllist **new_env) { int i; char *tp_data; char *tp_name; *new_env = NULL; i = 0; my_setenv("PATH", "/bin:/usr/bin", new_env); while (env[i] != NULL) { tp_name = mk_envname(env[i]); tp_data = mk_envdata(env[i]); my_setenv(tp_name, tp_data, new_env); free(tp_name); free(tp_data); i++; } my_setenv("OLDPWD", "", new_env); my_setenv("SHELL", "42sh", new_env); return (0); }
/* also should have return value reflect success or failure. */ void LoadLang(void) { char *s1, *s2, *s3, *s4; char buf [30]; s1 = setlocale(LC_ALL, settings.theme_locale_name); s2 = bindtextdomain(PACKAGE, TUXLOCALE); s3 = bind_textdomain_codeset(PACKAGE, "UTF-8"); s4 = textdomain(PACKAGE); DEBUGCODE { fprintf(stderr, "PACKAGE = %s\n", PACKAGE); fprintf(stderr, "TUXLOCALE = %s\n", TUXLOCALE); fprintf(stderr, "setlocale(LC_ALL, %s) returned: %s\n", settings.theme_locale_name, s1); fprintf(stderr, "bindtextdomain(PACKAGE, TUXLOCALE) returned: %s\n", s2); fprintf(stderr, "bind_textdomain_codeset(PACKAGE, \"UTF-8\") returned: %s\n", s3); fprintf(stderr, "textdomain(PACKAGE) returned: %s\n", s4); fprintf(stderr, "gettext(\"Fish\"): %s\n\n", gettext("Fish")); // fprintf(stderr, "After gettext() call\n"); } /* Also set LANG and LANGUAGE as fallbacks because setlocale() unreliable */ /* on some Windows versions, AFAICT */ snprintf(buf, 30, "%s", settings.theme_locale_name); buf[5] = '\0'; //en_US" rather than "en_US.utf8" DEBUGCODE { fprintf(stderr, "buf is: %s\n", buf); } if (my_setenv("LANG", buf) == -1) { fprintf(stderr, "Warning - could not set LANG to %s\n\n", buf); } if (my_setenv("LANGUAGE", buf) == -1) { fprintf(stderr, "Warning - could not set LANGUAGE to %s\n\n", buf); } return; }
int main (int argc, char *argv[]) { char name[] = "GREET"; char value[] = "bonjour"; assert(getenv(name) == NULL); assert(my_setenv(NULL, value, 0) == -1); assert(my_setenv(name, value, 0) == 0); assert(strcmp(getenv(name), "bonjour") == 0); value[0] = 'B'; value[1] = 'O'; assert(strcmp(getenv(name), "bonjour") == 0); assert(my_setenv(name, "guten tag", 0) == 0); assert(strcmp(getenv(name), "bonjour") == 0); assert(my_setenv(name, "guten tag", 1) == 0); assert(strcmp(getenv(name), "guten tag") == 0); assert(my_unsetenv(NULL) == -1); assert(my_unsetenv(name) == 0); assert(my_setenv("lorem", "ipsum", 0) == 0); assert(getenv(name) == NULL); assert(strcmp(getenv("lorem"), "ipsum") == 0); exit(EXIT_SUCCESS); }
void put_pwd(t_info *info) { char *tab[4]; tab[0] = "setenv"; tab[1] = "PWD"; tab[2] = my_pwd(); tab[3] = NULL; my_setenv(info, tab); if (info->last_pwd != NULL) xfree(info->last_pwd); info->last_pwd = info->pwd; info->pwd = tab[2]; }
/*======================================================================= DateTime_to_string_local Caller must free string If tz is null, gives the local time at the home locale =======================================================================*/ char *DateTime_to_string_local (const DateTime *self, const char *tz) { char *oldtz = NULL; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } char *s = ctime (&(self->priv->utime)); if (s[strlen(s) - 1] == 10) s[strlen(s) - 1] = 0; if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } return strdup (s); }
/*======================================================================= DateTime_get_day_of_year =======================================================================*/ int DateTime_get_day_of_year (const DateTime *self, const char *tz) { struct tm tm; char *oldtz = NULL; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } memcpy (&tm, gmtime (&self->priv->utime), sizeof (struct tm)); if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } return tm.tm_yday + 1; }
/*======================================================================= DateTime_get_jan_first Get start of year -- midnight on january first. Note the we need tz information, as midnight occurs at different universal times in different zones. =======================================================================*/ DateTime *DateTime_get_jan_first (const DateTime *self, const char *tz, BOOL utc) { struct tm tm; char *oldtz = NULL; if (utc) tz = "UTC0"; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } memcpy (&tm, localtime (&(self->priv->utime)), sizeof (struct tm)); tm.tm_mday = 0; tm.tm_mon = 0; /* tm.tm_year unchanged */ tm.tm_hour = 0; tm.tm_min = 0; tm.tm_sec = 0; tm.tm_isdst = -1; DateTime *r = DateTime_new_utime (mktime (&tm)); if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } return r; }
int setenv_flag(t_tab *tab, t_btree **tree) { int i; i = -1; while ((*tree)->right->stock[++i]); if (i != 3) { write(2, "USAGE: setenv [NAME] [WORDS]\n", 29); return (-1); } is_exist(tab, tree); my_setenv(tab, tree); return (0); }
/*======================================================================= DateTime_new_dmy_name Creates a new datetime at midnight on the specified date. Note that we need to pass timezone info, because midnight in one zone is not the same universal time as midnight in another =======================================================================*/ DateTime *DateTime_new_dmy_name (int day, int month, int year, const char *name, const char *tz, BOOL utc) { struct tm tm; time_t utime = 0; char *oldtz = NULL; if (utc) tz = "UTC0"; if (tz) { oldtz = getenv_dup ("TZ"); my_setenv ("TZ", tz, 1); tzset (); } tm.tm_mday = day; tm.tm_mon = month - 1; tm.tm_year = year - 1900; tm.tm_hour = 0; tm.tm_min = 0; tm.tm_sec = 0; tm.tm_isdst = -1; utime = mktime (&tm); if (tz) { //if (oldtz) { my_setenv ("TZ", oldtz, 1); if (oldtz) free (oldtz); tzset (); } //unmy_setenv ("TZ"); } DateTime *r = DateTime_new_utime (utime); DateTime_set_name (r, name); return r; }
int AST_ProcessCommand(AST_Command_t *command) { int argc = command->argc; char **argv; int i; int r; char r_str[5]; if ((argv = calloc(argc, sizeof(*argv))) == NULL) { goto process_command_fail; } /* Create the command arguments */ for (i = 0; i < argc; i++) { char *value; if (command->argv[i]->type == TOKEN_DOLLAR) { if ((value = my_getenv(command->argv[i]->str)) == NULL) { value = ""; } } else { value = command->argv[i]->str; } /* Take a copy of the string */ if ((argv[i] = strdup(value)) == NULL) { goto process_command_fail; } } r = Shell_RunCommand(argc, argv, command->background); snprintf(r_str, sizeof(r_str), "%d", r); my_setenv("?", r_str, true); return r; process_command_fail: if (argv) { for (i = 0; i < argc; i++) { if (argv[i]) { free(argv[i]); } } free(argv); } return -ENOMEM; }
int AST_ProcessForPipeline(AST_ForPipeline_t *forpipeline) { int i; int r = 0; if (forpipeline->words) { for (i = 0; i < forpipeline->words->nwords; i++) { my_setenv(forpipeline->var->str, forpipeline->words->words[i]->str, true); r = AST_ProcessList(forpipeline->list); } } else { /* TODO */ } return r; }
void init_envvars(char **envp) { char **wordtab; int i; i = 0; while (envp[i]) { wordtab = str_to_wordtab(envp[i], '='); if (!wordtab) return ; my_setenv(wordtab[0], wordtab[1]); free_wordtab(wordtab); ++i; } }