Changes in util.c [c0c48d14:8219374]
Legend:
- Unmodified
- Added
- Removed
-
util.c
rc0c48d14 r8219374 32 32 } 33 33 34 CALLER_OWN char *owl_util_homedir_for_user(const char *name) 35 { 36 int err; 37 struct passwd pw_buf; 38 struct passwd *pw; 39 40 char *pw_strbuf, *ret; 41 long pw_strbuf_len = sysconf(_SC_GETPW_R_SIZE_MAX); 42 if (pw_strbuf_len < 0) { 43 /* If we really hate ourselves, we can be fancy and loop until we stop 44 * getting ERANGE. For now just pick a random number. */ 45 owl_function_error("owl_util_homedir_for_user: Could not get _SC_GETPW_R_SIZE_MAX"); 46 pw_strbuf_len = 16384; 47 } 48 pw_strbuf = g_new0(char, pw_strbuf_len); 49 err = getpwnam_r(name, &pw_buf, pw_strbuf, pw_strbuf_len, &pw); 50 if (err) { 51 owl_function_error("getpwuid_r: %s", strerror(err)); 52 /* Fall through; pw will be NULL. */ 53 } 54 ret = pw ? g_strdup(pw->pw_dir) : NULL; 55 g_free(pw_strbuf); 56 return ret; 57 } 58 34 59 /* Return a "nice" version of the path. Tilde expansion is done, and 35 60 * duplicate slashes are removed. Caller must free the return. … … 42 67 /* Attempt tilde-expansion of the first component. Get the 43 68 tilde-prefix, which goes up to the next slash. */ 44 struct passwd *pw;45 69 const char *end = strchr(in + 1, '/'); 46 70 if (end == NULL) 47 71 end = in + strlen(in); 48 72 73 /* Patch together a new path. Replace the ~ and tilde-prefix with 74 the homedir, if available. */ 49 75 if (end == in + 1) { 50 /* My home directory. */ 51 pw = getpwuid(getuid()); 76 /* My home directory. Use the one in owl_global for consistency with 77 * owl_zephyr_dotfile. */ 78 out = g_strconcat(owl_global_get_homedir(&g), end, NULL); 52 79 } else { 53 80 /* Someone else's home directory. */ 54 81 char *user = g_strndup(in + 1, end - (in + 1)); 55 pw = getpwnam(user); 82 char *home = owl_util_homedir_for_user(user); 83 if (home) { 84 out = g_strconcat(home, end, NULL); 85 } else { 86 out = g_strdup(in); 87 } 88 g_free(home); 56 89 g_free(user); 57 90 } 58 59 /* Patch together a new path. Replace the ~ and tilde-prefix with60 the homedir. */61 if (pw) {62 out = g_strconcat(pw->pw_dir, end, NULL);63 } else {64 out = g_strdup(in);65 }66 91 } else { 67 92 out = g_strdup(in); 68 93 } 69 94 … … 388 413 if (!g_path_is_absolute(link_path)) { 389 414 char *last_dir = g_path_get_dirname(last_path); 390 char *tmp = g_build_path(G_DIR_SEPARATOR_S, 391 last_dir, 392 link_path, 393 NULL); 415 char *tmp = g_build_filename(last_dir, link_path, NULL); 394 416 g_free(last_dir); 395 417 g_free(link_path);
Note: See TracChangeset
for help on using the changeset viewer.