commit f034ca39df7f6f513d0d4d4f479194592ac48645
parent d535c86b0f886e1c4cf0ae73048bf4bdba6d0813
Author: tgoodwin <tgoodwin>
Date: Wed, 28 Oct 1998 14:15:21 +0000
use va_copy() where it exists
Diffstat:
5 files changed, 34 insertions(+), 104 deletions(-)
diff --git a/acconfig.h b/acconfig.h
@@ -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
@@ -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
@@ -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))
diff --git a/print.c b/print.c
@@ -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
diff --git a/proto.h b/proto.h
@@ -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>