Home | History | Annotate | Line # | Download | only in printf
printf.c revision 1.5
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.5      jtc #if !defined(SHELL) && !defined(BUILTIN)
     36  1.1      cgd char copyright[] =
     37  1.1      cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     38  1.1      cgd  All rights reserved.\n";
     39  1.5      jtc #endif
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd #ifndef lint
     43  1.3  mycroft /*static char sccsid[] = "from: @(#)printf.c	5.9 (Berkeley) 6/1/90";*/
     44  1.5      jtc static char rcsid[] = "$Id: printf.c,v 1.5 1993/11/19 20:30:57 jtc Exp $";
     45  1.1      cgd #endif /* not lint */
     46  1.1      cgd 
     47  1.4      jtc #include <ctype.h>
     48  1.1      cgd #include <stdio.h>
     49  1.4      jtc #include <stdlib.h>
     50  1.2  mycroft #include <string.h>
     51  1.4      jtc #include <limits.h>
     52  1.4      jtc #include <locale.h>
     53  1.4      jtc #include <err.h>
     54  1.4      jtc 
     55  1.5      jtc static void	 do_printf __P((char *));
     56  1.5      jtc static void	 print_escape_str();
     57  1.5      jtc static int	 print_escape();
     58  1.5      jtc 
     59  1.5      jtc static int	 getchr __P((void));
     60  1.5      jtc static double	 getdouble __P((void));
     61  1.5      jtc static int	 getint __P((void));
     62  1.5      jtc static long	 getlong __P((void));
     63  1.5      jtc static char	*getstr __P((void));
     64  1.5      jtc static char	*mklong __P((char *, int));
     65  1.5      jtc static void	 usage __P((void));
     66  1.4      jtc 
     67  1.5      jtc static int	rval;
     68  1.5      jtc static char  **gargv;
     69  1.4      jtc 
     70  1.4      jtc #define isodigit(c)	((c) >= '0' && (c) <= '7')
     71  1.4      jtc #define octtobin(c)	((c) - '0')
     72  1.4      jtc #define hextobin(c)	((c) >= 'A' && 'c' <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0')
     73  1.1      cgd 
     74  1.5      jtc #ifdef SHELL
     75  1.5      jtc #define main printfcmd
     76  1.5      jtc #include "../../bin/sh/bltin/bltin.h"
     77  1.5      jtc 
     78  1.5      jtc #ifdef __STDC__
     79  1.5      jtc #include <stdarg.h>
     80  1.5      jtc #else
     81  1.5      jtc #include <vararg.h>
     82  1.5      jtc #endif
     83  1.5      jtc 
     84  1.5      jtc static void
     85  1.5      jtc #ifdef __STDC__
     86  1.5      jtc warnx(const char *fmt, ...)
     87  1.5      jtc #else
     88  1.5      jtc warnx(fmt, va_alist)
     89  1.5      jtc 	const char *fmt;
     90  1.5      jtc 	va_dcl
     91  1.5      jtc #endif
     92  1.5      jtc {
     93  1.5      jtc 
     94  1.5      jtc 	char buf[64];
     95  1.5      jtc 	va_list ap;
     96  1.5      jtc 
     97  1.5      jtc #ifdef __STDC__
     98  1.5      jtc 	va_start(ap, fmt);
     99  1.5      jtc #else
    100  1.5      jtc 	va_start(ap);
    101  1.5      jtc #endif
    102  1.5      jtc 	vsprintf(buf, fmt, ap);
    103  1.5      jtc 	va_end(ap);
    104  1.5      jtc 
    105  1.5      jtc 	error(buf);
    106  1.5      jtc }
    107  1.5      jtc #endif /* SHELL */
    108  1.5      jtc 
    109  1.1      cgd #define PF(f, func) { \
    110  1.1      cgd 	if (fieldwidth) \
    111  1.1      cgd 		if (precision) \
    112  1.1      cgd 			(void)printf(f, fieldwidth, precision, func); \
    113  1.1      cgd 		else \
    114  1.1      cgd 			(void)printf(f, fieldwidth, func); \
    115  1.1      cgd 	else if (precision) \
    116  1.1      cgd 		(void)printf(f, precision, func); \
    117  1.1      cgd 	else \
    118  1.1      cgd 		(void)printf(f, func); \
    119  1.1      cgd }
    120  1.1      cgd 
    121  1.4      jtc int
    122  1.5      jtc #ifdef BUILTIN
    123  1.5      jtc progprintf(argc, argv)
    124  1.5      jtc #else
    125  1.1      cgd main(argc, argv)
    126  1.5      jtc #endif
    127  1.1      cgd 	int argc;
    128  1.1      cgd 	char **argv;
    129  1.1      cgd {
    130  1.4      jtc 	char *format;
    131  1.5      jtc 	int ch;
    132  1.4      jtc 
    133  1.5      jtc #if !defined(SHELL) && !defined(BUILTIN)
    134  1.4      jtc 	setlocale (LC_ALL, "");
    135  1.5      jtc #endif
    136  1.1      cgd 
    137  1.5      jtc 	while ((ch = getopt(argc, argv, "")) != -1) {
    138  1.5      jtc 		switch (ch) {
    139  1.5      jtc 		case '?':
    140  1.5      jtc 		default:
    141  1.5      jtc 			usage();
    142  1.5      jtc 			return (1);
    143  1.5      jtc 		}
    144  1.1      cgd 	}
    145  1.5      jtc 	argc -= optind;
    146  1.5      jtc 	argv += optind;
    147  1.1      cgd 
    148  1.5      jtc 	if (argc < 1) {
    149  1.5      jtc 		usage();
    150  1.5      jtc 		return (1);
    151  1.5      jtc 	}
    152  1.5      jtc 
    153  1.5      jtc 	format = *argv;
    154  1.5      jtc 	gargv = ++argv;
    155  1.4      jtc 
    156  1.4      jtc 	do {
    157  1.4      jtc 		do_printf(format);
    158  1.5      jtc 	} while (gargv > argv && *gargv);
    159  1.4      jtc 
    160  1.5      jtc 	return (rval);
    161  1.4      jtc }
    162  1.4      jtc 
    163  1.5      jtc static void
    164  1.4      jtc do_printf(format)
    165  1.4      jtc 	char *format;
    166  1.4      jtc {
    167  1.4      jtc 	static char *skip1, *skip2;
    168  1.4      jtc 	register char *fmt, *start;
    169  1.4      jtc 	register int fieldwidth, precision;
    170  1.5      jtc 	char convch, nextch;
    171  1.4      jtc 
    172  1.1      cgd 	/*
    173  1.1      cgd 	 * Basic algorithm is to scan the format string for conversion
    174  1.1      cgd 	 * specifications -- once one is found, find out if the field
    175  1.1      cgd 	 * width or precision is a '*'; if it is, gather up value.  Note,
    176  1.1      cgd 	 * format strings are reused as necessary to use up the provided
    177  1.1      cgd 	 * arguments, arguments of zero/null string are provided to use
    178  1.1      cgd 	 * up the format string.
    179  1.1      cgd 	 */
    180  1.1      cgd 	skip1 = "#-+ 0";
    181  1.1      cgd 	skip2 = "*0123456789";
    182  1.1      cgd 
    183  1.4      jtc 	/* find next format specification */
    184  1.4      jtc 	for (fmt = format; *fmt; fmt++) {
    185  1.4      jtc 		switch (*fmt) {
    186  1.4      jtc 		case '%':
    187  1.4      jtc 			start = fmt++;
    188  1.4      jtc 
    189  1.4      jtc 			if (*fmt == '%') {
    190  1.4      jtc 				putchar ('%');
    191  1.4      jtc 				break;
    192  1.4      jtc 			} else if (*fmt == 'b') {
    193  1.4      jtc 				char *p = getstr();
    194  1.4      jtc 				print_escape_str(p);
    195  1.4      jtc 				break;
    196  1.4      jtc 			}
    197  1.4      jtc 
    198  1.4      jtc 			/* skip to field width */
    199  1.4      jtc 			for (; index(skip1, *fmt); ++fmt);
    200  1.4      jtc 			fieldwidth = *fmt == '*' ? getint() : 0;
    201  1.4      jtc 
    202  1.4      jtc 			/* skip to possible '.', get following precision */
    203  1.4      jtc 			for (; index(skip2, *fmt); ++fmt);
    204  1.4      jtc 			if (*fmt == '.')
    205  1.4      jtc 				++fmt;
    206  1.4      jtc 			precision = *fmt == '*' ? getint() : 0;
    207  1.4      jtc 
    208  1.4      jtc 			for (; index(skip2, *fmt); ++fmt);
    209  1.1      cgd 			if (!*fmt) {
    210  1.5      jtc 				warnx ("missing format character");
    211  1.4      jtc 				rval = 1;
    212  1.4      jtc 				return;
    213  1.4      jtc 			}
    214  1.4      jtc 
    215  1.4      jtc 			convch = *fmt;
    216  1.4      jtc 			nextch = *(fmt + 1);
    217  1.4      jtc 			*(fmt + 1) = '\0';
    218  1.4      jtc 			switch(convch) {
    219  1.4      jtc 			case 'c': {
    220  1.4      jtc 				char p = getchr();
    221  1.4      jtc 				PF(start, p);
    222  1.4      jtc 				break;
    223  1.4      jtc 			}
    224  1.4      jtc 			case 's': {
    225  1.4      jtc 				char *p = getstr();
    226  1.4      jtc 				PF(start, p);
    227  1.4      jtc 				break;
    228  1.4      jtc 			}
    229  1.4      jtc 			case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
    230  1.4      jtc 				char *f = mklong(start, convch);
    231  1.4      jtc 				long p = getlong();
    232  1.4      jtc 				PF(f, p);
    233  1.4      jtc 				free(f);
    234  1.4      jtc 				break;
    235  1.4      jtc 			}
    236  1.4      jtc 			case 'e': case 'E': case 'f': case 'g': case 'G': {
    237  1.4      jtc 				double p = getdouble();
    238  1.4      jtc 				PF(start, p);
    239  1.4      jtc 				break;
    240  1.1      cgd 			}
    241  1.4      jtc 			default:
    242  1.5      jtc 				warnx ("%s: invalid directive", start);
    243  1.5      jtc 				rval = 1;
    244  1.1      cgd 			}
    245  1.4      jtc 			*(fmt + 1) = nextch;
    246  1.4      jtc 			break;
    247  1.1      cgd 
    248  1.4      jtc 		case '\\':
    249  1.4      jtc 			fmt += print_escape(fmt);
    250  1.4      jtc 			break;
    251  1.1      cgd 
    252  1.4      jtc 		default:
    253  1.4      jtc 			putchar (*fmt);
    254  1.1      cgd 			break;
    255  1.1      cgd 		}
    256  1.4      jtc 	}
    257  1.4      jtc }
    258  1.4      jtc 
    259  1.4      jtc 
    260  1.4      jtc /*
    261  1.4      jtc  * Print SysV echo(1) style escape string
    262  1.4      jtc  */
    263  1.5      jtc static void
    264  1.4      jtc print_escape_str(str)
    265  1.4      jtc 	register char *str;
    266  1.4      jtc {
    267  1.4      jtc 	int value;
    268  1.4      jtc 	int c;
    269  1.4      jtc 
    270  1.4      jtc 	while (*str) {
    271  1.4      jtc 		if (*str == '\\') {
    272  1.4      jtc 			str++;
    273  1.4      jtc 			/*
    274  1.4      jtc 			 * %b string octal constants are not like those in C.
    275  1.4      jtc 			 * They start with a \0, and are followed by 0, 1, 2,
    276  1.4      jtc 			 * or 3 octal digits.
    277  1.4      jtc 			 */
    278  1.4      jtc 			if (*str == '0') {
    279  1.4      jtc 				str++;
    280  1.4      jtc 				for (c = 3, value = 0; c-- && isodigit(*str); str++) {
    281  1.4      jtc 					value <<= 3;
    282  1.4      jtc 					value += octtobin(*str);
    283  1.4      jtc 				}
    284  1.4      jtc 				putchar (value);
    285  1.4      jtc 				str--;
    286  1.4      jtc 			} else if (*str == 'c') {
    287  1.4      jtc 				exit (rval);
    288  1.4      jtc 			} else {
    289  1.4      jtc 				str--;
    290  1.4      jtc 				str += print_escape(str);
    291  1.4      jtc 			}
    292  1.4      jtc 		} else {
    293  1.4      jtc 			putchar (*str);
    294  1.1      cgd 		}
    295  1.4      jtc 		str++;
    296  1.4      jtc 	}
    297  1.4      jtc }
    298  1.4      jtc 
    299  1.4      jtc /*
    300  1.4      jtc  * Print "standard" escape characters
    301  1.4      jtc  */
    302  1.5      jtc static int
    303  1.4      jtc print_escape(str)
    304  1.4      jtc 	register char *str;
    305  1.4      jtc {
    306  1.4      jtc 	char *start = str;
    307  1.4      jtc 	int c;
    308  1.4      jtc 	int value;
    309  1.4      jtc 
    310  1.4      jtc 	str++;
    311  1.4      jtc 
    312  1.4      jtc 	switch (*str) {
    313  1.4      jtc 	case '0': case '1': case '2': case '3':
    314  1.4      jtc 	case '4': case '5': case '6': case '7':
    315  1.4      jtc 		for (c = 3, value = 0; c-- && isodigit(*str); str++) {
    316  1.4      jtc 			value <<= 3;
    317  1.4      jtc 			value += octtobin(*str);
    318  1.1      cgd 		}
    319  1.4      jtc 		putchar(value);
    320  1.4      jtc 		return str - start - 1;
    321  1.4      jtc 		/* NOTREACHED */
    322  1.4      jtc 
    323  1.4      jtc 	case 'x':
    324  1.4      jtc 		str++;
    325  1.4      jtc 		for (value = 0; isxdigit(*str); str++) {
    326  1.4      jtc 			value <<= 4;
    327  1.4      jtc 			value += hextobin(*str);
    328  1.1      cgd 		}
    329  1.4      jtc 		if (value > UCHAR_MAX) {
    330  1.4      jtc 			warnx ("escape sequence out of range for character");
    331  1.4      jtc 			rval = 1;
    332  1.1      cgd 		}
    333  1.4      jtc 		putchar (value);
    334  1.4      jtc 		return str - start - 1;
    335  1.4      jtc 		/* NOTREACHED */
    336  1.4      jtc 
    337  1.4      jtc 	case '\\':			/* backslash */
    338  1.4      jtc 		putchar('\\');
    339  1.4      jtc 		break;
    340  1.4      jtc 
    341  1.4      jtc 	case '\'':			/* single quote */
    342  1.4      jtc 		putchar('\'');
    343  1.4      jtc 		break;
    344  1.4      jtc 
    345  1.4      jtc 	case '"':			/* double quote */
    346  1.4      jtc 		putchar('"');
    347  1.4      jtc 		break;
    348  1.4      jtc 
    349  1.4      jtc 	case 'a':			/* alert */
    350  1.4      jtc #ifdef __STDC__
    351  1.4      jtc 		putchar('\a');
    352  1.4      jtc #else
    353  1.4      jtc 		putchar(007);
    354  1.4      jtc #endif
    355  1.4      jtc 		break;
    356  1.4      jtc 
    357  1.4      jtc 	case 'b':			/* backspace */
    358  1.4      jtc 		putchar('\b');
    359  1.4      jtc 		break;
    360  1.4      jtc 
    361  1.4      jtc 	case 'e':			/* escape */
    362  1.4      jtc #ifdef __GNUC__
    363  1.4      jtc 		putchar('\e');
    364  1.4      jtc #else
    365  1.4      jtc 		putchar(033);
    366  1.4      jtc #endif
    367  1.4      jtc 		break;
    368  1.4      jtc 
    369  1.4      jtc 	case 'f':			/* form-feed */
    370  1.4      jtc 		putchar('\f');
    371  1.4      jtc 		break;
    372  1.4      jtc 
    373  1.4      jtc 	case 'n':			/* newline */
    374  1.4      jtc 		putchar('\n');
    375  1.4      jtc 		break;
    376  1.4      jtc 
    377  1.4      jtc 	case 'r':			/* carriage-return */
    378  1.4      jtc 		putchar('\r');
    379  1.4      jtc 		break;
    380  1.4      jtc 
    381  1.4      jtc 	case 't':			/* tab */
    382  1.4      jtc 		putchar('\t');
    383  1.4      jtc 		break;
    384  1.4      jtc 
    385  1.4      jtc 	case 'v':			/* vertical-tab */
    386  1.4      jtc 		putchar('\v');
    387  1.4      jtc 		break;
    388  1.4      jtc 
    389  1.4      jtc 	default:
    390  1.4      jtc 		putchar(*str);
    391  1.4      jtc 		warnx("unknown escape sequence `\\%c'", *str);
    392  1.4      jtc 		rval = 1;
    393  1.1      cgd 	}
    394  1.4      jtc 
    395  1.4      jtc 	return 1;
    396  1.1      cgd }
    397  1.1      cgd 
    398  1.5      jtc static char *
    399  1.1      cgd mklong(str, ch)
    400  1.1      cgd 	char *str, ch;
    401  1.1      cgd {
    402  1.5      jtc 	static char copy[64];
    403  1.5      jtc 	int len;
    404  1.1      cgd 
    405  1.1      cgd 	len = strlen(str) + 2;
    406  1.5      jtc 	memmove(copy, str, len - 3);
    407  1.1      cgd 	copy[len - 3] = 'l';
    408  1.1      cgd 	copy[len - 2] = ch;
    409  1.1      cgd 	copy[len - 1] = '\0';
    410  1.5      jtc 	return (copy);
    411  1.1      cgd }
    412  1.1      cgd 
    413  1.5      jtc static int
    414  1.1      cgd getchr()
    415  1.1      cgd {
    416  1.1      cgd 	if (!*gargv)
    417  1.1      cgd 		return((int)'\0');
    418  1.1      cgd 	return((int)**gargv++);
    419  1.1      cgd }
    420  1.1      cgd 
    421  1.5      jtc static char *
    422  1.1      cgd getstr()
    423  1.1      cgd {
    424  1.1      cgd 	if (!*gargv)
    425  1.1      cgd 		return("");
    426  1.1      cgd 	return(*gargv++);
    427  1.1      cgd }
    428  1.1      cgd 
    429  1.1      cgd static char *number = "+-.0123456789";
    430  1.5      jtc static int
    431  1.1      cgd getint()
    432  1.1      cgd {
    433  1.1      cgd 	if (!*gargv)
    434  1.1      cgd 		return(0);
    435  1.4      jtc 
    436  1.1      cgd 	if (index(number, **gargv))
    437  1.1      cgd 		return(atoi(*gargv++));
    438  1.4      jtc 
    439  1.4      jtc 	return 0;
    440  1.1      cgd }
    441  1.1      cgd 
    442  1.5      jtc static long
    443  1.1      cgd getlong()
    444  1.1      cgd {
    445  1.4      jtc 	long val;
    446  1.4      jtc 	char *ep;
    447  1.1      cgd 
    448  1.1      cgd 	if (!*gargv)
    449  1.4      jtc 		return(0L);
    450  1.4      jtc 
    451  1.4      jtc 	if (**gargv == '\"' || **gargv == '\'') {
    452  1.4      jtc 		val = gargv[0][1];
    453  1.4      jtc 		gargv++;
    454  1.4      jtc 		return val;
    455  1.4      jtc 	}
    456  1.4      jtc 
    457  1.4      jtc 	val = strtol (*gargv, &ep, 0);
    458  1.4      jtc 	if (*ep) {
    459  1.4      jtc 		warnx ("incompletely converted argument: %s", *gargv);
    460  1.4      jtc 		rval = 1;
    461  1.4      jtc 	}
    462  1.4      jtc 
    463  1.4      jtc 	gargv++;
    464  1.4      jtc 	return val;
    465  1.1      cgd }
    466  1.1      cgd 
    467  1.5      jtc static double
    468  1.1      cgd getdouble()
    469  1.1      cgd {
    470  1.4      jtc 	double val;
    471  1.4      jtc 	char *ep;
    472  1.1      cgd 
    473  1.1      cgd 	if (!*gargv)
    474  1.4      jtc 		return(0.0);
    475  1.4      jtc 
    476  1.4      jtc 	if (**gargv == '\"' || **gargv == '\'') {
    477  1.4      jtc 		val = gargv[0][1];
    478  1.4      jtc 		gargv++;
    479  1.4      jtc 		return val;
    480  1.4      jtc 	}
    481  1.1      cgd 
    482  1.4      jtc 	val = strtod (*gargv, &ep);
    483  1.4      jtc 	if (*ep) {
    484  1.4      jtc 		warnx ("incompletely converted argument: %s", *gargv);
    485  1.4      jtc 		rval = 1;
    486  1.4      jtc 	}
    487  1.1      cgd 
    488  1.4      jtc 	gargv++;
    489  1.4      jtc 	return val;
    490  1.5      jtc }
    491  1.5      jtc 
    492  1.5      jtc static void
    493  1.5      jtc usage()
    494  1.5      jtc {
    495  1.5      jtc 	(void)fprintf(stderr, "usage: printf format [arg ...]\n");
    496  1.1      cgd }
    497