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