Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / release / picobsd / tinyware / aps / main.c
1 /*-
2  * Copyright (c) 1998 Andrzej Bialecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/release/picobsd/tinyware/aps/main.c,v 1.2 1999/08/28 01:33:52 peter Exp $
27  * $DragonFly: src/release/picobsd/tinyware/aps/Attic/main.c,v 1.2 2003/06/17 04:27:20 dillon Exp $
28  *
29  */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35
36 /*
37  * Ok, I could extract almost anything from /proc, but I'm too lazy...
38  * I think it will suffice for now.
39  */
40
41 int
42 main(int argc, char *argv[])
43 {
44         DIR *d;
45         struct dirent *e;
46         FILE *fd;
47         char buf[100];
48         char *tok, *sep=" ", *sep1=",";
49         char *name, *pid, *ppid, *uid, *gid;
50         char *pgid, *sid, *tty, *cred;
51         char *major, *minor;
52         char con[10];
53
54         d=opendir("/proc");
55         printf(" PID   PPID  TTY  COMMAND\n");
56         while((e=readdir(d))!=NULL) {
57                 /* Skip '.' and '..' */
58                 if(e->d_name[0]=='.') continue;
59                 /* Skip 'curproc' - it's us */
60                 if(e->d_name[0]=='c') continue;
61                 sprintf(buf,"/proc/%s/status",e->d_name);
62                 fd=fopen(buf,"r");
63                 fgets(buf,99,fd);
64                 fclose(fd);
65                 name=strtok(buf,sep);
66                 pid=strtok(NULL,sep);
67                 ppid=strtok(NULL,sep);
68                 pgid=strtok(NULL,sep);
69                 sid=strtok(NULL,sep);
70                 tty=strtok(NULL,sep);
71                 tok=strtok(NULL,sep); /* flags */
72                 tok=strtok(NULL,sep); /* start */
73                 tok=strtok(NULL,sep); /* user time */
74                 tok=strtok(NULL,sep); /* system time */
75                 tok=strtok(NULL,sep); /* wchan */
76                 cred=strtok(NULL,sep); /* credentials */
77                 major=strtok(tty,sep1);
78                 minor=strtok(NULL,sep1);
79                 if(strcmp(minor,"-1")==0) {
80                         minor="?";
81                 }
82                 if(strcmp(major,"-1")==0) {
83                         major="?";
84                 } else if(strcmp(major,"12")==0) {
85                         major="v";
86                 } else if(strcmp(major,"0")==0) {
87                         major="con";
88                         minor="-";
89                 } else if(strcmp(major,"5")==0) {
90                         major="p";
91                 } else major="x";
92                 if((strcmp(major,"v")==0) && (strcmp(minor,"255")==0)) {
93                         major="con";
94                         minor="-";
95                 }
96                 sprintf(con,"%s%s",major,minor);
97                 printf("%5s %5s %4s (%s)\n",pid,ppid,con,name);
98
99         }
100         closedir(d);
101         exit(0);
102 }