Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    rc0c48d14 r8219374  
    3232}
    3333
     34CALLER_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
    3459/* Return a "nice" version of the path.  Tilde expansion is done, and
    3560 * duplicate slashes are removed.  Caller must free the return.
     
    4267    /* Attempt tilde-expansion of the first component. Get the
    4368       tilde-prefix, which goes up to the next slash. */
    44     struct passwd *pw;
    4569    const char *end = strchr(in + 1, '/');
    4670    if (end == NULL)
    4771      end = in + strlen(in);
    4872
     73    /* Patch together a new path. Replace the ~ and tilde-prefix with
     74       the homedir, if available. */
    4975    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);
    5279    } else {
    5380      /* Someone else's home directory. */
    5481      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);
    5689      g_free(user);
    5790    }
    58 
    59     /* Patch together a new path. Replace the ~ and tilde-prefix with
    60        the homedir. */
    61     if (pw) {
    62       out = g_strconcat(pw->pw_dir, end, NULL);
    63     } else {
    64       out = g_strdup(in);
    65     }
    6691  } else {
    67       out = g_strdup(in);
     92    out = g_strdup(in);
    6893  }
    6994
     
    388413    if (!g_path_is_absolute(link_path)) {
    389414      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);
    394416      g_free(last_dir);
    395417      g_free(link_path);
Note: See TracChangeset for help on using the changeset viewer.