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.
This commit is contained in:
Rob Landley 2006-07-12 19:17:55 +00:00
parent 801ab14013
commit c9c1a41c58
10 changed files with 102 additions and 339 deletions

View File

@ -16,6 +16,7 @@
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <netdb.h>
@ -25,10 +26,12 @@
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
@ -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. */

View File

@ -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 <rob@landley.net>
*
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#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
*/
const char *
u_signal_names(const char *str_sig, int *signo, int startnum)
int get_signum(char *name)
{
static char retstr[16];
const struct signal_name *s = signames;
static const char prefix[] = "SIG";
const char *sptr;
int i;
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++;
}
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
char *get_signame(int number)
{
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;
}

View File

@ -11,18 +11,8 @@
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#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));

View File

@ -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);

View File

@ -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");

View File

@ -9,18 +9,6 @@
*/
#include "busybox.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <dirent.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#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;i<fnic;i++) {
if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
@ -354,14 +341,13 @@ int fuser_main(int argc, char **argv)
else {
if(!fuser_file_to_dev_inode(
argv[fni[i]], &dev, &inode)) {
free(pids);
free(inodes);
bb_perror_msg_and_die(
"Could not open '%s'", argv[fni[i]]);
if (ENABLE_FEATURE_CLEAN_UP) free(inodes);
bb_perror_msg_and_die("Could not open '%s'", argv[fni[i]]);
}
fuser_add_inode(inodes, dev, inode);
}
}
pids = xmalloc(sizeof(pid_list));
success = fuser_scan_proc_pids(opt, inodes, pids);
/* if the first pid in the list is 0, none have been found */
if(pids->pid == 0) success = 0;

View File

@ -18,22 +18,11 @@
#include <string.h>
#include <unistd.h>
#define KILL 0
#define KILLALL 1
int kill_main(int argc, char **argv)
{
int whichApp, signo = SIGTERM;
const char *name;
int errors = 0;
int killall, signo = SIGTERM, errors = 0, quiet=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
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;
}

View File

@ -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];

View File

@ -22,18 +22,7 @@
#include "busybox.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <getopt.h>
#include <termios.h>
#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;

View File

@ -10,41 +10,12 @@
* Robert Schwebel <r.schwebel@pengutronix.de>
* Erik Andersen <andersen@codepoet.org>
*
* 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 <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <setjmp.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/wait.h>
#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);