* libamu/wire.c (print_wires): convert argument-less xsnprintf to
authorErez Zadok <ezk@cs.sunysb.edu>
Fri, 7 Oct 2005 01:40:03 +0000 (01:40 +0000)
committerErez Zadok <ezk@cs.sunysb.edu>
Fri, 7 Oct 2005 01:40:03 +0000 (01:40 +0000)
xstrlcpy.

* include/am_utils.h: use new HAVE_C99_VARARGS_MACROS or
HAVE_GCC_VARARGS_MACROS to pass file name and line number to
xsnprintf/xvsnprintf.

* libamu/strutil.c (xsnprintf, xvsnprintf): if debugging is on,
then also print the source file name and line number that called
xsnprintf/xvsnprintf with a buffer that wasn't large enough (most
likely an am-utils bug).

* Makefile.am (EXTRA_DIST_M4): distribute new
check_varargs_macros.m4 file.

* configure.in: execute new AMU_VARARGS_MACROS test.

* m4/macros/check_varargs_macros.m4: new test to check what style
of variable-length argument macros, if any, does the
compiler/pre-processor supports.

ChangeLog
Makefile.am
configure.in
include/am_utils.h
libamu/strutil.c
libamu/wire.c
m4/macros/check_varargs_macros.m4 [new file with mode: 0644]

index d950e65c42899618c39670c41fb9c0ebb6544eff..9641636e440c9a15956018a35e8453638ae3fa98 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,26 @@
 2005-10-06  Erez Zadok  <ezk@cs.sunysb.edu>
 
+       * libamu/wire.c (print_wires): convert argument-less xsnprintf to
+       xstrlcpy.
+
+       * include/am_utils.h: use new HAVE_C99_VARARGS_MACROS or
+       HAVE_GCC_VARARGS_MACROS to pass file name and line number to
+       xsnprintf/xvsnprintf.
+
+       * libamu/strutil.c (xsnprintf, xvsnprintf): if debugging is on,
+       then also print the source file name and line number that called
+       xsnprintf/xvsnprintf with a buffer that wasn't large enough (most
+       likely an am-utils bug).
+
+       * Makefile.am (EXTRA_DIST_M4): distribute new
+       check_varargs_macros.m4 file.
+
+       * configure.in: execute new AMU_VARARGS_MACROS test.
+
+       * m4/macros/check_varargs_macros.m4: new test to check what style
+       of variable-length argument macros, if any, does the
+       compiler/pre-processor supports.
+
        * conf/autofs/autofs_solaris_v2_v3.c (autofs_lookup_2_req)
        conf/autofs/autofs_solaris_v1.c (autofs_mount_1_req), amd/opts.c,
        amd/nfs_subr.c (nfsproc_lookup_2_svc), amd/nfs_start.c
index 760dce4cbd6bc1f33b01489d9e9c91ea18ddcc1e..e64c20e7e9bb9edc527c3ccf48c8407764a46d9a 100644 (file)
@@ -81,6 +81,7 @@ EXTRA_DIST_M4 =                               \
        m4/macros/check_umount_style.m4                 \
        m4/macros/check_unmount_args.m4                 \
        m4/macros/check_unmount_call.m4                 \
+       m4/macros/check_varargs_macros.m4               \
        m4/macros/expand_cpp_hex.m4                     \
        m4/macros/expand_cpp_int.m4                     \
        m4/macros/expand_cpp_string.m4                  \
index b28e787de28f7fdacd41e3e681f549b32a3ba16d..ff9e8da4c33875f006d7043a1ce5575e553215c7 100644 (file)
@@ -55,7 +55,7 @@ AH_BOTTOM([
 dnl
 dnl AC_CONFIG_AUX_DIR(m4)
 AC_PREREQ(2.52)
-AC_REVISION($Revision: 1.118 $)
+AC_REVISION($Revision: 1.119 $)
 AC_COPYRIGHT([Copyright (c) 1997-2005 Erez Zadok])
 dnl find out system type
 AC_MSG_NOTICE(*** SYSTEM TYPES ***)
@@ -772,6 +772,7 @@ dnl ======================================================================
 dnl Generic Compiler Characteristics
 AC_MSG_NOTICE(*** GENERIC COMPILER CHARACTERISTICS ***)
 AMU_C_VOID_P
+AMU_VARARGS_MACROS
 dnl ======================================================================
 
 dnl *********
index 4d745a8990b778934f415f3e2ce83fc63cf2a598..43a8bcd8f22ec01acc6e39515738961eb62dfd3f 100644 (file)
@@ -333,14 +333,26 @@ extern long get_server_pid(void);
 extern void setup_sighandler(int signum, void (*handler)(int));
 extern time_t clocktime(nfstime *nt);
 
+#if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
+# ifdef HAVE_C99_VARARGS_MACROS
+#define xsnprintf(str,size,fmt,...)    _xsnprintf(__FILE__,__LINE__,(str),(size),(fmt),__VA_ARGS__)
+# endif /* HAVE_C99_VARARGS_MACROS */
+# ifdef HAVE_GCC_VARARGS_MACROS
+#define xsnprintf(str,size,fmt,args...)                _xsnprintf(__FILE__,__LINE__,(str),(size),(fmt),args)
+# endif /* HAVE_GCC_VARARGS_MACROS */
+extern int _xsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, ...);
+#define xvsnprintf(str,size,fmt,ap)    _xvsnprintf(__FILE__,__LINE__,(str),(size),(fmt),(ap))
+extern int _xvsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, va_list ap);
+#else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
 extern int xsnprintf(char *str, size_t size, const char *format, ...);
 extern int xvsnprintf(char *str, size_t size, const char *format, va_list ap);
+#endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
 
 #ifdef DEBUG
-extern void _xstrlcat(char *dst, const char *src, size_t len, const char *filename, int lineno);
-# define xstrlcat(d,s,l)       _xstrlcat((d),(s),(l),__FILE__,__LINE__)
-extern void _xstrlcpy(char *dst, const char *src, size_t len, const char *filename, int lineno);
-# define xstrlcpy(d,s,l)       _xstrlcpy((d),(s),(l),__FILE__,__LINE__)
+extern void _xstrlcat(const char *filename, int lineno, char *dst, const char *src, size_t len);
+# define xstrlcat(d,s,l)       _xstrlcat(__FILE__,__LINE__,(d),(s),(l))
+extern void _xstrlcpy(const char *filename, int lineno, char *dst, const char *src, size_t len);
+# define xstrlcpy(d,s,l)       _xstrlcpy(__FILE__,__LINE__,(d),(s),(l))
 #else /* not DEBUG */
 extern void xstrlcat(char *dst, const char *src, size_t len);
 extern void xstrlcpy(char *dst, const char *src, size_t len);
index 2405f15c3a84a63dd3991a1c1a258cafb8764c0a..17ddc7cc22fa494527de73ff6830cc3408c806b8 100644 (file)
@@ -162,7 +162,7 @@ strsplit(char *s, int ch, int qc)
  */
 void
 #ifdef DEBUG
-_xstrlcpy(char *dst, const char *src, size_t len, const char *filename, int lineno)
+_xstrlcpy(const char *filename, int lineno, char *dst, const char *src, size_t len)
 #else /* not DEBUG */
 xstrlcpy(char *dst, const char *src, size_t len)
 #endif /* not DEBUG */
@@ -189,7 +189,7 @@ xstrlcpy(char *dst, const char *src, size_t len)
  */
 void
 #ifdef DEBUG
-_xstrlcat(char *dst, const char *src, size_t len, const char *filename, int lineno)
+_xstrlcat(const char *filename, int lineno, char *dst, const char *src, size_t len)
 #else /* not DEBUG */
 xstrlcat(char *dst, const char *src, size_t len)
 #endif /* not DEBUG */
@@ -211,13 +211,21 @@ xstrlcat(char *dst, const char *src, size_t len)
 
 /* our version of snprintf */
 int
+#if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
+_xsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, ...)
+#else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
 xsnprintf(char *str, size_t size, const char *format, ...)
+#endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
 {
   va_list ap;
   int ret = 0;
 
   va_start(ap, format);
+#if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
+  ret = _xvsnprintf(filename, lineno, str, size, format, ap);
+#else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
   ret = xvsnprintf(str, size, format, ap);
+#endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
   va_end(ap);
 
   return ret;
@@ -226,7 +234,11 @@ xsnprintf(char *str, size_t size, const char *format, ...)
 
 /* our version of vsnprintf */
 int
+#if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
+_xvsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, va_list ap)
+#else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
 xvsnprintf(char *str, size_t size, const char *format, va_list ap)
+#endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
 {
   int ret = 0;
 
@@ -245,8 +257,13 @@ xvsnprintf(char *str, size_t size, const char *format, va_list ap)
   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\")",
+#if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
+      plog(XLOG_ERROR, "xvsnprintf(%s:%d): string %p truncated (ret=%d, format=\"%s\")",
+           filename, lineno, str, ret, format);
+#else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
+      plog(XLOG_ERROR, "xvsnprintf: string %p truncated (ret=%d, format=\"%s\")",
            str, ret, format);
+#endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
   }
 
   return ret;
index 2a1c44cfb8e6b42f8c9a2c0475f98347dc66eb7d..21d11f951ef021840ec65364f9e983fee1e6ead5 100644 (file)
@@ -106,7 +106,7 @@ print_wires(void)
     return NULL;
 
   if (!localnets) {
-    xsnprintf(buf, buf_size, "No networks.\n");
+    xstrlcpy(buf, "No networks\n", buf_size);
     return buf;
   }
   /* check if there's more than one network */
diff --git a/m4/macros/check_varargs_macros.m4 b/m4/macros/check_varargs_macros.m4
new file mode 100644 (file)
index 0000000..dfec692
--- /dev/null
@@ -0,0 +1,31 @@
+dnl ######################################################################
+dnl check if CPP can handle variable-length argument macros
+AC_DEFUN([AMU_VARARGS_MACROS],
+[
+AC_CACHE_CHECK(if pre-processor can handle variable-length macros,
+ac_cv_varargs_macros,
+[
+# try C99 style
+AC_PREPROC_IFELSE(
+[
+#define foo(format, ...)    bar(format, __VA_ARGS__)
+], ac_cv_varargs_macros=c99,
+# else try gcc style
+AC_PREPROC_IFELSE(
+[
+#define foo(format, args...)   bar(format, args)
+], ac_cv_varargs_macros=gcc, ac_cv_varargs_macros=none))
+])
+if test "$ac_cv_varargs_macros" = c99
+then
+  AC_DEFINE(HAVE_C99_VARARGS_MACROS, 1,
+        [System supports C99-style variable-length argument macros])
+else
+  if test "$ac_cv_varargs_macros" = gcc
+  then
+    AC_DEFINE(HAVE_GCC_VARARGS_MACROS, 1,
+        [System supports GCC-style variable-length argument macros])
+  fi
+fi
+])
+dnl ======================================================================