aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-01-06 20:28:05 +0000
committerlandley <landley@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-01-06 20:28:05 +0000
commit129f57f04defd99cfb4b24fc15ce1456639daf1b (patch)
tree2b121941a13619a7851da8d9f8e1663ad798ae4d
parentfcb47366ebc1c8b91c7be87599839c16641f483e (diff)
downloadbusybox-w32-129f57f04defd99cfb4b24fc15ce1456639daf1b.tar.gz
busybox-w32-129f57f04defd99cfb4b24fc15ce1456639daf1b.tar.bz2
busybox-w32-129f57f04defd99cfb4b24fc15ce1456639daf1b.zip
Bug 624 wants quoted char support for printf, so you can do something like:
printf '%d\n' '"x"' and have it print out 120. This is the smallest implementation I can think of at the moment. git-svn-id: svn://busybox.net/trunk/busybox@13134 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--coreutils/printf.c104
1 files changed, 50 insertions, 54 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c
index da5e46a58..697a1c055 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -1,20 +1,11 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* printf - format and print data 2/* printf - format and print data
3 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
4 3
5 This program is free software; you can redistribute it and/or modify 4 Copyright 1999 Dave Cinege
6 it under the terms of the GNU General Public License as published by 5 Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9 6
10 This program is distributed in the hope that it will be useful, 7 Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 8*/
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 9
19/* Usage: printf format [argument...] 10/* Usage: printf format [argument...]
20 11
@@ -58,14 +49,56 @@
58#include <assert.h> 49#include <assert.h>
59#include "busybox.h" 50#include "busybox.h"
60 51
61static double xstrtod __P((char *s));
62static long xstrtol __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)); 52static int print_formatted __P((char *format, int argc, char **argv));
66static void print_direc __P( (char *start, size_t length, 53static void print_direc __P( (char *start, size_t length,
67 int field_width, int precision, char *argument)); 54 int field_width, int precision, char *argument));
68 55
56typedef int (*converter)(char *arg, void *result);
57void multiconvert(char *arg, void *result, converter convert)
58{
59 char s[16];
60 if (*arg == '"' || *arg == '\'') {
61 sprintf(s,"%d",(unsigned)*(++arg));
62 arg=s;
63 }
64 if(convert(arg,result)) fprintf(stderr, "%s", arg);
65}
66
67static unsigned long xstrtoul(char *arg)
68{
69 unsigned long result;
70
71 multiconvert(arg,&result, (converter)safe_strtoul);
72 return result;
73}
74
75static long xstrtol(char *arg)
76{
77 long result;
78 multiconvert(arg, &result, (converter)safe_strtol);
79 return result;
80}
81
82static double xstrtod(char *arg)
83{
84 double result;
85 multiconvert(arg, &result, (converter)safe_strtod);
86 return result;
87}
88
89static void print_esc_string(char *str)
90{
91 for (; *str; str++) {
92 if (*str == '\\') {
93 str++;
94 putchar(bb_process_escape_sequence((const char **)&str));
95 } else {
96 putchar(*str);
97 }
98
99 }
100}
101
69int printf_main(int argc, char **argv) 102int printf_main(int argc, char **argv)
70{ 103{
71 char *format; 104 char *format;
@@ -277,40 +310,3 @@ print_direc(char *start, size_t length, int field_width, int precision,
277 310
278 free(p); 311 free(p);
279} 312}
280
281static unsigned long xstrtoul(char *arg)
282{
283 unsigned long result;
284 if (safe_strtoul(arg, &result))
285 fprintf(stderr, "%s", arg);
286 return result;
287}
288
289static long xstrtol(char *arg)
290{
291 long result;
292 if (safe_strtol(arg, &result))
293 fprintf(stderr, "%s", arg);
294 return result;
295}
296
297static double xstrtod(char *arg)
298{
299 double result;
300 if (safe_strtod(arg, &result))
301 fprintf(stderr, "%s", arg);
302 return result;
303}
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}