version 1.86, 2011/09/05 05:59:08 |
version 1.87, 2013/04/23 17:49:45 |
|
|
char * |
char * |
tilde_expand_filename(const char *filename, uid_t uid) |
tilde_expand_filename(const char *filename, uid_t uid) |
{ |
{ |
const char *path; |
const char *path, *sep; |
char user[128], ret[MAXPATHLEN]; |
char user[128], *ret; |
struct passwd *pw; |
struct passwd *pw; |
u_int len, slash; |
u_int len, slash; |
|
|
|
|
} else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ |
} else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ |
fatal("tilde_expand_filename: No such uid %ld", (long)uid); |
fatal("tilde_expand_filename: No such uid %ld", (long)uid); |
|
|
if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret)) |
|
fatal("tilde_expand_filename: Path too long"); |
|
|
|
/* Make sure directory has a trailing '/' */ |
/* Make sure directory has a trailing '/' */ |
len = strlen(pw->pw_dir); |
len = strlen(pw->pw_dir); |
if ((len == 0 || pw->pw_dir[len - 1] != '/') && |
if ((len == 0 || pw->pw_dir[len - 1] != '/')) |
strlcat(ret, "/", sizeof(ret)) >= sizeof(ret)) |
sep = "/"; |
fatal("tilde_expand_filename: Path too long"); |
else |
|
sep = ""; |
|
|
/* Skip leading '/' from specified path */ |
/* Skip leading '/' from specified path */ |
if (path != NULL) |
if (path != NULL) |
filename = path + 1; |
filename = path + 1; |
if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret)) |
|
|
if (xasprintf(&ret, "%s%s%s", pw->pw_dir, sep, filename) >= MAXPATHLEN) |
fatal("tilde_expand_filename: Path too long"); |
fatal("tilde_expand_filename: Path too long"); |
|
|
return (xstrdup(ret)); |
return (ret); |
} |
} |
|
|
/* |
/* |