1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  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 #if !defined(BUILTIN) && !defined(SHELL)
35 #ifndef lint
36 static char const copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 #endif
41 
42 #ifndef lint
43 static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
44 #endif /* not lint */
45 
46 #include <sys/types.h>
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #ifdef SHELL
57 #define main printfcmd
58 #include "bltin/bltin.h"
59 #else
60 #define	warnx1(a, b, c)		warnx(a)
61 #define	warnx2(a, b, c)		warnx(a, b)
62 #define	warnx3(a, b, c)		warnx(a, b, c)
63 #endif
64 
65 #define PF(f, func) { \
66 	char *b = NULL; \
67 	if (fieldwidth) \
68 		if (precision) \
69 			(void)asprintf(&b, f, fieldwidth, precision, func); \
70 		else \
71 			(void)asprintf(&b, f, fieldwidth, func); \
72 	else if (precision) \
73 		(void)asprintf(&b, f, precision, func); \
74 	else \
75 		(void)asprintf(&b, f, func); \
76 	if (b) { \
77 		(void)fputs(b, stdout); \
78 		free(b); \
79 	} \
80 }
81 
82 static int	 asciicode __P((void));
83 static void	 escape __P((char *));
84 static int	 getchr __P((void));
85 static double	 getdouble __P((void));
86 static int	 getint __P((int *));
87 static int	 getlong __P((long *));
88 static char	*getstr __P((void));
89 static char	*mklong __P((char *, int));
90 static void	 usage __P((void));
91 
92 static char **gargv;
93 
94 int
95 #ifdef BUILTIN
progprintf(argc,argv)96 progprintf(argc, argv)
97 #else
98 main(argc, argv)
99 #endif
100 	int argc;
101 	char *argv[];
102 {
103 	extern int optind;
104 	static char *skip1, *skip2;
105 	int ch, end, fieldwidth, precision;
106 	char convch, nextch, *format, *fmt, *start;
107 
108 	while ((ch = getopt(argc, argv, "")) != -1)
109 		switch (ch) {
110 		case '?':
111 		default:
112 			usage();
113 			return (1);
114 		}
115 	argc -= optind;
116 	argv += optind;
117 
118 	if (argc < 1) {
119 		usage();
120 		return (1);
121 	}
122 
123 	/*
124 	 * Basic algorithm is to scan the format string for conversion
125 	 * specifications -- once one is found, find out if the field
126 	 * width or precision is a '*'; if it is, gather up value.  Note,
127 	 * format strings are reused as necessary to use up the provided
128 	 * arguments, arguments of zero/null string are provided to use
129 	 * up the format string.
130 	 */
131 	skip1 = "#-+ 0";
132 	skip2 = "0123456789";
133 
134 	escape(fmt = format = *argv);		/* backslash interpretation */
135 	gargv = ++argv;
136 	for (;;) {
137 		end = 0;
138 		/* find next format specification */
139 next:		for (start = fmt;; ++fmt) {
140 			if (!*fmt) {
141 				/* avoid infinite loop */
142 				if (end == 1) {
143 					warnx1("missing format character",
144 					    NULL, NULL);
145 					return (1);
146 				}
147 				end = 1;
148 				if (fmt > start)
149 					(void)printf("%s", start);
150 				if (!*gargv)
151 					return (0);
152 				fmt = format;
153 				goto next;
154 			}
155 			/* %% prints a % */
156 			if (*fmt == '%') {
157 				if (*++fmt != '%')
158 					break;
159 				*fmt++ = '\0';
160 				(void)printf("%s", start);
161 				goto next;
162 			}
163 		}
164 
165 		/* skip to field width */
166 		for (; strchr(skip1, *fmt); ++fmt);
167 		if (*fmt == '*') {
168 			if (getint(&fieldwidth))
169 				return (1);
170 			++fmt;
171 		} else {
172 			fieldwidth = 0;
173 
174 			/* skip to possible '.', get following precision */
175 			for (; strchr(skip2, *fmt); ++fmt);
176 		}
177 		if (*fmt == '.') {
178 			/* precision present? */
179 			++fmt;
180 			if (*fmt == '*') {
181 				if (getint(&precision))
182 					return (1);
183 				++fmt;
184 			} else {
185 				precision = 0;
186 
187 				/* skip to conversion char */
188 				for (; strchr(skip2, *fmt); ++fmt);
189 			}
190 		} else
191 			precision = 0;
192 		if (!*fmt) {
193 			warnx1("missing format character", NULL, NULL);
194 			return (1);
195 		}
196 
197 		convch = *fmt;
198 		nextch = *++fmt;
199 		*fmt = '\0';
200 		switch(convch) {
201 		case 'c': {
202 			char p;
203 
204 			p = getchr();
205 			PF(start, p);
206 			break;
207 		}
208 		case 's': {
209 			char *p;
210 
211 			p = getstr();
212 			PF(start, p);
213 			break;
214 		}
215 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
216 			long p;
217 			char *f;
218 
219 			if ((f = mklong(start, convch)) == NULL)
220 				return (1);
221 			if (getlong(&p))
222 				return (1);
223 			PF(f, p);
224 			break;
225 		}
226 		case 'e': case 'E': case 'f': case 'g': case 'G': {
227 			double p;
228 
229 			p = getdouble();
230 			PF(start, p);
231 			break;
232 		}
233 		default:
234 			warnx2("illegal format character %c", convch, NULL);
235 			return (1);
236 		}
237 		*fmt = nextch;
238 	}
239 	/* NOTREACHED */
240 }
241 
242 static char *
mklong(str,ch)243 mklong(str, ch)
244 	char *str;
245 	int ch;
246 {
247 	static char copy[64];
248 	int len;
249 
250 	len = strlen(str) + 2;
251 	memmove(copy, str, len - 3);
252 	copy[len - 3] = 'l';
253 	copy[len - 2] = ch;
254 	copy[len - 1] = '\0';
255 	return (copy);
256 }
257 
258 static void
escape(fmt)259 escape(fmt)
260 	register char *fmt;
261 {
262 	register char *store;
263 	register int value, c;
264 
265 	for (store = fmt; (c = *fmt); ++fmt, ++store) {
266 		if (c != '\\') {
267 			*store = c;
268 			continue;
269 		}
270 		switch (*++fmt) {
271 		case '\0':		/* EOS, user error */
272 			*store = '\\';
273 			*++store = '\0';
274 			return;
275 		case '\\':		/* backslash */
276 		case '\'':		/* single quote */
277 			*store = *fmt;
278 			break;
279 		case 'a':		/* bell/alert */
280 			*store = '\7';
281 			break;
282 		case 'b':		/* backspace */
283 			*store = '\b';
284 			break;
285 		case 'f':		/* form-feed */
286 			*store = '\f';
287 			break;
288 		case 'n':		/* newline */
289 			*store = '\n';
290 			break;
291 		case 'r':		/* carriage-return */
292 			*store = '\r';
293 			break;
294 		case 't':		/* horizontal tab */
295 			*store = '\t';
296 			break;
297 		case 'v':		/* vertical tab */
298 			*store = '\13';
299 			break;
300 					/* octal constant */
301 		case '0': case '1': case '2': case '3':
302 		case '4': case '5': case '6': case '7':
303 			for (c = 3, value = 0;
304 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
305 				value <<= 3;
306 				value += *fmt - '0';
307 			}
308 			--fmt;
309 			*store = value;
310 			break;
311 		default:
312 			*store = *fmt;
313 			break;
314 		}
315 	}
316 	*store = '\0';
317 }
318 
319 static int
getchr()320 getchr()
321 {
322 	if (!*gargv)
323 		return ('\0');
324 	return ((int)**gargv++);
325 }
326 
327 static char *
getstr()328 getstr()
329 {
330 	if (!*gargv)
331 		return ("");
332 	return (*gargv++);
333 }
334 
335 static char *Number = "+-.0123456789";
336 static int
getint(ip)337 getint(ip)
338 	int *ip;
339 {
340 	long val;
341 
342 	if (getlong(&val))
343 		return (1);
344 	if (val > INT_MAX) {
345 		warnx3("%s: %s", *gargv, strerror(ERANGE));
346 		return (1);
347 	}
348 	*ip = val;
349 	return (0);
350 }
351 
352 static int
getlong(lp)353 getlong(lp)
354 	long *lp;
355 {
356 	long val;
357 	char *ep;
358 
359 	if (!*gargv) {
360 		*lp = 0;
361 		return (0);
362 	}
363 	if (strchr(Number, **gargv)) {
364 		errno = 0;
365 		val = strtol(*gargv, &ep, 0);
366 		if (*ep != '\0') {
367 			warnx2("%s: illegal number", *gargv, NULL);
368 			return (1);
369 		}
370 		if (errno == ERANGE)
371 			if (val == LONG_MAX) {
372 				warnx3("%s: %s", *gargv, strerror(ERANGE));
373 				return (1);
374 			}
375 			if (val == LONG_MIN) {
376 				warnx3("%s: %s", *gargv, strerror(ERANGE));
377 				return (1);
378 			}
379 
380 		*lp = val;
381 		++gargv;
382 		return (0);
383 	}
384 	*lp =  (long)asciicode();
385 	return (0);
386 }
387 
388 static double
getdouble()389 getdouble()
390 {
391 	if (!*gargv)
392 		return ((double)0);
393 	if (strchr(Number, **gargv))
394 		return (atof(*gargv++));
395 	return ((double)asciicode());
396 }
397 
398 static int
asciicode()399 asciicode()
400 {
401 	register int ch;
402 
403 	ch = **gargv;
404 	if (ch == '\'' || ch == '"')
405 		ch = (*gargv)[1];
406 	++gargv;
407 	return (ch);
408 }
409 
410 static void
usage()411 usage()
412 {
413 	(void)fprintf(stderr, "usage: printf format [arg ...]\n");
414 }
415