summaryrefslogtreecommitdiffstats
path: root/patches/procps-3.2.8/0050-vmstat_headers.patch
blob: 8434d24682a60fc091258143632cee56cf07f254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
From a45013a749f144819a9a10cb5b5bb2455ca04703 Mon Sep 17 00:00:00 2001
From: http://ftp.de.debian.org/debian/pool/main/p/procps/procps_3.2.8-8.debian.tar.gz <info@debian.org>
Date: Fri, 19 Mar 2010 21:58:46 +0100
Subject: [PATCH 50/70] vmstat_headers

getopt replacement
Author: Liu Xing <liuxing@cn.fujitsu.com>
Author: Michael Tokarev <mjt@corpit.ru>
Bug-Debian: http://bugs.debian.org/436805
Bug-Debian: http://bugs.debian.org/408088
---
 vmstat.c |   80 ++++++++++++++++++++++++++-----------------------------------
 1 files changed, 34 insertions(+), 46 deletions(-)

diff --git a/vmstat.c b/vmstat.c
index f022928..e0d3c23 100644
--- a/vmstat.c
+++ b/vmstat.c
@@ -29,14 +29,15 @@
 #include "proc/sysinfo.h"
 #include "proc/version.h"
 
-static unsigned long dataUnit=1024;
-static char szDataUnit [16];
 #define UNIT_B        1
 #define UNIT_k        1000
 #define UNIT_K        1024
 #define UNIT_m        1000000
 #define UNIT_M        1048576
 
+static unsigned long dataUnit=UNIT_K;
+static char szDataUnit[3] = "K";
+
 #define VMSTAT        0
 #define DISKSTAT      0x00000001
 #define VMSUMSTAT     0x00000002
@@ -354,6 +355,7 @@ static void diskformat(void){
   if ((fDiskstat=fopen("/proc/diskstats", "rb"))){
     fclose(fDiskstat);
     ndisks=getdiskstat(&disks,&partitions);
+    if (!moreheaders) diskheader();
     for(k=0; k<ndisks; k++){
       if (moreheaders && ((k%height)==0)) diskheader();
       printf(format,
@@ -424,6 +426,7 @@ static void slabformat (void){
     return;
   }
 
+  if (!moreheaders) slabheader();
   nSlab = getslabinfo(&slabs);
   for(k=0; k<nSlab; k++){
     if (moreheaders && ((k%height)==0)) slabheader();
@@ -582,12 +585,10 @@ static int winhi(void) {
 ////////////////////////////////////////////////////////////////////////////
 
 int main(int argc, char *argv[]) {
-  char partition[16];
-  argc=0; /* redefined as number of integer arguments */
-  for (argv++;*argv;argv++) {
-    if ('-' ==(**argv)) {
-      switch (*(++(*argv))) {
-    
+  char *partition = NULL;
+  int c;
+
+  while((c = getopt(argc, argv, "VdafmDnp:S:s")) != EOF) switch(c) {
       case 'V':
 	display_version();
 	exit(0);
@@ -603,7 +604,7 @@ int main(int argc, char *argv[]) {
 	fork_format();
         exit(0);
       case 'm':
-        statMode |= SLABSTAT; 	
+        statMode |= SLABSTAT;
 	break;
       case 'D':
         statMode |= DISKSUMSTAT; 	
@@ -614,53 +615,40 @@ int main(int argc, char *argv[]) {
         break;
       case 'p':
         statMode |= PARTITIONSTAT;
-	if (argv[1]){
-	  char *cp = *++argv;
-	  if(!memcmp(cp,"/dev/",5)) cp += 5;
-	  snprintf(partition, sizeof partition, "%s", cp);
-	}else{
-	  fprintf(stderr, "-p requires an argument\n");
-          exit(EXIT_FAILURE);
-	}
+	partition = optarg;
+	if (memcmp(partition, "/dev/", 5) == 0) partition += 5;
         break;
       case 'S':
-	if (argv[1]){
-	      ++argv;
-	 	if (!strcmp(*argv, "k")) dataUnit=UNIT_k;
-	 	else if (!strcmp(*argv, "K")) dataUnit=UNIT_K;
-	 	else if (!strcmp(*argv, "m")) dataUnit=UNIT_m;
-	 	else if (!strcmp(*argv, "M")) dataUnit=UNIT_M;
-		else {fprintf(stderr, "-S requires k, K, m or M (default is kb)\n");
-		     exit(EXIT_FAILURE);
-		}
-		strcpy(szDataUnit, *argv);
-	 }else {fprintf(stderr, "-S requires an argument\n");
-		exit(EXIT_FAILURE);
-	 }
+	switch(optarg[0]) {
+	case 'b': case 'B': dataUnit = UNIT_B; break;
+	case 'k': dataUnit = UNIT_k; break;
+	case 'K': dataUnit = UNIT_K; break;
+	case 'm': dataUnit = UNIT_m; break;
+	case 'M': dataUnit = UNIT_M; break;
+	default:
+	  fprintf(stderr, "-S requires k, K, m or M (default is kb)\n");
+	  exit(EXIT_FAILURE);
+	}
+	szDataUnit[0] = optarg[0];
 	break;
       case 's':
-        statMode |= VMSUMSTAT; 	
+        statMode |= VMSUMSTAT;
 	break;
       default:
 	/* no other aguments defined yet. */
 	usage();
-      }
-   }else{
-      argc++;
-      switch (argc) {
-      case 1:
-        if ((sleep_time = atoi(*argv)) == 0)
+  }
+
+  if (optind < argc) {
+    if ((sleep_time = atoi(argv[optind++])) == 0)
          usage();
-       num_updates = ULONG_MAX;
-       break;
-      case 2:
-        num_updates = atol(*argv);
-       break;
-      default:
-       usage();
-      } /* switch */
+    num_updates = ULONG_MAX;
   }
-}
+  if (optind < argc)
+    num_updates = atol(argv[optind++]);
+  if (optind < argc)
+     usage();
+
   if (moreheaders) {
       int tmp=winhi()-3;
       height=((tmp>0)?tmp:22);
-- 
1.7.0