Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch_Modules/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 printn(n, b, flag, minwidth, putfn_p, putfn_arg)
60u_long n;
61int b, flag, minwidth;
62void (*putfn_p)();
63void *putfn_arg;
64{
65char prbuf[11];
66register char *cp;
67int width = 0, neg = 0;
68
69if (b == 10 && (int)n < 0) {
70neg = 1;
71n = (unsigned)(-(int)n);
72}
73cp = prbuf;
74do
75{
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(char *fmt, unsigned int *adx, void (*putfn_p)(), void *putfn_arg )
94{
95int b, c, len =0;
96char *s;
97int flag = 0, width = 0;
98int minwidth;
99
100loop:
101while ((c = *fmt++) != '%')
102{
103if(c == '\0')
104{
105return len;
106}
107if (putfn_p)
108{
109(*putfn_p)(c, putfn_arg);
110}
111len++;
112}
113minwidth = 0;
114again:
115c = *fmt++;
116switch (c) {
117case 'l':
118goto again;
119case ' ':
120flag |= SPACE;
121goto again;
122case '0':
123if (minwidth == 0)
124{
125/* this is a flag */
126flag |= ZERO;
127goto again;
128} /* fall through */
129case '1':
130case '2':
131case '3':
132case '4':
133case '5':
134case '6':
135case '7':
136case '8':
137case '9':
138minwidth *= 10;
139minwidth += c - '0';
140goto again;
141 case 'X':
142flag |= UCASE;
143/* fall through */
144case 'x':
145b = 16;
146goto number;
147case 'd':
148b = 10;
149goto number;
150case 'o': case 'O':
151b = 8;
152number:
153if (putfn_p)
154{
155printn((u_long)*adx, b, flag, minwidth, putfn_p, putfn_arg);
156}
157len++;
158break;
159case 's':
160s = (char *)*adx;
161while ((c = *s++))
162{
163if (putfn_p)
164{
165(*putfn_p)(c, putfn_arg);
166}
167len++;
168width++;
169}
170while (width++ < minwidth)
171{
172if (putfn_p)
173{
174(*putfn_p)(' ', putfn_arg);
175}
176len++;
177}
178break;
179case 'c':
180if (putfn_p)
181{
182(*putfn_p)((char)*adx, putfn_arg);
183}
184len++;
185break;
186default:
187break;
188}
189adx++;
190goto loop;
191}
192

Archive Download this file

Revision: 2238