Home | History | Annotate | Line # | Download | only in printf
printf.c revision 1.4
      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.1      cgd char copyright[] =
     36  1.1      cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     37  1.1      cgd  All rights reserved.\n";
     38  1.1      cgd #endif /* not lint */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.3  mycroft /*static char sccsid[] = "from: @(#)printf.c	5.9 (Berkeley) 6/1/90";*/
     42  1.3  mycroft static char rcsid[] = "$Id: printf.c,v 1.4 1993/11/05 20:12:40 jtc Exp $";
     43  1.1      cgd #endif /* not lint */
     44  1.1      cgd 
     45  1.4      jtc #include <ctype.h>
     46  1.1      cgd #include <stdio.h>
     47  1.4      jtc #include <stdlib.h>
     48  1.2  mycroft #include <string.h>
     49  1.4      jtc #include <limits.h>
     50  1.4      jtc #include <locale.h>
     51  1.4      jtc #include <err.h>
     52  1.4      jtc 
     53  1.4      jtc void	do_printf();
     54  1.4      jtc void	print_escape_str();
     55  1.4      jtc int	print_escape();
     56  1.4      jtc 
     57  1.4      jtc int	getchr();
     58  1.4      jtc int	getint();
     59  1.4      jtc long	getlong();
     60  1.4      jtc double	getdouble();
     61  1.4      jtc char   *getstr();
     62  1.4      jtc 
     63  1.4      jtc int	rval;
     64  1.4      jtc char  **gargv;
     65  1.4      jtc 
     66  1.4      jtc #define isodigit(c)	((c) >= '0' && (c) <= '7')
     67  1.4      jtc #define octtobin(c)	((c) - '0')
     68  1.4      jtc #define hextobin(c)	((c) >= 'A' && 'c' <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0')
     69  1.1      cgd 
     70  1.1      cgd #define PF(f, func) { \
     71  1.1      cgd 	if (fieldwidth) \
     72  1.1      cgd 		if (precision) \
     73  1.1      cgd 			(void)printf(f, fieldwidth, precision, func); \
     74  1.1      cgd 		else \
     75  1.1      cgd 			(void)printf(f, fieldwidth, func); \
     76  1.1      cgd 	else if (precision) \
     77  1.1      cgd 		(void)printf(f, precision, func); \
     78  1.1      cgd 	else \
     79  1.1      cgd 		(void)printf(f, func); \
     80  1.1      cgd }
     81  1.1      cgd 
     82  1.4      jtc int
     83  1.1      cgd main(argc, argv)
     84  1.1      cgd 	int argc;
     85  1.1      cgd 	char **argv;
     86  1.1      cgd {
     87  1.4      jtc 	char *format;
     88  1.4      jtc 
     89  1.4      jtc 	setlocale (LC_ALL, "");
     90  1.1      cgd 
     91  1.1      cgd 	if (argc < 2) {
     92  1.1      cgd 		fprintf(stderr, "usage: printf format [arg ...]\n");
     93  1.1      cgd 		exit(1);
     94  1.1      cgd 	}
     95  1.1      cgd 
     96  1.4      jtc 	format = argv[1];
     97  1.4      jtc 	gargv  = argv + 2;
     98  1.4      jtc 
     99  1.4      jtc 	do {
    100  1.4      jtc 		do_printf(format);
    101  1.4      jtc 	} while (*gargv);
    102  1.4      jtc 
    103  1.4      jtc 	exit (rval);
    104  1.4      jtc }
    105  1.4      jtc 
    106  1.4      jtc void
    107  1.4      jtc do_printf(format)
    108  1.4      jtc 	char *format;
    109  1.4      jtc {
    110  1.4      jtc 	static char *skip1, *skip2;
    111  1.4      jtc 	register char *fmt, *start;
    112  1.4      jtc 	register int fieldwidth, precision;
    113  1.4      jtc 	char convch, nextch, *mklong();
    114  1.4      jtc 
    115  1.1      cgd 	/*
    116  1.1      cgd 	 * Basic algorithm is to scan the format string for conversion
    117  1.1      cgd 	 * specifications -- once one is found, find out if the field
    118  1.1      cgd 	 * width or precision is a '*'; if it is, gather up value.  Note,
    119  1.1      cgd 	 * format strings are reused as necessary to use up the provided
    120  1.1      cgd 	 * arguments, arguments of zero/null string are provided to use
    121  1.1      cgd 	 * up the format string.
    122  1.1      cgd 	 */
    123  1.1      cgd 	skip1 = "#-+ 0";
    124  1.1      cgd 	skip2 = "*0123456789";
    125  1.1      cgd 
    126  1.4      jtc 	/* find next format specification */
    127  1.4      jtc 	for (fmt = format; *fmt; fmt++) {
    128  1.4      jtc 		switch (*fmt) {
    129  1.4      jtc 		case '%':
    130  1.4      jtc 			start = fmt++;
    131  1.4      jtc 
    132  1.4      jtc 			if (*fmt == '%') {
    133  1.4      jtc 				putchar ('%');
    134  1.4      jtc 				break;
    135  1.4      jtc 			} else if (*fmt == 'b') {
    136  1.4      jtc 				char *p = getstr();
    137  1.4      jtc 				print_escape_str(p);
    138  1.4      jtc 				break;
    139  1.4      jtc 			}
    140  1.4      jtc 
    141  1.4      jtc 			/* skip to field width */
    142  1.4      jtc 			for (; index(skip1, *fmt); ++fmt);
    143  1.4      jtc 			fieldwidth = *fmt == '*' ? getint() : 0;
    144  1.4      jtc 
    145  1.4      jtc 			/* skip to possible '.', get following precision */
    146  1.4      jtc 			for (; index(skip2, *fmt); ++fmt);
    147  1.4      jtc 			if (*fmt == '.')
    148  1.4      jtc 				++fmt;
    149  1.4      jtc 			precision = *fmt == '*' ? getint() : 0;
    150  1.4      jtc 
    151  1.4      jtc 			for (; index(skip2, *fmt); ++fmt);
    152  1.1      cgd 			if (!*fmt) {
    153  1.4      jtc 			        warnx ("missing format character");
    154  1.4      jtc 				rval = 1;
    155  1.4      jtc 				return;
    156  1.4      jtc 			}
    157  1.4      jtc 
    158  1.4      jtc 			convch = *fmt;
    159  1.4      jtc 			nextch = *(fmt + 1);
    160  1.4      jtc 			*(fmt + 1) = '\0';
    161  1.4      jtc 			switch(convch) {
    162  1.4      jtc 			case 'c': {
    163  1.4      jtc 				char p = getchr();
    164  1.4      jtc 				PF(start, p);
    165  1.4      jtc 				break;
    166  1.4      jtc 			}
    167  1.4      jtc 			case 's': {
    168  1.4      jtc 				char *p = getstr();
    169  1.4      jtc 				PF(start, p);
    170  1.4      jtc 				break;
    171  1.4      jtc 			}
    172  1.4      jtc 			case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
    173  1.4      jtc 				char *f = mklong(start, convch);
    174  1.4      jtc 				long p = getlong();
    175  1.4      jtc 				PF(f, p);
    176  1.4      jtc 				free(f);
    177  1.4      jtc 				break;
    178  1.4      jtc 			}
    179  1.4      jtc 			case 'e': case 'E': case 'f': case 'g': case 'G': {
    180  1.4      jtc 				double p = getdouble();
    181  1.4      jtc 				PF(start, p);
    182  1.4      jtc 				break;
    183  1.1      cgd 			}
    184  1.4      jtc 			default:
    185  1.4      jtc 			        warnx ("%s: invalid directive", start);
    186  1.4      jtc 			        rval = 1;
    187  1.1      cgd 			}
    188  1.4      jtc 			*(fmt + 1) = nextch;
    189  1.4      jtc 			break;
    190  1.1      cgd 
    191  1.4      jtc 		case '\\':
    192  1.4      jtc 			fmt += print_escape(fmt);
    193  1.4      jtc 			break;
    194  1.1      cgd 
    195  1.4      jtc 		default:
    196  1.4      jtc 			putchar (*fmt);
    197  1.1      cgd 			break;
    198  1.1      cgd 		}
    199  1.4      jtc 	}
    200  1.4      jtc }
    201  1.4      jtc 
    202  1.4      jtc 
    203  1.4      jtc /*
    204  1.4      jtc  * Print SysV echo(1) style escape string
    205  1.4      jtc  */
    206  1.4      jtc void
    207  1.4      jtc print_escape_str(str)
    208  1.4      jtc 	register char *str;
    209  1.4      jtc {
    210  1.4      jtc 	int value;
    211  1.4      jtc 	int c;
    212  1.4      jtc 
    213  1.4      jtc 	while (*str) {
    214  1.4      jtc 		if (*str == '\\') {
    215  1.4      jtc 			str++;
    216  1.4      jtc 			/*
    217  1.4      jtc 			 * %b string octal constants are not like those in C.
    218  1.4      jtc 			 * They start with a \0, and are followed by 0, 1, 2,
    219  1.4      jtc 			 * or 3 octal digits.
    220  1.4      jtc 			 */
    221  1.4      jtc 			if (*str == '0') {
    222  1.4      jtc 				str++;
    223  1.4      jtc 				for (c = 3, value = 0; c-- && isodigit(*str); str++) {
    224  1.4      jtc 					value <<= 3;
    225  1.4      jtc 					value += octtobin(*str);
    226  1.4      jtc 				}
    227  1.4      jtc 				putchar (value);
    228  1.4      jtc 				str--;
    229  1.4      jtc 			} else if (*str == 'c') {
    230  1.4      jtc 				exit (rval);
    231  1.4      jtc 			} else {
    232  1.4      jtc 				str--;
    233  1.4      jtc 				str += print_escape(str);
    234  1.4      jtc 			}
    235  1.4      jtc 		} else {
    236  1.4      jtc 			putchar (*str);
    237  1.1      cgd 		}
    238  1.4      jtc 		str++;
    239  1.4      jtc 	}
    240  1.4      jtc }
    241  1.4      jtc 
    242  1.4      jtc /*
    243  1.4      jtc  * Print "standard" escape characters
    244  1.4      jtc  */
    245  1.4      jtc int
    246  1.4      jtc print_escape(str)
    247  1.4      jtc 	register char *str;
    248  1.4      jtc {
    249  1.4      jtc 	char *start = str;
    250  1.4      jtc 	int c;
    251  1.4      jtc 	int value;
    252  1.4      jtc 
    253  1.4      jtc 	str++;
    254  1.4      jtc 
    255  1.4      jtc 	switch (*str) {
    256  1.4      jtc 	case '0': case '1': case '2': case '3':
    257  1.4      jtc 	case '4': case '5': case '6': case '7':
    258  1.4      jtc 		for (c = 3, value = 0; c-- && isodigit(*str); str++) {
    259  1.4      jtc 			value <<= 3;
    260  1.4      jtc 			value += octtobin(*str);
    261  1.1      cgd 		}
    262  1.4      jtc 		putchar(value);
    263  1.4      jtc 		return str - start - 1;
    264  1.4      jtc 		/* NOTREACHED */
    265  1.4      jtc 
    266  1.4      jtc 	case 'x':
    267  1.4      jtc 		str++;
    268  1.4      jtc 		for (value = 0; isxdigit(*str); str++) {
    269  1.4      jtc 			value <<= 4;
    270  1.4      jtc 			value += hextobin(*str);
    271  1.1      cgd 		}
    272  1.4      jtc 		if (value > UCHAR_MAX) {
    273  1.4      jtc 			warnx ("escape sequence out of range for character");
    274  1.4      jtc 			rval = 1;
    275  1.1      cgd 		}
    276  1.4      jtc 		putchar (value);
    277  1.4      jtc 		return str - start - 1;
    278  1.4      jtc 		/* NOTREACHED */
    279  1.4      jtc 
    280  1.4      jtc 	case '\\':			/* backslash */
    281  1.4      jtc 		putchar('\\');
    282  1.4      jtc 		break;
    283  1.4      jtc 
    284  1.4      jtc 	case '\'':			/* single quote */
    285  1.4      jtc 		putchar('\'');
    286  1.4      jtc 		break;
    287  1.4      jtc 
    288  1.4      jtc 	case '"':			/* double quote */
    289  1.4      jtc 		putchar('"');
    290  1.4      jtc 		break;
    291  1.4      jtc 
    292  1.4      jtc 	case 'a':			/* alert */
    293  1.4      jtc #ifdef __STDC__
    294  1.4      jtc 		putchar('\a');
    295  1.4      jtc #else
    296  1.4      jtc 		putchar(007);
    297  1.4      jtc #endif
    298  1.4      jtc 		break;
    299  1.4      jtc 
    300  1.4      jtc 	case 'b':			/* backspace */
    301  1.4      jtc 		putchar('\b');
    302  1.4      jtc 		break;
    303  1.4      jtc 
    304  1.4      jtc 	case 'e':			/* escape */
    305  1.4      jtc #ifdef __GNUC__
    306  1.4      jtc 		putchar('\e');
    307  1.4      jtc #else
    308  1.4      jtc 		putchar(033);
    309  1.4      jtc #endif
    310  1.4      jtc 		break;
    311  1.4      jtc 
    312  1.4      jtc 	case 'f':			/* form-feed */
    313  1.4      jtc 		putchar('\f');
    314  1.4      jtc 		break;
    315  1.4      jtc 
    316  1.4      jtc 	case 'n':			/* newline */
    317  1.4      jtc 		putchar('\n');
    318  1.4      jtc 		break;
    319  1.4      jtc 
    320  1.4      jtc 	case 'r':			/* carriage-return */
    321  1.4      jtc 		putchar('\r');
    322  1.4      jtc 		break;
    323  1.4      jtc 
    324  1.4      jtc 	case 't':			/* tab */
    325  1.4      jtc 		putchar('\t');
    326  1.4      jtc 		break;
    327  1.4      jtc 
    328  1.4      jtc 	case 'v':			/* vertical-tab */
    329  1.4      jtc 		putchar('\v');
    330  1.4      jtc 		break;
    331  1.4      jtc 
    332  1.4      jtc 	default:
    333  1.4      jtc 		putchar(*str);
    334  1.4      jtc 		warnx("unknown escape sequence `\\%c'", *str);
    335  1.4      jtc 		rval = 1;
    336  1.1      cgd 	}
    337  1.4      jtc 
    338  1.4      jtc 	return 1;
    339  1.1      cgd }
    340  1.1      cgd 
    341  1.4      jtc 
    342  1.1      cgd char *
    343  1.1      cgd mklong(str, ch)
    344  1.1      cgd 	char *str, ch;
    345  1.1      cgd {
    346  1.1      cgd 	int len;
    347  1.4      jtc 	char *copy;
    348  1.1      cgd 
    349  1.1      cgd 	len = strlen(str) + 2;
    350  1.4      jtc 	if (!(copy = malloc((unsigned int)len))) {
    351  1.4      jtc 	        err(1, NULL);
    352  1.4      jtc 		/* NOTREACHED */
    353  1.1      cgd 	}
    354  1.1      cgd 	bcopy(str, copy, len - 3);
    355  1.1      cgd 	copy[len - 3] = 'l';
    356  1.1      cgd 	copy[len - 2] = ch;
    357  1.1      cgd 	copy[len - 1] = '\0';
    358  1.1      cgd 	return(copy);
    359  1.1      cgd }
    360  1.1      cgd 
    361  1.4      jtc int
    362  1.1      cgd getchr()
    363  1.1      cgd {
    364  1.1      cgd 	if (!*gargv)
    365  1.1      cgd 		return((int)'\0');
    366  1.1      cgd 	return((int)**gargv++);
    367  1.1      cgd }
    368  1.1      cgd 
    369  1.1      cgd char *
    370  1.1      cgd getstr()
    371  1.1      cgd {
    372  1.1      cgd 	if (!*gargv)
    373  1.1      cgd 		return("");
    374  1.1      cgd 	return(*gargv++);
    375  1.1      cgd }
    376  1.1      cgd 
    377  1.1      cgd static char *number = "+-.0123456789";
    378  1.4      jtc int
    379  1.1      cgd getint()
    380  1.1      cgd {
    381  1.1      cgd 	if (!*gargv)
    382  1.1      cgd 		return(0);
    383  1.4      jtc 
    384  1.1      cgd 	if (index(number, **gargv))
    385  1.1      cgd 		return(atoi(*gargv++));
    386  1.4      jtc 
    387  1.4      jtc 	return 0;
    388  1.1      cgd }
    389  1.1      cgd 
    390  1.1      cgd long
    391  1.1      cgd getlong()
    392  1.1      cgd {
    393  1.4      jtc 	long val;
    394  1.4      jtc 	char *ep;
    395  1.1      cgd 
    396  1.1      cgd 	if (!*gargv)
    397  1.4      jtc 		return(0L);
    398  1.4      jtc 
    399  1.4      jtc 	if (**gargv == '\"' || **gargv == '\'') {
    400  1.4      jtc 		val = gargv[0][1];
    401  1.4      jtc 		gargv++;
    402  1.4      jtc 		return val;
    403  1.4      jtc 	}
    404  1.4      jtc 
    405  1.4      jtc 	val = strtol (*gargv, &ep, 0);
    406  1.4      jtc 	if (*ep) {
    407  1.4      jtc 		warnx ("incompletely converted argument: %s", *gargv);
    408  1.4      jtc 		rval = 1;
    409  1.4      jtc 	}
    410  1.4      jtc 
    411  1.4      jtc 	gargv++;
    412  1.4      jtc 	return val;
    413  1.1      cgd }
    414  1.1      cgd 
    415  1.1      cgd double
    416  1.1      cgd getdouble()
    417  1.1      cgd {
    418  1.4      jtc 	double val;
    419  1.4      jtc 	char *ep;
    420  1.1      cgd 
    421  1.1      cgd 	if (!*gargv)
    422  1.4      jtc 		return(0.0);
    423  1.4      jtc 
    424  1.4      jtc 	if (**gargv == '\"' || **gargv == '\'') {
    425  1.4      jtc 		val = gargv[0][1];
    426  1.4      jtc 		gargv++;
    427  1.4      jtc 		return val;
    428  1.4      jtc 	}
    429  1.1      cgd 
    430  1.4      jtc 	val = strtod (*gargv, &ep);
    431  1.4      jtc 	if (*ep) {
    432  1.4      jtc 		warnx ("incompletely converted argument: %s", *gargv);
    433  1.4      jtc 		rval = 1;
    434  1.4      jtc 	}
    435  1.1      cgd 
    436  1.4      jtc 	gargv++;
    437  1.4      jtc 	return val;
    438  1.1      cgd }
    439