Article View: comp.sys.ibm.pc
Article #26445Re: How about a `gprintf' for Turbo C
From: Chris Torek
Date: Wed, 20 Apr 1988 22:34
Date: Wed, 20 Apr 1988 22:34
51 lines
1691 bytes
1691 bytes
In article <4576@cup.portal.com> Devin_E_Ben-Hur@cup.portal.com writes:
>int gprintf(char *format, ...) {
> char buf[81];
...
> ret = vsprintf(buf,format,args);
> outtext(buf);
I thought we just finished arguing over why `sprintf' is evil :-) .
Note that this limits you to printing 80 characters or less. If
you had a function-style stdio, you could do this:
#include <stdio.h>
#include <stdarg.h>
/* etc */
#define GP_BUFSIZE 80 /* or whatever */
static int gwrite(void *context, const char *buf, int n) {
char *realbuf = (char *)context;
int k, left = n;
while (left > 0) {
k = left > GP_BUFSIZE ? GP_BUFSIZE : left;
(void) memmove((void *)realbuf, (void *)buf, k);
realbuf[k] = 0;
outtext(realbuf);
buf += k;
left -= k;
}
return (n);
}
int gprintf(char *fmt, ...) {
FILE *fp;
va_list ap;
int rv;
char buf[GP_BUFSIZE + 1]; /* leave room for '\0'! */
if ((fp = fwopen((void *)buf, gwrite)) == NULL)
return (-1);
va_start(ap, fmt);
rv = vfprintf(fp, fmt, ap);
va_end(ap);
(void) fclose(fp);
return (rv);
}
A bit clumsy, I admit, but this has no output restrictions.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
Technical Details
Message-ID:
<11141@mimsy.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!uunet!lll-winken!lll-tis!ames!umd5!mimsy!chrisReferences:
<686@leah.Albany.Edu> <4576@cup.portal.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!lll-winken!lll-tis!ames!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.sys.ibm.pc,comp.lang.c Subject: Re: How about a `gprintf' for Turbo C Message-ID: <11141@mimsy.UUCP> Date: 20 Apr 88 22:34:51 GMT References: <686@leah.Albany.Edu> <4576@cup.portal.com> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 50