OSDN Git Service

Fix no pic
[uclinux-h8/uClinux-dist.git] / user / procps / vmstat.c
1 // old: "Copyright 1994 by Henry Ware <al172@yfn.ysu.edu>. Copyleft same year."
2 // most code copyright 2002 Albert Cahalan
3 // 
4 // 27/05/2003 (Fabian Frederick) : Add unit conversion + interface
5 //                       Export proc/stat access to libproc
6 //                       Adapt vmstat helpfile
7 // 31/05/2003 (Fabian) : Add diskstat support (/libproc)
8 // June 2003 (Fabian) : -S <x> -s & -s -S <x> patch
9 // June 2003 (Fabian) : -Adding diskstat against 3.1.9, slabinfo
10 //                       -patching 'header' in disk & slab
11 // July 2003 (Fabian) : -Adding disk partition output
12 //                      -Adding disk table
13 //                      -Syncing help / usage
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <termios.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <sys/dir.h>
27 #include <dirent.h>
28
29 #include "proc/sysinfo.h"
30 #include "proc/version.h"
31
32 static unsigned long dataUnit=1024;
33 static char szDataUnit [16];
34 #define UNIT_B        1
35 #define UNIT_k        1000
36 #define UNIT_K        1024
37 #define UNIT_m        1000000
38 #define UNIT_M        1048576
39
40 #define VMSTAT        0
41 #define DISKSTAT      0x00000001
42 #define VMSUMSTAT     0x00000002
43 #define SLABSTAT      0x00000004
44 #define PARTITIONSTAT 0x00000008
45 #define DISKSUMSTAT   0x00000010
46
47 static int statMode=VMSTAT;
48
49 #define FALSE 0
50 #define TRUE 1
51
52 static int a_option; /* "-a" means "show active/inactive" */
53
54 static unsigned sleep_time = 1;
55 static unsigned long num_updates;
56
57 static unsigned int height;   // window height
58 static unsigned int moreheaders=TRUE;
59
60
61 /////////////////////////////////////////////////////////////////////////
62
63 static void usage(void) NORETURN;
64 static void usage(void) {
65   fprintf(stderr,"usage: vmstat [-V] [-n] [delay [count]]\n");
66   fprintf(stderr,"              -V prints version.\n");
67   fprintf(stderr,"              -n causes the headers not to be reprinted regularly.\n");
68   fprintf(stderr,"              -a print inactive/active page stats.\n");
69   fprintf(stderr,"              -d prints disk statistics\n");
70   fprintf(stderr,"              -D prints disk table\n");
71   fprintf(stderr,"              -p prints disk partition statistics\n");
72   fprintf(stderr,"              -s prints vm table\n");
73   fprintf(stderr,"              -m prints slabinfo\n");
74   fprintf(stderr,"              -S unit size\n");
75   fprintf(stderr,"              delay is the delay between updates in seconds. \n");
76   fprintf(stderr,"              unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)\n");
77   fprintf(stderr,"              count is the number of updates.\n");
78   exit(EXIT_FAILURE);
79 }
80
81 /////////////////////////////////////////////////////////////////////////////
82
83 #if 0
84 // produce:  "  6  ", "123  ", "123k ", etc.
85 static int format_1024(unsigned long long val64, char *restrict dst){
86   unsigned oldval;
87   const char suffix[] = " kmgtpe";
88   unsigned level = 0;
89   unsigned val32;
90
91   if(val64 < 1000){   // special case to avoid "6.0  " when plain "  6  " would do
92     val32 = val64;
93     return sprintf(dst,"%3u  ",val32);
94   }
95
96   while(val64 > 0xffffffffull){
97     level++;
98     val64 /= 1024;
99   }
100
101   val32 = val64;
102
103   while(val32 > 999){
104     level++;
105     oldval = val32;
106     val32 /= 1024;
107   }
108
109   if(val32 < 10){
110     unsigned fract = (oldval % 1024) * 10 / 1024;
111     return sprintf(dst, "%u.%u%c ", val32, fract, suffix[level]);
112   }
113   return sprintf(dst, "%3u%c ", val32, suffix[level]);
114 }
115
116
117 // produce:  "  6  ", "123  ", "123k ", etc.
118 static int format_1000(unsigned long long val64, char *restrict dst){
119   unsigned oldval;
120   const char suffix[] = " kmgtpe";
121   unsigned level = 0;
122   unsigned val32;
123
124   if(val64 < 1000){   // special case to avoid "6.0  " when plain "  6  " would do
125     val32 = val64;
126     return sprintf(dst,"%3u  ",val32);
127   }
128
129   while(val64 > 0xffffffffull){
130     level++;
131     val64 /= 1000;
132   }
133
134   val32 = val64;
135
136   while(val32 > 999){
137     level++;
138     oldval = val32;
139     val32 /= 1000;
140   }
141
142   if(val32 < 10){
143     unsigned fract = (oldval % 1000) / 100;
144     return sprintf(dst, "%u.%u%c ", val32, fract, suffix[level]);
145   }
146   return sprintf(dst, "%3u%c ", val32, suffix[level]);
147 }
148 #endif
149
150 ////////////////////////////////////////////////////////////////////////////
151
152 static void new_header(void){
153   printf("procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----\n");
154   printf(
155     "%2s %2s %6s %6s %6s %6s %4s %4s %5s %5s %4s %4s %2s %2s %2s %2s\n",
156     "r","b",
157     "swpd", "free", a_option?"inact":"buff", a_option?"active":"cache",
158     "si","so",
159     "bi","bo",
160     "in","cs",
161     "us","sy","id","wa"
162   );
163 }
164
165 ////////////////////////////////////////////////////////////////////////////
166
167 static unsigned long unitConvert(unsigned int size){
168  float cvSize;
169  cvSize=(float)size/dataUnit*((statMode==SLABSTAT)?1:1024);
170  return ((unsigned long) cvSize);
171 }
172
173 ////////////////////////////////////////////////////////////////////////////
174
175 static void new_format(void) {
176   const char format[]="%2u %2u %6lu %6lu %6lu %6lu %4u %4u %5u %5u %4u %4u %2u %2u %2u %2u\n";
177   unsigned int tog=0; /* toggle switch for cleaner code */
178   unsigned int i;
179   unsigned int hz = Hertz;
180   unsigned int running,blocked,dummy_1,dummy_2;
181   jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2], cpu_xxx[2], cpu_yyy[2], cpu_zzz[2];
182   jiff duse, dsys, didl, diow, dstl, Div, divo2;
183   unsigned long pgpgin[2], pgpgout[2], pswpin[2], pswpout[2];
184   unsigned int intr[2], ctxt[2];
185   unsigned int sleep_half; 
186   unsigned long kb_per_page = sysconf(_SC_PAGESIZE) / 1024ul;
187   int debt = 0;  // handle idle ticks running backwards
188
189   sleep_half=(sleep_time/2);
190   new_header();
191   meminfo();
192
193   getstat(cpu_use,cpu_nic,cpu_sys,cpu_idl,cpu_iow,cpu_xxx,cpu_yyy,cpu_zzz,
194           pgpgin,pgpgout,pswpin,pswpout,
195           intr,ctxt,
196           &running,&blocked,
197           &dummy_1, &dummy_2);
198
199   duse= *cpu_use + *cpu_nic; 
200   dsys= *cpu_sys + *cpu_xxx + *cpu_yyy;
201   didl= *cpu_idl;
202   diow= *cpu_iow;
203   dstl= *cpu_zzz;
204   Div= duse+dsys+didl+diow+dstl;
205   divo2= Div/2UL;
206   printf(format,
207          running, blocked,
208          unitConvert(kb_swap_used), unitConvert(kb_main_free),
209          unitConvert(a_option?kb_inactive:kb_main_buffers),
210          unitConvert(a_option?kb_active:kb_main_cached),
211          (unsigned)( (*pswpin  * unitConvert(kb_per_page) * hz + divo2) / Div ),
212          (unsigned)( (*pswpout * unitConvert(kb_per_page) * hz + divo2) / Div ),
213          (unsigned)( (*pgpgin                * hz + divo2) / Div ),
214          (unsigned)( (*pgpgout               * hz + divo2) / Div ),
215          (unsigned)( (*intr                  * hz + divo2) / Div ),
216          (unsigned)( (*ctxt                  * hz + divo2) / Div ),
217          (unsigned)( (100*duse                    + divo2) / Div ),
218          (unsigned)( (100*dsys                    + divo2) / Div ),
219          (unsigned)( (100*didl                    + divo2) / Div ),
220          (unsigned)( (100*diow                    + divo2) / Div ) /* ,
221          (unsigned)( (100*dstl                    + divo2) / Div ) */
222   );
223
224   for(i=1;i<num_updates;i++) { /* \\\\\\\\\\\\\\\\\\\\ main loop ////////////////// */
225     sleep(sleep_time);
226     if (moreheaders && ((i%height)==0)) new_header();
227     tog= !tog;
228
229     meminfo();
230
231     getstat(cpu_use+tog,cpu_nic+tog,cpu_sys+tog,cpu_idl+tog,cpu_iow+tog,cpu_xxx+tog,cpu_yyy+tog,cpu_zzz+tog,
232           pgpgin+tog,pgpgout+tog,pswpin+tog,pswpout+tog,
233           intr+tog,ctxt+tog,
234           &running,&blocked,
235           &dummy_1,&dummy_2);
236
237     duse= cpu_use[tog]-cpu_use[!tog] + cpu_nic[tog]-cpu_nic[!tog];
238     dsys= cpu_sys[tog]-cpu_sys[!tog] + cpu_xxx[tog]-cpu_xxx[!tog] + cpu_yyy[tog]-cpu_yyy[!tog];
239     didl= cpu_idl[tog]-cpu_idl[!tog];
240     diow= cpu_iow[tog]-cpu_iow[!tog];
241     dstl= cpu_zzz[tog]-cpu_zzz[!tog];
242
243     /* idle can run backwards for a moment -- kernel "feature" */
244     if(debt){
245       didl = (int)didl + debt;
246       debt = 0;
247     }
248     if( (int)didl < 0 ){
249       debt = (int)didl;
250       didl = 0;
251     }
252
253     Div= duse+dsys+didl+diow+dstl;
254     divo2= Div/2UL;
255     printf(format,
256            running, blocked,
257            unitConvert(kb_swap_used),unitConvert(kb_main_free),
258            unitConvert(a_option?kb_inactive:kb_main_buffers),
259            unitConvert(a_option?kb_active:kb_main_cached),
260            (unsigned)( ( (pswpin [tog] - pswpin [!tog])*unitConvert(kb_per_page)+sleep_half )/sleep_time ), /*si*/
261            (unsigned)( ( (pswpout[tog] - pswpout[!tog])*unitConvert(kb_per_page)+sleep_half )/sleep_time ), /*so*/
262            (unsigned)( (  pgpgin [tog] - pgpgin [!tog]             +sleep_half )/sleep_time ), /*bi*/
263            (unsigned)( (  pgpgout[tog] - pgpgout[!tog]             +sleep_half )/sleep_time ), /*bo*/
264            (unsigned)( (  intr   [tog] - intr   [!tog]             +sleep_half )/sleep_time ), /*in*/
265            (unsigned)( (  ctxt   [tog] - ctxt   [!tog]             +sleep_half )/sleep_time ), /*cs*/
266            (unsigned)( (100*duse+divo2)/Div ), /*us*/
267            (unsigned)( (100*dsys+divo2)/Div ), /*sy*/
268            (unsigned)( (100*didl+divo2)/Div ), /*id*/
269            (unsigned)( (100*diow+divo2)/Div )/*, //wa
270            (unsigned)( (100*dstl+divo2)/Div )  //st  */
271     );
272   }
273 }
274
275 ////////////////////////////////////////////////////////////////////////////
276
277 static void diskpartition_header(const char *partition_name){
278   printf("%-10s %10s %10s %10s %10s\n",partition_name, "reads  ", "read sectors", "writes   ", "requested writes");
279 }
280
281 ////////////////////////////////////////////////////////////////////////////
282
283 static int diskpartition_format(const char* partition_name){
284     FILE *fDiskstat;
285     struct disk_stat *disks;
286     struct partition_stat *partitions, *current_partition=NULL;
287     unsigned long ndisks, j, k, npartitions;
288     const char format[] = "%20u %10llu %10u %10u\n";
289
290     fDiskstat=fopen("/proc/diskstats","rb");
291     if(!fDiskstat){
292         fprintf(stderr, "Your kernel doesn't support diskstat. (2.5.70 or above required)\n"); 
293         exit(0);
294     }
295
296     fclose(fDiskstat);
297     ndisks=getdiskstat(&disks,&partitions);
298     npartitions=getpartitions_num(disks, ndisks);
299     for(k=0; k<npartitions; k++){
300        if(!strcmp(partition_name, partitions[k].partition_name)){
301                 current_partition=&(partitions[k]); 
302        }        
303     }
304     if(!current_partition){
305          return -1;
306     }
307     diskpartition_header(partition_name);
308     printf (format,
309        current_partition->reads,current_partition->reads_sectors,current_partition->writes,current_partition->requested_writes);
310     fflush(stdout);
311     free(disks);
312     free(partitions);
313     for(j=1; j<num_updates; j++){ 
314         if (moreheaders && ((j%height)==0)) diskpartition_header(partition_name);
315         sleep(sleep_time);
316         ndisks=getdiskstat(&disks,&partitions);
317         npartitions=getpartitions_num(disks, ndisks);
318         current_partition=NULL;
319         for(k=0; k<npartitions; k++){
320           if(!strcmp(partition_name, partitions[k].partition_name)){
321                   current_partition=&(partitions[k]); 
322           }     
323         }
324         if(!current_partition){
325            return -1;
326         }
327         printf (format,
328         current_partition->reads,current_partition->reads_sectors,current_partition->writes,current_partition->requested_writes);
329         fflush(stdout);
330         free(disks);
331         free(partitions);
332     }
333     return 0;
334 }
335
336 ////////////////////////////////////////////////////////////////////////////
337
338 static void diskheader(void){
339   printf("disk- ------------reads------------ ------------writes----------- -----IO------\n");
340
341   printf("%5s %6s %6s %7s %7s %6s %6s %7s %7s %6s %6s\n",
342          " ", "total", "merged","sectors","ms","total","merged","sectors","ms","cur","sec");
343
344 }
345
346 ////////////////////////////////////////////////////////////////////////////
347
348 static void diskformat(void){
349   FILE *fDiskstat;
350   struct disk_stat *disks;
351   struct partition_stat *partitions;
352   unsigned long ndisks,i,j,k;
353   const char format[]="%-5s %6u %6u %7llu %7u %6u %6u %7llu %7u %6u %6u\n";
354   if ((fDiskstat=fopen("/proc/diskstats", "rb"))){
355     fclose(fDiskstat);
356     ndisks=getdiskstat(&disks,&partitions);
357     for(k=0; k<ndisks; k++){
358       if (moreheaders && ((k%height)==0)) diskheader();
359       printf(format,
360         disks[k].disk_name,
361         disks[k].reads,
362         disks[k].merged_reads,
363         disks[k].reads_sectors,
364         disks[k].milli_reading,
365         disks[k].writes,
366         disks[k].merged_writes,
367         disks[k].written_sectors,
368         disks[k].milli_writing,
369         disks[k].inprogress_IO?disks[k].inprogress_IO/1000:0,
370         disks[k].milli_spent_IO?disks[k].milli_spent_IO/1000:0/*,
371         disks[i].weighted_milli_spent_IO/1000*/
372       );
373       fflush(stdout);
374     }
375     free(disks);
376     free(partitions);
377     for(j=1; j<num_updates; j++){ 
378       sleep(sleep_time);
379       ndisks=getdiskstat(&disks,&partitions);
380       for(i=0; i<ndisks; i++,k++){
381         if (moreheaders && ((k%height)==0)) diskheader();
382         printf(format,
383           disks[i].disk_name,
384           disks[i].reads,
385           disks[i].merged_reads,
386           disks[i].reads_sectors,
387           disks[i].milli_reading,
388           disks[i].writes,
389           disks[i].merged_writes,
390           disks[i].written_sectors,
391           disks[i].milli_writing,
392           disks[i].inprogress_IO?disks[i].inprogress_IO/1000:0,
393           disks[i].milli_spent_IO?disks[i].milli_spent_IO/1000:0/*,
394           disks[i].weighted_milli_spent_IO/1000*/
395         );
396         fflush(stdout);
397       }
398       free(disks);
399       free(partitions);
400     }
401   }else{
402     fprintf(stderr, "Your kernel doesn't support diskstat (2.5.70 or above required)\n"); 
403     exit(0);
404   } 
405 }
406
407 ////////////////////////////////////////////////////////////////////////////
408
409 static void slabheader(void){
410   printf("%-24s %6s %6s %6s %6s\n","Cache","Num", "Total", "Size", "Pages");
411 }
412
413 ////////////////////////////////////////////////////////////////////////////
414
415 static void slabformat (void){
416   FILE *fSlab;
417   struct slab_cache *slabs;
418   unsigned long nSlab,i,j,k;
419   const char format[]="%-24s %6u %6u %6u %6u\n";
420
421   fSlab=fopen("/proc/slabinfo", "rb");
422   if(!fSlab){
423     fprintf(stderr, "Your kernel doesn't support slabinfo.\n");    
424     return;
425   }
426
427   nSlab = getslabinfo(&slabs);
428   for(k=0; k<nSlab; k++){
429     if (moreheaders && ((k%height)==0)) slabheader();
430     printf(format,
431       slabs[k].name,
432       slabs[k].active_objs,
433       slabs[k].num_objs,
434       slabs[k].objsize,
435       slabs[k].objperslab
436     );
437   }
438   free(slabs);
439   for(j=1,k=1; j<num_updates; j++) { 
440     sleep(sleep_time);
441     nSlab = getslabinfo(&slabs);
442     for(i=0; i<nSlab; i++,k++){
443       if (moreheaders && ((k%height)==0)) slabheader();
444       printf(format,
445         slabs[i].name,
446         slabs[i].active_objs,
447         slabs[i].num_objs,
448         slabs[i].objsize,
449         slabs[i].objperslab
450       );
451     }
452     free(slabs);
453   }
454 }
455
456 ////////////////////////////////////////////////////////////////////////////
457
458 static void disksum_format(void) {
459
460   FILE *fDiskstat;
461   struct disk_stat *disks;
462   struct partition_stat *partitions;
463   int ndisks, i;
464   unsigned long reads, merged_reads, read_sectors, milli_reading, writes,
465                 merged_writes, written_sectors, milli_writing, inprogress_IO,
466                 milli_spent_IO, weighted_milli_spent_IO;
467
468   reads=merged_reads=read_sectors=milli_reading=writes=merged_writes= \
469   written_sectors=milli_writing=inprogress_IO=milli_spent_IO= \
470   weighted_milli_spent_IO=0;
471
472   if ((fDiskstat=fopen("/proc/diskstats", "rb"))){
473     fclose(fDiskstat);
474     ndisks=getdiskstat(&disks, &partitions);
475     printf("%13d disks \n", ndisks);
476     printf("%13d partitions \n", getpartitions_num(disks, ndisks));
477
478     for(i=0; i<ndisks; i++){
479          reads+=disks[i].reads;
480          merged_reads+=disks[i].merged_reads;
481          read_sectors+=disks[i].reads_sectors;
482          milli_reading+=disks[i].milli_reading;
483          writes+=disks[i].writes;
484          merged_writes+=disks[i].merged_writes;
485          written_sectors+=disks[i].written_sectors;
486          milli_writing+=disks[i].milli_writing;
487          inprogress_IO+=disks[i].inprogress_IO?disks[i].inprogress_IO/1000:0;
488          milli_spent_IO+=disks[i].milli_spent_IO?disks[i].milli_spent_IO/1000:0;
489       }
490
491     printf("%13lu total reads\n",reads);
492     printf("%13lu merged reads\n",merged_reads);
493     printf("%13lu read sectors\n",read_sectors);
494     printf("%13lu milli reading\n",milli_reading);
495     printf("%13lu writes\n",writes);
496     printf("%13lu merged writes\n",merged_writes);
497     printf("%13lu written sectors\n",written_sectors);
498     printf("%13lu milli writing\n",milli_writing);
499     printf("%13lu inprogress IO\n",inprogress_IO);
500     printf("%13lu milli spent IO\n",milli_spent_IO);
501
502     free(disks);
503     free(partitions);
504   }
505 }
506
507 ////////////////////////////////////////////////////////////////////////////
508
509 static void sum_format(void) {
510   unsigned int running, blocked, btime, processes;
511   jiff cpu_use, cpu_nic, cpu_sys, cpu_idl, cpu_iow, cpu_xxx, cpu_yyy, cpu_zzz;
512   unsigned long pgpgin, pgpgout, pswpin, pswpout;
513   unsigned int intr, ctxt;
514
515   meminfo();
516
517   getstat(&cpu_use, &cpu_nic, &cpu_sys, &cpu_idl,
518           &cpu_iow, &cpu_xxx, &cpu_yyy, &cpu_zzz,
519           &pgpgin, &pgpgout, &pswpin, &pswpout,
520           &intr, &ctxt,
521           &running, &blocked,
522           &btime, &processes);
523
524   printf("%13lu %s total memory\n", unitConvert(kb_main_total),szDataUnit);
525   printf("%13lu %s used memory\n", unitConvert(kb_main_used),szDataUnit);
526   printf("%13lu %s active memory\n", unitConvert(kb_active),szDataUnit);
527   printf("%13lu %s inactive memory\n", unitConvert(kb_inactive),szDataUnit);
528   printf("%13lu %s free memory\n", unitConvert(kb_main_free),szDataUnit);
529   printf("%13lu %s buffer memory\n", unitConvert(kb_main_buffers),szDataUnit);
530   printf("%13lu %s swap cache\n", unitConvert(kb_main_cached),szDataUnit);
531   printf("%13lu %s total swap\n", unitConvert(kb_swap_total),szDataUnit);
532   printf("%13lu %s used swap\n", unitConvert(kb_swap_used),szDataUnit);
533   printf("%13lu %s free swap\n", unitConvert(kb_swap_free),szDataUnit);
534   printf("%13Lu non-nice user cpu ticks\n", cpu_use);
535   printf("%13Lu nice user cpu ticks\n", cpu_nic);
536   printf("%13Lu system cpu ticks\n", cpu_sys);
537   printf("%13Lu idle cpu ticks\n", cpu_idl);
538   printf("%13Lu IO-wait cpu ticks\n", cpu_iow);
539   printf("%13Lu IRQ cpu ticks\n", cpu_xxx);
540   printf("%13Lu softirq cpu ticks\n", cpu_yyy);
541   printf("%13Lu stolen cpu ticks\n", cpu_zzz);
542   printf("%13lu pages paged in\n", pgpgin);
543   printf("%13lu pages paged out\n", pgpgout);
544   printf("%13lu pages swapped in\n", pswpin);
545   printf("%13lu pages swapped out\n", pswpout);
546   printf("%13u interrupts\n", intr);
547   printf("%13u CPU context switches\n", ctxt);
548   printf("%13u boot time\n", btime);
549   printf("%13u forks\n", processes);
550 }
551
552 ////////////////////////////////////////////////////////////////////////////
553
554 static void fork_format(void) {
555   unsigned int running, blocked, btime, processes;
556   jiff cpu_use, cpu_nic, cpu_sys, cpu_idl, cpu_iow, cpu_xxx, cpu_yyy, cpu_zzz;
557   unsigned long pgpgin, pgpgout, pswpin, pswpout;
558   unsigned int intr, ctxt;
559
560   getstat(&cpu_use, &cpu_nic, &cpu_sys, &cpu_idl,
561           &cpu_iow, &cpu_xxx, &cpu_yyy, &cpu_zzz,
562           &pgpgin, &pgpgout, &pswpin, &pswpout,
563           &intr, &ctxt,
564           &running, &blocked,
565           &btime, &processes);
566
567   printf("%13u forks\n", processes);
568 }
569
570 ////////////////////////////////////////////////////////////////////////////
571
572 static int winhi(void) {
573     struct winsize win;
574     int rows = 24;
575  
576     if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_row > 0)
577       rows = win.ws_row;
578  
579     return rows;
580 }
581
582 ////////////////////////////////////////////////////////////////////////////
583
584 int main(int argc, char *argv[]) {
585   char partition[16];
586   argc=0; /* redefined as number of integer arguments */
587   for (argv++;*argv;argv++) {
588     if ('-' ==(**argv)) {
589       switch (*(++(*argv))) {
590     
591       case 'V':
592         display_version();
593         exit(0);
594       case 'd':
595         statMode |= DISKSTAT;
596         break;
597       case 'a':
598         /* active/inactive mode */
599         a_option=1;
600         break;
601       case 'f':
602         // FIXME: check for conflicting args
603         fork_format();
604         exit(0);
605       case 'm':
606         statMode |= SLABSTAT;   
607         break;
608       case 'D':
609         statMode |= DISKSUMSTAT;        
610         break;
611       case 'n':
612         /* print only one header */
613         moreheaders=FALSE;
614         break;
615       case 'p':
616         statMode |= PARTITIONSTAT;
617         if (argv[1]){
618           char *cp = *++argv;
619           if(!memcmp(cp,"/dev/",5)) cp += 5;
620           snprintf(partition, sizeof partition, "%s", cp);
621         }else{
622           fprintf(stderr, "-p requires an argument\n");
623           exit(EXIT_FAILURE);
624         }
625         break;
626       case 'S':
627         if (argv[1]){
628               ++argv;
629                 if (!strcmp(*argv, "k")) dataUnit=UNIT_k;
630                 else if (!strcmp(*argv, "K")) dataUnit=UNIT_K;
631                 else if (!strcmp(*argv, "m")) dataUnit=UNIT_m;
632                 else if (!strcmp(*argv, "M")) dataUnit=UNIT_M;
633                 else {fprintf(stderr, "-S requires k, K, m or M (default is kb)\n");
634                      exit(EXIT_FAILURE);
635                 }
636                 strcpy(szDataUnit, *argv);
637          }else {fprintf(stderr, "-S requires an argument\n");
638                 exit(EXIT_FAILURE);
639          }
640         break;
641       case 's':
642         statMode |= VMSUMSTAT;  
643         break;
644       default:
645         /* no other aguments defined yet. */
646         usage();
647       }
648    }else{
649       argc++;
650       switch (argc) {
651       case 1:
652         if ((sleep_time = atoi(*argv)) == 0)
653          usage();
654        num_updates = ULONG_MAX;
655        break;
656       case 2:
657         num_updates = atol(*argv);
658        break;
659       default:
660        usage();
661       } /* switch */
662   }
663 }
664   if (moreheaders) {
665       int tmp=winhi()-3;
666       height=((tmp>0)?tmp:22);
667   }    
668   setlinebuf(stdout);
669   switch(statMode){
670         case(VMSTAT):        new_format();
671                              break;
672         case(VMSUMSTAT):     sum_format();
673                              break;
674         case(DISKSTAT):      diskformat();
675                              break;
676         case(PARTITIONSTAT): if(diskpartition_format(partition)==-1)
677                                   printf("Partition was not found\n");
678                              break;     
679         case(SLABSTAT):      slabformat();
680                              break;
681         case(DISKSUMSTAT):   disksum_format();  
682                              break;     
683         default:             usage();
684                              break;
685   }
686   return 0;
687 }
688
689