Use SLIST instead of TAILQ for pidof(8)

This commit is contained in:
sin 2015-10-07 14:53:41 +02:00
parent d3efa66c82
commit a8f95b2f9a
1 changed files with 6 additions and 12 deletions

18
pidof.c
View File

@ -15,10 +15,10 @@
struct pidentry { struct pidentry {
pid_t pid; pid_t pid;
TAILQ_ENTRY(pidentry) entry; SLIST_ENTRY(pidentry) entry;
}; };
static TAILQ_HEAD(omitpid_head, pidentry) omitpid_head; static SLIST_HEAD(, pidentry) omitpid_head;
static void static void
usage(void) usage(void)
@ -36,7 +36,7 @@ main(int argc, char *argv[])
char cmdline[BUFSIZ], *cmd, *cmdbase = NULL, *p, *arg = NULL; char cmdline[BUFSIZ], *cmd, *cmdbase = NULL, *p, *arg = NULL;
int i, found = 0; int i, found = 0;
int sflag = 0, oflag = 0; int sflag = 0, oflag = 0;
struct pidentry *pe, *tmp; struct pidentry *pe;
ARGBEGIN { ARGBEGIN {
case 's': case 's':
@ -53,7 +53,7 @@ main(int argc, char *argv[])
if (!argc) if (!argc)
return 1; return 1;
TAILQ_INIT(&omitpid_head); SLIST_INIT(&omitpid_head);
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) { for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
pe = emalloc(sizeof(*pe)); pe = emalloc(sizeof(*pe));
@ -61,7 +61,7 @@ main(int argc, char *argv[])
pe->pid = getppid(); pe->pid = getppid();
else else
pe->pid = estrtol(p, 10); pe->pid = estrtol(p, 10);
TAILQ_INSERT_TAIL(&omitpid_head, pe, entry); SLIST_INSERT_HEAD(&omitpid_head, pe, entry);
} }
if (!(dp = opendir("/proc"))) if (!(dp = opendir("/proc")))
@ -72,7 +72,7 @@ main(int argc, char *argv[])
continue; continue;
pid = estrtol(entry->d_name, 10); pid = estrtol(entry->d_name, 10);
if (oflag) { if (oflag) {
TAILQ_FOREACH(pe, &omitpid_head, entry) SLIST_FOREACH(pe, &omitpid_head, entry)
if (pe->pid == pid) if (pe->pid == pid)
break; break;
if (pe) if (pe)
@ -110,11 +110,5 @@ out:
closedir(dp); closedir(dp);
for (pe = TAILQ_FIRST(&omitpid_head); pe; pe = tmp) {
tmp = TAILQ_NEXT(pe, entry);
TAILQ_REMOVE(&omitpid_head, pe, entry);
free(pe);
}
return 0; return 0;
} }