On Fri, 30 May 2008, Bernhard Fischer wrote:

> On Thu, May 29, 2008 at 11:53:03PM +0200, Cristian Ionescu-Idbohrn wrote:
> >@@ -8540,6 +8541,9 @@
> >     { BUILTIN_NOSPEC        "let", letcmd },
> > #endif
> >     { BUILTIN_ASSIGN        "local", localcmd },
> >+#if ENABLE_ASH_BUILTIN_PRINTF
> >+    { BUILTIN_REGULAR               "[", printfcmd },
> >+#endif
>
> are you sure that "[" is correct here?

Of course it isn't.  And that possibly has something to do with the
odd behaviour I experienced:

| On the negative side, the above command line outputs all 100000
| lines to stdout, even if I redirect to /dev/null

> Sounds a bit like it should be "printf", but i didn't look..

Of course it should.

Brain and fingers temporarily disconnected :)
Corrected patch attached.

What I'm unsure about is if this:

-       p = xmalloc((unsigned) (length + 1));
+       p = alloca(length + 1);

is a good move.  I ran this command line:

# ./busybox ash -c 'for x in 1;do S=; for N in $(seq 1 100000);do \
C=${C}x;done; printf "%s\n" $C >/dev/null;done'

to check if anything gets smashed, but no bad surprise.

gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) reposts this
on one of my boxes:

function                                             old     new   delta
.rodata                                            76384   76416     +32
builtintab                                           336     344      +8
collect_fork                                         112     119      +7
make_device                                         1146    1152      +6
send_tree                                            369     373      +4
diffreg                                             1741    1743      +2
count_lines                                           68      70      +2
passwd_main                                          989     987      -2
print_direc                                          456     443     -13
diff_main                                            837     823     -14
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 7/3 up/down: 61/-29)             Total: 32 bytes
   text    data     bss     dec     hex filename
 342426    1977   17792  362195   586d3 busybox_old
 342450    1977   17792  362219   586eb busybox_unstripped

Another one reports +6 bytes (another gcc version).


Cheers,

-- 
Cristian
Index: shell/ash.c
===================================================================
--- shell/ash.c	(revision 22011)
+++ shell/ash.c	(working copy)
@@ -8488,8 +8488,9 @@
  * Apart from the above, [[ expr ]] should work as [ expr ]
  */
 
-#define testcmd test_main
-#define echocmd echo_main
+#define echocmd   echo_main
+#define printfcmd printf_main
+#define testcmd   test_main
 
 /* Keep these in proper order since it is searched via bsearch() */
 static const struct builtincmd builtintab[] = {
@@ -8540,6 +8541,9 @@
 	{ BUILTIN_NOSPEC        "let", letcmd },
 #endif
 	{ BUILTIN_ASSIGN        "local", localcmd },
+#if ENABLE_ASH_BUILTIN_PRINTF
+	{ BUILTIN_REGULAR		"printf", printfcmd },
+#endif
 	{ BUILTIN_NOSPEC        "pwd", pwdcmd },
 	{ BUILTIN_REGULAR       "read", readcmd },
 	{ BUILTIN_SPEC_REG_ASSG "readonly", exportcmd },
Index: shell/Config.in
===================================================================
--- shell/Config.in	(revision 22001)
+++ shell/Config.in	(working copy)
@@ -114,6 +114,13 @@
 	help
 	  Enable support for echo, builtin to ash.
 
+config ASH_BUILTIN_PRINTF
+	bool "Builtin version of 'printf'"
+	default y
+	depends on ASH
+	help
+	  Enable support for printf, builtin to ash.
+
 config ASH_BUILTIN_TEST
 	bool "Builtin version of 'test'"
 	default y
Index: coreutils/printf.c
===================================================================
--- coreutils/printf.c	(revision 22011)
+++ coreutils/printf.c	(working copy)
@@ -39,6 +39,7 @@
 //   19990508 Busy Boxed! Dave Cinege
 
 #include "libbb.h"
+#include <alloca.h>
 
 typedef void (*converter)(const char *arg, void *result);
 
@@ -107,12 +108,12 @@
 	}
 }
 
-static void print_direc(char *start, size_t length, int field_width, int precision,
-		const char *argument)
+static void print_direc(char *start, size_t length, int field_width,
+		int precision, const char *argument)
 {
 	char *p;		/* Null-terminated copy of % directive. */
 
-	p = xmalloc((unsigned) (length + 1));
+	p = alloca(length + 1);
 	strncpy(p, start, length);
 	p[length] = 0;
 
@@ -181,8 +182,6 @@
 		}
 		break;
 	}
-
-	free(p);
 }
 
 /* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
@@ -278,7 +277,6 @@
 	return argv;
 }
 
-int printf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int printf_main(int argc ATTRIBUTE_UNUSED, char **argv)
 {
 	char *format;
Index: include/applets.h
===================================================================
--- include/applets.h	(revision 22084)
+++ include/applets.h	(working copy)
@@ -275,7 +275,7 @@
 USE_PKILL(APPLET_ODDNAME(pkill, pgrep, _BB_DIR_USR_BIN, _BB_SUID_NEVER, pkill))
 USE_HALT(APPLET_ODDNAME(poweroff, halt, _BB_DIR_SBIN, _BB_SUID_NEVER, poweroff))
 USE_PRINTENV(APPLET(printenv, _BB_DIR_BIN, _BB_SUID_NEVER))
-USE_PRINTF(APPLET(printf, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
+USE_PRINTF(APPLET_NOFORK(printf, printf, _BB_DIR_USR_BIN, _BB_SUID_NEVER, printf))
 USE_PS(APPLET(ps, _BB_DIR_BIN, _BB_SUID_NEVER))
 USE_PSCAN(APPLET(pscan, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_PWD(APPLET_NOFORK(pwd, pwd, _BB_DIR_BIN, _BB_SUID_NEVER, pwd))
Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 22110)
+++ include/libbb.h	(working copy)
@@ -882,6 +882,7 @@
 int bb_cat(char** argv);
 /* If shell needs them, these three "exist" even if not enabled as applets */
 int echo_main(int argc, char** argv) USE_ECHO(MAIN_EXTERNALLY_VISIBLE);
+int printf_main(int argc, char **argv) USE_PRINTF(MAIN_EXTERNALLY_VISIBLE);
 int test_main(int argc, char **argv) USE_TEST(MAIN_EXTERNALLY_VISIBLE);
 int kill_main(int argc, char **argv) USE_KILL(MAIN_EXTERNALLY_VISIBLE);
 int chown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to