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