From b5bd5542eb99f97561848113887d100b83e9bc43 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Sat, 5 Jun 2004 20:26:56 +0000 Subject: [PATCH] Neatened up some of the glob-expand code and made a few other minor tweaks. --- util.c | 83 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/util.c b/util.c index a01a38d1..f2158a02 100644 --- a/util.c +++ b/util.c @@ -134,7 +134,7 @@ int set_modtime(char *fname, time_t modtime) if (verbose > 2) { rprintf(FINFO, "set modtime of %s to (%ld) %s", - fname, (long) modtime, + fname, (long)modtime, asctime(localtime(&modtime))); } @@ -439,7 +439,8 @@ void kill_all(int sig) int name_to_uid(char *name, uid_t *uid) { struct passwd *pass; - if (!name || !*name) return 0; + if (!name || !*name) + return 0; pass = getpwnam(name); if (pass) { *uid = pass->pw_uid; @@ -452,7 +453,8 @@ int name_to_uid(char *name, uid_t *uid) int name_to_gid(char *name, gid_t *gid) { struct group *grp; - if (!name || !*name) return 0; + if (!name || !*name) + return 0; grp = getgrnam(name); if (grp) { *gid = grp->gr_gid; @@ -493,77 +495,78 @@ static int exclude_server_path(char *arg) return 0; } -static void glob_expand_one(char *s, char **argv, int *argc, int maxargs) +static void glob_expand_one(char *s, char **argv, int *argc_ptr, int maxargs) { + int argc = *argc_ptr; #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H)) - if (maxargs <= *argc) + if (maxargs <= argc) return; if (!*s) s = "."; - s = argv[*argc] = strdup(s); + s = argv[argc++] = strdup(s); exclude_server_path(s); - (*argc)++; #else glob_t globbuf; int i; + if (maxargs <= argc) + return; if (!*s) s = "."; - s = argv[*argc] = strdup(s); + s = strdup(s); if (sanitize_paths) sanitize_path(s, NULL); memset(&globbuf, 0, sizeof globbuf); if (!exclude_server_path(s)) glob(s, 0, NULL, &globbuf); - if (globbuf.gl_pathc == 0) { - (*argc)++; - globfree(&globbuf); - return; - } - for (i = 0; i < maxargs - *argc && i < (int)globbuf.gl_pathc; i++) { - if (i == 0) - free(s); - argv[*argc + i] = strdup(globbuf.gl_pathv[i]); - if (!argv[*argc + i]) - out_of_memory("glob_expand"); + if (globbuf.gl_pathc == 0) + argv[argc++] = s; + else { + int j = globbuf.gl_pathc; + if (j > maxargs - argc) + j = maxargs - argc; + free(s); + for (i = 0; i < j; i++) { + if (!(argv[argc++] = strdup(globbuf.gl_pathv[i]))) + out_of_memory("glob_expand_one"); + } } globfree(&globbuf); - *argc += i; #endif + *argc_ptr = argc; } /* This routine is only used in daemon mode. */ -void glob_expand(char *base1, char **argv, int *argc, int maxargs) +void glob_expand(char *base1, char **argv, int *argc_ptr, int maxargs) { - char *s = argv[*argc]; + char *s = argv[*argc_ptr]; char *p, *q; char *base = base1; int base_len = strlen(base); - if (!s || !*s) return; + if (!s || !*s) + return; if (strncmp(s, base, base_len) == 0) s += base_len; - s = strdup(s); - if (!s) out_of_memory("glob_expand"); + if (!(s = strdup(s))) + out_of_memory("glob_expand"); - if (asprintf(&base," %s/", base1) <= 0) out_of_memory("glob_expand"); + if (asprintf(&base," %s/", base1) <= 0) + out_of_memory("glob_expand"); base_len++; - q = s; - while ((p = strstr(q,base)) != NULL && *argc < maxargs) { - /* split it at this point */ - *p = 0; - glob_expand_one(q, argv, argc, maxargs); - q = p + base_len; + for (q = s; *q; q = p + base_len) { + if ((p = strstr(q, base)) != NULL) + *p = '\0'; /* split it at this point */ + glob_expand_one(q, argv, argc_ptr, maxargs); + if (!p) + break; } - if (*q && *argc < maxargs) - glob_expand_one(q, argv, argc, maxargs); - free(s); free(base); } @@ -574,8 +577,8 @@ void glob_expand(char *base1, char **argv, int *argc, int maxargs) void strlower(char *s) { while (*s) { - if (isupper(* (unsigned char *) s)) - *s = tolower(* (unsigned char *) s); + if (isupper(*(unsigned char *)s)) + *s = tolower(*(unsigned char *)s); s++; } } @@ -638,7 +641,8 @@ void clean_fname(char *name) int l; int modified = 1; - if (!name) return; + if (!name) + return; while (modified) { modified = 0; @@ -974,7 +978,8 @@ int unsafe_symlink(const char *dest, const char *src) int depth = 0; /* all absolute and null symlinks are unsafe */ - if (!dest || !*dest || *dest == '/') return 1; + if (!dest || !*dest || *dest == '/') + return 1; /* find out what our safety margin is */ for (name = src; (slash = strchr(name, '/')) != 0; name = slash+1) { -- 2.34.1