Skip to contentSkip to conversation

prlimit: add opt_id array to match short options to limit ids

PATCH Applied Bernhard Voelker 3 participants 3 messages Nov 14, 2011 – Nov 16, 2011 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 2/15 prlimit: add opt_id array to match short options to limit ids

[PATCH 02/15] prlimit: add opt_id array to match short options to limit ids

---
 sys-utils/prlimit.c |   54 
++++++++++++++++++++------------------------------
 1 files changed, 22 insertions(+), 32 deletions(-)
sys-utils/prlimit.c +22 -32
index fed4f38..525351a 100644
@@ -482,6 +482,26 @@ int main(int argc, char **argv)
 		{ "verbose",    no_argument, NULL, VERBOSE_OPTION },
 		{ NULL, 0, NULL, 0 }
 	};
+	
+	static const size_t opt_id[] = {
+		['c'] = CORE,
+		['d'] = DATA,
+		['e'] = NICE,
+		['f'] = FSIZE,
+		['i'] = SIGPENDING,
+		['l'] = MEMLOCK,
+		['m'] = RSS,
+		['n'] = NOFILE,
+		['q'] = MSGQUEUE,
+		['r'] = RTPRIO,
+		['s'] = STACK,
+		['t'] = CPU,
+		['u'] = NPROC,
+		['v'] = AS,
+		['x'] = LOCKS,
+		['y'] = RTTIME
+		/* h = help, o = output columns, p = pid, V = version */
+	};
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
@@ -498,53 +518,23 @@ int main(int argc, char **argv)
 				 longopts, NULL)) != -1) {
 		switch(opt) {
 		case 'c':
-			add_prlim(optarg, &lims[n++], CORE);
-			break;
 		case 'd':
-			add_prlim(optarg, &lims[n++], DATA);
-			break;
 		case 'e':
-			add_prlim(optarg, &lims[n++], NICE);
-			break;
 		case 'f':
-			add_prlim(optarg, &lims[n++], FSIZE);
-			break;
 		case 'i':
-			add_prlim(optarg, &lims[n++], SIGPENDING);
-			break;
 		case 'l':
-			add_prlim(optarg, &lims[n++], MEMLOCK);
-			break;
 		case 'm':
-			add_prlim(optarg, &lims[n++], RSS);
-			break;
 		case 'n':
-			add_prlim(optarg, &lims[n++], NOFILE);
-			break;
 		case 'q':
-			add_prlim(optarg, &lims[n++], MSGQUEUE);
-			break;
 		case 'r':
-			add_prlim(optarg, &lims[n++], RTPRIO);
-			break;
 		case 's':
-			add_prlim(optarg, &lims[n++], STACK);
-			break;
 		case 't':
-			add_prlim(optarg, &lims[n++], CPU);
-			break;
 		case 'u':
-			add_prlim(optarg, &lims[n++], NPROC);
-			break;
 		case 'v':
-			add_prlim(optarg, &lims[n++], AS);
-			break;
 		case 'x':
-			add_prlim(optarg, &lims[n++], LOCKS);
-			break;
 		case 'y':
-			add_prlim(optarg, &lims[n++], RTTIME);
-			break;
+				add_prlim(optarg, &lims[n++], opt_id[opt]);
+				break;
 
 		case 'p':
 			if (pid) /* we only work one pid at a time */
maintainer
#1 lore raw
Quoted 9 lines · Bernhard Voelker
sys-utils/prlimit.c +9 -0
+	static const size_t opt_id[] = {
+		['c'] = CORE,
+		['d'] = DATA,
+		['e'] = NICE,
+		['f'] = FSIZE,
+		['i'] = SIGPENDING,
+		['l'] = MEMLOCK,
+		['m'] = RSS,
+		['n'] = NOFILE,

sys-utils/prlimit.c int main(int argc, char **argv) · line 504

Quoted 10 lines
sys-utils/prlimit.c +10 -0
+		['q'] = MSGQUEUE,
+		['r'] = RTPRIO,
+		['s'] = STACK,
+		['t'] = CPU,
+		['u'] = NPROC,
+		['v'] = AS,
+		['x'] = LOCKS,
+		['y'] = RTTIME
+		/* h = help, o = output columns, p = pid, V = version */
+	};

Sorry, not applied. This change does not make the code more robust or readable and does not fix any error.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw
Quoted 15 lines · Karel Zak
Quoted 14 lines · Bernhard Voelker
sys-utils/prlimit.c +14 -0
+	static const size_t opt_id[] = {
+		['c'] = CORE,
+		['d'] = DATA,
+		['e'] = NICE,
+		['f'] = FSIZE,
+		['i'] = SIGPENDING,
+		['l'] = MEMLOCK,
+		['m'] = RSS,
+		['n'] = NOFILE,
+		['q'] = MSGQUEUE,
+		['r'] = RTPRIO,
+		['s'] = STACK,
+		['t'] = CPU,
+		['u'] = NPROC,
Quoted 8 lines

sys-utils/prlimit.c int main(int argc, char **argv) · line 504

Quoted 5 lines
sys-utils/prlimit.c +5 -0
+		['v'] = AS,
+		['x'] = LOCKS,
+		['y'] = RTTIME
+		/* h = help, o = output columns, p = pid, V = version */
+	};

Sorry, not applied. This change does not make the code more robust or readable and does not fix any error.

Later on in [PATCH 11/15], I needed do n++ in a separate statement in each case (because lims[] is no longer filled as arguments can be parsed but instead, there's one element per limit type.

@@ -573,7 +584,8 @@ int main(int argc, char **argv)
case 'v':
case 'x':
case 'y':
- add_prlim(optarg, &lims[n++], opt_id[opt]);
+ add_prlim(optarg, &lims[opt_id[opt]], opt_id[opt]);
+ n++;
break;

This additional statement made the switch/case rather huge.

Berny