138589Sabial/*- 238589Sabial * Copyright (c) 1998 Andrzej Bialecki 338589Sabial * All rights reserved. 438589Sabial * 538589Sabial * Redistribution and use in source and binary forms, with or without 638589Sabial * modification, are permitted provided that the following conditions 738589Sabial * are met: 838589Sabial * 1. Redistributions of source code must retain the above copyright 938589Sabial * notice, this list of conditions and the following disclaimer. 1038589Sabial * 2. Redistributions in binary form must reproduce the above copyright 1138589Sabial * notice, this list of conditions and the following disclaimer in the 1238589Sabial * documentation and/or other materials provided with the distribution. 1338589Sabial * 1438589Sabial * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1538589Sabial * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1638589Sabial * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1738589Sabial * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1838589Sabial * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1938589Sabial * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2038589Sabial * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2138589Sabial * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2238589Sabial * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2338589Sabial * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2438589Sabial * SUCH DAMAGE. 2538589Sabial * 2650479Speter * $FreeBSD$ 2738589Sabial * 2838589Sabial */ 2938589Sabial 3038589Sabial#include <stdio.h> 3138589Sabial#include <string.h> 3238589Sabial#include <sys/types.h> 3338589Sabial#include <dirent.h> 3438589Sabial 3538589Sabial/* 3638589Sabial * Ok, I could extract almost anything from /proc, but I'm too lazy... 3738589Sabial * I think it will suffice for now. 3838589Sabial */ 3938589Sabial 4038589Sabialint 4138589Sabialmain(int argc, char *argv[]) 4238589Sabial{ 4338589Sabial DIR *d; 4438589Sabial struct dirent *e; 4538589Sabial FILE *fd; 4638589Sabial char buf[100]; 4738589Sabial char *tok, *sep=" ", *sep1=","; 4838589Sabial char *name, *pid, *ppid, *uid, *gid; 4938589Sabial char *pgid, *sid, *tty, *cred; 5038589Sabial char *major, *minor; 5138589Sabial char con[10]; 5238589Sabial 5338589Sabial d=opendir("/proc"); 5438589Sabial printf(" PID PPID TTY COMMAND\n"); 5538589Sabial while((e=readdir(d))!=NULL) { 5638589Sabial /* Skip '.' and '..' */ 5738589Sabial if(e->d_name[0]=='.') continue; 5838589Sabial /* Skip 'curproc' - it's us */ 5938589Sabial if(e->d_name[0]=='c') continue; 6038589Sabial sprintf(buf,"/proc/%s/status",e->d_name); 6138589Sabial fd=fopen(buf,"r"); 6238589Sabial fgets(buf,99,fd); 6338589Sabial fclose(fd); 6438589Sabial name=strtok(buf,sep); 6538589Sabial pid=strtok(NULL,sep); 6638589Sabial ppid=strtok(NULL,sep); 6738589Sabial pgid=strtok(NULL,sep); 6838589Sabial sid=strtok(NULL,sep); 6938589Sabial tty=strtok(NULL,sep); 7038589Sabial tok=strtok(NULL,sep); /* flags */ 7138589Sabial tok=strtok(NULL,sep); /* start */ 7238589Sabial tok=strtok(NULL,sep); /* user time */ 7338589Sabial tok=strtok(NULL,sep); /* system time */ 7438589Sabial tok=strtok(NULL,sep); /* wchan */ 7538589Sabial cred=strtok(NULL,sep); /* credentials */ 7638589Sabial major=strtok(tty,sep1); 7738589Sabial minor=strtok(NULL,sep1); 7838589Sabial if(strcmp(minor,"-1")==0) { 7938589Sabial minor="?"; 8038589Sabial } 8138589Sabial if(strcmp(major,"-1")==0) { 8238589Sabial major="?"; 8338589Sabial } else if(strcmp(major,"12")==0) { 8438589Sabial major="v"; 8538589Sabial } else if(strcmp(major,"0")==0) { 8638589Sabial major="con"; 8738589Sabial minor="-"; 8838589Sabial } else if(strcmp(major,"5")==0) { 8938589Sabial major="p"; 9038589Sabial } else major="x"; 9138589Sabial if((strcmp(major,"v")==0) && (strcmp(minor,"255")==0)) { 9238589Sabial major="con"; 9338589Sabial minor="-"; 9438589Sabial } 9538589Sabial sprintf(con,"%s%s",major,minor); 9638589Sabial printf("%5s %5s %4s (%s)\n",pid,ppid,con,name); 9738589Sabial 9838589Sabial } 9938589Sabial closedir(d); 10038589Sabial exit(0); 10138589Sabial} 102