1
/* $NetBSD: printf.c,v 1.28 2003/06/25 12:56:59 dsl Exp $ */
4
* Copyright (c) 1989, 1993
5
* The Regents of the University of California. All rights reserved.
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
* 3. Neither the name of the University nor the names of its contributors
16
* may be used to endorse or promote products derived from this software
17
* without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
#include <sys/cdefs.h>
36
#define __COPYRIGHT(arg)
42
#if !defined(BUILTIN) && !defined(SHELL)
43
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
44
The Regents of the University of California. All rights reserved.\n");
50
static char sccsid[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95";
52
__RCSID("$NetBSD: printf.c,v 1.28 2003/06/25 12:56:59 dsl Exp $");
56
#include <sys/types.h>
75
//#include "compat_linux.h"
84
static void conv_escape_str(char *, void (*)(int));
85
static char *conv_escape(char *, char *);
86
static char *conv_expand(const char *);
87
static int getchr(void);
89
static double getdouble(void);
91
static int getwidth(void);
92
static intmax_t getintmax(void);
93
static uintmax_t getuintmax(void);
94
static char *getstr(void);
95
static char *mklong(const char *, int);
96
static void check_conversion(const char *, const char *);
97
static void usage(void);
99
static void b_count(int);
100
static void b_output(int);
107
#ifdef BUILTIN /* csh builtin */
108
#define main progprintf
111
#ifdef SHELL /* sh (aka ash) builtin */
112
#define main printfcmd
113
#include "bltin/bltin.h"
116
#define PF(f, func) { \
117
if (fieldwidth != -1) { \
118
if (precision != -1) \
119
(void)printf(f, fieldwidth, precision, func); \
121
(void)printf(f, fieldwidth, func); \
122
} else if (precision != -1) \
123
(void)printf(f, precision, func); \
125
(void)printf(f, func); \
128
#define APF(cpp, f, func) { \
129
if (fieldwidth != -1) { \
130
if (precision != -1) \
131
(void)asprintf(cpp, f, fieldwidth, precision, func); \
133
(void)asprintf(cpp, f, fieldwidth, func); \
134
} else if (precision != -1) \
135
(void)asprintf(cpp, f, precision, func); \
137
(void)asprintf(cpp, f, func); \
140
int main(int, char **);
141
int main(int argc, char *argv[])
144
int fieldwidth, precision;
149
while ((ch = getopt(argc, argv, "")) != -1) {
168
#define SKIP1 "#-+ 0"
169
#define SKIP2 "*0123456789"
172
* Basic algorithm is to scan the format string for conversion
173
* specifications -- once one is found, find out if the field
174
* width or precision is a '*'; if it is, gather up value.
175
* Note, format strings are reused as necessary to use up the
176
* provided arguments, arguments of zero/null string are
177
* provided to use up the format string.
180
/* find next format specification */
181
for (fmt = format; (ch = *fmt++) ;) {
184
fmt = conv_escape(fmt, &c_ch);
188
if (ch != '%' || (*fmt == '%' && ++fmt)) {
193
/* Ok - we've found a format specification,
194
Save its address for a later printf(). */
197
/* skip to field width */
198
fmt += strspn(fmt, SKIP1);
199
fieldwidth = *fmt == '*' ? getwidth() : -1;
201
/* skip to possible '.', get following precision */
202
fmt += strspn(fmt, SKIP2);
205
precision = *fmt == '*' ? getwidth() : -1;
207
fmt += strspn(fmt, SKIP2);
211
fprintf(stderr, "missing format character");
214
/* null terminate format string to we can use it
215
as an argument to printf. */
221
const char *p = conv_expand(getstr());
227
/* There has to be a better way to do this,
228
* but the string we generate might have
232
/* Free on entry in case shell longjumped out */
239
/* Count number of bytes we want to output */
241
conv_escape_str(cp, b_count);
242
t = malloc(b_length + 1);
245
memset(t, 'x', b_length);
247
/* Get printf to calculate the lengths */
251
/* Output leading spaces and data bytes */
252
conv_escape_str(cp, b_output);
253
/* Add any trailing spaces */
269
intmax_t p = getintmax();
270
char *f = mklong(start, ch);
278
uintmax_t p = getuintmax();
279
char *f = mklong(start, ch);
289
double p = getdouble();
295
fprintf(stderr, "%s: invalid directive", start);
300
/* escape if a \c was encountered */
302
return (rval & ~0x100);
304
} while (gargv != argv && *gargv);
309
/* helper functions for conv_escape_str */
317
/* Output one converted character for every 'x' in the 'format' */
339
* Print SysV echo(1) style escape string
340
* Halts processing string if a \c escape is encountered.
343
conv_escape_str(char *str, void (*do_putchar)(int))
349
while ((ch = *str++)) {
357
/* \c as in SYSV echo - abort all processing.... */
363
* %b string octal constants are not like those in C.
364
* They start with a \0, and are followed by 0, 1, 2,
368
char octnum[4], *oct_end;
373
do_putchar(strtoul(octnum, &oct_end, 8));
374
str += oct_end - octnum;
378
/* \[M][^|-]C as defined by vis(3) */
379
if (ch == 'M' && *str == '-') {
380
do_putchar(0200 | str[1]);
384
if (ch == 'M' && *str == '^') {
400
/* Finally test for sequences valid in the format string */
401
str = conv_escape(str - 1, &c);
407
* Print "standard" escape characters
410
conv_escape(char *str, char *conv_ch)
414
char num_buf[4], *num_end;
419
case '0': case '1': case '2': case '3':
420
case '4': case '5': case '6': case '7':
424
num_buf[2] = ch ? str[1] : 0;
426
value = strtoul(num_buf, &num_end, 8);
427
str += num_end - (num_buf + 1);
431
/* Hexadecimal character constants are not required to be
432
supported (by SuS v1) because there is no consistent
433
way to detect the end of the constant.
434
Supporting 2 byte constants is a compromise. */
437
num_buf[1] = ch ? str[1] : 0;
439
value = strtoul(num_buf, &num_end, 16);
440
str += num_end - num_buf;
443
case '\\': value = '\\'; break; /* backslash */
444
case '\'': value = '\''; break; /* single quote */
445
case '"': value = '"'; break; /* double quote */
446
case 'a': value = '\a'; break; /* alert */
447
case 'b': value = '\b'; break; /* backspace */
448
case 'e': value = ESCAPE; break; /* escape */
449
case 'f': value = '\f'; break; /* form-feed */
450
case 'n': value = '\n'; break; /* newline */
451
case 'r': value = '\r'; break; /* carriage-return */
452
case 't': value = '\t'; break; /* tab */
453
case 'v': value = '\v'; break; /* vertical-tab */
456
fprintf(stderr, "unknown escape sequence `\\%c'", ch);
466
/* expand a string so that everything is printable */
469
conv_expand(const char *str)
471
static char *conv_str;
477
/* get a buffer that is definitely large enough.... */
478
conv_str = malloc(4 * strlen(str) + 1);
480
return "<no memory>";
483
while ((ch = *(unsigned char *)str++)) {
485
/* Use C escapes for expected control characters */
486
case '\\': ch = '\\'; break; /* backslash */
487
case '\'': ch = '\''; break; /* single quote */
488
case '"': ch = '"'; break; /* double quote */
489
case '\a': ch = 'a'; break; /* alert */
490
case '\b': ch = 'b'; break; /* backspace */
491
case ESCAPE: ch = 'e'; break; /* escape */
492
case '\f': ch = 'f'; break; /* form-feed */
493
case '\n': ch = 'n'; break; /* newline */
494
case '\r': ch = 'r'; break; /* carriage-return */
495
case '\t': ch = 't'; break; /* tab */
496
case '\v': ch = 'v'; break; /* vertical-tab */
498
/* Copy anything printable */
503
/* Use vis(3) encodings for the rest */
532
mklong(const char *str, int ch)
534
static char copy[64];
537
len = strlen(str) + 2;
538
if (len > sizeof copy) {
539
fprintf(stderr, "format %s too complex\n", str);
542
(void)memmove(copy, str, len - 3);
545
copy[len - 1] = '\0';
554
return ((int)**gargv++);
577
val = strtoul(s, &ep, 0);
578
check_conversion(s, ep);
580
/* Arbitrarily 'restrict' field widths to 1Mbyte */
581
if (val < 0 || val > 1 << 20) {
582
fprintf(stderr, "%s: invalid field width", s);
600
if (*cp == '\"' || *cp == '\'')
604
val = strtoimax(cp, &ep, 0);
605
check_conversion(cp, ep);
620
if (*cp == '\"' || *cp == '\'')
623
/* strtoumax won't error -ve values */
624
while (isspace(*(unsigned char *)cp))
627
fprintf(stderr, "%s: expected positive numeric value", cp);
633
val = strtoumax(cp, &ep, 0);
634
check_conversion(cp, ep);
648
if (**gargv == '\"' || **gargv == '\'')
649
return (double) *((*gargv++)+1);
652
val = strtod(*gargv, &ep);
653
check_conversion(*gargv++, ep);
659
check_conversion(const char *s, const char *ep)
663
fprintf(stderr, "%s: expected numeric value", s);
665
fprintf(stderr, "%s: not completely converted", s);
667
} else if (errno == ERANGE) {
668
fprintf(stderr, "%s: %s", s, strerror(ERANGE));
676
(void)fprintf(stderr, "usage: printf format [arg ...]\n");