OSDN Git Service

* Makefile.in (kill.exe): Add as a specific target.
authorcgf <cgf>
Mon, 13 May 2002 05:13:57 +0000 (05:13 +0000)
committercgf <cgf>
Mon, 13 May 2002 05:13:57 +0000 (05:13 +0000)
* 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.

winsup/utils/ChangeLog
winsup/utils/Makefile.in
winsup/utils/kill.cc
winsup/utils/mount.cc

index 9d5b703..3343f89 100644 (file)
@@ -1,3 +1,14 @@
+2002-05-13  Christopher Faylor  <cgf@redhat.com>
+
+       * 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  <cgf@redhat.com>
 
        * mount.cc (do_mount): Default to non-exec option for remote drives.
index 615ee97..19346c4 100644 (file)
@@ -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)
 
index 2f538ea..da1721f 100644 (file)
@@ -16,6 +16,20 @@ details. */
 #include <errno.h>
 #include <windows.h>
 #include <sys/cygwin.h>
+#include <getopt.h>
+
+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;
index c44d04b..959cdf9 100644 (file)
@@ -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)