[hackers] [ubase] Implement -o for killall5 || sin

From: <git_AT_suckless.org>
Date: Mon, 17 Mar 2014 13:29:42 +0100

commit 4fb9eabf8ef32728a4af0552a92c01e8c430c48b
Author: sin <sin_AT_2f30.org>
Date: Mon Mar 17 14:26:19 2014 +0200

    Implement -o for killall5

diff --git a/killall5.c b/killall5.c
index 7f91e06..8862210 100644
--- a/killall5.c
+++ b/killall5.c
_AT_@ -4,7 +4,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <strings.h>
+#include <string.h>
 #include <unistd.h>
 #include "proc.h"
 #include "util.h"
_AT_@ -23,12 +23,20 @@ struct {
 static void
 usage(void)
 {
- eprintf("usage: %s [-s signal]
", argv0);
+ eprintf("usage: %s [-o pid1,pid2,..,pidN] [-s signal]
", argv0);
 }
 
+static struct omit {
+ pid_t pid;
+ struct omit *next;
+} *omithead;
+
 int
 main(int argc, char *argv[])
 {
+ struct omit *onode, *tmp;
+ int oflag = 0;
+ char *p, *arg = NULL;
         DIR *dp;
         struct dirent *entry;
         char *end, *v;
_AT_@ -51,10 +59,23 @@ main(int argc, char *argv[])
                 if(i == LEN(sigs))
                         eprintf("%s: unknown signal
", v);
                 break;
+ case 'o':
+ oflag = 1;
+ arg = EARGF(usage());
+ break;
         default:
                 usage();
         } ARGEND;
 
+ for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
+ onode = malloc(sizeof(*onode));
+ if (!onode)
+ eprintf("malloc:");
+ onode->pid = estrtol(p, 10);
+ onode->next = omithead;
+ omithead = onode;
+ }
+
         if (sig != SIGSTOP && sig != SIGCONT)
                 kill(-1, SIGSTOP);
 
_AT_@ -67,6 +88,13 @@ main(int argc, char *argv[])
                 if (pid == 1 || pid == getpid() ||
                     getsid(pid) == getsid(0) || getsid(pid) == 0)
                         continue;
+ if (oflag == 1) {
+ for (onode = omithead; onode; onode = onode->next)
+ if (onode->pid == pid)
+ break;
+ if (onode)
+ continue;
+ }
                 kill(pid, sig);
         }
         closedir(dp);
_AT_@ -74,5 +102,12 @@ main(int argc, char *argv[])
         if (sig != SIGSTOP && sig != SIGCONT)
                 kill(-1, SIGCONT);
 
+ onode = omithead;
+ while (onode) {
+ tmp = onode->next;
+ free(onode);
+ onode = tmp;
+ }
+
         return EXIT_SUCCESS;
 }
Received on Mon Mar 17 2014 - 13:29:42 CET

This archive was generated by hypermail 2.3.0 : Mon Mar 17 2014 - 13:36:15 CET