File: printf.c

package info (click to toggle)
silo 1.4.14%2Bgit20120819-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,644 kB
  • sloc: ansic: 13,383; asm: 3,004; makefile: 351; sh: 102; perl: 85
file content (238 lines) | stat: -rw-r--r-- 5,270 bytes parent folder | download | duplicates (2)
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
/* Dumb printing routines
   
   Copyright (C) 1996 Pete A. Zaitcev
                 1997 Jakub Jelinek
		 2001 Ben Collins
   
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA.  */

#include "promlib.h"
#include <stringops.h>

/*
 * This part is rewritten by Igor Timkin <ivt@msu.su>. Than I
 * rewritten it again but kept the MISS style, originated from
 * Wladimir Butenko. --P3
 */
#define OBSIZE      200

#ifndef NULL
#define NULL (void *)0
#endif

static void putchar_1 (char c)
{
    /* P3: This buffer can be static while xprintf flushes it on exit. */
    static char buff[OBSIZE + 1];
    static int ox;

    if ((buff[ox] = c) == 0) {
	prom_puts (buff, ox);
	ox = 0;
    } else {
	if (++ox >= OBSIZE) {
	    buff[ox] = 0;
	    prom_puts (buff, ox);
	    ox = 0;
	}
    }
}

int putchar (int __c)
{
    char c = (char) __c;
    if (c == '\n')
	putchar_1 ('\r');
    putchar_1 (c);

    return __c;
}

/*
 * Print an unsigned integer in base b, avoiding recursion.
 */
static int printn (long long n, int b)
{
    static char prbuf[33];
    register char *cp;
    int count = 0;

    if (b == 10 && n < 0) {
	putchar ('-');
	count++;
	n = -n;
    }
    cp = prbuf;
    do
	*cp++ = "0123456789ABCDEF"[(unsigned int) (((unsigned long)n) % b)];
    while ((n = ((unsigned long long)n) / b & 0x0FFFFFFFFFFFFFFFULL));
    do {
	putchar (*--cp);
	count++;
    } while (cp > prbuf);

    return count;
}

int vprintf (char *fmt, va_list adx)
{
    register int c;
    char *s;
    int count = 0;

    for (;;) {
	while ((c = *fmt++) != '%') {
	    if (c == '\0') {
		putchar (0);
		return count;
	    }
	    putchar (c);
	}
	c = *fmt++;
	if (c == 'd' || c == 'o' || c == 'x' || c == 'X') {
	    count += printn ((long long) va_arg (adx, unsigned),
			     c == 'o' ? 8 : (c == 'd' ? 10 : 16));
	} else if (c == 'c') {
	    putchar (va_arg (adx, unsigned));
	    count++;
	} else if (c == 's') {
	    if ((s = va_arg (adx, char *)) == NULL)
		s = (char *)"(null)";
	    while ((c = *s++)) {
		putchar (c);
		count++;
	    }
	} else if (c == 'l' || c == 'O') {
	    count += printn ((long long) va_arg (adx, long), c == 'l' ? 10 : 8);
	} else if (c == 'L') {
	    int hex = 0;
	    if (*fmt == 'x') {
		fmt++;
		hex = 1;
	    }
	    count += printn ((long long) va_arg (adx, long long), hex ? 16 : 10);
	} else {
	    /* This is basically what libc's printf does */
	    putchar('%'); putchar(c);
	    count += 2;
	}
    }

    return count;
}

/*
 * Scaled down version of C Library printf.
 * Only %c %s %d (==%u) %o %x %X %l %O are recognized.
 */

void prom_printf (char *fmt,...)
{
    va_list x1;

    va_start (x1, fmt);
    vprintf (fmt, x1);
    va_end (x1);
}

static int sprintn (char *str, long long n, int b)
{
    static char prbuf[33];
    register char *cp;
    int count = 0;

    if (b == 10 && n < 0) {
	memset (str + count, '-', 1);
	count++;
	n = -n;
    }
    cp = prbuf;
    do
	*cp++ = "0123456789ABCDEF"[(unsigned int) (((unsigned long)n) % b)];
    while ((n = ((unsigned long long)n) / b & 0x0FFFFFFFFFFFFFFFULL));
    do {
	memset (str + count, *--cp, 1);
	count++;
    } while (cp > prbuf);

    return count;
}

int vsprintf (char *str, char *fmt, va_list adx)
{
    register int c;
    char *s;
    int count = 0;

    for (;;) {
	while ((c = *fmt++) != '%') {
	    memset (str + count, c, 1);
	    if (c == '\0') {
		return count;
	    }
	}
	c = *fmt++;
	if (c == 'd' || c == 'o' || c == 'x' || c == 'X') {
	    count += sprintn (str + count, (long long) va_arg (adx, unsigned),
			     c == 'o' ? 8 : (c == 'd' ? 10 : 16));
	} else if (c == 'c') {
	    memset (str + count, va_arg (adx, unsigned), 1);
	    count++;
	} else if (c == 's') {
	    if ((s = va_arg (adx, char *)) == NULL)
		s = (char *)"(null)";
	    while ((c = *s++)) {
		memset (str + count, c, 1);
		count++;
	    }
	} else if (c == 'l' || c == 'O') {
	    count += sprintn (str + count, (long long) va_arg (adx, long), c == 'l' ? 10 : 8);
	} else if (c == 'L') {
	    int hex = 0;
	    if (*fmt == 'x') {
		fmt++;
		hex = 1;
	    }
	    count += sprintn (str + count, (long long) va_arg (adx, long long), hex ? 16 : 10);
	} else {
	    /* This is basically what libc's printf does */
	    memset (str + count, '%', 1);
	    count++;
	    memset (str + count, c, 1);
	    count++;
	}
    }

    return count;
}

/*
 * Scaled down version of C Library sprintf.
 * Only %c %s %d (==%u) %o %x %X %l %O are recognized.
 */

int sprintf (char *s, char *format, ...)
{
    va_list arg;
    int done;

    va_start (arg, format);
    done = vsprintf (s, format, arg);
    va_end (arg);

    return done;
}