svn commit: trunk/busybox/coreutils

landley at busybox.net landley at busybox.net
Fri Jan 6 20:28:07 UTC 2006


Author: landley
Date: 2006-01-06 12:28:05 -0800 (Fri, 06 Jan 2006)
New Revision: 13134

Log:
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.


Modified:
   trunk/busybox/coreutils/printf.c


Changeset:
Modified: trunk/busybox/coreutils/printf.c
===================================================================
--- trunk/busybox/coreutils/printf.c	2006-01-06 18:22:05 UTC (rev 13133)
+++ trunk/busybox/coreutils/printf.c	2006-01-06 20:28:05 UTC (rev 13134)
@@ -1,21 +1,12 @@
 /* vi: set sw=4 ts=4: */
 /* printf - format and print data
-   Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   Copyright 1999 Dave Cinege
+   Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+   Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
+*/
 
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
-
 /* Usage: printf format [argument...]
 
    A front end to the printf function that lets it be used from the shell.
@@ -58,14 +49,56 @@
 #include <assert.h>
 #include "busybox.h"
 
-static double xstrtod __P((char *s));
-static long xstrtol __P((char *s));
-static unsigned long xstrtoul __P((char *s));
-static void print_esc_string __P((char *str));
 static int print_formatted __P((char *format, int argc, char **argv));
 static void print_direc __P( (char *start, size_t length,
 			int field_width, int precision, char *argument));
 
+typedef int (*converter)(char *arg, void *result);
+void multiconvert(char *arg, void *result, converter convert)
+{
+	char s[16];
+	if (*arg == '"' || *arg == '\'') {
+		sprintf(s,"%d",(unsigned)*(++arg));
+		arg=s;
+	}
+	if(convert(arg,result)) fprintf(stderr, "%s", arg);
+}
+	
+static unsigned long xstrtoul(char *arg)
+{
+	unsigned long result;
+
+	multiconvert(arg,&result, (converter)safe_strtoul);	
+	return result;
+}
+
+static long xstrtol(char *arg)
+{
+	long result;
+	multiconvert(arg, &result, (converter)safe_strtol);
+	return result;
+}
+
+static double xstrtod(char *arg)
+{
+	double result;
+	multiconvert(arg, &result, (converter)safe_strtod);
+	return result;
+}
+
+static void print_esc_string(char *str)
+{
+	for (; *str; str++) {
+		if (*str == '\\') {
+			str++;
+			putchar(bb_process_escape_sequence((const char **)&str));
+		} else {
+			putchar(*str);
+		}
+
+	}
+}
+
 int printf_main(int argc, char **argv)
 {
 	char *format;
@@ -277,40 +310,3 @@
 
 	free(p);
 }
-
-static unsigned long xstrtoul(char *arg)
-{
-	unsigned long result;
-	if (safe_strtoul(arg, &result))
-		fprintf(stderr, "%s", arg);
-	return result;
-}
-
-static long xstrtol(char *arg)
-{
-	long result;
-	if (safe_strtol(arg, &result))
-		fprintf(stderr, "%s", arg);
-	return result;
-}
-
-static double xstrtod(char *arg)
-{
-	double result;
-	if (safe_strtod(arg, &result))
-		fprintf(stderr, "%s", arg);
-	return result;
-}
-
-static void print_esc_string(char *str)
-{
-	for (; *str; str++) {
-		if (*str == '\\') {
-			str++;
-			putchar(bb_process_escape_sequence((const char **)&str));
-		} else {
-			putchar(*str);
-		}
-
-	}
-}




More information about the busybox-cvs mailing list