Chops a long line and removes an overfluous cast.
I'd like to optionaly be able to have printf builtin into ash.
Playing with it right now. On the positive side, my poor attempt
shows that this command line:
# ./busybox ash -c 'for D in $(date +%s);do for N in $(seq 1 100000);do \
printf "%d. %d\n" $N 23 >/dev/null;done; \
echo "elapsed $(($(date +%s) - $D))s";done'
take 96s to complete on my box with unmodified bb-svn, and 2-3s with my
first go at an ash bultin prinf. That's 32-48 times faster.
On the negative side, the above command line outputs all 100000 lines to
stdout, even if I redirect to /dev/null, but it doesn't do that if I run
it like this:
# ./busybox ash
$ for D in $(date +%s);do for N in $(seq 1 100000);do \
printf "%d. %d\n" $N 23 >/dev/null;done; \
echo "elapsed $(($(date +%s) - $D))s";done
$ ^D
Obviously I'm missing something, but I can't figure it out right now :(
2nd attachment is my simple ash-printf-builtin patch attempt. Seems to
cost some 6 bytes. I'd be glad to hear some comments.
Cheers,
--
Cristian
Index: coreutils/printf.c
===================================================================
--- coreutils/printf.c (revision 22011)
+++ coreutils/printf.c (working copy)
@@ -107,12 +107,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 = xmalloc(length + 1);
strncpy(p, start, length);
p[length] = 0;
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 "[", 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