Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!uwvax!oddjob!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: using varargs function to call another varargs function Message-ID: <7269@mimsy.UUCP> Date: Tue, 30-Jun-87 21:16:53 EDT Article-I.D.: mimsy.7269 Posted: Tue Jun 30 21:16:53 1987 Date-Received: Wed, 1-Jul-87 07:24:27 EDT References: <1332@rosevax.Rosemount.COM> <7268@mimsy.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 55 Oops: I left a `va_end(l)' out of the source in the parent article, <7268@mimsy.UUCP>. For a second example, here is the error() routine from the local C library. Note that it `cheats' by declaring three fixed arguments before the variable argument list. It also depends on crt0.o (the thing that calls main()) to set `_argv0' equal to main's argv[0]. This way all programs that call `error' print out their own names before printing their error messages. #include #include char *_argv0; /* argv[0], set by C startup code */ /* * error - University of Maryland specific (sigh) * * Useful for printing error messages. Will print the program name * and (optionally) the system error associated with the values in * . */ error(quit, e, fmt, va_alist) int quit; register int e; char *fmt; va_dcl { extern char *sys_errlist[]; extern int sys_nerr, errno; va_list l; register char *p = _argv0; if (e < 0) e = errno; (void) fflush(stdout); /* somewhat gratuitous */ if (p != NULL) (void) fprintf(stderr, "%s: ", p); /*** the next three lines are the interesting ones ***/ va_start(l); (void) vfprintf(stderr, fmt, l); va_end(l); /******/ if (e > 0) { if (e < sys_nerr) (void) fprintf(stderr, ": %s", sys_errlist[e]); else (void) fprintf(stderr, ": unknown error number %d", e); } (void) putc('\n', stderr); (void) fflush(stderr); if (quit) exit(quit); } -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris