From: Erez Zadok Date: Mon, 3 Oct 2005 01:01:10 +0000 (+0000) Subject: * libamu/Makefile.am: use strutil.c, not util.c. X-Git-Tag: before-xstr X-Git-Url: https://git.fsl.cs.stonybrook.edu/?a=commitdiff_plain;h=38d3034720034092abb6cbddf75a0ca1362cd6b3;p=am-utils-6.1.git * libamu/Makefile.am: use strutil.c, not util.c. * libamu/strutil.c: rename from util.c to explain better purpose of file. Move xvsnprintf and xsnprintf from xutil.c to this file. * libamu/xutil.c: explain purpose of file. Move mkdirs/rmdirs code from old util.c. --- diff --git a/ChangeLog b/ChangeLog index 973c352..f9ea3cf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2005-10-02 Erez Zadok + + * libamu/Makefile.am: use strutil.c, not util.c. + + * libamu/strutil.c: rename from util.c to explain better purpose + of file. Move xvsnprintf and xsnprintf from xutil.c to this file. + + * libamu/xutil.c: explain purpose of file. Move mkdirs/rmdirs + code from old util.c. + 2005-10-01 Erez Zadok * m4/macros/header_templates.m4: templates for FFS. diff --git a/libamu/Makefile.am b/libamu/Makefile.am index fc9f508..76706c8 100644 --- a/libamu/Makefile.am +++ b/libamu/Makefile.am @@ -16,7 +16,7 @@ libamu_la_SOURCES = \ mount_fs.c \ mtab.c \ nfs_prot_xdr.c \ - util.c \ + strutil.c \ wire.c \ xdr_func.c \ xutil.c diff --git a/libamu/strutil.c b/libamu/strutil.c index c2d0bc7..8688a83 100644 --- a/libamu/strutil.c +++ b/libamu/strutil.c @@ -37,12 +37,12 @@ * SUCH DAMAGE. * * - * File: am-utils/libamu/util.c + * File: am-utils/libamu/strutil.c * */ /* - * General Utilities. + * String Utilities. */ #ifdef HAVE_CONFIG_H @@ -82,45 +82,6 @@ str3cat(char *p, char *s1, char *s2, char *s3) } -/* - * Use generic strlcpy to copy a string more carefully, null-terminating it - * as needed. However, if the copied string was truncated due to lack of - * space, then warn us. - * - * For now, xstrlcpy returns VOID because it doesn't look like anywhere in - * the Amd code do we actually use the return value of strncpy/strlcpy. - */ -void -xstrlcpy(char *dst, const char *src, size_t len) -{ - if (len == 0) - return; - if (strlcpy(dst, src, len) >= len) - plog(XLOG_ERROR, "xstrlcpy: string \"%s\" truncated to \"%s\"", src, dst); -} - - -/* - * Use generic strlcat to concatenate a string more carefully, - * null-terminating it as needed. However, if the copied string was - * truncated due to lack of space, then warn us. - * - * For now, xstrlcat returns VOID because it doesn't look like anywhere in - * the Amd code do we actually use the return value of strncat/strlcat. - */ -void -xstrlcat(char *dst, const char *src, size_t len) -{ - if (len == 0) - return; - if (strlcat(dst, src, len) >= len) { - /* strlcat does not null terminate if the size of src is equal to len. */ - dst[strlen(dst) - 1] = '\0'; - plog(XLOG_ERROR, "xstrlcat: string \"%s\" truncated to \"%s\"", src, dst); - } -} - - /* * Split s using ch as delimiter and qc as quote character */ @@ -192,89 +153,83 @@ strsplit(char *s, int ch, int qc) /* - * Make all the directories in the path. + * Use generic strlcpy to copy a string more carefully, null-terminating it + * as needed. However, if the copied string was truncated due to lack of + * space, then warn us. + * + * For now, xstrlcpy returns VOID because it doesn't look like anywhere in + * the Amd code do we actually use the return value of strncpy/strlcpy. */ -int -mkdirs(char *path, int mode) +void +xstrlcpy(char *dst, const char *src, size_t len) { - /* - * take a copy in case path is in readonly store - */ - char *p2 = strdup(path); - char *sp = p2; - struct stat stb; - int error_so_far = 0; - - /* - * Skip through the string make the directories. - * Mostly ignore errors - the result is tested at the end. - * - * This assumes we are root so that we can do mkdir in a - * mode 555 directory... - */ - while ((sp = strchr(sp + 1, '/'))) { - *sp = '\0'; - if (mkdir(p2, mode) < 0) { - error_so_far = errno; - } else { - dlog("mkdir(%s)", p2); - } - *sp = '/'; - } - - if (mkdir(p2, mode) < 0) { - error_so_far = errno; - } else { - dlog("mkdir(%s)", p2); - } - - XFREE(p2); - - return stat(path, &stb) == 0 && - (stb.st_mode & S_IFMT) == S_IFDIR ? 0 : error_so_far; + if (len == 0) + return; + if (strlcpy(dst, src, len) >= len) + plog(XLOG_ERROR, "xstrlcpy: string \"%s\" truncated to \"%s\"", src, dst); } /* - * Remove as many directories in the path as possible. - * Give up if the directory doesn't appear to have - * been created by Amd (not mode dr-x) or an rmdir - * fails for any reason. + * Use generic strlcat to concatenate a string more carefully, + * null-terminating it as needed. However, if the copied string was + * truncated due to lack of space, then warn us. + * + * For now, xstrlcat returns VOID because it doesn't look like anywhere in + * the Amd code do we actually use the return value of strncat/strlcat. */ void -rmdirs(char *dir) +xstrlcat(char *dst, const char *src, size_t len) { - char *xdp = strdup(dir); - char *dp; + if (len == 0) + return; + if (strlcat(dst, src, len) >= len) { + /* strlcat does not null terminate if the size of src is equal to len. */ + dst[strlen(dst) - 1] = '\0'; + plog(XLOG_ERROR, "xstrlcat: string \"%s\" truncated to \"%s\"", src, dst); + } +} - do { - struct stat stb; - /* - * Try to find out whether this was - * created by amd. Do this by checking - * for owner write permission. - */ - if (stat(xdp, &stb) == 0 && (stb.st_mode & 0200) == 0) { - if (rmdir(xdp) < 0) { - if (errno != ENOTEMPTY && - errno != EBUSY && - errno != EEXIST && - errno != EROFS && - errno != EINVAL) - plog(XLOG_ERROR, "rmdir(%s): %m", xdp); - break; - } else { - dlog("rmdir(%s)", xdp); - } - } else { - break; - } - dp = strrchr(xdp, '/'); - if (dp) - *dp = '\0'; - } while (dp && dp > xdp); +/* our version of snprintf */ +int +xsnprintf(char *str, size_t size, const char *format, ...) +{ + va_list ap; + int ret = 0; + + va_start(ap, format); + ret = xvsnprintf(str, size, format, ap); + va_end(ap); - XFREE(xdp); + return ret; } + +/* our version of vsnprintf */ +int +xvsnprintf(char *str, size_t size, const char *format, va_list ap) +{ + int ret = 0; + +#ifdef HAVE_VSNPRINTF + ret = vsnprintf(str, size, format, ap); +#else /* not HAVE_VSNPRINTF */ + ret = vsprintf(str, format, ap); /* less secure version */ +#endif /* not HAVE_VSNPRINTF */ + /* + * If error or truncation, plog error. + * + * WARNING: we use the static 'maxtrunc' variable below to break out any + * possible infinite recursion between plog() and xvsnprintf(). If it + * ever happens, it'd indicate a bug in Amd. + */ + if (ret < 0 || ret >= size) { /* error or truncation occured */ + static int maxtrunc; /* hack to avoid inifinite loop */ + if (++maxtrunc > 10) + plog(XLOG_ERROR, "BUG: string %p truncated (ret=%d, format=\"%s\")", + str, ret, format); + } + + return ret; +} diff --git a/libamu/xutil.c b/libamu/xutil.c index 46fba74..9ded0bd 100644 --- a/libamu/xutil.c +++ b/libamu/xutil.c @@ -41,6 +41,10 @@ * */ +/* + * Miscellaneous Utilities: Logging, TTY, timers, signals, RPC, memory, etc. + */ + #ifdef HAVE_CONFIG_H # include #endif /* HAVE_CONFIG_H */ @@ -901,7 +905,6 @@ get_amd_program_number(void) /* set the rpc program number used for amd */ void set_amd_program_number(u_long program) - { amd_program_number = program; } @@ -978,50 +981,6 @@ amu_release_controlling_tty(void) } -/* our version of snprintf */ -int -xsnprintf(char *str, size_t size, const char *format, ...) -{ - va_list ap; - int ret = 0; - - va_start(ap, format); - ret = xvsnprintf(str, size, format, ap); - va_end(ap); - - return ret; -} - - -/* our version of vsnprintf */ -int -xvsnprintf(char *str, size_t size, const char *format, va_list ap) -{ - int ret = 0; - -#ifdef HAVE_VSNPRINTF - ret = vsnprintf(str, size, format, ap); -#else /* not HAVE_VSNPRINTF */ - ret = vsprintf(str, format, ap); /* less secure version */ -#endif /* not HAVE_VSNPRINTF */ - /* - * If error or truncation, plog error. - * - * WARNING: we use the static 'maxtrunc' variable below to break out any - * possible infinite recursion between plog() and xvsnprintf(). If it - * ever happens, it'd indicate a bug in Amd. - */ - if (ret < 0 || ret >= size) { /* error or truncation occured */ - static int maxtrunc; /* hack to avoid inifinite loop */ - if (++maxtrunc > 10) - plog(XLOG_ERROR, "BUG: string %p truncated (ret=%d, format=\"%s\")", - str, ret, format); - } - - return ret; -} - - /* setup a single signal handler */ void setup_sighandler(int signum, void (*handler)(int)) @@ -1062,3 +1021,92 @@ clocktime(nfstime *nt) } return (time_t) now.tv_sec; } + + +/* + * Make all the directories in the path. + */ +int +mkdirs(char *path, int mode) +{ + /* + * take a copy in case path is in readonly store + */ + char *p2 = strdup(path); + char *sp = p2; + struct stat stb; + int error_so_far = 0; + + /* + * Skip through the string make the directories. + * Mostly ignore errors - the result is tested at the end. + * + * This assumes we are root so that we can do mkdir in a + * mode 555 directory... + */ + while ((sp = strchr(sp + 1, '/'))) { + *sp = '\0'; + if (mkdir(p2, mode) < 0) { + error_so_far = errno; + } else { + dlog("mkdir(%s)", p2); + } + *sp = '/'; + } + + if (mkdir(p2, mode) < 0) { + error_so_far = errno; + } else { + dlog("mkdir(%s)", p2); + } + + XFREE(p2); + + return stat(path, &stb) == 0 && + (stb.st_mode & S_IFMT) == S_IFDIR ? 0 : error_so_far; +} + + +/* + * Remove as many directories in the path as possible. + * Give up if the directory doesn't appear to have + * been created by Amd (not mode dr-x) or an rmdir + * fails for any reason. + */ +void +rmdirs(char *dir) +{ + char *xdp = strdup(dir); + char *dp; + + do { + struct stat stb; + /* + * Try to find out whether this was + * created by amd. Do this by checking + * for owner write permission. + */ + if (stat(xdp, &stb) == 0 && (stb.st_mode & 0200) == 0) { + if (rmdir(xdp) < 0) { + if (errno != ENOTEMPTY && + errno != EBUSY && + errno != EEXIST && + errno != EROFS && + errno != EINVAL) + plog(XLOG_ERROR, "rmdir(%s): %m", xdp); + break; + } else { + dlog("rmdir(%s)", xdp); + } + } else { + break; + } + + dp = strrchr(xdp, '/'); + if (dp) + *dp = '\0'; + } while (dp && dp > xdp); + + XFREE(xdp); +} +