Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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 <sys/param.h>
42
43#define SPACE1
44#define ZERO2
45#define UCASE 16
46
47/*
48 * Scaled down version of C Library printf.
49 * Used to print diagnostic information directly on console tty.
50 * Since it is not interrupt driven, all system activities are
51 * suspended.
52 *
53 */
54
55/*
56 * Printn prints a number n in base b.
57 * We don't use recursion to avoid deep kernel stacks.
58 */
59static void
60printn(n, b, flag, minwidth, putfn_p, putfn_arg)
61u_long n;
62int b, flag, minwidth;
63void (*putfn_p)();
64void *putfn_arg;
65{
66char prbuf[11];
67register char *cp;
68int width = 0, neg = 0;
69
70if (b == 10 && (int)n < 0) {
71neg = 1;
72n = (unsigned)(-(int)n);
73}
74cp = prbuf;
75do {
76*cp++ = "0123456789abcdef0123456789ABCDEF"[(flag & UCASE) + n%b];
77n /= b;
78width++;
79} while (n);
80
81if (neg) {
82(*putfn_p)('-', putfn_arg);
83width++;
84}
85while (width++ < minwidth)
86(*putfn_p)( (flag & ZERO) ? '0' : ' ', putfn_arg);
87
88do
89(*putfn_p)(*--cp, putfn_arg);
90while (cp > prbuf);
91}
92
93int prf(
94char *fmt,
95unsigned int *adx,
96void (*putfn_p)(),
97void *putfn_arg
98)
99{
100int b, c, len =0;
101char *s;
102int flag = 0, width = 0;
103int minwidth;
104
105loop:
106while ((c = *fmt++) != '%') {
107if(c == '\0')
108{
109return len;
110}
111if (putfn_p)
112{
113(*putfn_p)(c, putfn_arg);
114}
115len++;
116}
117minwidth = 0;
118again:
119c = *fmt++;
120switch (c) {
121case 'l':
122goto again;
123case ' ':
124flag |= SPACE;
125goto again;
126case '0':
127if (minwidth == 0)
128{
129/* this is a flag */
130flag |= ZERO;
131goto again;
132} /* fall through */
133case '1':
134case '2':
135case '3':
136case '4':
137case '5':
138case '6':
139case '7':
140case '8':
141case '9':
142minwidth *= 10;
143minwidth += c - '0';
144goto again;
145 case 'X':
146flag |= UCASE;
147/* fall through */
148case 'x':
149b = 16;
150goto number;
151case 'd':
152b = 10;
153goto number;
154case 'o': case 'O':
155b = 8;
156number:
157if (putfn_p)
158{
159printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg);
160}
161len++;
162break;
163case 's':
164s = (char *)*adx;
165while ((c = *s++))
166{
167if (putfn_p)
168{
169(*putfn_p)(c, putfn_arg);
170}
171len++;
172width++;
173}
174while (width++ < minwidth)
175{
176if (putfn_p)
177{
178(*putfn_p)(' ', putfn_arg);
179}
180len++;
181}
182break;
183case 'c':
184if (putfn_p)
185{
186(*putfn_p)((char)*adx, putfn_arg);
187}
188len++;
189break;
190default:
191break;
192}
193adx++;
194goto loop;
195}
196

Archive Download this file

Revision: 2323