utils: make variables more local in parsePath()

Declare variables where they are really used.
This commit is contained in:
Max Kellermann 2008-12-29 17:42:46 +01:00
parent 3c9992aead
commit db849d1eed

View File

@ -114,38 +114,43 @@ G_GNUC_MALLOC void *xcalloc(size_t nmemb, size_t size)
char *parsePath(char *path) char *parsePath(char *path)
{ {
ConfigParam *param;
struct passwd *passwd;
char *newPath;
char *c;
int foundSlash = 0;
int pos = 1;
if (path[0] != '/' && path[0] != '~') { if (path[0] != '/' && path[0] != '~') {
g_warning("\"%s\" is not an absolute path", path); g_warning("\"%s\" is not an absolute path", path);
return NULL; return NULL;
} else if (path[0] == '~') { } else if (path[0] == '~') {
size_t pos = 1;
const char *home;
char *newPath;
if (path[1] == '/' || path[1] == '\0') { if (path[1] == '/' || path[1] == '\0') {
param = getConfigParam(CONF_USER); ConfigParam *param = getConfigParam(CONF_USER);
if (param && param->value) { if (param && param->value) {
passwd = getpwnam(param->value); struct passwd *passwd = getpwnam(param->value);
if (!passwd) { if (!passwd) {
g_warning("no such user %s", g_warning("no such user %s",
param->value); param->value);
return NULL; return NULL;
} }
home = passwd->pw_dir;
} else { } else {
passwd = getpwuid(geteuid()); struct passwd *passwd = getpwuid(geteuid());
if (!passwd) { if (!passwd) {
g_warning("problems getting passwd " g_warning("problems getting passwd "
"entry for current user"); "entry for current user");
return NULL; return NULL;
} }
home = passwd->pw_dir;
} }
} else { } else {
bool foundSlash = false;
struct passwd *passwd;
char *c;
for (c = path + 1; *c != '\0' && *c != '/'; c++); for (c = path + 1; *c != '\0' && *c != '/'; c++);
if (*c == '/') { if (*c == '/') {
foundSlash = 1; foundSlash = true;
*c = '\0'; *c = '\0';
} }
pos = c - path; pos = c - path;
@ -158,16 +163,17 @@ char *parsePath(char *path)
if (foundSlash) if (foundSlash)
*c = '/'; *c = '/';
home = passwd->pw_dir;
} }
newPath = xmalloc(strlen(passwd->pw_dir) + strlen(path + pos) + 1); newPath = xmalloc(strlen(home) + strlen(path + pos) + 1);
strcpy(newPath, passwd->pw_dir); strcpy(newPath, home);
strcat(newPath, path + pos); strcat(newPath, path + pos);
return newPath;
} else { } else {
newPath = xstrdup(path); return xstrdup(path);
} }
return newPath;
} }
int set_nonblocking(int fd) int set_nonblocking(int fd)