xref: /netbsd-src/usr.bin/printf/printf.c (revision 811e6386f8c5e4a3521c7003da29ec8673e344fa)
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[] = "@(#)printf.c	5.9 (Berkeley) 6/1/90";
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <stdio.h>
46 #include <string.h>
47 
48 #define PF(f, func) { \
49 	if (fieldwidth) \
50 		if (precision) \
51 			(void)printf(f, fieldwidth, precision, func); \
52 		else \
53 			(void)printf(f, fieldwidth, func); \
54 	else if (precision) \
55 		(void)printf(f, precision, func); \
56 	else \
57 		(void)printf(f, func); \
58 }
59 
60 char **gargv;
61 
62 main(argc, argv)
63 	int argc;
64 	char **argv;
65 {
66 	static char *skip1, *skip2;
67 	register char *format, *fmt, *start;
68 	register int end, fieldwidth, precision;
69 	char convch, nextch, *getstr(), *mklong();
70 	double getdouble();
71 	long getlong();
72 
73 	if (argc < 2) {
74 		fprintf(stderr, "usage: printf format [arg ...]\n");
75 		exit(1);
76 	}
77 
78 	/*
79 	 * Basic algorithm is to scan the format string for conversion
80 	 * specifications -- once one is found, find out if the field
81 	 * width or precision is a '*'; if it is, gather up value.  Note,
82 	 * format strings are reused as necessary to use up the provided
83 	 * arguments, arguments of zero/null string are provided to use
84 	 * up the format string.
85 	 */
86 	skip1 = "#-+ 0";
87 	skip2 = "*0123456789";
88 
89 	escape(fmt = format = *++argv);		/* backslash interpretation */
90 	gargv = ++argv;
91 	for (;;) {
92 		end = 0;
93 		/* find next format specification */
94 next:		for (start = fmt;; ++fmt) {
95 			if (!*fmt) {
96 				/* avoid infinite loop */
97 				if (end == 1) {
98 					fprintf(stderr,
99 					    "printf: missing format character.\n");
100 					exit(1);
101 				}
102 				end = 1;
103 				if (fmt > start)
104 					(void)printf("%s", start);
105 				if (!*gargv)
106 					exit(0);
107 				fmt = format;
108 				goto next;
109 			}
110 			/* %% prints a % */
111 			if (*fmt == '%') {
112 				if (*++fmt != '%')
113 					break;
114 				*fmt++ = '\0';
115 				(void)printf("%s", start);
116 				goto next;
117 			}
118 		}
119 
120 		/* skip to field width */
121 		for (; index(skip1, *fmt); ++fmt);
122 		fieldwidth = *fmt == '*' ? getint() : 0;
123 
124 		/* skip to possible '.', get following precision */
125 		for (; index(skip2, *fmt); ++fmt);
126 		if (*fmt == '.')
127 			++fmt;
128 		precision = *fmt == '*' ? getint() : 0;
129 
130 		/* skip to conversion char */
131 		for (; index(skip2, *fmt); ++fmt);
132 		if (!*fmt) {
133 			fprintf(stderr, "printf: missing format character.\n");
134 			exit(1);
135 		}
136 
137 		convch = *fmt;
138 		nextch = *++fmt;
139 		*fmt = '\0';
140 		switch(convch) {
141 		case 'c': {
142 			char p = getchr();
143 			PF(start, p);
144 			break;
145 		}
146 		case 's': {
147 			char *p = getstr();
148 			PF(start, p);
149 			break;
150 		}
151 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
152 			char *f = mklong(start, convch);
153 			long p = getlong();
154 			PF(f, p);
155 			break;
156 		}
157 		case 'e': case 'E': case 'f': case 'g': case 'G': {
158 			double p = getdouble();
159 			PF(start, p);
160 			break;
161 		}
162 		default:
163 			fprintf(stderr, "printf: illegal format character.\n");
164 			exit(1);
165 		}
166 		*fmt = nextch;
167 	}
168 	/* NOTREACHED */
169 }
170 
171 char *
172 mklong(str, ch)
173 	char *str, ch;
174 {
175 	int len;
176 	char *copy, *malloc();
177 
178 	len = strlen(str) + 2;
179 	if (!(copy = malloc((u_int)len))) {	/* never freed; XXX */
180 		fprintf(stderr, "printf: out of memory.\n");
181 		exit(1);
182 	}
183 	bcopy(str, copy, len - 3);
184 	copy[len - 3] = 'l';
185 	copy[len - 2] = ch;
186 	copy[len - 1] = '\0';
187 	return(copy);
188 }
189 
190 escape(fmt)
191 	register char *fmt;
192 {
193 	register char *store;
194 	register int value, c;
195 
196 	for (store = fmt; c = *fmt; ++fmt, ++store) {
197 		if (c != '\\') {
198 			*store = c;
199 			continue;
200 		}
201 		switch (*++fmt) {
202 		case '\0':		/* EOS, user error */
203 			*store = '\\';
204 			*++store = '\0';
205 			return;
206 		case '\\':		/* backslash */
207 		case '\'':		/* single quote */
208 			*store = *fmt;
209 			break;
210 		case 'a':		/* bell/alert */
211 			*store = '\7';
212 			break;
213 		case 'b':		/* backspace */
214 			*store = '\b';
215 			break;
216 		case 'f':		/* form-feed */
217 			*store = '\f';
218 			break;
219 		case 'n':		/* newline */
220 			*store = '\n';
221 			break;
222 		case 'r':		/* carriage-return */
223 			*store = '\r';
224 			break;
225 		case 't':		/* horizontal tab */
226 			*store = '\t';
227 			break;
228 		case 'v':		/* vertical tab */
229 			*store = '\13';
230 			break;
231 					/* octal constant */
232 		case '0': case '1': case '2': case '3':
233 		case '4': case '5': case '6': case '7':
234 			for (c = 3, value = 0;
235 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
236 				value <<= 3;
237 				value += *fmt - '0';
238 			}
239 			--fmt;
240 			*store = value;
241 			break;
242 		default:
243 			*store = *fmt;
244 			break;
245 		}
246 	}
247 	*store = '\0';
248 }
249 
250 getchr()
251 {
252 	if (!*gargv)
253 		return((int)'\0');
254 	return((int)**gargv++);
255 }
256 
257 char *
258 getstr()
259 {
260 	if (!*gargv)
261 		return("");
262 	return(*gargv++);
263 }
264 
265 static char *number = "+-.0123456789";
266 getint()
267 {
268 	if (!*gargv)
269 		return(0);
270 	if (index(number, **gargv))
271 		return(atoi(*gargv++));
272 	return(asciicode());
273 }
274 
275 long
276 getlong()
277 {
278 	long atol();
279 
280 	if (!*gargv)
281 		return((long)0);
282 	if (index(number, **gargv))
283 		return(strtol(*gargv++, (char **)NULL, 0));
284 	return((long)asciicode());
285 }
286 
287 double
288 getdouble()
289 {
290 	double atof();
291 
292 	if (!*gargv)
293 		return((double)0);
294 	if (index(number, **gargv))
295 		return(atof(*gargv++));
296 	return((double)asciicode());
297 }
298 
299 asciicode()
300 {
301 	register char ch;
302 
303 	ch = **gargv;
304 	if (ch == '\'' || ch == '"')
305 		ch = (*gargv)[1];
306 	++gargv;
307 	return(ch);
308 }
309