aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Whitley <markw@lineo.com>2001-03-03 00:44:55 +0000
committerMark Whitley <markw@lineo.com>2001-03-03 00:44:55 +0000
commit9ead68975c05100e4d1fda0b750fbc3688e68cfd (patch)
treeae3a45ace62693d2b64c9aa1e7248f710de38fb4
parent323434be429554d41f3f4764c34fd85bfbeed79a (diff)
downloadbusybox-w32-9ead68975c05100e4d1fda0b750fbc3688e68cfd.tar.gz
busybox-w32-9ead68975c05100e4d1fda0b750fbc3688e68cfd.tar.bz2
busybox-w32-9ead68975c05100e4d1fda0b750fbc3688e68cfd.zip
Added some words on use of getopt in applets.
-rw-r--r--docs/contributing.txt5
-rw-r--r--docs/style-guide.txt46
2 files changed, 50 insertions, 1 deletions
diff --git a/docs/contributing.txt b/docs/contributing.txt
index 696e63c2c..d9a7d3d00 100644
--- a/docs/contributing.txt
+++ b/docs/contributing.txt
@@ -197,6 +197,11 @@ Janitorial Work
197 197
198These are dirty jobs, but somebody's gotta do 'em. 198These are dirty jobs, but somebody's gotta do 'em.
199 199
200 - Converting applets to use getopt() for option processing. Type 'grep -L
201 getopt *.c' to get a listing of the applets that currently don't use
202 getopt. If a .c file processes no options, it should have a line that
203 reads: /* no options, no getopt */ somewhere in the file.
204
200 - Security audits: 205 - Security audits:
201 http://www.securityfocus.com/frames/?content=/forums/secprog/secure-programming.html 206 http://www.securityfocus.com/frames/?content=/forums/secprog/secure-programming.html
202 207
diff --git a/docs/style-guide.txt b/docs/style-guide.txt
index 1f06662ac..b4c3bac02 100644
--- a/docs/style-guide.txt
+++ b/docs/style-guide.txt
@@ -26,7 +26,9 @@ Here is the order in which code should be laid out in a file:
26 - commented author name and email address(es) 26 - commented author name and email address(es)
27 - commented GPL boilerplate 27 - commented GPL boilerplate
28 - commented longer description / notes for the program (if needed) 28 - commented longer description / notes for the program (if needed)
29 - #includes and #defines 29 - #includes of .h files with angle brackets (<>) around them
30 - #includes of .h files with quotes ("") around them
31 - #defines (if any, note the section below titled "Avoid the Preprocessor")
30 - const and global variables 32 - const and global variables
31 - function declarations (if necessary) 33 - function declarations (if necessary)
32 - function implementations 34 - function implementations
@@ -607,3 +609,45 @@ illustrates emphasizing logical blocks:
607 /* clean up */ 609 /* clean up */
608 free(line); 610 free(line);
609 } 611 }
612
613
614Processing Options with getopt
615~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
616
617If your applet needs to process command-line switches, please use getopt() to
618do so. Numerous examples can be seen in many of the existing applets, but
619basically it boils down to two things: at the top of the .c file, have this
620line in the midst of your #includes:
621
622 #include <getopt.h>
623
624And a code block similar to the following near the top of your applet_main()
625routine:
626
627 while ((opt = getopt(argc, argv, "abc")) > 0) {
628 switch (opt) {
629 case 'a':
630 do_a_opt = 1;
631 break;
632 case 'b':
633 do_b_opt = 1;
634 break;
635 case 'c':
636 do_c_opt = 1;
637 break;
638 default:
639 show_usage(); /* in utility.c */
640 }
641 }
642
643If your applet takes no options (such as 'init'), there should be a line
644somewhere in the file reads:
645
646 /* no options, no getopt */
647
648That way, when people go grepping to see which applets need to be converted to
649use getopt, they won't get false positives.
650
651Additional Note: Do not use the getopt_long library function and do not try to
652hand-roll your own long option parsing. Busybox applets should only support
653short options, plus explanations and examples in usage.h.