diff --git a/winsup/utils/ChangeLog b/winsup/utils/ChangeLog index 9d5b703a4..3343f8922 100644 --- a/winsup/utils/ChangeLog +++ b/winsup/utils/ChangeLog @@ -1,3 +1,14 @@ +2002-05-13 Christopher Faylor + + * Makefile.in (kill.exe): Add as a specific target. + * kill.cc (longopts): New. + (opts): Ditto. + (get_sig): Accept const char * parameter. Return -1 on unknown signal. + (test_for_unknown_sig): New function. + (listsig): New function. + (main): Use getopt_long for option parsing. Implement -l, and -s + options. Use test_for_unknown_sig() to test for signal validity. + 2002-05-12 Christopher Faylor * mount.cc (do_mount): Default to non-exec option for remote drives. diff --git a/winsup/utils/Makefile.in b/winsup/utils/Makefile.in index 615ee97da..19346c474 100644 --- a/winsup/utils/Makefile.in +++ b/winsup/utils/Makefile.in @@ -111,7 +111,6 @@ else ${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $(@D)/$(basename $@)$o $(DUMPER_INCLUDES) $< endif - module_info.o: module_info.cc ifdef VERBOSE ${filter-out -nostdinc,$(COMPILE_CXX)} $c -o $@ $(DUMPER_INCLUDES) ${firstword $^} @@ -168,6 +167,14 @@ else ${filter-out -I$(newlib_source)/%,$(COMPILE_CXX)} $c -o $(@D)/$(basename $@)$o $(MINGW_CXXFLAGS) $< endif +kill.exe: kill.o $(bupdir1)/libiberty/strsignal.o +ifdef VERBOSE + $(CXX) -o $@ $^ -B$(cygwin_build)/ $(ALL_LDFLAGS) $(KILL_LIB) +else + @echo $(CXX) -o $@ $^ ${filter-out -B%, $(ALL_LDFLAGS)};\ + $(CXX) -o $@ $^ -B$(cygwin_build)/ $(ALL_LDFLAGS) $(KILL_LIB) +endif + clean: rm -f *.o $(CLEAN_PROGS) diff --git a/winsup/utils/kill.cc b/winsup/utils/kill.cc index 2f538ea49..da1721f0e 100644 --- a/winsup/utils/kill.cc +++ b/winsup/utils/kill.cc @@ -16,6 +16,20 @@ details. */ #include #include #include +#include + +static struct option longopts[] = +{ + {"help", no_argument, NULL, 'h' }, + {"list", optional_argument, NULL, 'l'}, + {"force", no_argument, NULL, 'f'}, + {"signal", required_argument, NULL, 's'}, + {NULL, 0, NULL, 0} +}; + +static char opts[] = "hl::fs:"; + +extern "C" const char *strsigno (int); static void usage (void) @@ -25,10 +39,11 @@ usage (void) } static int -getsig (char *in_sig) +getsig (const char *in_sig) { - char *sig; + const char *sig; char buf[80]; + int intsig; if (strncmp (in_sig, "SIG", 3) == 0) sig = in_sig; @@ -37,7 +52,37 @@ getsig (char *in_sig) sprintf (buf, "SIG%s", in_sig); sig = buf; } - return (strtosigno (sig) ?: atoi (in_sig)); + intsig = strtosigno (sig) ?: atoi (in_sig); + char *p; + if (!intsig && (strcmp (buf, "SIG0") != 0 && (strtol (in_sig, &p, 10) != 0 || *p))) + intsig = -1; + return intsig; +} + +static void +test_for_unknown_sig (int sig, const char *sigstr) +{ + if (sig < 0 || sig > NSIG) + { + fprintf (stderr, "kill: unknown signal: %s\n", sigstr); + usage (); + exit (1); + } +} + +static void +listsig (const char *in_sig) +{ + int sig; + if (!in_sig) + for (sig = 1; sig < NSIG; sig++) + printf ("%s%c", strsigno (sig) + 3, (sig < NSIG - 1) ? ' ' : '\n'); + else + { + sig = getsig (in_sig); + test_for_unknown_sig (sig, in_sig); + puts (strsigno (sig) + 3); + } } static void __stdcall @@ -59,36 +104,59 @@ main (int argc, char **argv) { int sig = SIGTERM; int force = 0; - int gotsig = 0; + char *gotsig = NULL; int ret = 0; if (argc == 1) usage (); - while (*++argv && **argv == '-') - if (strcmp (*argv + 1, "f") == 0) - force = 1; - else if (gotsig) - break; - else if (strcmp(*argv + 1, "0") != 0) - { - sig = getsig (*argv + 1); - gotsig = 1; - } - else - { - argv++; - sig = 0; - goto sig0; - } - - if (sig <= 0 || sig > NSIG) + opterr = 0; + for (;;) { - fprintf (stderr, "kill: unknown signal: %s\n", argv[-1]); - exit (1); + int ch; + char **av = argv + optind; + if ((ch = getopt_long (argc, argv, opts, longopts, NULL)) == EOF) + break; + switch (ch) + { + case 's': + gotsig = optarg; + sig = getsig (gotsig); + break; + case 'l': + if (!optarg) + { + optarg = argv[optind]; + if (optarg) + { + optind++; + optreset = 1; + } + } + if (argv[optind]) + usage (); + listsig (optarg); + break; + case 'f': + force = 1; + break; + case '?': + if (gotsig) + usage (); + optreset = 1; + optind = 1 + av - argv; + gotsig = *av + 1; + sig = getsig (gotsig); + break; + default: + usage (); + break; + } } -sig0: + test_for_unknown_sig (sig, gotsig); + + argv += optind; while (*argv != NULL) { char *p; diff --git a/winsup/utils/mount.cc b/winsup/utils/mount.cc index c44d04bd1..959cdf956 100644 --- a/winsup/utils/mount.cc +++ b/winsup/utils/mount.cc @@ -109,7 +109,7 @@ do_mount (const char *dev, const char *where, int flags) exit (0); } -struct option longopts[] = +static struct option longopts[] = { {"help", no_argument, NULL, 'h' }, {"binary", no_argument, NULL, 'b'}, @@ -127,7 +127,7 @@ struct option longopts[] = {NULL, 0, NULL, 0} }; -char opts[] = "hbfstuxXEpicm"; +static char opts[] = "hbfstuxXEpicm"; static void usage (void)