summaryrefslogtreecommitdiffstats
path: root/procname.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-08-03 17:12:07 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-08-03 17:12:07 +0000
commit189fb08e13984cc2a8b28379ee9defe4156e1878 (patch)
tree6fca26f8329ddadc855738532320ea5d843015e4 /procname.c
parentac54dee9bbd33e71910fed8d5f8ddb1cde9b9a35 (diff)
Split the comparison into a function to make this code smaller and more
understandable.
Diffstat (limited to 'procname.c')
-rw-r--r--procname.c118
1 files changed, 60 insertions, 58 deletions
diff --git a/procname.c b/procname.c
index d7644898..61f3a46f 100644
--- a/procname.c
+++ b/procname.c
@@ -34,7 +34,55 @@
#define is_stopped(p) \
((p)->p_stat == SSTOP || (p)->p_stat == SZOMB || (p)->p_stat == SDEAD)
-char *get_proc_name(int, char *);
+struct proc *cmp_procs(struct proc *, struct proc *);
+char *get_proc_name(int, char *);
+
+struct proc *
+cmp_procs(struct proc *p1, struct proc *p2)
+{
+ void *ptr1, *ptr2;
+
+ if (is_runnable(p1) && !is_runnable(p2))
+ return (p1);
+ if (!is_runnable(p1) && is_runnable(p2))
+ return (p2);
+
+ if (is_stopped(p1) && !is_stopped(p2))
+ return (p1);
+ if (!is_stopped(p1) && is_stopped(p2))
+ return (p2);
+
+ if (p1->p_estcpu > p2->p_estcpu)
+ return (p1);
+ if (p1->p_estcpu < p2->p_estcpu)
+ return (p2);
+
+ if (p1->p_slptime < p2->p_slptime)
+ return (p1);
+ if (p1->p_slptime > p2->p_slptime)
+ return (p2);
+
+ if ((p1->p_flag & P_SINTR) && !(p2->p_flag & P_SINTR))
+ return (p1);
+ if (!(p1->p_flag & P_SINTR) && (p2->p_flag & P_SINTR))
+ return (p2);
+
+ ptr1 = LIST_FIRST(&p1->p_children);
+ ptr2 = LIST_FIRST(&p2->p_children);
+ if (ptr1 == NULL && ptr2 != NULL)
+ return (p1);
+ if (ptr1 != NULL && ptr2 == NULL)
+ return (p2);
+
+ if (strcmp(p1->p_comm, p2->p_comm) < 0)
+ return (p1);
+ if (strcmp(p1->p_comm, p2->p_comm) > 0)
+ return (p2);
+
+ if (p1->p_pid > p2->p_pid)
+ return (p1);
+ return (p2);
+}
char *
get_proc_name(int fd, char *tty)
@@ -59,17 +107,14 @@ retry:
return (NULL);
len = (len * 5) / 4;
- if ((newbuf = realloc(buf, len)) == NULL) {
- free(buf);
- return (NULL);
- }
+ if ((newbuf = realloc(buf, len)) == NULL)
+ goto error;
buf = newbuf;
if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) == -1) {
if (errno == ENOMEM)
goto retry;
- free(buf);
- return (NULL);
+ goto error;
}
bestp = NULL;
@@ -77,57 +122,10 @@ retry:
if (buf[i].kp_eproc.e_tdev != sb.st_rdev)
continue;
p = &buf[i].kp_proc;
- if (bestp == NULL) {
- bestp = p;
- continue;
- }
-
- if (is_runnable(p) && !is_runnable(bestp)) {
- bestp = p;
- continue;
- } else if (!is_runnable(p) && is_runnable(bestp))
- continue;
-
- if (!is_stopped(p) && is_stopped(bestp)) {
- bestp = p;
- continue;
- } else if (is_stopped(p) && !is_stopped(bestp))
- continue;
-
- if (p->p_estcpu > bestp->p_estcpu) {
- bestp = p;
- continue;
- } else if (p->p_estcpu < bestp->p_estcpu)
- continue;
-
- if (p->p_slptime < bestp->p_slptime) {
- bestp = p;
- continue;
- } else if (p->p_slptime > bestp->p_slptime)
- continue;
-
- if (p->p_flag & P_SINTR && !(bestp->p_flag & P_SINTR)) {
- bestp = p;
- continue;
- } else if (!(p->p_flag & P_SINTR) && bestp->p_flag & P_SINTR)
- continue;
-
- if (LIST_FIRST(&p->p_children) == NULL &&
- LIST_FIRST(&bestp->p_children) != NULL) { /* XXX ugh */
- bestp = p;
- continue;
- } else if (LIST_FIRST(&p->p_children) != NULL &&
- LIST_FIRST(&bestp->p_children) == NULL)
- continue;
-
- if (strcmp(p->p_comm, bestp->p_comm) < 0) {
- bestp = p;
- continue;
- } else if (strcmp(p->p_comm, bestp->p_comm) > 0)
- continue;
-
- if (p->p_pid > bestp->p_pid)
- bestp = p;
+ if (bestp == NULL)
+ bestp = &buf[i].kp_proc;
+ else
+ bestp = cmp_procs(&buf[i].kp_proc, bestp);
}
name = NULL;
@@ -136,4 +134,8 @@ retry:
free(buf);
return (name);
+
+error:
+ free(buf);
+ return (NULL);
}