char *pa_get_user_name(char *s, size_t l) { const char *p; char *name = NULL; #ifdef OS_IS_WIN32 char buf[1024]; #endif #ifdef HAVE_PWD_H struct passwd *r; #endif pa_assert(s); pa_assert(l > 0); p = NULL; #ifdef HAVE_GETUID p = getuid() == 0 ? "root" : NULL; #endif if (!p) p = getenv("USER"); if (!p) p = getenv("LOGNAME"); if (!p) p = getenv("USERNAME"); if (p) { name = pa_strlcpy(s, p, l); } else { #ifdef HAVE_PWD_H if ((r = pa_getpwuid_malloc(getuid())) == NULL) { pa_snprintf(s, l, "%lu", (unsigned long) getuid()); return s; } name = pa_strlcpy(s, r->pw_name, l); pa_getpwuid_free(r); #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */ DWORD size = sizeof(buf); if (!GetUserName(buf, &size)) { errno = ENOENT; return NULL; } name = pa_strlcpy(s, buf, l); #else /* HAVE_PWD_H */ return NULL; #endif /* HAVE_PWD_H */ } return name; }
char *pa_get_home_dir(char *s, size_t l) { char *e; char *dir; #ifdef HAVE_PWD_H struct passwd *r; #endif pa_assert(s); pa_assert(l > 0); if ((e = getenv("HOME"))) { dir = pa_strlcpy(s, e, l); goto finish; } if ((e = getenv("USERPROFILE"))) { dir = pa_strlcpy(s, e, l); goto finish; } #ifdef HAVE_PWD_H errno = 0; if ((r = pa_getpwuid_malloc(getuid())) == NULL) { if (!errno) errno = ENOENT; return NULL; } dir = pa_strlcpy(s, r->pw_dir, l); pa_getpwuid_free(r); #endif /* HAVE_PWD_H */ finish: if (!dir) { errno = ENOENT; return NULL; } if (!pa_is_path_absolute(dir)) { pa_log("Failed to get the home directory, not an absolute path: %s", dir); errno = ENOENT; return NULL; } return dir; }
char *pa_get_home_dir(char *s, size_t l) { char *e, *dir; #ifdef HAVE_PWD_H struct passwd *r; #endif pa_assert(s); pa_assert(l > 0); if ((e = getenv("HOME"))) return pa_strlcpy(s, e, l); if ((e = getenv("USERPROFILE"))) return pa_strlcpy(s, e, l); #ifdef HAVE_PWD_H errno = 0; if ((r = pa_getpwuid_malloc(getuid())) == NULL) { if (!errno) errno = ENOENT; return NULL; } dir = pa_strlcpy(s, r->pw_dir, l); pa_getpwuid_free(r); return dir; #else /* HAVE_PWD_H */ errno = ENOENT; return NULL; #endif }