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>
39
#if !defined(BUILTIN) && !defined(SHELL)
40
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
41
The Regents of the University of California. All rights reserved.\n");
47
static char sccsid[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95";
49
__RCSID("$NetBSD: printf.c,v 1.28 2003/06/25 12:56:59 dsl Exp $");
53
#include <sys/types.h>
72
#include "compat_linux.h"
81
static void conv_escape_str(char *, void (*)(int));
82
static char *conv_escape(char *, char *);
83
static char *conv_expand(const char *);
84
static int getchr(void);
86
static double getdouble(void);
88
static int getwidth(void);
89
static intmax_t getintmax(void);
90
static uintmax_t getuintmax(void);
91
static char *getstr(void);
92
static char *mklong(const char *, int);
93
static void check_conversion(const char *, const char *);
94
static void usage(void);
96
static void b_count(int);
97
static void b_output(int);
104
#ifdef BUILTIN /* csh builtin */
105
#define main progprintf
108
#ifdef SHELL /* sh (aka ash) builtin */
109
#define main printfcmd
110
#include "bltin/bltin.h"
113
#define PF(f, func) { \
114
if (fieldwidth != -1) { \
115
if (precision != -1) \
116
(void)printf(f, fieldwidth, precision, func); \
118
(void)printf(f, fieldwidth, func); \
119
} else if (precision != -1) \
120
(void)printf(f, precision, func); \
122
(void)printf(f, func); \
125
#define APF(cpp, f, func) { \
126
if (fieldwidth != -1) { \
127
if (precision != -1) \
128
(void)asprintf(cpp, f, fieldwidth, precision, func); \
130
(void)asprintf(cpp, f, fieldwidth, func); \
131
} else if (precision != -1) \
132
(void)asprintf(cpp, f, precision, func); \
134
(void)asprintf(cpp, f, func); \
137
int main(int, char **);
138
int main(int argc, char *argv[])
141
int fieldwidth, precision;
146
#if !defined(SHELL) && !defined(BUILTIN)
147
(void)setlocale (LC_ALL, "");
150
while ((ch = getopt(argc, argv, "")) != -1) {
169
#define SKIP1 "#-+ 0"
170
#define SKIP2 "*0123456789"
173
* Basic algorithm is to scan the format string for conversion
174
* specifications -- once one is found, find out if the field
175
* width or precision is a '*'; if it is, gather up value.
176
* Note, format strings are reused as necessary to use up the
177
* provided arguments, arguments of zero/null string are
178
* provided to use up the format string.
181
/* find next format specification */
182
for (fmt = format; (ch = *fmt++) ;) {
185
fmt = conv_escape(fmt, &c_ch);
189
if (ch != '%' || (*fmt == '%' && ++fmt)) {
194
/* Ok - we've found a format specification,
195
Save its address for a later printf(). */
198
/* skip to field width */
199
fmt += strspn(fmt, SKIP1);
200
fieldwidth = *fmt == '*' ? getwidth() : -1;
202
/* skip to possible '.', get following precision */
203
fmt += strspn(fmt, SKIP2);
206
precision = *fmt == '*' ? getwidth() : -1;
208
fmt += strspn(fmt, SKIP2);
212
warnx("missing format character");
215
/* null terminate format string to we can use it
216
as an argument to printf. */
222
const char *p = conv_expand(getstr());
228
/* There has to be a better way to do this,
229
* but the string we generate might have
233
/* Free on entry in case shell longjumped out */
240
/* Count number of bytes we want to output */
242
conv_escape_str(cp, b_count);
243
t = malloc(b_length + 1);
246
memset(t, 'x', b_length);
248
/* Get printf to calculate the lengths */
252
/* Output leading spaces and data bytes */
253
conv_escape_str(cp, b_output);
254
/* Add any trailing spaces */
270
intmax_t p = getintmax();
271
char *f = mklong(start, ch);
279
uintmax_t p = getuintmax();
280
char *f = mklong(start, ch);
290
double p = getdouble();
296
warnx("%s: invalid directive", start);
301
/* escape if a \c was encountered */
303
return (rval & ~0x100);
305
} while (gargv != argv && *gargv);
310
/* helper functions for conv_escape_str */
318
/* Output one converted character for every 'x' in the 'format' */
340
* Print SysV echo(1) style escape string
341
* Halts processing string if a \c escape is encountered.
344
conv_escape_str(char *str, void (*do_putchar)(int))
350
while ((ch = *str++)) {
358
/* \c as in SYSV echo - abort all processing.... */
364
* %b string octal constants are not like those in C.
365
* They start with a \0, and are followed by 0, 1, 2,
369
char octnum[4], *oct_end;
374
do_putchar(strtoul(octnum, &oct_end, 8));
375
str += oct_end - octnum;
379
/* \[M][^|-]C as defined by vis(3) */
380
if (ch == 'M' && *str == '-') {
381
do_putchar(0200 | str[1]);
385
if (ch == 'M' && *str == '^') {
401
/* Finally test for sequences valid in the format string */
402
str = conv_escape(str - 1, &c);
408
* Print "standard" escape characters
411
conv_escape(char *str, char *conv_ch)
415
char num_buf[4], *num_end;
420
case '0': case '1': case '2': case '3':
421
case '4': case '5': case '6': case '7':
425
num_buf[2] = ch ? str[1] : 0;
427
value = strtoul(num_buf, &num_end, 8);
428
str += num_end - (num_buf + 1);
432
/* Hexadecimal character constants are not required to be
433
supported (by SuS v1) because there is no consistent
434
way to detect the end of the constant.
435
Supporting 2 byte constants is a compromise. */
438
num_buf[1] = ch ? str[1] : 0;
440
value = strtoul(num_buf, &num_end, 16);
441
str += num_end - num_buf;
444
case '\\': value = '\\'; break; /* backslash */
445
case '\'': value = '\''; break; /* single quote */
446
case '"': value = '"'; break; /* double quote */
447
case 'a': value = '\a'; break; /* alert */
448
case 'b': value = '\b'; break; /* backspace */
449
case 'e': value = ESCAPE; break; /* escape */
450
case 'f': value = '\f'; break; /* form-feed */
451
case 'n': value = '\n'; break; /* newline */
452
case 'r': value = '\r'; break; /* carriage-return */
453
case 't': value = '\t'; break; /* tab */
454
case 'v': value = '\v'; break; /* vertical-tab */
457
warnx("unknown escape sequence `\\%c'", ch);
467
/* expand a string so that everything is printable */
470
conv_expand(const char *str)
472
static char *conv_str;
478
/* get a buffer that is definitely large enough.... */
479
conv_str = malloc(4 * strlen(str) + 1);
481
return "<no memory>";
484
while ((ch = *(unsigned char *)str++)) {
486
/* Use C escapes for expected control characters */
487
case '\\': ch = '\\'; break; /* backslash */
488
case '\'': ch = '\''; break; /* single quote */
489
case '"': ch = '"'; break; /* double quote */
490
case '\a': ch = 'a'; break; /* alert */
491
case '\b': ch = 'b'; break; /* backspace */
492
case ESCAPE: ch = 'e'; break; /* escape */
493
case '\f': ch = 'f'; break; /* form-feed */
494
case '\n': ch = 'n'; break; /* newline */
495
case '\r': ch = 'r'; break; /* carriage-return */
496
case '\t': ch = 't'; break; /* tab */
497
case '\v': ch = 'v'; break; /* vertical-tab */
499
/* Copy anything printable */
504
/* Use vis(3) encodings for the rest */
533
mklong(const char *str, int ch)
535
static char copy[64];
538
len = strlen(str) + 2;
539
if (len > sizeof copy) {
540
warnx("format %s too complex\n", str);
543
(void)memmove(copy, str, len - 3);
546
copy[len - 1] = '\0';
555
return ((int)**gargv++);
578
val = strtoul(s, &ep, 0);
579
check_conversion(s, ep);
581
/* Arbitrarily 'restrict' field widths to 1Mbyte */
582
if (val < 0 || val > 1 << 20) {
583
warnx("%s: invalid field width", s);
601
if (*cp == '\"' || *cp == '\'')
605
val = strtoimax(cp, &ep, 0);
606
check_conversion(cp, ep);
621
if (*cp == '\"' || *cp == '\'')
624
/* strtoumax won't error -ve values */
625
while (isspace(*(unsigned char *)cp))
628
warnx("%s: expected positive numeric value", cp);
634
val = strtoumax(cp, &ep, 0);
635
check_conversion(cp, ep);
649
if (**gargv == '\"' || **gargv == '\'')
650
return (double) *((*gargv++)+1);
653
val = strtod(*gargv, &ep);
654
check_conversion(*gargv++, ep);
660
check_conversion(const char *s, const char *ep)
664
warnx("%s: expected numeric value", s);
666
warnx("%s: not completely converted", s);
668
} else if (errno == ERANGE) {
669
warnx("%s: %s", s, strerror(ERANGE));
677
(void)fprintf(stderr, "usage: printf format [arg ...]\n");