Changeset 8219374
- Timestamp:
- Jul 16, 2011, 12:28:10 AM (14 years ago)
- Branches:
- master, release-1.10, release-1.8, release-1.9
- Children:
- f5f6ec0
- Parents:
- 99ac28a
- git-author:
- David Benjamin <davidben@mit.edu> (07/10/11 19:11:46)
- git-committer:
- David Benjamin <davidben@mit.edu> (07/16/11 00:28:10)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tester.c
r396505be r8219374 4 4 #undef WINDOW 5 5 6 #include <errno.h>7 6 #include <unistd.h> 8 #include <pwd.h>9 7 #include <stdio.h> 10 8 #include <stdlib.h> 11 9 #include <string.h> 12 #include <sys/types.h>13 10 14 11 #undef instr … … 136 133 { 137 134 int numfailed=0; 138 const char *home; 139 char *s, *path; 140 struct passwd *pw; 135 char *s, *path, *home; 141 136 142 137 printf("# BEGIN testing owl_util\n"); … … 274 269 g_free(s); 275 270 276 errno = 0; 277 home = owl_global_get_homedir(&g); 271 home = g_strdup(owl_global_get_homedir(&g)); 278 272 s = owl_util_makepath("~"); 279 273 FAIL_UNLESS("makepath ~", !strcmp(home, s)); … … 285 279 g_free(s); 286 280 g_free(path); 287 288 errno = 0; 289 pw = getpwnam("root"); 290 if (pw) { 291 home = pw->pw_dir; 292 } else { 281 g_free(home); 282 283 home = owl_util_homedir_for_user("root"); 284 if (home == NULL) { 293 285 /* Just make some noise so we notice. */ 294 home = "<WHAT>";295 fprintf(stderr, " getpwnam: %s", errno ? strerror(errno) : "No such user");286 home = g_strdup("<WHAT>"); 287 fprintf(stderr, "owl_util_homedir_for_user failed"); 296 288 } 297 289 … … 305 297 g_free(s); 306 298 g_free(path); 299 g_free(home); 307 300 308 301 /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */ -
util.c
r396505be 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. … … 55 80 /* Someone else's home directory. */ 56 81 char *user = g_strndup(in + 1, end - (in + 1)); 57 struct passwd *pw = getpwnam(user); 58 g_free(user); 59 if (pw) { 60 out = g_strconcat(pw->pw_dir, end, NULL); 82 char *home = owl_util_homedir_for_user(user); 83 if (home) { 84 out = g_strconcat(home, end, NULL); 61 85 } else { 62 86 out = g_strdup(in); 63 87 } 88 g_free(home); 89 g_free(user); 64 90 } 65 91 } else {
Note: See TracChangeset
for help on using the changeset viewer.