xref: /netbsd-src/usr.bin/printf/printf.c (revision e9d867ef5010fbab8d48045c13025636f5cd7479)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)printf.c	5.9 (Berkeley) 6/1/90";*/
42 static char rcsid[] = "$Id: printf.c,v 1.3 1993/08/01 18:10:03 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <stdio.h>
47 #include <string.h>
48 
49 #define PF(f, func) { \
50 	if (fieldwidth) \
51 		if (precision) \
52 			(void)printf(f, fieldwidth, precision, func); \
53 		else \
54 			(void)printf(f, fieldwidth, func); \
55 	else if (precision) \
56 		(void)printf(f, precision, func); \
57 	else \
58 		(void)printf(f, func); \
59 }
60 
61 char **gargv;
62 
63 main(argc, argv)
64 	int argc;
65 	char **argv;
66 {
67 	static char *skip1, *skip2;
68 	register char *format, *fmt, *start;
69 	register int end, fieldwidth, precision;
70 	char convch, nextch, *getstr(), *mklong();
71 	double getdouble();
72 	long getlong();
73 
74 	if (argc < 2) {
75 		fprintf(stderr, "usage: printf format [arg ...]\n");
76 		exit(1);
77 	}
78 
79 	/*
80 	 * Basic algorithm is to scan the format string for conversion
81 	 * specifications -- once one is found, find out if the field
82 	 * width or precision is a '*'; if it is, gather up value.  Note,
83 	 * format strings are reused as necessary to use up the provided
84 	 * arguments, arguments of zero/null string are provided to use
85 	 * up the format string.
86 	 */
87 	skip1 = "#-+ 0";
88 	skip2 = "*0123456789";
89 
90 	escape(fmt = format = *++argv);		/* backslash interpretation */
91 	gargv = ++argv;
92 	for (;;) {
93 		end = 0;
94 		/* find next format specification */
95 next:		for (start = fmt;; ++fmt) {
96 			if (!*fmt) {
97 				/* avoid infinite loop */
98 				if (end == 1) {
99 					fprintf(stderr,
100 					    "printf: missing format character.\n");
101 					exit(1);
102 				}
103 				end = 1;
104 				if (fmt > start)
105 					(void)printf("%s", start);
106 				if (!*gargv)
107 					exit(0);
108 				fmt = format;
109 				goto next;
110 			}
111 			/* %% prints a % */
112 			if (*fmt == '%') {
113 				if (*++fmt != '%')
114 					break;
115 				*fmt++ = '\0';
116 				(void)printf("%s", start);
117 				goto next;
118 			}
119 		}
120 
121 		/* skip to field width */
122 		for (; index(skip1, *fmt); ++fmt);
123 		fieldwidth = *fmt == '*' ? getint() : 0;
124 
125 		/* skip to possible '.', get following precision */
126 		for (; index(skip2, *fmt); ++fmt);
127 		if (*fmt == '.')
128 			++fmt;
129 		precision = *fmt == '*' ? getint() : 0;
130 
131 		/* skip to conversion char */
132 		for (; index(skip2, *fmt); ++fmt);
133 		if (!*fmt) {
134 			fprintf(stderr, "printf: missing format character.\n");
135 			exit(1);
136 		}
137 
138 		convch = *fmt;
139 		nextch = *++fmt;
140 		*fmt = '\0';
141 		switch(convch) {
142 		case 'c': {
143 			char p = getchr();
144 			PF(start, p);
145 			break;
146 		}
147 		case 's': {
148 			char *p = getstr();
149 			PF(start, p);
150 			break;
151 		}
152 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
153 			char *f = mklong(start, convch);
154 			long p = getlong();
155 			PF(f, p);
156 			break;
157 		}
158 		case 'e': case 'E': case 'f': case 'g': case 'G': {
159 			double p = getdouble();
160 			PF(start, p);
161 			break;
162 		}
163 		default:
164 			fprintf(stderr, "printf: illegal format character.\n");
165 			exit(1);
166 		}
167 		*fmt = nextch;
168 	}
169 	/* NOTREACHED */
170 }
171 
172 char *
173 mklong(str, ch)
174 	char *str, ch;
175 {
176 	int len;
177 	char *copy, *malloc();
178 
179 	len = strlen(str) + 2;
180 	if (!(copy = malloc((u_int)len))) {	/* never freed; XXX */
181 		fprintf(stderr, "printf: out of memory.\n");
182 		exit(1);
183 	}
184 	bcopy(str, copy, len - 3);
185 	copy[len - 3] = 'l';
186 	copy[len - 2] = ch;
187 	copy[len - 1] = '\0';
188 	return(copy);
189 }
190 
191 escape(fmt)
192 	register char *fmt;
193 {
194 	register char *store;
195 	register int value, c;
196 
197 	for (store = fmt; c = *fmt; ++fmt, ++store) {
198 		if (c != '\\') {
199 			*store = c;
200 			continue;
201 		}
202 		switch (*++fmt) {
203 		case '\0':		/* EOS, user error */
204 			*store = '\\';
205 			*++store = '\0';
206 			return;
207 		case '\\':		/* backslash */
208 		case '\'':		/* single quote */
209 			*store = *fmt;
210 			break;
211 		case 'a':		/* bell/alert */
212 			*store = '\7';
213 			break;
214 		case 'b':		/* backspace */
215 			*store = '\b';
216 			break;
217 		case 'f':		/* form-feed */
218 			*store = '\f';
219 			break;
220 		case 'n':		/* newline */
221 			*store = '\n';
222 			break;
223 		case 'r':		/* carriage-return */
224 			*store = '\r';
225 			break;
226 		case 't':		/* horizontal tab */
227 			*store = '\t';
228 			break;
229 		case 'v':		/* vertical tab */
230 			*store = '\13';
231 			break;
232 					/* octal constant */
233 		case '0': case '1': case '2': case '3':
234 		case '4': case '5': case '6': case '7':
235 			for (c = 3, value = 0;
236 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
237 				value <<= 3;
238 				value += *fmt - '0';
239 			}
240 			--fmt;
241 			*store = value;
242 			break;
243 		default:
244 			*store = *fmt;
245 			break;
246 		}
247 	}
248 	*store = '\0';
249 }
250 
251 getchr()
252 {
253 	if (!*gargv)
254 		return((int)'\0');
255 	return((int)**gargv++);
256 }
257 
258 char *
259 getstr()
260 {
261 	if (!*gargv)
262 		return("");
263 	return(*gargv++);
264 }
265 
266 static char *number = "+-.0123456789";
267 getint()
268 {
269 	if (!*gargv)
270 		return(0);
271 	if (index(number, **gargv))
272 		return(atoi(*gargv++));
273 	return(asciicode());
274 }
275 
276 long
277 getlong()
278 {
279 	long atol();
280 
281 	if (!*gargv)
282 		return((long)0);
283 	if (index(number, **gargv))
284 		return(strtol(*gargv++, (char **)NULL, 0));
285 	return((long)asciicode());
286 }
287 
288 double
289 getdouble()
290 {
291 	double atof();
292 
293 	if (!*gargv)
294 		return((double)0);
295 	if (index(number, **gargv))
296 		return(atof(*gargv++));
297 	return((double)asciicode());
298 }
299 
300 asciicode()
301 {
302 	register char ch;
303 
304 	ch = **gargv;
305 	if (ch == '\'' || ch == '"')
306 		ch = (*gargv)[1];
307 	++gargv;
308 	return(ch);
309 }
310