ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/sgps/sgps.c
(Generate patch)

Comparing projects/libstatgrab/src/sgps/sgps.c (file contents):
Revision 1.1 by pajs, Mon Apr 4 14:10:08 2005 UTC vs.
Revision 1.2 by pajs, Thu Apr 7 16:41:29 2005 UTC

# Line 5 | Line 5
5   #include <pwd.h>
6   #include "statgrab.h"
7  
8 + 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   char *size_conv(long long number){
22          char type[] = {'B', 'K', 'M', 'G', 'T'};
23          int x=0;
# Line 66 | Line 79 | int main(int argc, char **argv){
79          int full=0;
80          int detailed=0;
81          int nolookup=0;
82 <        char *user = NULL;
82 >        struct passwd *pwd_ent;
83 >        uid_t uid = -1;
84  
85          int (*sortby_ptr)(const void *va, const void *vb);
86  
# Line 78 | Line 92 | int main(int argc, char **argv){
92          /* Default behaviour - sort by pid */
93          sortby_ptr = sg_process_compare_pid;
94  
95 <        while ((c = getopt(argc, argv, "neAfu:o:")) != -1){
95 >        while ((c = getopt(argc, argv, "hneAfU:u:o:")) != -1){
96                  switch (c){
97                          case 'e':
98                          case 'A':
# Line 90 | Line 104 | int main(int argc, char **argv){
104                          case 'n':
105                                  nolookup=1;
106                                  break;
107 +                        case 'U':
108                          case 'u':
109 <                                user = strdup(optarg);
110 <                                if (user == NULL) exit(1);
109 >                                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                                  break;
116                          case 'o':
117                                  if(!strncasecmp(optarg, "cpu", 3)){
# Line 123 | Line 142 | int main(int argc, char **argv){
142                                          sortby_ptr = sg_process_compare_gid;
143                                          break;
144                                  }
145 +                        case 'h':
146                          default:
147 <                                sortby_ptr = sg_process_compare_cpu;
147 >                                usage(argv[0]);
148                                  break;
149 +                                exit(1);
150                  }
151          }
152  
# Line 133 | Line 154 | int main(int argc, char **argv){
154          sg_proc = sg_get_process_stats(&entries);
155          qsort(sg_proc, entries, sizeof *sg_proc, sortby_ptr);
156  
157 +        /* 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          if (full){
161 <                printf("%9s %6s %6s %6s %7s %7s %4s %s\n", "User", "pid", "parent", "state", "size", "res", "CPU", "Process");
161 >                fprintf(stderr, "%9s %6s %6s %6s %7s %7s %4s %s\n", "User", "pid", "parent", "state", "size", "res", "CPU", "Process");
162          }else{
163 <                printf("%6s %7s %4s %s\n", "pid", "size", "CPU", "Process");
163 >                fprintf(stderr, "%6s %7s %4s %s\n", "pid", "size", "CPU", "Process");
164          }
165  
166          for(x=0; x<entries; x++){
# Line 147 | Line 171 | int main(int argc, char **argv){
171                          proc_out = sg_proc->process_name;
172                  }
173  
174 <                if(full){
175 <                        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);
176 <                }else{
177 <                        printf("%6d %7s %2.2f %s\n", (int)sg_proc->pid, size_conv(sg_proc->proc_size), sg_proc->cpu_percent, proc_out);
174 >                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                  }
181  
182                  sg_proc++;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines