diff options
| -rw-r--r-- | acconfig.h | 3 | ||||
| -rw-r--r-- | acinclude.m4 | 12 | ||||
| -rw-r--r-- | configure.ac | 6 | ||||
| -rw-r--r-- | print.c | 110 | ||||
| -rw-r--r-- | proto.h | 7 |
5 files changed, 34 insertions, 104 deletions
@@ -75,3 +75,6 @@ /* Define if you have POSIX getgroups(). */ #undef HAVE_POSIX_GETGROUPS + +/* Define if you have va_copy(). */ +#undef HAVE_VA_COPY diff --git a/acinclude.m4 b/acinclude.m4 index 60ec318..2ac2cab 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -34,3 +34,15 @@ int main(void) { yes) AC_DEFINE(SETPGRP_VOID) ;; esac ]) + +dnl Check for va_copy() in <stdarg.h>. This is new in C 9x. +AC_DEFUN(RC_HAVE_VA_COPY, [ + AC_CACHE_CHECK(for va_copy(), rc_cv_have_va_copy, AC_EGREP_CPP(yes, [ +#ifdef va_copy +yes +#endif +], rc_cv_have_va_copy=yes, rc_cv_have_va_copy=no)) + case "$rc_cv_have_va_copy" in + yes) AC_DEFINE(HAVE_VA_COPY) ;; + esac +]) diff --git a/configure.ac b/configure.ac index cfcd54f..d55ca11 100644 --- a/configure.ac +++ b/configure.ac @@ -1,3 +1,6 @@ +dnl A lot of the stuff in this file should be moved into acinclude.m4, and +dnl eventually into the automake distribution... + AC_INIT(rc.h) dnl Automake stuff. @@ -224,6 +227,9 @@ main() { ;; esac +RC_HAVE_VA_COPY + +dnl Now handle arguments. AC_ARG_ENABLE(builtin-echo, [ --disable-builtin-echo Don't include \`echo' as a builtin], test "x$enableval" != "xno" && AC_DEFINE(RC_ECHO), AC_DEFINE(RC_ECHO)) @@ -289,11 +289,11 @@ extern int fmtprint(Format *format, const char *fmt,...) { va_list ap, saveargs; va_start(ap, fmt); - saveargs = format->args; - format->args = ap; + va_copy(saveargs, format->args); + va_copy(format->args, ap); n += printfmt(format, fmt); va_end(format->args); - format->args = saveargs; + va_copy(format->args, saveargs); return n + format->flushed; } @@ -320,7 +320,7 @@ extern int fprint(int fd, const char *fmt,...) { format.u.n = fd; va_start(ap, fmt); - format.args = ap; + va_copy(format.args, ap); printfmt(&format, fmt); va_end(format.args); @@ -365,7 +365,7 @@ extern char *mprint(const char *fmt,...) { format.u.n = 1; va_start(ap, fmt); - format.args = ap; + va_copy(format.args, ap); result = memprint(&format, fmt, ealloc(PRINT_ALLOCSIZE), PRINT_ALLOCSIZE); va_end(format.args); return result; @@ -378,106 +378,8 @@ extern char *nprint(const char *fmt,...) { format.u.n = 0; va_start(ap, fmt); - format.args = ap; + va_copy(format.args, ap); result = memprint(&format, fmt, nalloc(PRINT_ALLOCSIZE), PRINT_ALLOCSIZE); va_end(format.args); return result; } - - -/* THESE ARE UNUSED IN rc */ - -#if 0 - -extern int print(const char *fmt,...) { - char buf[1024]; - Format format; - - format.buf = buf; - format.bufbegin = buf; - format.bufend = buf + sizeof buf; - format.grow = fprint_flush; - format.flushed = 0; - format.u.n = 1; - - va_start(format.args, fmt); - printfmt(&format, fmt); - va_end(format.args); - - fprint_flush(&format, 0); - return format.flushed; -} - -extern int eprint(const char *fmt,...) { - char buf[1024]; - Format format; - - format.buf = buf; - format.bufbegin = buf; - format.bufend = buf + sizeof buf; - format.grow = fprint_flush; - format.flushed = 0; - format.u.n = 2; - - va_start(format.args, fmt); - printfmt(&format, fmt); - va_end(format.args); - - fprint_flush(&format, 0); - return format.flushed; -} - -static void snprint_grow(Format *format, size_t more) { - longjmp(format->u.p, 1); -} - -extern int snprint(char *buf, int buflen, const char *fmt,...) { - int n; - jmp_buf jbuf; - Format format; - - if (setjmp(jbuf)) { - *format.buf = '\0'; - return format.buf - format.bufbegin; - } - - format.buf = buf; - format.bufbegin = buf; - format.bufend = buf + buflen - 1; - format.grow = snprint_grow; - format.flushed = 0; - format.u.p = jbuf; - - va_start(format.args, fmt); - n = printfmt(&format, fmt); - va_end(format.args); - - *format.buf = '\0'; - return n; -} - -extern int sprint(char *buf, const char *fmt,...) { - int n; - jmp_buf jbuf; - Format format; - - if (setjmp(jbuf)) { - *format.buf = '\0'; - return format.buf - format.bufbegin; - } - - format.buf = buf; - format.bufbegin = buf; - format.bufend = buf + SPRINT_BUFSIZ - 1; - format.grow = snprint_grow; - format.flushed = 0; - format.u.p = jbuf; - - va_start(format.args, fmt); - n = printfmt(&format, fmt); - va_end(format.args); - - *format.buf = '\0'; - return n; -} -#endif @@ -23,6 +23,13 @@ typedef long align_t; #include <stdarg.h> +/* C 9x specifies a va_copy() macro which should be used for copying +objects of type va_list. Of course, most places don't have this yet, +but where it does exist we need to use it. */ +#ifndef va_copy +#define va_copy(x,y) (x)=(y) +#endif + #if STDC_HEADERS #include <stdlib.h> |