Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!pasteur!ucbvax!MIMSY.UMD.EDU!chris From: chris@MIMSY.UMD.EDU (Chris Torek) Newsgroups: comp.sys.pyramid Subject: Re: Wrapper function to sprintf Message-ID: <8803040308.AA02292@mimsy.umd.edu> Date: 4 Mar 88 03:08:20 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 62 I am afraid I have to agree with Greg Hackney's own description of his method: I couldn't figure out how to pass an array of pointers to sprintf() and make it work correctly, but here's a dippy way that does work. The reason you cannot do this `correctly' is simple. sprintf takes a variable argument list; the desired `wrapper' function also takes a variable argument list; but there is no way to say `take my argument list, whatever it may have been, package it up, and send it on to this other function'. In other words, the only correct call to sprintf is `direct' one. There *is* a way to implement the desired `dialog' function, however. A careful scan of the manuals reveals the `vsprintf' function. (This is in SysV, at any rate, and an equivalent 4BSD version appears below. The 4BSD version is not Officially Approved.) #include #include /* char *dialog(char *fmt, ...); */ char *dialog(); main() { (void) dialog("%s\t%s\t%s", "one", "two", "three"); } char * dialog(va_alist) va_dcl { va_list ap; char *format; char buffer[512]; /* this had best be big enough */ va_start(ap); format = va_arg(ap, char *); (void) vsprintf(buffer, format, ap); va_end(ap); printf("%s\n", buffer); return (buffer); } /* if you need it: */ #ifdef notdef int vsprintf(str, fmt, arg) char *str, *fmt; va_list arg; { FILE f; int ret; f._flag = _IOSTRG; /* leave out _IOWRT to avoid libc bug */ f._base = f._ptr = str; ret = _doprnt(fmt, arg, &f); *f._ptr = 0; return (ret); } #endif