Computer Systems Research Group BSD Distribution
Check-in [2277bfaf23]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:First "working" version. Rip out roman number and binary options; add working 'l' flag, varargs, '#' and '+' flags, do "%%" in first switch basically a complete and total reformat/rewrite, diff won't work
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2277bfaf232ce90a66dbedf562fed23b269375b2
User & Date: bostic 1988-05-07 11:55:36
Context
1988-05-08
11:49
add Berkeley header; yank to 5.1 check-in: 674a7d4ff2 user: bostic tags: trunk
1988-05-07
11:55
First "working" version. Rip out roman number and binary options; add working 'l' flag, varargs, '#' and '+' flags, do "%%" in first switch basically a complete and total reformat/rewrite, diff won't work check-in: 2277bfaf23 user: bostic tags: trunk
10:20
vax 8200 support from torek: generalize cpu support, ops, clock ops check-in: 4f5157548e user: karels tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to lib/libc/stdio/vfprintf.c.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

95
96

97

98
99
100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160









161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189
190
191
192
193
194


195
196
197
198









199



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221


222



223
224
225
226
227
228
229
230
231

232
233
234
235
236


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312


313
314
315
316
317
318
319

320

321
322




323

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405


406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

431
432
From stevesu@copper.UUCP Wed Mar 25 23:35:32 1987
Path: seismo!ut-sally!husc6!bacchus!mit-eddie!genrad!decvax!tektronix!teklds!copper!stevesu
From: stevesu@copper.TEK.COM (Steve Summit)
Newsgroups: net.sources
Subject: Public Domain _doprnt in C
Message-ID: <938@copper.TEK.COM>
Date: 26 Mar 87 04:35:32 GMT
Reply-To: stevesu@copper.UUCP (Steve Summit)
Distribution: world
Organization: Tektronix, Inc., Beaverton, OR.
Lines: 420

Mark Pulver is looking for a C version of _doprnt, so I thought
I'd pass mine along.  I wrote this from the ground up; it is
absolutely underived from anything proprietary.

This version is not complete, and has the following two key
omissions:

	It doesn't do floating point (%f, %e, or %g).

	It will handle %ld (%lx, etc.) incorrectly on machines
	where sizeof(long) != sizeof(int).

It also does not implement the %# stuff which appeared in the 4.2
documentation but which I haven't seen in any implementation yet.

I believe it handles everything else correctly, although I have
not tested it exhaustively.

There are two "fun" additions: %b is binary, and %r is roman.

You are free to use this code as you wish, but please leave the
identification comment intact.  I can offer no support for this
code, although if I ever implement floating point or pdp11
support (I'm acutely embarrassed to admit making the typical VAX
int/long equivalence assumption) I'll try to remember to post
those additions.

                                           Steve Summit
                                           stevesu@copper.tek.com

--------------------- cut here for doprnt.c ---------------------
/*
 *  Common code for printf et al.
 *
 *  The calling routine typically takes a variable number of arguments,
 *  and passes the address of the first one.  This implementation
 *  assumes a straightforward, stack implementation, aligned to the
 *  machine's wordsize.  Increasing addresses are assumed to point to
 *  successive arguments (left-to-right), as is the case for a machine
 *  with a downward-growing stack with arguments pushed right-to-left.
 *
 *  To write, for example, fprintf() using this routine, the code
 *
 *	fprintf(fd, format, args)
 *	FILE *fd;
 *	char *format;
 *	{
 *	_doprnt(format, &args, fd);
 *	}
 *
 *  would suffice.  (This example does not handle the fprintf's "return
 *  value" correctly, but who looks at the return value of fprintf
 *  anyway?)
 *
 *  This version implements the following printf features:
 *
 *	%d	decimal conversion
 *	%u	unsigned conversion
 *	%x	hexadecimal conversion
 *	%X	hexadecimal conversion with capital letters
 *	%o	octal conversion
 *	%c	character
 *	%s	string
 *	%m.n	field width, precision
 *	%-m.n	left adjustment
 *	%0m.n	zero-padding
 *	%*.*	width and precision taken from arguments
 *
 *  This version does not implement %f, %e, or %g.  It accepts, but
 *  ignores, an `l' as in %ld, %lo, %lx, and %lu, and therefore will not
 *  work correctly on machines for which sizeof(long) != sizeof(int).
 *  It does not even parse %D, %O, or %U; you should be using %ld, %o and
 *  %lu if you mean long conversion.
 *
 *  This version implements the following nonstandard features:
 *
 *	%b	binary conversion
 *	%r	roman numeral conversion
 *	%R	roman numeral conversion with capital letters
 *
 *  As mentioned, this version does not return any reasonable value.
 *

 *  Permission is granted to use, modify, or propagate this code as
 *  long as this notice is incorporated.

 *

 *  Steve Summit 3/25/87
 */

#include <stdio.h>

#define TRUE 1
#define FALSE 0

#define ROMAN


#define isdigit(d) ((d) >= '0' && (d) <= '9')
#define Ctod(c) ((c) - '0')

#define MAXBUF (sizeof(long int) * 8)		 /* enough for binary */

#ifdef ROMAN
static tack();
static doit();
#endif

_doprnt(fmt, argp, fd)
register char *fmt;
register int *argp;
FILE *fd;
{
register char *p;
char *p2;
int size;
int length;
int prec;
int ladjust;
char padc;
int n;
unsigned int u;
int base;
char buf[MAXBUF];
int negflag;
char *digs;
#ifdef ROMAN
char *rdigs;
int d;
#endif

while(*fmt != '\0')
	{

	if(*fmt != '%')
		{
		putc(*fmt++, fd);
		continue;
		}

	fmt++;

	if(*fmt == 'l')
		fmt++;	     /* need to use it if sizeof(int) < sizeof(long) */

	length = 0;
	prec = -1;
	ladjust = FALSE;
	padc = ' ';

	if(*fmt == '-')
		{









		ladjust = TRUE;
		fmt++;
		}

	if(*fmt == '0')
		{
		padc = '0';
		fmt++;
		}

	if(isdigit(*fmt))
		{
		while(isdigit(*fmt))
			length = 10 * length + Ctod(*fmt++);
		}
	else if(*fmt == '*')
		{
		length = *argp++;
		fmt++;
		if(length < 0)
			{

			ladjust = !ladjust;
			length = -length;
			}
		}

	if(*fmt == '.')
		{
		fmt++;
		if(isdigit(*fmt))
			{
			prec = 0;
			while(isdigit(*fmt))
				prec = 10 * prec + Ctod(*fmt++);


			}
		else if(*fmt == '*')
			{
			prec = *argp++;









			fmt++;



			}
		}

	negflag = FALSE;
	digs = "0123456789abcdef";
#ifdef ROMAN
	rdigs = "  mdclxvi";
#endif

	switch(*fmt)
		{
		case 'b':
		case 'B':
			u = *argp++;
			base = 2;
			goto donum;

		case 'c':
			putc(*argp++, fd);
			break;

		case 'd':


		case 'D':



			n = *argp++;

			if(n >= 0)
				u = n;
			else	{
				u = -n;
				negflag = TRUE;
				}


			base = 10;

			goto donum;

		case 'o':


		case 'O':
			u = *argp++;
			base = 8;
			goto donum;
#ifdef ROMAN
		case 'R':
			rdigs = "  MDCLXVI";
		case 'r':
			n = *argp++;
			p2 = &buf[MAXBUF - 1];

			d = n % 10;
			tack(d, &rdigs[6], &p2);
			n = n / 10;

			d = n % 10;
			tack(d, &rdigs[4], &p2);
			n = n / 10;

			d = n % 10;
			tack(d, &rdigs[2], &p2);
			n /= 10;

			d = n % 10;
			tack(d, rdigs, &p2);

			p = p2;

			goto putpad;
#endif
		case 's':
			p = (char *)(*argp++);

			if(p == NULL)
				p = "(NULL)";

			if(length > 0 && !ladjust)
				{
				n = 0;
				p2 = p;

				for(; *p != '\0' &&
						(prec == -1 || n < prec); p++)
					n++;

				p = p2;

				while(n < length)
					{
					putc(' ', fd);
					n++;
					}
				}

			n = 0;

			while(*p != '\0')
				{
				if(++n > prec && prec != -1)
					break;

				putc(*p++, fd);
				}

			if(n < length && ladjust)
				{
				while(n < length)
					{
					putc(' ', fd);
					n++;
					}
				}

			break;

		case 'u':


		case 'U':
			u = *argp++;
			base = 10;
			goto donum;

		case 'X':
			digs = "0123456789ABCDEF";

		case 'x':

			u = *argp++;
			base = 16;






donum:			p = &buf[MAXBUF - 1];

			do	{
				*p-- = digs[u % base];
				u /= base;
				} while(u != 0);

			if(negflag)
				putc('-', fd);
putpad:
			size = &buf[MAXBUF - 1] - p;

			if(size < length && !ladjust)
				{
				while(length > size)
					{
					putc(padc, fd);
					length--;
					}
				}

			while(++p != &buf[MAXBUF])
				putc(*p, fd);

			if(size < length)	/* must be ladjust */
				{
				while(length > size)
					{

					putc(padc, fd);
					length--;
					}
				}

			break;

		case '\0':
			fmt--;
			break;

		default:
			putc(*fmt, fd);
		}
	fmt++;
	}
}

#ifdef ROMAN

static
tack(d, digs, p)
int d;
char *digs;
char **p;
{
if(d == 0) return;
if(d >= 1 && d <= 3)
	{
	doit(d, digs[2], p);
	return;
	}

if(d == 4 || d == 5)
	{
	**p = digs[1];
	(*p)--;

	}

if(d == 4)
	{
	**p = digs[2];
	(*p)--;
	return;
	}

if(d == 5) return;

if(d >= 6 && d <= 8)
	{
	doit(d - 5, digs[2], p);
	**p = digs[1];
	(*p)--;
	return;


	}

/* d == 9 */

**p = digs[0];
(*p)--;
**p = digs[2];
(*p)--;
return;
}

static
doit(d, one, p)
int d;
char one;
char **p;
{
int i;

for(i = 0; i < d; i++)
	{
	**p = one;
	(*p)--;
	}
}


#endif
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
|
<
<
|
<
|
<
<
|
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
|
<
>

>
|


|
|
|
<

|
|
>
|
<

|
|
|
<
<
<

|
|
|
|

|
|
<
<
<
<
<
<
<
|
<
<
|
<
|
<
<
|
<
|
>
|
<
|
|


<
|
<
<
<
<
|
<
|
|
<
|
>
>
>
>
>
>
>
>
>
|
<
<
|
<
<
<
<
<
|
|
<
<
<
<
|
<
|
|
<
<
>
|
<
<
<
|
|
<
<
|
<
|
<
|
>
>

|
<
|
>
>
>
>
>
>
>
>
>
|
>
>
>
|
|
<
<
|
<
<
<

|
<
<
<
<
<
<
<

|

<

>
>
|
>
>
>
|
|
<
<
|
|
<
|
|
>

<

<

>
>
|
|


<
|
<
<
<
<
|
<
<
<
|
<
<
<
|
<
<
<
|
<
<

|
|
<
<
<
<
|
<
|
|
<
<
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
|

<
|
|
<
|
<
|
<
|
<
<
<
|

<

>
>
|
|


<


>

>
|
|
>
>
>
>
|
>
|
<
|
|
|
|
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
>
|
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
|
<
<
<
<
<
<
|
<
<
<
<
<
<
<
|
<
<
<
|
>
|
|
|
<
|
<
<
<
|
<
|
<
<
<
|
|
|
>
>
|
|
<
|
<
<
<
<
|
<
|
<
<
<
<
<
<
<
|
<
<
<
<
|
|
>
|
<











1



2


3

4


5


6













































7





















8
9

10
11
12
13
14
15
16
17
18

19
20
21
22
23

24
25
26
27



28
29
30
31
32
33
34
35







36


37

38


39

40
41
42

43
44
45
46

47




48

49
50

51
52
53
54
55
56
57
58
59
60
61


62





63
64




65

66
67


68
69



70
71


72

73

74
75
76
77
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94


95



96
97







98
99
100

101
102
103
104
105
106
107
108
109


110
111

112
113
114
115

116

117
118
119
120
121
122
123

124




125



126



127



128


129
130
131




132

133
134


135

136













137



138
139

140
141

142

143

144



145
146

147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167

168
169
170
171




172
173














174

175
176
177










178




179






180







181



182
183
184
185
186

187



188

189



190
191
192
193
194
195
196

197




198

199







200




201
202
203
204












/*



 * Copyright (c) 1988 Regents of the University of California.


 * All rights reserved.

 *


 * Redistribution and use in source and binary forms are permitted


 * provided that this notice is preserved and that due credit is given













































 * to the University of California at Berkeley. The name of the University





















 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software

 * is provided ``as is'' without express or implied warranty.
 *
 * Originally derived from code posted by Steve Summit to USENET,
 * dated 3/25/87
 */

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)vfprintf.c	5.2 (Berkeley) 05/07/88";
#endif /* LIBC_SCCS and not lint */


#include <sys/param.h>
#include <varargs.h>
#include <stdio.h>
#include <ctype.h>


#define	MAXBUF		(sizeof(long) * 8)	 /* enough for binary */
#define	PUTC(ch, fd)	{ ++cnt; putc(ch, fd); }
#define	todigit(ch)	((ch) - '0')




doprnt(fmt, argp, fd)
	register char *fmt;
	va_list argp;
	register FILE *fd;
{
	register u_long reg_ulong;
	register long reg_long;







	register int base;


	register char *digs, *p, padc;

	char printsign, buf[MAXBUF];


	int alternate, cnt, n, ladjust, length, setlong, prec, size;


	for (cnt = 0; *fmt; ++fmt) {
		if (*fmt != '%') {

			PUTC(*fmt, fd);
			continue;
		}


		alternate = ladjust = length = setlong = 0;




		prec = -1;

		padc = ' ';
		printsign = '\0';


flags:		switch (*++fmt) {
		case '#':
			alternate = 1;
			goto flags;
		case '%':			/* "%#%" prints as "%" */
			PUTC('%', fd);
			continue;
		case '*':
			if ((length = va_arg(argp, int)) < 0) {
				ladjust = !ladjust;


				length = -length;





			}
			goto flags;




		case '+':

			printsign = '+';
			goto flags;


		case '-':
			ladjust = 1;



			goto flags;
		case '.':


			if (isdigit(*++fmt)) {

				do {

					prec = 10 * prec + todigit(*fmt);
				} while isdigit(*++fmt);
				--fmt;
			}
			else if (*fmt == '*')

				prec = va_arg(argp, int);
			goto flags;
		case '0':
			padc = '0';
			goto flags;
		case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			do {
				length = 10 * length + todigit(*fmt);
			} while isdigit(*++fmt);
			--fmt;
		case 'l':
			setlong = 1;
			goto flags;
		}



		digs = "0123456789abcdef";




		switch (*fmt) {







		case 'c':
			PUTC(va_arg(argp, int), fd);
			break;

		case 'd':
			if (setlong)
				reg_long = va_arg(argp, long);
			else
				reg_long = va_arg(argp, int);
			if (reg_long < 0) {
				reg_ulong = -reg_long;
				printsign = '-';
			}


			else {
				reg_ulong = reg_long;

			}
			if (printsign)
				PUTC(printsign, fd);
			base = 10;

			goto donum;

		case 'o':
			if (setlong)
				reg_ulong = va_arg(argp, long);
			else
				reg_ulong = va_arg(argp, int);
			base = 8;
			goto donum;

		case 's':




			if (!(p = va_arg(argp, char *)))



				p = "(null)";



			if (length > 0 && !ladjust) {



				char *savep;



				savep = p;
				for (n = 0; *p && (prec == -1 || n < prec);




				    n++, p++);

				p = savep;
				while (n++ < length)


					PUTC(' ', fd);

			}













			for (n = 0; *p; ++p) {



				if (++n > prec && prec != -1)
					break;

				PUTC(*p, fd);
			}

			if (n < length && ladjust)

				do {

					PUTC(' ', fd);



				} while (++n < length);
			break;

		case 'u':
			if (setlong)
				reg_ulong = va_arg(argp, long);
			else
				reg_ulong = va_arg(argp, int);
			base = 10;
			goto donum;

		case 'X':
			digs = "0123456789ABCDEF";
			/*FALLTHROUGH*/
		case 'x':
			if (setlong)
				reg_ulong = va_arg(argp, long);
			else
				reg_ulong = va_arg(argp, int);
			if (alternate && reg_ulong) {
				PUTC('0', fd);
				PUTC(*fmt, fd);
			}
			base = 16;
donum:			p = &buf[sizeof(buf) - 1];

			do {
				*p-- = digs[reg_ulong % base];
				reg_ulong /= base;
			} while(reg_ulong);




			size = &buf[sizeof(buf) - 1] - p;
			if (reg_ulong && alternate && *fmt == 'o') {














				if (size < --length && !ladjust)

					do {
						PUTC(padc, fd);
					} while (--length > size);










				PUTC('0', fd);




				while (++p != &buf[MAXBUF])






					PUTC(*p, fd);







				if (size < length)	/* must be ladjust */



					for (; length > size; --length)
						PUTC(padc, fd);
			}
			else {
				if (size < length && !ladjust)

					do {



						PUTC(padc, fd);

					} while (--length > size);



				while (++p != &buf[MAXBUF])
					PUTC(*p, fd);
				if (size < length)	/* must be ladjust */
					for (; length > size; --length)
						PUTC(padc, fd);
			}
			break;

		case '\0':		/* "%?" prints ?, unless ? is NULL */




			return(cnt);

		default:







			PUTC(*fmt, fd);




		}
	}
	return(cnt);
}