* libamu/Makefile.am: use strutil.c, not util.c. before-xstr
authorErez Zadok <ezk@cs.sunysb.edu>
Mon, 3 Oct 2005 01:01:10 +0000 (01:01 +0000)
committerErez Zadok <ezk@cs.sunysb.edu>
Mon, 3 Oct 2005 01:01:10 +0000 (01:01 +0000)
* 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.

ChangeLog
libamu/Makefile.am
libamu/strutil.c
libamu/xutil.c

index 973c3520a9d17d696e0f544d9d948c316350e41c..f9ea3cf833a971a0cb9d7c949d07c32369b32e61 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2005-10-02  Erez Zadok  <ezk@cs.sunysb.edu>
+
+       * 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  <ezk@cs.sunysb.edu>
 
        * m4/macros/header_templates.m4: templates for FFS.
index fc9f50811b58b0c8e379e06598402ff3df23fd34..76706c84184aeb0fde2c474a2dceb4b163cdc529 100644 (file)
@@ -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
index c2d0bc71d82e4543f891fc9378b0cc5e9a4a0236..8688a8398fcfc33c62ccc35bb03558b5152e08ca 100644 (file)
  * 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;
+}
index 46fba74b19e654e5540e8dff6e12ceeb0dc2b983..9ded0bd437c7866faac10d0633f225b28bd20d4c 100644 (file)
  *
  */
 
+/*
+ * Miscellaneous Utilities: Logging, TTY, timers, signals, RPC, memory, etc.
+ */
+
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #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);
+}
+