Chameleon

Chameleon Svn Source Tree

Root/branches/zenith432/i386/libsa/prf.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * Copyright (c) 1988 Carnegie-Mellon University
29 * Copyright (c) 1987 Carnegie-Mellon University
30 * All rights reserved. The CMU software License Agreement specifies
31 * the terms and conditions for use and redistribution.
32 */
33/*
34 * Copyright (c) 1982, 1986 Regents of the University of California.
35 * All rights reserved. The Berkeley software License Agreement
36 * specifies the terms and conditions for redistribution.
37 *
38 *@(#)prf.c7.1 (Berkeley) 6/5/86
39 */
40
41#include <stdarg.h>
42
43#define SPACE1
44#define ZERO2
45#define UCASE16
46#define SIGNED32
47
48/*
49 * Scaled down version of C Library printf.
50 * Used to print diagnostic information directly on console tty.
51 * Since it is not interrupt driven, all system activities are
52 * suspended.
53 *
54 */
55
56/*
57 * Printn prints a number n in base b.
58 * We don't use recursion to avoid deep kernel stacks.
59 */
60static int printn(
61unsigned long long n,
62int b,
63int flag,
64int minwidth,
65int (*putfn_p)(),
66void* putfn_arg
67)
68{
69char prbuf[22];
70register char *cp;
71int width = 0, neg = 0;
72
73if ((flag & SIGNED) && (long long)n < 0)
74{
75neg = 1;
76n = (unsigned long long)(-(long long)n);
77}
78cp = prbuf;
79do
80{
81*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + (int) (n%b)];
82n /= b;
83width++;
84} while (n);
85
86if (neg)
87{
88if (putfn_p)
89{
90(void)(*putfn_p)('-', putfn_arg);
91}
92width++;
93}
94if (!putfn_p)
95{
96return (width < minwidth) ? minwidth : width;
97}
98for (;width < minwidth; width++)
99(void)(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);
100
101do
102(void)(*putfn_p)(*--cp, putfn_arg);
103while (cp > prbuf);
104return width;
105}
106
107int prf(
108const char *fmt,
109va_list ap,
110int (*putfn_p)(),
111void *putfn_arg
112)
113{
114int b, c, len = 0;
115const char *s;
116int flag, width, ells;
117int minwidth;
118
119loop:
120while ((c = *fmt++) != '%')
121{
122if(c == '\0')
123{
124return len;
125}
126
127if (putfn_p)
128{
129(void)(*putfn_p)(c, putfn_arg);
130}
131len++;
132}
133minwidth = 0;
134flag = 0;
135ells = 0;
136again:
137c = *fmt++;
138switch (c)
139{
140case 'l':
141if (ells < 2)
142{
143++ells;
144}
145goto again;
146case ' ':
147flag |= SPACE;
148goto again;
149case '0':
150if (minwidth == 0)
151{
152/* this is a flag */
153flag |= ZERO;
154goto again;
155} /* fall through */
156case '1':
157case '2':
158case '3':
159case '4':
160case '5':
161case '6':
162case '7':
163case '8':
164case '9':
165minwidth *= 10;
166minwidth += c - '0';
167goto again;
168case 'X':
169flag |= UCASE;
170/* fall through */
171case 'x':
172b = 16;
173goto number;
174case 'd':
175flag |= SIGNED;
176/* fall through */
177case 'u':
178b = 10;
179goto number;
180case 'o': case 'O':
181b = 8;
182number:
183switch (ells)
184{
185case 2:
186len += printn(va_arg(ap, unsigned long long), b, flag, minwidth, putfn_p, putfn_arg);
187break;
188case 1:
189len += printn(va_arg(ap, unsigned long), b, flag, minwidth, putfn_p, putfn_arg);
190break;
191default:
192len += printn(va_arg(ap, unsigned int), b, flag, minwidth, putfn_p, putfn_arg);
193break;
194}
195break;
196case 's':
197s = va_arg(ap, const char*);
198width = 0;
199while (s && (c = *s++))
200{
201if (putfn_p)
202{
203(void)(*putfn_p)(c, putfn_arg);
204}
205len++;
206width++;
207}
208while (width++ < minwidth)
209{
210if (putfn_p)
211{
212(void)(*putfn_p)(' ', putfn_arg);
213}
214len++;
215}
216break;
217case 'c':
218if (putfn_p)
219{
220(void)(*putfn_p)((char) va_arg(ap, int), putfn_arg);
221}
222len++;
223break;
224default:
225break;
226}
227goto loop;
228}
229

Archive Download this file

Revision: 2820