aboutsummaryrefslogtreecommitdiff
path: root/coreutils/printf.c
diff options
context:
space:
mode:
authorerik <erik@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-02-08 19:58:47 +0000
committererik <erik@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-02-08 19:58:47 +0000
commita18125005d67c38a8ad7fb454571bb996664ad96 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /coreutils/printf.c
parent8ef3b44285a8ce5b83bed9cf32ce5f40b30ba72f (diff)
downloadbusybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.tar.gz
busybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.tar.bz2
busybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.zip
Some formatting updates (ran the code through indent)
-Erik git-svn-id: svn://busybox.net/trunk/busybox@357 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils/printf.c')
-rw-r--r--coreutils/printf.c632
1 files changed, 292 insertions, 340 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c
index 5fd5ea303..41ab2e442 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* printf - format and print data 2/* printf - format and print data
2 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc. 3 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 4
@@ -42,7 +43,7 @@
42 to convert all of the given arguments. 43 to convert all of the given arguments.
43 44
44 David MacKenzie <djm@gnu.ai.mit.edu> */ 45 David MacKenzie <djm@gnu.ai.mit.edu> */
45 46
46 47
47// 19990508 Busy Boxed! Dave Cinege 48// 19990508 Busy Boxed! Dave Cinege
48 49
@@ -84,11 +85,11 @@
84#if !defined(S_ISSOCK) && defined(S_IFSOCK) 85#if !defined(S_ISSOCK) && defined(S_IFSOCK)
85# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) 86# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
86#endif 87#endif
87#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */ 88#if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
88# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB) 89# define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
89# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC) 90# define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
90#endif 91#endif
91#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */ 92#if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
92# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK) 93# define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
93#endif 94#endif
94 95
@@ -121,407 +122,358 @@
121#define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0') 122#define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0')
122#define octtobin(c) ((c) - '0') 123#define octtobin(c) ((c) - '0')
123 124
124static double xstrtod __P ((char *s)); 125static double xstrtod __P((char *s));
125static int print_esc __P ((char *escstart)); 126static int print_esc __P((char *escstart));
126static int print_formatted __P ((char *format, int argc, char **argv)); 127static int print_formatted __P((char *format, int argc, char **argv));
127static long xstrtol __P ((char *s)); 128static long xstrtol __P((char *s));
128static unsigned long xstrtoul __P ((char *s)); 129static unsigned long xstrtoul __P((char *s));
129static void print_direc __P ((char *start, size_t length, int field_width, int precision, char *argument)); 130static void print_direc
130static void print_esc_char __P ((int c)); 131__P(
131static void print_esc_string __P ((char *str)); 132
132static void verify __P ((char *s, char *end)); 133 (char *start, size_t length, int field_width, int precision,
134 char *argument));
135static void print_esc_char __P((int c));
136static void print_esc_string __P((char *str));
137static void verify __P((char *s, char *end));
133 138
134/* The value to return to the calling program. */ 139/* The value to return to the calling program. */
135static int exit_status; 140static int exit_status;
136 141
137static const char printf_usage[] = "printf format [argument...]\n"; 142static const char printf_usage[] = "printf format [argument...]\n";
138 143
139int 144int printf_main(int argc, char **argv)
140printf_main(int argc, char** argv)
141{ 145{
142 char *format; 146 char *format;
143 int args_used; 147 int args_used;
144 148
145 exit_status = 0; 149 exit_status = 0;
146 if ( argc <= 1 || **(argv+1) == '-' ) { 150 if (argc <= 1 || **(argv + 1) == '-') {
147 usage (printf_usage); 151 usage(printf_usage);
148 } 152 }
149 153
150 format = argv[1]; 154 format = argv[1];
151 argc -= 2; 155 argc -= 2;
152 argv += 2; 156 argv += 2;
153 157
154 do 158 do {
155 { 159 args_used = print_formatted(format, argc, argv);
156 args_used = print_formatted (format, argc, argv); 160 argc -= args_used;
157 argc -= args_used; 161 argv += args_used;
158 argv += args_used; 162 }
159 } 163 while (args_used > 0 && argc > 0);
160 while (args_used > 0 && argc > 0);
161 164
162/* 165/*
163 if (argc > 0) 166 if (argc > 0)
164 fprintf(stderr, "excess args ignored"); 167 fprintf(stderr, "excess args ignored");
165*/ 168*/
166 169
167 exit (exit_status); 170 exit(exit_status);
168} 171}
169 172
170/* Print the text in FORMAT, using ARGV (with ARGC elements) for 173/* Print the text in FORMAT, using ARGV (with ARGC elements) for
171 arguments to any `%' directives. 174 arguments to any `%' directives.
172 Return the number of elements of ARGV used. */ 175 Return the number of elements of ARGV used. */
173 176
174static int 177static int print_formatted(char *format, int argc, char **argv)
175print_formatted (char *format, int argc, char **argv)
176{ 178{
177 int save_argc = argc; /* Preserve original value. */ 179 int save_argc = argc; /* Preserve original value. */
178 char *f; /* Pointer into `format'. */ 180 char *f; /* Pointer into `format'. */
179 char *direc_start; /* Start of % directive. */ 181 char *direc_start; /* Start of % directive. */
180 size_t direc_length; /* Length of % directive. */ 182 size_t direc_length; /* Length of % directive. */
181 int field_width; /* Arg to first '*', or -1 if none. */ 183 int field_width; /* Arg to first '*', or -1 if none. */
182 int precision; /* Arg to second '*', or -1 if none. */ 184 int precision; /* Arg to second '*', or -1 if none. */
183 185
184 for (f = format; *f; ++f) 186 for (f = format; *f; ++f) {
185 { 187 switch (*f) {
186 switch (*f) 188 case '%':
187 { 189 direc_start = f++;
188 case '%': 190 direc_length = 1;
189 direc_start = f++; 191 field_width = precision = -1;
190 direc_length = 1; 192 if (*f == '%') {
191 field_width = precision = -1; 193 putchar('%');
192 if (*f == '%') 194 break;
193 { 195 }
194 putchar ('%'); 196 if (*f == 'b') {
195 break; 197 if (argc > 0) {
196 } 198 print_esc_string(*argv);
197 if (*f == 'b') 199 ++argv;
198 { 200 --argc;
199 if (argc > 0) 201 }
200 { 202 break;
201 print_esc_string (*argv); 203 }
202 ++argv; 204 if (strchr("-+ #", *f)) {
203 --argc; 205 ++f;
204 } 206 ++direc_length;
205 break; 207 }
206 } 208 if (*f == '*') {
207 if (strchr ("-+ #", *f)) 209 ++f;
208 { 210 ++direc_length;
209 ++f; 211 if (argc > 0) {
210 ++direc_length; 212 field_width = xstrtoul(*argv);
211 } 213 ++argv;
212 if (*f == '*') 214 --argc;
213 { 215 } else
214 ++f; 216 field_width = 0;
215 ++direc_length; 217 } else
216 if (argc > 0) 218 while (ISDIGIT(*f)) {
217 { 219 ++f;
218 field_width = xstrtoul (*argv); 220 ++direc_length;
219 ++argv; 221 }
220 --argc; 222 if (*f == '.') {
223 ++f;
224 ++direc_length;
225 if (*f == '*') {
226 ++f;
227 ++direc_length;
228 if (argc > 0) {
229 precision = xstrtoul(*argv);
230 ++argv;
231 --argc;
232 } else
233 precision = 0;
234 } else
235 while (ISDIGIT(*f)) {
236 ++f;
237 ++direc_length;
238 }
239 }
240 if (*f == 'l' || *f == 'L' || *f == 'h') {
241 ++f;
242 ++direc_length;
243 }
244 /*
245 if (!strchr ("diouxXfeEgGcs", *f))
246 fprintf(stderr, "%%%c: invalid directive", *f);
247 */
248 ++direc_length;
249 if (argc > 0) {
250 print_direc(direc_start, direc_length, field_width,
251 precision, *argv);
252 ++argv;
253 --argc;
254 } else
255 print_direc(direc_start, direc_length, field_width,
256 precision, "");
257 break;
258
259 case '\\':
260 f += print_esc(f);
261 break;
262
263 default:
264 putchar(*f);
221 } 265 }
222 else
223 field_width = 0;
224 }
225 else
226 while (ISDIGIT (*f))
227 {
228 ++f;
229 ++direc_length;
230 }
231 if (*f == '.')
232 {
233 ++f;
234 ++direc_length;
235 if (*f == '*')
236 {
237 ++f;
238 ++direc_length;
239 if (argc > 0)
240 {
241 precision = xstrtoul (*argv);
242 ++argv;
243 --argc;
244 }
245 else
246 precision = 0;
247 }
248 else
249 while (ISDIGIT (*f))
250 {
251 ++f;
252 ++direc_length;
253 }
254 }
255 if (*f == 'l' || *f == 'L' || *f == 'h')
256 {
257 ++f;
258 ++direc_length;
259 }
260 /*
261 if (!strchr ("diouxXfeEgGcs", *f))
262 fprintf(stderr, "%%%c: invalid directive", *f);
263 */
264 ++direc_length;
265 if (argc > 0)
266 {
267 print_direc (direc_start, direc_length, field_width,
268 precision, *argv);
269 ++argv;
270 --argc;
271 }
272 else
273 print_direc (direc_start, direc_length, field_width,
274 precision, "");
275 break;
276
277 case '\\':
278 f += print_esc (f);
279 break;
280
281 default:
282 putchar (*f);
283 } 266 }
284 }
285 267
286 return save_argc - argc; 268 return save_argc - argc;
287} 269}
288 270
289/* Print a \ escape sequence starting at ESCSTART. 271/* Print a \ escape sequence starting at ESCSTART.
290 Return the number of characters in the escape sequence 272 Return the number of characters in the escape sequence
291 besides the backslash. */ 273 besides the backslash. */
292 274
293static int 275static int print_esc(char *escstart)
294print_esc (char *escstart)
295{ 276{
296 register char *p = escstart + 1; 277 register char *p = escstart + 1;
297 int esc_value = 0; /* Value of \nnn escape. */ 278 int esc_value = 0; /* Value of \nnn escape. */
298 int esc_length; /* Length of \nnn escape. */ 279 int esc_length; /* Length of \nnn escape. */
299 280
300 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */ 281 /* \0ooo and \xhhh escapes have maximum length of 3 chars. */
301 if (*p == 'x') 282 if (*p == 'x') {
302 { 283 for (esc_length = 0, ++p;
303 for (esc_length = 0, ++p; 284 esc_length < 3 && ISXDIGIT(*p); ++esc_length, ++p)
304 esc_length < 3 && ISXDIGIT (*p); 285 esc_value = esc_value * 16 + hextobin(*p);
305 ++esc_length, ++p)
306 esc_value = esc_value * 16 + hextobin (*p);
307/* if (esc_length == 0) 286/* if (esc_length == 0)
308 fprintf(stderr, "missing hex in esc"); 287 fprintf(stderr, "missing hex in esc");
309*/ 288*/
310 putchar (esc_value); 289 putchar(esc_value);
311 } 290 } else if (*p == '0') {
312 else if (*p == '0') 291 for (esc_length = 0, ++p;
313 { 292 esc_length < 3 && isodigit(*p); ++esc_length, ++p)
314 for (esc_length = 0, ++p; 293 esc_value = esc_value * 8 + octtobin(*p);
315 esc_length < 3 && isodigit (*p); 294 putchar(esc_value);
316 ++esc_length, ++p) 295 } else if (strchr("\"\\abcfnrtv", *p))
317 esc_value = esc_value * 8 + octtobin (*p); 296 print_esc_char(*p++);
318 putchar (esc_value);
319 }
320 else if (strchr ("\"\\abcfnrtv", *p))
321 print_esc_char (*p++);
322/* else 297/* else
323 fprintf(stderr, "\\%c: invalid esc", *p); 298 fprintf(stderr, "\\%c: invalid esc", *p);
324*/ 299*/
325 return p - escstart - 1; 300 return p - escstart - 1;
326} 301}
327 302
328/* Output a single-character \ escape. */ 303/* Output a single-character \ escape. */
329 304
330static void 305static void print_esc_char(int c)
331print_esc_char (int c)
332{ 306{
333 switch (c) 307 switch (c) {
334 { 308 case 'a': /* Alert. */
335 case 'a': /* Alert. */ 309 putchar(7);
336 putchar (7); 310 break;
337 break; 311 case 'b': /* Backspace. */
338 case 'b': /* Backspace. */ 312 putchar(8);
339 putchar (8); 313 break;
340 break; 314 case 'c': /* Cancel the rest of the output. */
341 case 'c': /* Cancel the rest of the output. */ 315 exit(0);
342 exit (0); 316 break;
343 break; 317 case 'f': /* Form feed. */
344 case 'f': /* Form feed. */ 318 putchar(12);
345 putchar (12); 319 break;
346 break; 320 case 'n': /* New line. */
347 case 'n': /* New line. */ 321 putchar(10);
348 putchar (10); 322 break;
349 break; 323 case 'r': /* Carriage return. */
350 case 'r': /* Carriage return. */ 324 putchar(13);
351 putchar (13); 325 break;
352 break; 326 case 't': /* Horizontal tab. */
353 case 't': /* Horizontal tab. */ 327 putchar(9);
354 putchar (9); 328 break;
355 break; 329 case 'v': /* Vertical tab. */
356 case 'v': /* Vertical tab. */ 330 putchar(11);
357 putchar (11); 331 break;
358 break; 332 default:
359 default: 333 putchar(c);
360 putchar (c); 334 break;
361 break; 335 }
362 }
363} 336}
364 337
365/* Print string STR, evaluating \ escapes. */ 338/* Print string STR, evaluating \ escapes. */
366 339
367static void 340static void print_esc_string(char *str)
368print_esc_string (char *str)
369{ 341{
370 for (; *str; str++) 342 for (; *str; str++)
371 if (*str == '\\') 343 if (*str == '\\')
372 str += print_esc (str); 344 str += print_esc(str);
373 else 345 else
374 putchar (*str); 346 putchar(*str);
375} 347}
376 348
377static void 349static void
378print_direc (char *start, size_t length, int field_width, int precision, char *argument) 350print_direc(char *start, size_t length, int field_width, int precision,
351 char *argument)
379{ 352{
380 char *p; /* Null-terminated copy of % directive. */ 353 char *p; /* Null-terminated copy of % directive. */
381 354
382 p = xmalloc ((unsigned) (length + 1)); 355 p = xmalloc((unsigned) (length + 1));
383 strncpy (p, start, length); 356 strncpy(p, start, length);
384 p[length] = 0; 357 p[length] = 0;
385 358
386 switch (p[length - 1]) 359 switch (p[length - 1]) {
387 { 360 case 'd':
388 case 'd': 361 case 'i':
389 case 'i': 362 if (field_width < 0) {
390 if (field_width < 0) 363 if (precision < 0)
391 { 364 printf(p, xstrtol(argument));
392 if (precision < 0) 365 else
393 printf (p, xstrtol (argument)); 366 printf(p, precision, xstrtol(argument));
394 else 367 } else {
395 printf (p, precision, xstrtol (argument)); 368 if (precision < 0)
396 } 369 printf(p, field_width, xstrtol(argument));
397 else 370 else
398 { 371 printf(p, field_width, precision, xstrtol(argument));
399 if (precision < 0) 372 }
400 printf (p, field_width, xstrtol (argument)); 373 break;
401 else 374
402 printf (p, field_width, precision, xstrtol (argument)); 375 case 'o':
403 } 376 case 'u':
404 break; 377 case 'x':
405 378 case 'X':
406 case 'o': 379 if (field_width < 0) {
407 case 'u': 380 if (precision < 0)
408 case 'x': 381 printf(p, xstrtoul(argument));
409 case 'X': 382 else
410 if (field_width < 0) 383 printf(p, precision, xstrtoul(argument));
411 { 384 } else {
412 if (precision < 0) 385 if (precision < 0)
413 printf (p, xstrtoul (argument)); 386 printf(p, field_width, xstrtoul(argument));
414 else 387 else
415 printf (p, precision, xstrtoul (argument)); 388 printf(p, field_width, precision, xstrtoul(argument));
416 } 389 }
417 else 390 break;
418 { 391
419 if (precision < 0) 392 case 'f':
420 printf (p, field_width, xstrtoul (argument)); 393 case 'e':
421 else 394 case 'E':
422 printf (p, field_width, precision, xstrtoul (argument)); 395 case 'g':
423 } 396 case 'G':
424 break; 397 if (field_width < 0) {
425 398 if (precision < 0)
426 case 'f': 399 printf(p, xstrtod(argument));
427 case 'e': 400 else
428 case 'E': 401 printf(p, precision, xstrtod(argument));
429 case 'g': 402 } else {
430 case 'G': 403 if (precision < 0)
431 if (field_width < 0) 404 printf(p, field_width, xstrtod(argument));
432 { 405 else
433 if (precision < 0) 406 printf(p, field_width, precision, xstrtod(argument));
434 printf (p, xstrtod (argument)); 407 }
435 else 408 break;
436 printf (p, precision, xstrtod (argument)); 409
437 } 410 case 'c':
438 else 411 printf(p, *argument);
439 { 412 break;
440 if (precision < 0) 413
441 printf (p, field_width, xstrtod (argument)); 414 case 's':
442 else 415 if (field_width < 0) {
443 printf (p, field_width, precision, xstrtod (argument)); 416 if (precision < 0)
444 } 417 printf(p, argument);
445 break; 418 else
446 419 printf(p, precision, argument);
447 case 'c': 420 } else {
448 printf (p, *argument); 421 if (precision < 0)
449 break; 422 printf(p, field_width, argument);
450 423 else
451 case 's': 424 printf(p, field_width, precision, argument);
452 if (field_width < 0) 425 }
453 { 426 break;
454 if (precision < 0)
455 printf (p, argument);
456 else
457 printf (p, precision, argument);
458 }
459 else
460 {
461 if (precision < 0)
462 printf (p, field_width, argument);
463 else
464 printf (p, field_width, precision, argument);
465 } 427 }
466 break;
467 }
468 428
469 free (p); 429 free(p);
470} 430}
471 431
472static unsigned long 432static unsigned long xstrtoul(char *s)
473xstrtoul (char *s)
474{ 433{
475 char *end; 434 char *end;
476 unsigned long val; 435 unsigned long val;
477 436
478 errno = 0; 437 errno = 0;
479 val = strtoul (s, &end, 0); 438 val = strtoul(s, &end, 0);
480 verify (s, end); 439 verify(s, end);
481 return val; 440 return val;
482} 441}
483 442
484static long 443static long xstrtol(char *s)
485xstrtol (char *s)
486{ 444{
487 char *end; 445 char *end;
488 long val; 446 long val;
489 447
490 errno = 0; 448 errno = 0;
491 val = strtol (s, &end, 0); 449 val = strtol(s, &end, 0);
492 verify (s, end); 450 verify(s, end);
493 return val; 451 return val;
494} 452}
495 453
496static double 454static double xstrtod(char *s)
497xstrtod (char *s)
498{ 455{
499 char *end; 456 char *end;
500 double val; 457 double val;
501 458
502 errno = 0; 459 errno = 0;
503 val = strtod (s, &end); 460 val = strtod(s, &end);
504 verify (s, end); 461 verify(s, end);
505 return val; 462 return val;
506} 463}
507 464
508static void 465static void verify(char *s, char *end)
509verify (char *s, char *end)
510{ 466{
511 if (errno) 467 if (errno) {
512 { 468 fprintf(stderr, "%s", s);
513 fprintf(stderr, "%s", s); 469 exit_status = 1;
514 exit_status = 1; 470 } else if (*end) {
515 } 471 /*
516 else if (*end) 472 if (s == end)
517 { 473 fprintf(stderr, "%s: expected numeric", s);
518 /* 474 else
519 if (s == end) 475 fprintf(stderr, "%s: not completely converted", s);
520 fprintf(stderr, "%s: expected numeric", s); 476 */
521 else 477 exit_status = 1;
522 fprintf(stderr, "%s: not completely converted", s); 478 }
523 */
524 exit_status = 1;
525 }
526} 479}
527