/* Return the user's home directory (strdup-ed), or NULL if none is found. */ char * home_dir (void) { static char *buf = NULL; static char *home, *ret; int len; if (!home) { home = getenv ("HOME"); if (!home) { #if defined(MSDOS) /* Under MSDOS, if $HOME isn't defined, use the directory where `wget.exe' resides. */ const char *_w32_get_argv0 (void); /* in libwatt.a/pcconfig.c */ char *p; buff = _w32_get_argv0 (); p = strrchr (buf, '/'); /* djgpp */ if (!p) p = strrchr (buf, '\\'); /* others */ assert (p); len = p - buff + 1; buff = malloc (len + 1); if (buff == NULL) return NULL; strncpy (buff, _w32_get_argv0 (), len); buff[len] = '\0'; home = buf; #elif !defined(WINDOWS) /* If HOME is not defined, try getting it from the password file. */ struct passwd *pwd = getpwuid (getuid ()); if (!pwd || !pwd->pw_dir) return NULL; home = pwd->pw_dir; #else /* !WINDOWS */ /* Under Windows, if $HOME isn't defined, use the directory where `wget.exe' resides. */ home = ws_mypath (); #endif /* WINDOWS */ } } ret = home ? xstrdup (home) : NULL; if (buf) free (buf); return ret; }
/* Return the user's home directory (strdup-ed), or NULL if none is found. */ char * home_dir (void) { char *home = getenv ("HOME"); if (!home) { #if defined(MSDOS) /* Under MSDOS, if $HOME isn't defined, use the directory where `wget.exe' resides. */ const char *_w32_get_argv0 (void); /* in libwatt.a/pcconfig.c */ char *p, buf[PATH_MAX]; strcpy (buf, _w32_get_argv0 ()); p = strrchr (buf, '/'); /* djgpp */ if (!p) p = strrchr (buf, '\\'); /* others */ assert (p); *p = '\0'; home = buf; #elif !defined(WINDOWS) /* If HOME is not defined, try getting it from the password file. */ struct passwd *pwd = getpwuid (getuid ()); if (!pwd || !pwd->pw_dir) return NULL; home = pwd->pw_dir; #else /* !WINDOWS */ /* Under Windows, if $HOME isn't defined, use the directory where `wget.exe' resides. */ home = ws_mypath (); #endif /* WINDOWS */ } return home ? xstrdup (home) : NULL; }