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$
27 *
28 */
29
30#include <stdio.h>
31#include <string.h>
32#include <sys/types.h>
33#include <dirent.h>
34
35/*
36 * Ok, I could extract almost anything from /proc, but I'm too lazy...
37 * I think it will suffice for now.
38 */
39
40int
41main(int argc, char *argv[])
42{
43	DIR *d;
44	struct dirent *e;
45	FILE *fd;
46	char buf[100];
47	char *tok, *sep=" ", *sep1=",";
48	char *name, *pid, *ppid, *uid, *gid;
49	char *pgid, *sid, *tty, *cred;
50	char *major, *minor;
51	char con[10];
52
53	d=opendir("/proc");
54	printf(" PID   PPID  TTY  COMMAND\n");
55	while((e=readdir(d))!=NULL) {
56		/* Skip '.' and '..' */
57		if(e->d_name[0]=='.') continue;
58		/* Skip 'curproc' - it's us */
59		if(e->d_name[0]=='c') continue;
60		sprintf(buf,"/proc/%s/status",e->d_name);
61		fd=fopen(buf,"r");
62		fgets(buf,99,fd);
63		fclose(fd);
64		name=strtok(buf,sep);
65		pid=strtok(NULL,sep);
66		ppid=strtok(NULL,sep);
67		pgid=strtok(NULL,sep);
68		sid=strtok(NULL,sep);
69		tty=strtok(NULL,sep);
70		tok=strtok(NULL,sep); /* flags */
71		tok=strtok(NULL,sep); /* start */
72		tok=strtok(NULL,sep); /* user time */
73		tok=strtok(NULL,sep); /* system time */
74		tok=strtok(NULL,sep); /* wchan */
75		cred=strtok(NULL,sep); /* credentials */
76		major=strtok(tty,sep1);
77		minor=strtok(NULL,sep1);
78		if(strcmp(minor,"-1")==0) {
79			minor="?";
80		}
81		if(strcmp(major,"-1")==0) {
82			major="?";
83		} else if(strcmp(major,"12")==0) {
84			major="v";
85		} else if(strcmp(major,"0")==0) {
86			major="con";
87			minor="-";
88		} else if(strcmp(major,"5")==0) {
89			major="p";
90		} else major="x";
91		if((strcmp(major,"v")==0) && (strcmp(minor,"255")==0)) {
92			major="con";
93			minor="-";
94		}
95		sprintf(con,"%s%s",major,minor);
96		printf("%5s %5s %4s (%s)\n",pid,ppid,con,name);
97
98	}
99	closedir(d);
100	exit(0);
101}
102