From c9c1a41c581101f53cc36efae53cd8ebb568962f Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 12 Jul 2006 19:17:55 +0000 Subject: [PATCH] A couple things that got tangled up in my tree, easier to check in both than untangle them: Rewrite u_signal_names() into get_signum() and get_signame(), plus trim the signal list to that required by posix (they can specify the numbers for the rest if they really need them). (This is preparatory cleanup for adding a timeout applet like Roberto Foglietta wants.) Export the itoa (added due to Denis Vlasenko, although it's not quite his preferred implementation) from xfuncs.c so it's actually used, and remove several other redundant implementations of itoa and utoa() in the tree. --- include/libbb.h | 11 ++- libbb/u_signal_names.c | 200 ++++++++-------------------------------- networking/netstat.c | 25 +---- networking/traceroute.c | 10 +- networking/wget.c | 17 ++-- procps/fuser.c | 22 +---- procps/kill.c | 57 +++++------- shell/ash.c | 27 ++---- shell/lash.c | 31 ------- shell/msh.c | 41 +------- 10 files changed, 102 insertions(+), 339 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index 0a52b6421..2f9041273 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -25,10 +26,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include @@ -178,6 +181,10 @@ extern void bb_xdaemon(int nochdir, int noclose); extern void bb_xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); extern void bb_xlisten(int s, int backlog); extern void bb_xchdir(const char *path); +extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen); +extern char *utoa(unsigned n); +extern void itoa_to_buf(int n, char *buf, unsigned buflen); +extern char *itoa(int n); #define BB_GETOPT_ERROR 0x80000000UL extern const char *bb_opt_complementally; @@ -331,7 +338,9 @@ char *dirname (char *path); int bb_make_directory (char *path, long mode, int flags); -const char *u_signal_names(const char *str_sig, int *signo, int startnum); +int get_signum(char *name); +char *get_signame(int number); + char *bb_simplify_path(const char *path); enum { /* DO NOT CHANGE THESE VALUES! cp.c depends on them. */ diff --git a/libbb/u_signal_names.c b/libbb/u_signal_names.c index bf65fa3e9..62fab810d 100644 --- a/libbb/u_signal_names.c +++ b/libbb/u_signal_names.c @@ -1,179 +1,59 @@ /* vi: set sw=4 ts=4: */ /* - * Utility routines. + * Signal name/number conversion routines. * - * Copyright (C) many different people. - * If you wrote this, please acknowledge your work. + * Copyright 2006 Rob Landley * * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include -#include -#include -#include -#include - #include "libbb.h" -struct signal_name { - const char *name; +static struct signal_name { + char *name; int number; +} signals[] = { + // SUSv3 says kill must support these, and specifies the numerical values, + // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html + {"0", 0}, {"HUP", 1}, {"INT", 2}, {"QUIT", 3}, {"ABRT", 6}, {"KILL", 9}, + {"ALRM", 14}, {"TERM", 15}, + // And Posix adds the following: + {"ILL", SIGILL}, {"TRAP", SIGTRAP}, {"FPE", SIGFPE}, {"USR1", SIGUSR1}, + {"SEGV", SIGSEGV}, {"USR2", SIGUSR2}, {"PIPE", SIGPIPE}, {"CHLD", SIGCHLD}, + {"CONT", SIGCONT}, {"STOP", SIGSTOP}, {"TSTP", SIGTSTP}, {"TTIN", SIGTTIN}, + {"TTOU", SIGTTOU} }; -static const struct signal_name signames[] = { - /* POSIX signals */ - { "EXIT", 0 }, /* 0 */ - { "HUP", SIGHUP }, /* 1 */ - { "INT", SIGINT }, /* 2 */ - { "QUIT", SIGQUIT }, /* 3 */ - { "ILL", SIGILL }, /* 4 */ - { "ABRT", SIGABRT }, /* 6 */ - { "FPE", SIGFPE }, /* 8 */ - { "KILL", SIGKILL }, /* 9 */ - { "SEGV", SIGSEGV }, /* 11 */ - { "PIPE", SIGPIPE }, /* 13 */ - { "ALRM", SIGALRM }, /* 14 */ - { "TERM", SIGTERM }, /* 15 */ - { "USR1", SIGUSR1 }, /* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */ - { "USR2", SIGUSR2 }, /* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */ - { "CHLD", SIGCHLD }, /* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */ - { "CONT", SIGCONT }, /* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */ - { "STOP", SIGSTOP }, /* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */ - { "TSTP", SIGTSTP }, /* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */ - { "TTIN", SIGTTIN }, /* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */ - { "TTOU", SIGTTOU }, /* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */ - /* Miscellaneous other signals */ -#ifdef SIGTRAP - { "TRAP", SIGTRAP }, /* 5 */ -#endif -#ifdef SIGIOT - { "IOT", SIGIOT }, /* 6, same as SIGABRT */ -#endif -#ifdef SIGEMT - { "EMT", SIGEMT }, /* 7 (mips,alpha,sparc*) */ -#endif -#ifdef SIGBUS - { "BUS", SIGBUS }, /* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */ -#endif -#ifdef SIGSYS - { "SYS", SIGSYS }, /* 12 (mips,alpha,sparc*) */ -#endif -#ifdef SIGSTKFLT - { "STKFLT", SIGSTKFLT }, /* 16 (arm,i386,m68k,ppc) */ -#endif -#ifdef SIGURG - { "URG", SIGURG }, /* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */ -#endif -#ifdef SIGIO - { "IO", SIGIO }, /* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */ -#endif -#ifdef SIGPOLL - { "POLL", SIGPOLL }, /* same as SIGIO */ -#endif -#ifdef SIGCLD - { "CLD", SIGCLD }, /* same as SIGCHLD (mips) */ -#endif -#ifdef SIGXCPU - { "XCPU", SIGXCPU }, /* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */ -#endif -#ifdef SIGXFSZ - { "XFSZ", SIGXFSZ }, /* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */ -#endif -#ifdef SIGVTALRM - { "VTALRM", SIGVTALRM }, /* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */ -#endif -#ifdef SIGPROF - { "PROF", SIGPROF }, /* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */ -#endif -#ifdef SIGPWR - { "PWR", SIGPWR }, /* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */ -#endif -#ifdef SIGINFO - { "INFO", SIGINFO }, /* 29 (alpha) */ -#endif -#ifdef SIGLOST - { "LOST", SIGLOST }, /* 29 (arm,i386,m68k,ppc,sparc*) */ -#endif -#ifdef SIGWINCH - { "WINCH", SIGWINCH }, /* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */ -#endif -#ifdef SIGUNUSED - { "UNUSED", SIGUNUSED }, /* 31 (arm,i386,m68k,ppc) */ -#endif - {0, 0} -}; +// Convert signal name to number. -/* - if str_sig == NULL returned signal name [*signo], - if str_sig != NULL - set *signo from signal_name, - findings with digit number or with or without SIG-prefix name - - if startnum=0 flag for support finding zero signal, - but str_sig="0" always found, (hmm - standart or realize?) - if startnum<0 returned reverse signal_number <-> signal_name - if found error - returned NULL +int get_signum(char *name) +{ + int i; + + i = atoi(name); + if(i) return i; + for(i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) + if (!strcasecmp(signals[i].name, name) || + (!strncasecmp(signals[i].name, "SIG", 3) + && !strcasecmp(signals[i].name+3, signals[i].name))) + return signals[i].number; + return -1; +} -*/ +// Convert signal number to name -const char * -u_signal_names(const char *str_sig, int *signo, int startnum) +char *get_signame(int number) { - static char retstr[16]; - const struct signal_name *s = signames; - static const char prefix[] = "SIG"; - const char *sptr; - - if(startnum) - s++; - if(str_sig==NULL) { - while (s->name != 0) { - if(s->number == *signo) - break; - s++; - } - } else { - if (isdigit(((unsigned char)*str_sig))) { - char *endp; - long int sn = strtol(str_sig, &endp, 10); - /* test correct and overflow */ - if(*endp == 0 && sn >= 0 && sn < NSIG) { - *signo = (int)sn; - /* test for unnamed */ - sptr = u_signal_names(0, signo, 0); - if(sptr==NULL) - return NULL; - if(sn!=0) - sptr += 3; - return sptr; - } - } else { - sptr = str_sig; - while (s->name != 0) { - if (strcasecmp(s->name, sptr) == 0) { - *signo = s->number; - if(startnum<0) { - sprintf(retstr, "%d", *signo); - return retstr; - } - break; - } - if(s!=signames && sptr == str_sig && - strncasecmp(sptr, prefix, 3) == 0) { - sptr += 3; /* strlen(prefix) */ - continue; - } - sptr = str_sig; - s++; - } + int i; + static char buf[8]; + + itoa_to_buf(number, buf, 8); + for (i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) { + if (number == signals[i].number) { + sprintf("SIG%s", signals[i].name); + break; } } - if(s->name==0) - return NULL; - if(s!=signames) - strcpy(retstr, prefix); - else - retstr[0] = 0; - return strcat(retstr, s->name); + + return buf; } diff --git a/networking/netstat.c b/networking/netstat.c index 4faa40cd4..6d91bb3f4 100644 --- a/networking/netstat.c +++ b/networking/netstat.c @@ -11,18 +11,8 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "inet_common.h" #include "busybox.h" -#include "pwd_.h" +#include "inet_common.h" #ifdef CONFIG_ROUTE extern void displayroutes(int noresolve, int netstatfmt); @@ -87,19 +77,6 @@ typedef enum { #define SO_WAITDATA (1<<17) /* wait data to read */ #define SO_NOSPACE (1<<18) /* no space to write */ -static char *itoa(unsigned int i) -{ - /* 21 digits plus null terminator, good for 64-bit or smaller ints */ - static char local[22]; - char *p = &local[21]; - *p-- = '\0'; - do { - *p-- = '0' + i % 10; - i /= 10; - } while (i > 0); - return p + 1; -} - static char *get_sname(int port, const char *proto, int num) { char *str=itoa(ntohs(port)); diff --git a/networking/traceroute.c b/networking/traceroute.c index 190f19ddc..79f3957a6 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -548,7 +548,7 @@ static int wait_for_reply(int sock, struct sockaddr_in *fromp, const struct timeval *tp) { fd_set fds; - struct timeval now, wait; + struct timeval now, tvwait; struct timezone tz; int cc = 0; socklen_t fromlen = sizeof(*fromp); @@ -556,12 +556,12 @@ wait_for_reply(int sock, struct sockaddr_in *fromp, const struct timeval *tp) FD_ZERO(&fds); FD_SET(sock, &fds); - wait.tv_sec = tp->tv_sec + waittime; - wait.tv_usec = tp->tv_usec; + tvwait.tv_sec = tp->tv_sec + waittime; + tvwait.tv_usec = tp->tv_usec; (void)gettimeofday(&now, &tz); - tvsub(&wait, &now); + tvsub(&tvwait, &now); - if (select(sock + 1, &fds, NULL, NULL, &wait) > 0) + if (select(sock + 1, &fds, NULL, NULL, &tvwait) > 0) cc = recvfrom(sock, (char *)packet, sizeof(packet), 0, (struct sockaddr *)fromp, &fromlen); diff --git a/networking/wget.c b/networking/wget.c index 64cdf6220..6565bb1f3 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -697,12 +697,11 @@ updateprogressmeter(int ignore) errno = save_errno; } -static void -alarmtimer(int wait) +static void alarmtimer(int iwait) { struct itimerval itv; - itv.it_value.tv_sec = wait; + itv.it_value.tv_sec = iwait; itv.it_value.tv_usec = 0; itv.it_interval = itv.it_value; setitimer(ITIMER_REAL, &itv, NULL); @@ -715,7 +714,7 @@ progressmeter(int flag) static struct timeval lastupdate; static off_t lastsize, totalsize; - struct timeval now, td, wait; + struct timeval now, td, tvwait; off_t abbrevsize; int elapsed, ratio, barlength, i; char buf[256]; @@ -753,18 +752,18 @@ progressmeter(int flag) /* See http://en.wikipedia.org/wiki/Tera */ fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' '); - timersub(&now, &lastupdate, &wait); + timersub(&now, &lastupdate, &tvwait); if (transferred > lastsize) { lastupdate = now; lastsize = transferred; - if (wait.tv_sec >= STALLTIME) - timeradd(&start, &wait, &start); - wait.tv_sec = 0; + if (tvwait.tv_sec >= STALLTIME) + timeradd(&start, &tvwait, &start); + tvwait.tv_sec = 0; } timersub(&now, &start, &td); elapsed = td.tv_sec; - if (wait.tv_sec >= STALLTIME) { + if (tvwait.tv_sec >= STALLTIME) { fprintf(stderr, " - stalled -"); } else if (transferred <= 0 || elapsed <= 0 || transferred > totalsize || chunked) { fprintf(stderr, "--:--:-- ETA"); diff --git a/procps/fuser.c b/procps/fuser.c index 1a4f612f1..2965fc34b 100644 --- a/procps/fuser.c +++ b/procps/fuser.c @@ -9,18 +9,6 @@ */ #include "busybox.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #define FUSER_PROC_DIR "/proc" #define FUSER_MAX_LINE 255 @@ -335,7 +323,7 @@ int fuser_main(int argc, char **argv) optn = fuser_option(argv[i]); if(optn) opt |= optn; else if(argv[i][0] == '-') { - if(!(u_signal_names(argv[i]+1, &killsig, 0))) + if(0>(killsig = get_signum(argv[i]+1))) killsig = SIGTERM; } else { @@ -345,7 +333,6 @@ int fuser_main(int argc, char **argv) } if(!fnic) return 1; - pids = xmalloc(sizeof(pid_list)); inodes = xmalloc(sizeof(inode_list)); for(i=0;ipid == 0) success = 0; diff --git a/procps/kill.c b/procps/kill.c index ca6f4203a..1814e1963 100644 --- a/procps/kill.c +++ b/procps/kill.c @@ -18,22 +18,11 @@ #include #include -#define KILL 0 -#define KILLALL 1 - int kill_main(int argc, char **argv) { - int whichApp, signo = SIGTERM; - const char *name; - int errors = 0; - -#ifdef CONFIG_KILLALL - int quiet=0; - /* Figure out what we are trying to do here */ - whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL; -#else - whichApp = KILL; -#endif + int killall, signo = SIGTERM, errors = 0, quiet=0; + + killall = (ENABLE_KILLALL && bb_applet_name[4]=='a') ? 1 : 0; /* Parse any options */ if (argc < 2) @@ -50,32 +39,38 @@ int kill_main(int argc, char **argv) if(argc==2) { /* Print the whole signal list */ int col = 0; - for(signo=1; signo < NSIG; signo++) { - name = u_signal_names(0, &signo, 1); - if(name==NULL) /* unnamed */ - continue; - col += printf("%2d) %-16s", signo, name); + + for(signo = 0;;) { + char *name = get_signame(++signo); + if (isdigit(*name)) break; + if (col > 60) { printf("\n"); col = 0; } + col += printf("%2d) %-16s", signo, name); } printf("\n"); - } else { for(argv++; *argv; argv++) { - name = u_signal_names(*argv, &signo, -1); - if(name!=NULL) - printf("%s\n", name); + char *name; + + if (isdigit(**argv)) name = get_signame(atoi(*argv)); + else { + int temp = get_signum(*argv); + if (temp<0) + bb_error_msg_and_die("unknown signal %s", *argv); + name = get_signame(temp); + } + puts(name); } } /* If they specified -l, were all done */ return EXIT_SUCCESS; } -#ifdef CONFIG_KILLALL /* The -q quiet option */ - if(whichApp != KILL && argv[1][1]=='q' && argv[1][2]=='\0'){ + if(killall && argv[1][1]=='q' && argv[1][2]=='\0'){ quiet++; argv++; argc--; @@ -83,9 +78,8 @@ int kill_main(int argc, char **argv) goto do_it_now; } } -#endif - if(!u_signal_names(argv[1]+1, &signo, 0)) + if(0>(signo = get_signum(argv[1]+1))) bb_error_msg_and_die( "bad signal name '%s'", argv[1]+1); argv+=2; argc-=2; @@ -96,7 +90,7 @@ do_it_now: if (argc <= 0) bb_show_usage(); - if (whichApp == KILL) { + if (!killall) { /* Looks like they want to do a kill. Do that */ while (--argc >= 0) { int pid; @@ -111,10 +105,9 @@ do_it_now: argv++; } - } -#ifdef CONFIG_KILLALL - else { + } else { pid_t myPid=getpid(); + /* Looks like they want to do a killall. Do that */ while (--argc >= 0) { long* pidList; @@ -141,6 +134,6 @@ do_it_now: argv++; } } -#endif + return errors; } diff --git a/shell/ash.c b/shell/ash.c index ba99381a2..de8d06e90 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2035,7 +2035,6 @@ static void onsig(int); static int dotrap(void); static void setinteractive(int); static void exitshell(void) ATTRIBUTE_NORETURN; -static int decode_signal(const char *, int); /* * This routine is called when an error or an interrupt occurs in an @@ -6548,7 +6547,7 @@ usage: } if (**++argv == '-') { - signo = decode_signal(*argv + 1, 1); + signo = get_signum(*argv + 1); if (signo < 0) { int c; @@ -6562,7 +6561,7 @@ usage: list = 1; break; case 's': - signo = decode_signal(optionarg, 1); + signo = get_signum(optionarg); if (signo < 0) { sh_error( "invalid signal number or name: %s", @@ -6588,14 +6587,14 @@ usage: if (!*argv) { for (i = 1; i < NSIG; i++) { - name = u_signal_names(0, &i, 1); - if (name) + name = get_signame(i); + if (isdigit(*name)) out1fmt(snlfmt, name); } return 0; } - name = u_signal_names(*argptr, &signo, -1); - if (name) + name = get_signame(signo); + if (isdigit(*name)) out1fmt(snlfmt, name); else sh_error("invalid signal number or exit status: %s", *argptr); @@ -11617,9 +11616,7 @@ trapcmd(int argc, char **argv) if (trap[signo] != NULL) { const char *sn; - sn = u_signal_names(0, &signo, 0); - if (sn == NULL) - sn = "???"; + sn = get_signame(signo); out1fmt("trap -- %s %s\n", single_quote(trap[signo]), sn); } @@ -11631,7 +11628,7 @@ trapcmd(int argc, char **argv) else action = *ap++; while (*ap) { - if ((signo = decode_signal(*ap, 0)) < 0) + if ((signo = get_signum(*ap)) < 0) sh_error("%s: bad trap", *ap); INTOFF; if (action) { @@ -11934,14 +11931,6 @@ out: /* NOTREACHED */ } -static int decode_signal(const char *string, int minsig) -{ - int signo; - const char *name = u_signal_names(string, &signo, minsig); - - return name ? signo : -1; -} - /* var.c */ static struct var *vartab[VTABSIZE]; diff --git a/shell/lash.c b/shell/lash.c index c5aaf1d1f..92c24d1c2 100644 --- a/shell/lash.c +++ b/shell/lash.c @@ -22,18 +22,7 @@ #include "busybox.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include #include "cmdedit.h" #ifdef CONFIG_LOCALE_SUPPORT @@ -697,26 +686,6 @@ static int get_command(FILE * source, char *command) return 0; } -static char* itoa(int i) -{ - static char a[7]; /* Max 7 ints */ - char *b = a + sizeof(a) - 1; - int sign = (i < 0); - - if (sign) - i = -i; - *b = 0; - do - { - *--b = '0' + (i % 10); - i /= 10; - } - while (i); - if (sign) - *--b = '-'; - return b; -} - static char * strsep_space( char *string, int * ix) { char *token; diff --git a/shell/msh.c b/shell/msh.c index 633070112..b491a08a4 100644 --- a/shell/msh.c +++ b/shell/msh.c @@ -10,41 +10,12 @@ * Robert Schwebel * Erik Andersen * - * 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 of the License, or - * (at your option) any later version. - * - * 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. - * - * 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 - * - * Original copyright notice is retained at the end of this file. + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ #include "busybox.h" -#include -#include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include #include "cmdedit.h" @@ -293,7 +264,6 @@ static char *space(int n); static char *strsave(char *s, int a); static char *evalstr(char *cp, int f); static char *putn(int n); -static char *itoa(int n); static char *unquote(char *as); static struct var *lookup(char *n); static int rlookup(char *n); @@ -1252,15 +1222,6 @@ static char *putn(int n) return (itoa(n)); } -static char *itoa(int n) -{ - static char s[20]; - - snprintf(s, sizeof(s), "%u", n); - return (s); -} - - static void next(int f) { PUSHIO(afile, f, filechar); -- 2.25.1