* does tilde expansion of strings of type ``~user/foo'' * if ``user'' isn't valid user name or ``txt'' doesn't start * w/ '~', returns pointer to strdup()ed copy of ``txt'' * * it's the caller's responsibility to free() the returned string */
| 69 | * it's the caller's responsibility to free() the returned string |
| 70 | */ |
| 71 | char * |
| 72 | fn_tilde_expand(const char *txt) |
| 73 | { |
| 74 | #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT) |
| 75 | struct passwd pwres; |
| 76 | char pwbuf[1024]; |
| 77 | #endif |
| 78 | struct passwd *pass; |
| 79 | char *temp; |
| 80 | size_t len = 0; |
| 81 | |
| 82 | if (txt[0] != '~') |
| 83 | return strdup(txt); |
| 84 | |
| 85 | temp = strchr(txt + 1, '/'); |
| 86 | if (temp == NULL) { |
| 87 | temp = strdup(txt + 1); |
| 88 | if (temp == NULL) |
| 89 | return NULL; |
| 90 | } else { |
| 91 | /* text until string after slash */ |
| 92 | len = (size_t)(temp - txt + 1); |
| 93 | temp = el_malloc(len * sizeof(*temp)); |
| 94 | if (temp == NULL) |
| 95 | return NULL; |
| 96 | (void)strncpy(temp, txt + 1, len - 2); |
| 97 | temp[len - 2] = '\0'; |
| 98 | } |
| 99 | if (temp[0] == 0) { |
| 100 | #ifdef HAVE_GETPW_R_POSIX |
| 101 | if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf), |
| 102 | &pass) != 0) |
| 103 | pass = NULL; |
| 104 | #elif HAVE_GETPW_R_DRAFT |
| 105 | pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf)); |
| 106 | #else |
| 107 | pass = getpwuid(getuid()); |
| 108 | #endif |
| 109 | } else { |
| 110 | #ifdef HAVE_GETPW_R_POSIX |
| 111 | if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0) |
| 112 | pass = NULL; |
| 113 | #elif HAVE_GETPW_R_DRAFT |
| 114 | pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf)); |
| 115 | #else |
| 116 | pass = getpwnam(temp); |
| 117 | #endif |
| 118 | } |
| 119 | el_free(temp); /* value no more needed */ |
| 120 | if (pass == NULL) |
| 121 | return strdup(txt); |
| 122 | |
| 123 | /* update pointer txt to point at string immedially following */ |
| 124 | /* first slash */ |
| 125 | txt += len; |
| 126 | |
| 127 | len = strlen(pass->pw_dir) + 1 + strlen(txt) + 1; |
| 128 | temp = el_malloc(len * sizeof(*temp)); |
no test coverage detected