ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/sgps/sgps.c
Revision: 1.3
Committed: Mon Apr 25 11:01:19 2005 UTC (19 years, 1 month ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +0 -0 lines
State: FILE REMOVED
Log Message:
Nuke sgps - Pete's decided it's actually rather pointless as it doesn't
offer anything that other ps programs don't.

File Contents

# User Rev Content
1 pajs 1.1 #include <stdio.h>
2     #include <stdlib.h>
3     #include <unistd.h>
4     #include <strings.h>
5     #include <pwd.h>
6     #include "statgrab.h"
7    
8 pajs 1.2 char usage(char *progname){
9     fprintf(stderr," Usage: %s [-efAnh] [-u user] [-o sort]\n\n", progname);
10     fprintf(stderr, "\t-h\tDisplays this help\n");
11     fprintf(stderr, "\t-e\tAll processes\n");
12     fprintf(stderr, "\t-f\tFull listing\n");
13     fprintf(stderr, "\t-a\tAll processes (same as -e)\n");
14     fprintf(stderr, "\t-n\tDont look up usernames\n");
15     fprintf(stderr, "\t-u\tLook for just this user\n");
16     fprintf(stderr, "\t-o\tSorting method. Valid are cpu, mem, pid, uid, gid and size\n");
17     fprintf(stderr, "\n");
18     exit(1);
19     }
20    
21 pajs 1.1 char *size_conv(long long number){
22     char type[] = {'B', 'K', 'M', 'G', 'T'};
23     int x=0;
24     static char string[10];
25    
26     for(;x<5;x++){
27     if( (number/1024) < (100)) {
28     break;
29     }
30     number = (number/1024);
31     }
32    
33     snprintf(string, 10, "%lld%c", number, type[x]);
34     return string;
35    
36     }
37    
38     char *username(uid_t uid){
39     static struct passwd *pass;
40    
41     pass = getpwuid(uid);
42     if(pass == NULL){
43     return "NULL";
44     }
45    
46     return pass->pw_name;
47     }
48    
49     char *proc_state(sg_process_state state){
50     static char *pstat = "Unk";
51    
52     if(state == SG_PROCESS_STATE_RUNNING){
53     pstat="Run";
54     }
55    
56     if(state == SG_PROCESS_STATE_SLEEPING){
57     pstat="Sleep";
58     }
59    
60     if(state == SG_PROCESS_STATE_STOPPED){
61     pstat="Stop";
62     }
63    
64     if(state == SG_PROCESS_STATE_ZOMBIE){
65     pstat="Zomb";
66     }
67    
68     if(state == SG_PROCESS_STATE_UNKNOWN){
69     pstat="Unk";
70     }
71    
72     return pstat;
73     }
74    
75     int main(int argc, char **argv){
76     extern char *optarg;
77     int c;
78     int x;
79     int full=0;
80     int detailed=0;
81     int nolookup=0;
82 pajs 1.2 struct passwd *pwd_ent;
83     uid_t uid = -1;
84 pajs 1.1
85     int (*sortby_ptr)(const void *va, const void *vb);
86    
87     int entries;
88     extern char *optarg;
89    
90    
91     sg_process_stats *sg_proc = NULL;
92     /* Default behaviour - sort by pid */
93     sortby_ptr = sg_process_compare_pid;
94    
95 pajs 1.2 while ((c = getopt(argc, argv, "hneAfU:u:o:")) != -1){
96 pajs 1.1 switch (c){
97     case 'e':
98     case 'A':
99     full=1;
100     break;
101     case 'f':
102     detailed=1;
103     break;
104     case 'n':
105     nolookup=1;
106     break;
107 pajs 1.2 case 'U':
108 pajs 1.1 case 'u':
109 pajs 1.2 pwd_ent = getpwnam(optarg);
110     if (pwd_ent == NULL){
111     fprintf(stderr, "Error: user %s does not exist\n", optarg);
112     exit(1);
113     }
114     uid = pwd_ent->pw_uid;
115 pajs 1.1 break;
116     case 'o':
117     if(!strncasecmp(optarg, "cpu", 3)){
118     sortby_ptr = sg_process_compare_cpu;
119     break;
120     }
121     if(!strncasecmp(optarg, "size", 3)){
122     sortby_ptr = sg_process_compare_size;
123     break;
124     }
125     if(!strncasecmp(optarg, "pid", 3)){
126     sortby_ptr = sg_process_compare_pid;
127     break;
128     }
129     if(!strncasecmp(optarg, "mem", 3)){
130     sortby_ptr = sg_process_compare_res;
131     break;
132     }
133     if(!strncasecmp(optarg, "res", 3)){
134     sortby_ptr = sg_process_compare_res;
135     break;
136     }
137     if(!strncasecmp(optarg, "uid", 3)){
138     sortby_ptr = sg_process_compare_uid;
139     break;
140     }
141     if(!strncasecmp(optarg, "gid", 3)){
142     sortby_ptr = sg_process_compare_gid;
143     break;
144     }
145 pajs 1.2 case 'h':
146 pajs 1.1 default:
147 pajs 1.2 usage(argv[0]);
148 pajs 1.1 break;
149 pajs 1.2 exit(1);
150 pajs 1.1 }
151     }
152    
153     /* Get the procs - and sort them */
154     sg_proc = sg_get_process_stats(&entries);
155     qsort(sg_proc, entries, sizeof *sg_proc, sortby_ptr);
156    
157 pajs 1.2 /* Print the headers out on stderr, so should someone want to use this in a script, they wont need to
158     * strip out the first line.
159     */
160 pajs 1.1 if (full){
161 pajs 1.2 fprintf(stderr, "%9s %6s %6s %6s %7s %7s %4s %s\n", "User", "pid", "parent", "state", "size", "res", "CPU", "Process");
162 pajs 1.1 }else{
163 pajs 1.2 fprintf(stderr, "%6s %7s %4s %s\n", "pid", "size", "CPU", "Process");
164 pajs 1.1 }
165    
166     for(x=0; x<entries; x++){
167     char *proc_out=NULL;
168     if (detailed) {
169     proc_out = sg_proc->proctitle;
170     }else{
171     proc_out = sg_proc->process_name;
172     }
173    
174 pajs 1.2 if((uid == -1) || (uid == sg_proc->uid)){
175     if(full){
176     printf("%9s %6d %6d %6s %7s %7s %2.2f %s\n", username(sg_proc->uid), (int)sg_proc->pid, (int)sg_proc->parent, proc_state(sg_proc->state), size_conv(sg_proc->proc_size), size_conv(sg_proc->proc_resident), sg_proc->cpu_percent, proc_out);
177     }else{
178     printf("%6d %7s %2.2f %s\n", (int)sg_proc->pid, size_conv(sg_proc->proc_size), sg_proc->cpu_percent, proc_out);
179     }
180 pajs 1.1 }
181    
182     sg_proc++;
183     }
184    
185     return 0;
186     }