1*69fb957aSbostic /*-
2*69fb957aSbostic * Copyright (c) 1991 The Regents of the University of California.
3*69fb957aSbostic * All rights reserved.
4*69fb957aSbostic *
5*69fb957aSbostic * %sccs.include.redist.c%
6*69fb957aSbostic *
7*69fb957aSbostic * @(#)clnp_sprintf.c 7.1 (Berkeley) 05/08/91
8*69fb957aSbostic */
9*69fb957aSbostic
10*69fb957aSbostic /*
11*69fb957aSbostic * CLNP needs a version of sprintf in the kernel. If anything else
12*69fb957aSbostic * ever needs it, this can trivially be dropped into kern/subr_prf.c.
13*69fb957aSbostic */
14*69fb957aSbostic #include "param.h"
15*69fb957aSbostic
16*69fb957aSbostic /*
17*69fb957aSbostic * Note that stdarg.h and the ANSI style va_start macro is used for both
18*69fb957aSbostic * ANSI and traditional C compilers.
19*69fb957aSbostic */
20*69fb957aSbostic #include <machine/stdarg.h>
21*69fb957aSbostic
22*69fb957aSbostic int sprintf __P((char *, const char *, ...));
23*69fb957aSbostic
24*69fb957aSbostic /*
25*69fb957aSbostic * Scaled down version of sprintf(3).
26*69fb957aSbostic */
27*69fb957aSbostic #ifdef __STDC__
sprintf(char * buf,const char * fmt,...)28*69fb957aSbostic sprintf(char *buf, const char *fmt, ...)
29*69fb957aSbostic #else
30*69fb957aSbostic sprintf(buf, fmt /*, va_alist */)
31*69fb957aSbostic char *buf, *fmt;
32*69fb957aSbostic #endif
33*69fb957aSbostic {
34*69fb957aSbostic register char *p, *bp;
35*69fb957aSbostic register int ch, base;
36*69fb957aSbostic u_long ul;
37*69fb957aSbostic int lflag; /* hold a long in base 8 */
38*69fb957aSbostic char num[(sizeof(long) * NBBY / 3) + 1];
39*69fb957aSbostic va_list ap;
40*69fb957aSbostic
41*69fb957aSbostic va_start(ap, fmt);
42*69fb957aSbostic for (bp = buf;;) {
43*69fb957aSbostic while ((ch = *fmt++) != '%') {
44*69fb957aSbostic if ((*bp = ch) == '\0')
45*69fb957aSbostic return(bp - buf);
46*69fb957aSbostic *bp++ = ch;
47*69fb957aSbostic }
48*69fb957aSbostic lflag = 0;
49*69fb957aSbostic reswitch: switch (ch = *fmt++) {
50*69fb957aSbostic case 'l':
51*69fb957aSbostic lflag = 1;
52*69fb957aSbostic goto reswitch;
53*69fb957aSbostic case 'c':
54*69fb957aSbostic *bp++ = va_arg(ap, int);
55*69fb957aSbostic break;
56*69fb957aSbostic case 's':
57*69fb957aSbostic p = va_arg(ap, char *);
58*69fb957aSbostic while (*bp++ = *p++);
59*69fb957aSbostic --bp;
60*69fb957aSbostic break;
61*69fb957aSbostic case 'D':
62*69fb957aSbostic lflag = 1;
63*69fb957aSbostic /* FALLTHROUGH */
64*69fb957aSbostic case 'd':
65*69fb957aSbostic ul = lflag ?
66*69fb957aSbostic va_arg(ap, long) : va_arg(ap, int);
67*69fb957aSbostic if ((long)ul < 0) {
68*69fb957aSbostic *bp++ = '-';
69*69fb957aSbostic ul = -(long)ul;
70*69fb957aSbostic }
71*69fb957aSbostic base = 10;
72*69fb957aSbostic goto number;
73*69fb957aSbostic break;
74*69fb957aSbostic case 'O':
75*69fb957aSbostic lflag = 1;
76*69fb957aSbostic /* FALLTHROUGH */
77*69fb957aSbostic case 'o':
78*69fb957aSbostic ul = lflag ?
79*69fb957aSbostic va_arg(ap, u_long) : va_arg(ap, u_int);
80*69fb957aSbostic base = 8;
81*69fb957aSbostic goto number;
82*69fb957aSbostic break;
83*69fb957aSbostic case 'U':
84*69fb957aSbostic lflag = 1;
85*69fb957aSbostic /* FALLTHROUGH */
86*69fb957aSbostic case 'u':
87*69fb957aSbostic ul = lflag ?
88*69fb957aSbostic va_arg(ap, u_long) : va_arg(ap, u_int);
89*69fb957aSbostic base = 10;
90*69fb957aSbostic goto number;
91*69fb957aSbostic break;
92*69fb957aSbostic case 'X':
93*69fb957aSbostic lflag = 1;
94*69fb957aSbostic /* FALLTHROUGH */
95*69fb957aSbostic case 'x':
96*69fb957aSbostic ul = lflag ?
97*69fb957aSbostic va_arg(ap, u_long) : va_arg(ap, u_int);
98*69fb957aSbostic base = 16;
99*69fb957aSbostic number: p = num;
100*69fb957aSbostic do {
101*69fb957aSbostic *p++ = "0123456789abcdef"[ul % base];
102*69fb957aSbostic } while (ul /= base);
103*69fb957aSbostic do {
104*69fb957aSbostic *bp++ = *--p;
105*69fb957aSbostic } while (p > num);
106*69fb957aSbostic break;
107*69fb957aSbostic default:
108*69fb957aSbostic *bp++ = '%';
109*69fb957aSbostic if (lflag)
110*69fb957aSbostic *bp++ = 'l';
111*69fb957aSbostic *bp++ = ch;
112*69fb957aSbostic }
113*69fb957aSbostic }
114*69fb957aSbostic va_end(ap);
115*69fb957aSbostic }
116