aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2004-09-15 02:05:23 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2004-09-15 02:05:23 +0000
commit240a91d8a17ba8168775de85dd91eebef8c243a6 (patch)
treee731e0a6e3738a5deceb04cfcfc9efee1d9cafe1
parentb6570d07c4ae956a1ab5c11a6c38a4e7ef611b6d (diff)
downloadbusybox-w32-240a91d8a17ba8168775de85dd91eebef8c243a6.tar.gz
busybox-w32-240a91d8a17ba8168775de85dd91eebef8c243a6.tar.bz2
busybox-w32-240a91d8a17ba8168775de85dd91eebef8c243a6.zip
Patch by Felipe Kellermann, use the common escape handling function and remove some unused code.
-rw-r--r--coreutils/printf.c154
1 files changed, 21 insertions, 133 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c
index 053b01ee5..da5e46a58 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -58,68 +58,19 @@
58#include <assert.h> 58#include <assert.h>
59#include "busybox.h" 59#include "busybox.h"
60 60
61
62#ifndef S_IFMT
63static const int S_IFMT = 0170000;
64#endif
65#if !defined(S_ISBLK) && defined(S_IFBLK)
66# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
67#endif
68#if !defined(S_ISCHR) && defined(S_IFCHR)
69# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
70#endif
71#if !defined(S_ISDIR) && defined(S_IFDIR)
72# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
73#endif
74#if !defined(S_ISREG) && defined(S_IFREG)
75# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
76#endif
77#if !defined(S_ISFIFO) && defined(S_IFIFO)
78# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
79#endif
80#if !defined(S_ISLNK) && defined(S_IFLNK)
81# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
82#endif
83#if !defined(S_ISSOCK) && defined(S_IFSOCK)
84# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
85#endif
86#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
87# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
88# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
89#endif
90#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
91# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
92#endif
93
94#define IN_CTYPE_DOMAIN(c) 1
95
96#define ISXDIGIT(c) (IN_CTYPE_DOMAIN (c) && isxdigit (c))
97#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
98#define ISDIGIT(c) (((unsigned char) (c)) - '0' <= 9)
99
100#define isodigit(c) ((c) >= '0' && (c) <= '7')
101#define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
102#define octtobin(c) ((c) - '0')
103
104static double xstrtod __P((char *s)); 61static double xstrtod __P((char *s));
105static int print_esc __P((char *escstart));
106static int print_formatted __P((char *format, int argc, char **argv));
107static long xstrtol __P((char *s)); 62static long xstrtol __P((char *s));
108static unsigned long xstrtoul __P((char *s)); 63static unsigned long xstrtoul __P((char *s));
64static void print_esc_string __P((char *str));
65static int print_formatted __P((char *format, int argc, char **argv));
109static void print_direc __P( (char *start, size_t length, 66static void print_direc __P( (char *start, size_t length,
110 int field_width, int precision, char *argument)); 67 int field_width, int precision, char *argument));
111static void print_esc_char __P((int c));
112static void print_esc_string __P((char *str));
113
114/* The value to return to the calling program. */
115static int exit_status;
116 68
117int printf_main(int argc, char **argv) 69int printf_main(int argc, char **argv)
118{ 70{
119 char *format; 71 char *format;
120 int args_used; 72 int args_used;
121 73
122 exit_status = 0;
123 if (argc <= 1 || **(argv + 1) == '-') { 74 if (argc <= 1 || **(argv + 1) == '-') {
124 bb_show_usage(); 75 bb_show_usage();
125 } 76 }
@@ -140,7 +91,7 @@ int printf_main(int argc, char **argv)
140 fprintf(stderr, "excess args ignored"); 91 fprintf(stderr, "excess args ignored");
141*/ 92*/
142 93
143 return(exit_status); 94 return EXIT_SUCCESS;
144} 95}
145 96
146/* Print the text in FORMAT, using ARGV (with ARGC elements) for 97/* Print the text in FORMAT, using ARGV (with ARGC elements) for
@@ -188,7 +139,7 @@ static int print_formatted(char *format, int argc, char **argv)
188 } else 139 } else
189 field_width = 0; 140 field_width = 0;
190 } else 141 } else
191 while (ISDIGIT(*f)) { 142 while (isdigit(*f)) {
192 ++f; 143 ++f;
193 ++direc_length; 144 ++direc_length;
194 } 145 }
@@ -205,7 +156,7 @@ static int print_formatted(char *format, int argc, char **argv)
205 } else 156 } else
206 precision = 0; 157 precision = 0;
207 } else 158 } else
208 while (ISDIGIT(*f)) { 159 while (isdigit(*f)) {
209 ++f; 160 ++f;
210 ++direc_length; 161 ++direc_length;
211 } 162 }
@@ -230,7 +181,10 @@ static int print_formatted(char *format, int argc, char **argv)
230 break; 181 break;
231 182
232 case '\\': 183 case '\\':
233 f += print_esc(f); 184 if (*++f == 'c')
185 exit(0);
186 putchar(bb_process_escape_sequence((const char **)&f));
187 f--;
234 break; 188 break;
235 189
236 default: 190 default:
@@ -241,84 +195,6 @@ static int print_formatted(char *format, int argc, char **argv)
241 return save_argc - argc; 195 return save_argc - argc;
242} 196}
243 197
244/* Print a \ escape sequence starting at ESCSTART.
245 Return the number of characters in the escape sequence
246 besides the backslash. */
247
248static int print_esc(char *escstart)
249{
250 register char *p = escstart + 1;
251 int esc_value = 0; /* Value of \nnn escape. */
252 int esc_length; /* Length of \nnn escape. */
253
254 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
255 if (*p == 'x') {
256 for (esc_length = 0, ++p;
257 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
258 esc_value = esc_value * 16 + hextobin(*p);
259/* if (esc_length == 0)
260 fprintf(stderr, "missing hex in esc");
261*/
262 putchar(esc_value);
263 } else if (*p == '0') {
264 for (esc_length = 0, ++p;
265 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
266 esc_value = esc_value * 8 + octtobin(*p);
267 putchar(esc_value);
268 } else if (strchr("\"\\abcfnrtv", *p))
269 print_esc_char(*p++);
270/* else
271 fprintf(stderr, "\\%c: invalid esc", *p);
272*/
273 return p - escstart - 1;
274}
275
276/* Output a single-character \ escape. */
277
278static void print_esc_char(int c)
279{
280 switch (c) {
281 case 'a': /* Alert. */
282 putchar(7);
283 break;
284 case 'b': /* Backspace. */
285 putchar(8);
286 break;
287 case 'c': /* Cancel the rest of the output. */
288 exit(0);
289 break;
290 case 'f': /* Form feed. */
291 putchar(12);
292 break;
293 case 'n': /* New line. */
294 putchar(10);
295 break;
296 case 'r': /* Carriage return. */
297 putchar(13);
298 break;
299 case 't': /* Horizontal tab. */
300 putchar(9);
301 break;
302 case 'v': /* Vertical tab. */
303 putchar(11);
304 break;
305 default:
306 putchar(c);
307 break;
308 }
309}
310
311/* Print string STR, evaluating \ escapes. */
312
313static void print_esc_string(char *str)
314{
315 for (; *str; str++)
316 if (*str == '\\')
317 str += print_esc(str);
318 else
319 putchar(*str);
320}
321
322static void 198static void
323print_direc(char *start, size_t length, int field_width, int precision, 199print_direc(char *start, size_t length, int field_width, int precision,
324 char *argument) 200 char *argument)
@@ -426,3 +302,15 @@ static double xstrtod(char *arg)
426 return result; 302 return result;
427} 303}
428 304
305static void print_esc_string(char *str)
306{
307 for (; *str; str++) {
308 if (*str == '\\') {
309 str++;
310 putchar(bb_process_escape_sequence((const char **)&str));
311 } else {
312 putchar(*str);
313 }
314
315 }
316}