ack/modules/src/print/format.c

109 lines
1.6 KiB
C
Raw Normal View History

1987-03-09 15:15:03 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1987-01-05 17:37:37 +00:00
/* $Header$ */
1993-11-10 12:09:49 +00:00
#include <system.h>
#include "print.h"
1988-04-15 15:34:19 +00:00
extern char *long2str();
1987-01-05 17:37:37 +00:00
static int
integral(c)
{
switch (c) {
case 'b':
return -2;
case 'd':
return 10;
case 'o':
return -8;
case 'u':
return -10;
case 'x':
return -16;
}
return 0;
}
/*FORMAT1 $
%s = char *
%l = long
%c = int
%[uxbo] = unsigned int
%d = int
$ */
1987-01-05 17:37:37 +00:00
int
_format(buf, fmt, argp)
char *buf, *fmt;
1988-04-15 15:34:19 +00:00
register va_list argp;
1987-01-05 17:37:37 +00:00
{
1988-04-15 15:34:19 +00:00
register char *pf = fmt;
1987-01-05 17:37:37 +00:00
register char *pb = buf;
while (*pf) {
if (*pf == '%') {
register width, base, pad, npad;
char *arg;
char cbuf[2];
char *badformat = "<bad format>";
/* get padder */
if (*++pf == '0') {
pad = '0';
++pf;
}
else
pad = ' ';
/* get width */
width = 0;
while (*pf >= '0' && *pf <= '9')
width = 10 * width + *pf++ - '0';
if (*pf == 's') {
1988-04-15 15:34:19 +00:00
arg = va_arg(argp, char *);
1987-01-05 17:37:37 +00:00
}
else
if (*pf == 'c') {
1988-04-15 15:34:19 +00:00
cbuf[0] = va_arg(argp, int);
1987-01-05 17:37:37 +00:00
cbuf[1] = '\0';
arg = &cbuf[0];
}
else
if (*pf == 'l') {
/* alignment ??? */
if (base = integral(*++pf)) {
1988-04-15 15:34:19 +00:00
arg = long2str(va_arg(argp,long), base);
1987-01-05 17:37:37 +00:00
}
else {
pf--;
arg = badformat;
}
}
else
if (base = integral(*pf)) {
1988-04-15 15:34:19 +00:00
arg = long2str((long)va_arg(argp,int), base);
1987-01-05 17:37:37 +00:00
}
else
if (*pf == '%')
arg = "%";
else
arg = badformat;
npad = width - strlen(arg);
while (npad-- > 0)
*pb++ = pad;
while (*pb++ = *arg++);
pb--;
pf++;
}
else
*pb++ = *pf++;
}
return pb - buf;
}