use varargs when needed

This commit is contained in:
ceriel 1988-04-15 15:34:19 +00:00
parent 2549099d3b
commit f5be4dafc6
6 changed files with 44 additions and 26 deletions

View file

@ -5,14 +5,15 @@
/* $Header$ */ /* $Header$ */
#include <system.h> #include <system.h>
#include <varargs.h>
#include "param.h" #include "param.h"
doprnt(fp, fmt, argp) doprnt(fp, fmt, argp)
File *fp; File *fp;
char *fmt; char *fmt;
int argp[]; va_list argp;
{ {
char buf[SSIZE]; char buf[SSIZE];
sys_write(fp, buf, _format(buf, fmt, (char *)argp)); sys_write(fp, buf, _format(buf, fmt, argp));
} }

View file

@ -4,6 +4,8 @@
*/ */
/* $Header$ */ /* $Header$ */
#include <varargs.h>
char *long2str(); char *long2str();
static int static int
@ -27,9 +29,9 @@ integral(c)
int int
_format(buf, fmt, argp) _format(buf, fmt, argp)
char *buf, *fmt; char *buf, *fmt;
char *argp; register va_list argp;
{ {
register char *pf = fmt, *pa = argp; register char *pf = fmt;
register char *pb = buf; register char *pb = buf;
while (*pf) { while (*pf) {
@ -52,24 +54,20 @@ _format(buf, fmt, argp)
while (*pf >= '0' && *pf <= '9') while (*pf >= '0' && *pf <= '9')
width = 10 * width + *pf++ - '0'; width = 10 * width + *pf++ - '0';
/* get text and move pa */
if (*pf == 's') { if (*pf == 's') {
arg = *(char **)pa; arg = va_arg(argp, char *);
pa += sizeof(char *);
} }
else else
if (*pf == 'c') { if (*pf == 'c') {
cbuf[0] = * (int *) pa; cbuf[0] = va_arg(argp, int);
cbuf[1] = '\0'; cbuf[1] = '\0';
pa += sizeof(int);
arg = &cbuf[0]; arg = &cbuf[0];
} }
else else
if (*pf == 'l') { if (*pf == 'l') {
/* alignment ??? */ /* alignment ??? */
if (base = integral(*++pf)) { if (base = integral(*++pf)) {
arg = long2str(*(long *)pa, base); arg = long2str(va_arg(argp,long), base);
pa += sizeof(long);
} }
else { else {
pf--; pf--;
@ -78,8 +76,7 @@ _format(buf, fmt, argp)
} }
else else
if (base = integral(*pf)) { if (base = integral(*pf)) {
arg = long2str((long)*(int *)pa, base); arg = long2str((long)va_arg(argp,int), base);
pa += sizeof(int);
} }
else else
if (*pf == '%') if (*pf == '%')

View file

@ -4,16 +4,23 @@
*/ */
/* $Header$ */ /* $Header$ */
#include <varargs.h>
#include <system.h> #include <system.h>
#include "param.h" #include "param.h"
/*VARARGS1*/ /*VARARGS1*/
fprint(fp, fmt, args) fprint(va_alist)
va_dcl
{
File *fp; File *fp;
char *fmt; char *fmt;
int args; va_list args;
{
char buf[SSIZE]; char buf[SSIZE];
sys_write(fp, buf, _format(buf, fmt, &args)); va_start(args);
fp = va_arg(args, File *);
fmt = va_arg(args, char *);
sys_write(fp, buf, _format(buf, fmt, args));
va_end(args);
} }

View file

@ -5,6 +5,7 @@ print, fprint, sprint, doprnt -- very simple formatted-output routines
.SH SYNOPSIS .SH SYNOPSIS
.nf .nf
.B #include <system.h> .B #include <system.h>
.B #include <varargs.h>
.PP .PP
.B print(format [, arg] ... ) .B print(format [, arg] ... )
.B char *format; .B char *format;
@ -19,7 +20,7 @@ print, fprint, sprint, doprnt -- very simple formatted-output routines
.B doprnt(filep, format, args) .B doprnt(filep, format, args)
.B File *filep; .B File *filep;
.B char *format; .B char *format;
.B int args[]; .B va_list args;
.fi .fi
.SH DESCRIPTION .SH DESCRIPTION
.I Print .I Print

View file

@ -4,15 +4,20 @@
*/ */
/* $Header$ */ /* $Header$ */
#include <varargs.h>
#include <system.h> #include <system.h>
#include "param.h" #include "param.h"
/*VARARGS1*/ /*VARARGS1*/
print(fmt, args) print(va_alist)
char *fmt; va_dcl
int args;
{ {
char *fmt;
va_list args;
char buf[SSIZE]; char buf[SSIZE];
sys_write(STDOUT, buf, _format(buf, fmt, &args)); va_start(args);
fmt = va_arg(args, char *);
sys_write(STDOUT, buf, _format(buf, fmt, args));
va_end(args);
} }

View file

@ -4,15 +4,22 @@
*/ */
/* $Header$ */ /* $Header$ */
#include <varargs.h>
#include <system.h> #include <system.h>
#include "param.h" #include "param.h"
/*VARARGS1*/ /*VARARGS1*/
char * char *
sprint(buf, fmt, args) sprint(va_alist)
char *buf, *fmt; va_dcl
int args;
{ {
buf[_format(buf, fmt, &args)] = '\0'; char *buf, *fmt;
va_list args;
va_start(args);
buf = va_arg(args, char *);
fmt = va_arg(args, char *);
buf[_format(buf, fmt, args)] = '\0';
va_end(args);
return buf; return buf;
} }