blob: ba0cdbaa487e7f6579746c7d6101e9a3d8416bc4 [file] [log] [blame]
Mark Whitley40bfc762000-07-24 22:36:06 +00001Busybox Style Guide
2===================
3
4This document describes the coding style conventions used in Busybox. If you
5add a new file to Busybox or are editing an existing file, please format your
6code according to this style. If you are the maintainer of a file that does
7not follow these guidelines, please -- at your own convenience -- modify the
8file(s) you maintain to bring them into conformance with this style guide.
9Please note that this is a low priority task.
10
11To help you format the whitespace of your programs, an ".indent.pro" file is
12included in the main Busybox source directory that contains option flags to
13format code as per this style guide. This way you can run GNU indent on your
14files by typing 'indent myfile.c myfile.h' and it will magically apply all the
15right formatting rules to your file. Please _do_not_ run this on all the files
16in the directory, just your own.
17
Mark Whitley2368a382000-08-22 00:20:21 +000018
Mark Whitleyd58ff872000-11-22 19:25:39 +000019
Mark Whitley40bfc762000-07-24 22:36:06 +000020Declaration Order
21-----------------
22
23Here is the order in which code should be laid out in a file:
24
Mark Whitley9028e2c2000-11-17 21:28:39 +000025 - commented program name and one-line description
Mark Whitley40bfc762000-07-24 22:36:06 +000026 - commented author name and email address(es)
27 - commented GPL boilerplate
Mark Whitley9028e2c2000-11-17 21:28:39 +000028 - commented longer description / notes for the program (if needed)
Mark Whitley9ead6892001-03-03 00:44:55 +000029 - #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")
Mark Whitley9028e2c2000-11-17 21:28:39 +000032 - const and global variables
Mark Whitley40bfc762000-07-24 22:36:06 +000033 - function declarations (if necessary)
34 - function implementations
35
Mark Whitley2368a382000-08-22 00:20:21 +000036
Mark Whitleyd58ff872000-11-22 19:25:39 +000037
38Whitespace and Formatting
39-------------------------
Mark Whitley40bfc762000-07-24 22:36:06 +000040
Mark Whitley2368a382000-08-22 00:20:21 +000041This is everybody's favorite flame topic so let's get it out of the way right
42up front.
43
44
Mark Whitley9028e2c2000-11-17 21:28:39 +000045Tabs vs. Spaces in Line Indentation
Mark Whitleyd58ff872000-11-22 19:25:39 +000046~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mark Whitley2368a382000-08-22 00:20:21 +000047
48The preference in Busybox is to indent lines with tabs. Do not indent lines
49with spaces and do not indents lines using a mixture of tabs and spaces. (The
50indentation style in the Apache and Postfix source does this sort of thing:
51\s\s\s\sif (expr) {\n\tstmt; --ick.) The only exception to this rule is
52multi-line comments that use an asterisk at the beginning of each line, i.e.:
Mark Whitley40bfc762000-07-24 22:36:06 +000053
Mike Frysinger434ccd92006-07-05 22:19:21 +000054 \t/*
55 \t * This is a block comment.
56 \t * Note that it has multiple lines
57 \t * and that the beginning of each line has a tab plus a space
58 \t * except for the opening '/*' line where the slash
59 \t * is used instead of a space.
60 \t */
Mark Whitley40bfc762000-07-24 22:36:06 +000061
62Furthermore, The preference is that tabs be set to display at four spaces
63wide, but the beauty of using only tabs (and not spaces) at the beginning of
Mark Whitley9028e2c2000-11-17 21:28:39 +000064lines is that you can set your editor to display tabs at *whatever* number of
Mark Whitley40bfc762000-07-24 22:36:06 +000065spaces is desired and the code will still look fine.
66
67
Mark Whitley2368a382000-08-22 00:20:21 +000068Operator Spacing
69~~~~~~~~~~~~~~~~
70
71Put spaces between terms and operators. Example:
Mark Whitley40bfc762000-07-24 22:36:06 +000072
73 Don't do this:
74
75 for(i=0;i<num_items;i++){
76
77 Do this instead:
78
79 for (i = 0; i < num_items; i++) {
80
81 While it extends the line a bit longer, the spaced version is more
82 readable. An allowable exception to this rule is the situation where
83 excluding the spacing makes it more obvious that we are dealing with a
Mark Whitley9028e2c2000-11-17 21:28:39 +000084 single term (even if it is a compound term) such as:
Mark Whitley40bfc762000-07-24 22:36:06 +000085
86 if (str[idx] == '/' && str[idx-1] != '\\')
87
88 or
89
90 if ((argc-1) - (optind+1) > 0)
91
92
Mark Whitley2368a382000-08-22 00:20:21 +000093Bracket Spacing
94~~~~~~~~~~~~~~~
95
96If an opening bracket starts a function, it should be on the
Mark Whitley9028e2c2000-11-17 21:28:39 +000097next line with no spacing before it. However, if a bracket follows an opening
Mark Whitley40bfc762000-07-24 22:36:06 +000098control block, it should be on the same line with a single space (not a tab)
Mark Whitley9028e2c2000-11-17 21:28:39 +000099between it and the opening control block statement. Examples:
Mark Whitley40bfc762000-07-24 22:36:06 +0000100
101 Don't do this:
102
Mark Whitley9028e2c2000-11-17 21:28:39 +0000103 while (!done)
104 {
105
106 do
107 {
108
109 Don't do this either:
110
Mark Whitley40bfc762000-07-24 22:36:06 +0000111 while (!done){
Mark Whitley925edb82001-02-03 00:20:14 +0000112
Mark Whitley40bfc762000-07-24 22:36:06 +0000113 do{
114
Mark Whitley3680c582000-12-20 22:35:12 +0000115 And for heaven's sake, don't do this:
116
117 while (!done)
118 {
Mark Whitley925edb82001-02-03 00:20:14 +0000119
Mark Whitley3680c582000-12-20 22:35:12 +0000120 do
121 {
122
Mark Whitley40bfc762000-07-24 22:36:06 +0000123 Do this instead:
124
125 while (!done) {
Mark Whitley925edb82001-02-03 00:20:14 +0000126
Mark Whitley40bfc762000-07-24 22:36:06 +0000127 do {
128
Mike Frysinger0612b5f2006-02-24 01:18:24 +0000129Exceptions:
130
131 - if you have long logic statements that need to be wrapped, then uncuddling
132 the bracket to improve readability is allowed:
133
134 if (some_really_long_checks && some_other_really_long_checks \
135 && some_more_really_long_checks)
136 {
137 do_foo_now;
Mark Whitley2368a382000-08-22 00:20:21 +0000138
Mark Whitley925edb82001-02-03 00:20:14 +0000139Spacing around Parentheses
140~~~~~~~~~~~~~~~~~~~~~~~~~~
Mark Whitley2368a382000-08-22 00:20:21 +0000141
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000142Put a space between C keywords and left parens, but not between function names
143and the left paren that starts it's parameter list (whether it is being
144declared or called). Examples:
Mark Whitley2368a382000-08-22 00:20:21 +0000145
146 Don't do this:
147
148 while(foo) {
149 for(i = 0; i < n; i++) {
150
151 Do this instead:
152
153 while (foo) {
154 for (i = 0; i < n; i++) {
155
Mark Whitley9028e2c2000-11-17 21:28:39 +0000156 But do functions like this:
Mark Whitley2368a382000-08-22 00:20:21 +0000157
158 static int my_func(int foo, char bar)
159 ...
160 baz = my_func(1, 2);
161
Mark Whitley925edb82001-02-03 00:20:14 +0000162Also, don't put a space between the left paren and the first term, nor between
163the last arg and the right paren.
164
165 Don't do this:
166
167 if ( x < 1 )
168 strcmp( thisstr, thatstr )
169
170 Do this instead:
171
172 if (x < 1)
173 strcmp(thisstr, thatstr)
174
Mark Whitley2368a382000-08-22 00:20:21 +0000175
176Cuddled Elses
177~~~~~~~~~~~~~
178
Mark Whitley9028e2c2000-11-17 21:28:39 +0000179Also, please "cuddle" your else statements by putting the else keyword on the
180same line after the right bracket that closes an 'if' statement.
Mark Whitley40bfc762000-07-24 22:36:06 +0000181
182 Don't do this:
183
184 if (foo) {
185 stmt;
186 }
187 else {
188 stmt;
189 }
190
191 Do this instead:
192
193 if (foo) {
194 stmt;
195 } else {
196 stmt;
197 }
198
Mark Whitley9028e2c2000-11-17 21:28:39 +0000199The exception to this rule is if you want to include a comment before the else
200block. Example:
201
202 if (foo) {
203 stmts...
204 }
205 /* otherwise, we're just kidding ourselves, so re-frob the input */
206 else {
207 other_stmts...
208 }
209
Mark Whitley40bfc762000-07-24 22:36:06 +0000210
Mark Whitleyd58ff872000-11-22 19:25:39 +0000211
Mark Whitley40bfc762000-07-24 22:36:06 +0000212Variable and Function Names
213---------------------------
214
215Use the K&R style with names in all lower-case and underscores occasionally
Mark Whitley9028e2c2000-11-17 21:28:39 +0000216used to separate words (e.g., "variable_name" and "numchars" are both
Mark Whitley40bfc762000-07-24 22:36:06 +0000217acceptable). Using underscores makes variable and function names more readable
218because it looks like whitespace; using lower-case is easy on the eyes.
219
Mark Whitley3680c582000-12-20 22:35:12 +0000220 Frowned upon:
221
222 hitList
223 TotalChars
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000224 szFileName
225 pf_Nfol_TriState
Mark Whitley3680c582000-12-20 22:35:12 +0000226
227 Preferred:
228
229 hit_list
230 total_chars
231 file_name
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000232 sensible_name
Mark Whitley3680c582000-12-20 22:35:12 +0000233
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000234Exceptions:
235
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000236 - Enums, macros, and constant variables are occasionally written in all
237 upper-case with words optionally seperatedy by underscores (i.e. FIFOTYPE,
238 ISBLKDEV()).
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000239
240 - Nobody is going to get mad at you for using 'pvar' as the name of a
241 variable that is a pointer to 'var'.
Mark Whitley3680c582000-12-20 22:35:12 +0000242
Mark Whitley40bfc762000-07-24 22:36:06 +0000243
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000244Converting to K&R
245~~~~~~~~~~~~~~~~~
246
247The Busybox codebase is very much a mixture of code gathered from a variety of
248sources. This explains why the current codebase contains such a hodge-podge of
249different naming styles (Java, Pascal, K&R, just-plain-weird, etc.). The K&R
250guideline explained above should therefore be used on new files that are added
251to the repository. Furthermore, the maintainer of an existing file that uses
252alternate naming conventions should, at his own convenience, convert those
253names over to K&R style. Converting variable names is a very low priority
254task.
255
256If you want to do a search-and-replace of a single variable name in different
257files, you can do the following in the busybox directory:
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000258
259 $ perl -pi -e 's/\bOldVar\b/new_var/g' *.[ch]
260
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000261If you want to convert all the non-K&R vars in your file all at once, follow
262these steps:
263
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000264 - In the busybox directory type 'examples/mk2knr.pl files-to-convert'. This
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000265 does not do the actual conversion, rather, it generates a script called
266 'convertme.pl' that shows what will be converted, giving you a chance to
267 review the changes beforehand.
268
269 - Review the 'convertme.pl' script that gets generated in the busybox
270 directory and remove / edit any of the substitutions in there. Please
271 especially check for false positives (strings that should not be
272 converted).
273
274 - Type './convertme.pl same-files-as-before' to perform the actual
275 conversion.
276
277 - Compile and see if everything still works.
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000278
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000279Please be aware of changes that have cascading effects into other files. For
280example, if you're changing the name of something in, say utility.c, you
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000281should probably run 'examples/mk2knr.pl utility.c' at first, but when you run
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000282the 'convertme.pl' script you should run it on _all_ files like so:
283'./convertme.pl *.[ch]'.
284
Mark Whitley40bfc762000-07-24 22:36:06 +0000285
Mark Whitley40bfc762000-07-24 22:36:06 +0000286
Mark Whitleyd58ff872000-11-22 19:25:39 +0000287Avoid The Preprocessor
288----------------------
Mark Whitley40bfc762000-07-24 22:36:06 +0000289
Mark Whitleyd58ff872000-11-22 19:25:39 +0000290At best, the preprocessor is a necessary evil, helping us account for platform
291and architecture differences. Using the preprocessor unnecessarily is just
292plain evil.
Mark Whitley2368a382000-08-22 00:20:21 +0000293
Mark Whitley40bfc762000-07-24 22:36:06 +0000294
Mark Whitleyd58ff872000-11-22 19:25:39 +0000295The Folly of #define
296~~~~~~~~~~~~~~~~~~~~
Mark Whitley40bfc762000-07-24 22:36:06 +0000297
Mark Whitleyd58ff872000-11-22 19:25:39 +0000298Use 'const <type> var' for declaring constants.
Mark Whitley40bfc762000-07-24 22:36:06 +0000299
300 Don't do this:
301
Mark Whitleyd58ff872000-11-22 19:25:39 +0000302 #define var 80
303
304 Do this instead, when the variable is in a header file and will be used in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000305 several source files:
Mark Whitleyd58ff872000-11-22 19:25:39 +0000306
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000307 const int var = 80;
Mark Whitleyd58ff872000-11-22 19:25:39 +0000308
309 Or do this when the variable is used only in a single source file:
310
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000311 static const int var = 80;
Mark Whitleyd58ff872000-11-22 19:25:39 +0000312
313Declaring variables as '[static] const' gives variables an actual type and
314makes the compiler do type checking for you; the preprocessor does _no_ type
315checking whatsoever, making it much more error prone. Declaring variables with
316'[static] const' also makes debugging programs much easier since the value of
317the variable can be easily queried and displayed.
318
319
320The Folly of Macros
321~~~~~~~~~~~~~~~~~~~
322
323Use 'static inline' instead of a macro.
324
325 Don't do this:
326
327 #define mini_func(param1, param2) (param1 << param2)
Mark Whitley40bfc762000-07-24 22:36:06 +0000328
329 Do this instead:
330
Mark Whitleyd58ff872000-11-22 19:25:39 +0000331 static inline int mini_func(int param1, param2)
Mark Whitley40bfc762000-07-24 22:36:06 +0000332 {
Mark Whitleyd58ff872000-11-22 19:25:39 +0000333 return (param1 << param2);
334 }
Mark Whitley40bfc762000-07-24 22:36:06 +0000335
Mark Whitleyd58ff872000-11-22 19:25:39 +0000336Static inline functions are greatly preferred over macros. They provide type
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000337safety, have no length limitations, no formatting limitations, have an actual
338return value, and under gcc they are as cheap as macros. Besides, really long
339macros with backslashes at the end of each line are ugly as sin.
Mark Whitleyd58ff872000-11-22 19:25:39 +0000340
341
342The Folly of #ifdef
343~~~~~~~~~~~~~~~~~~~
344
345Code cluttered with ifdefs is difficult to read and maintain. Don't do it.
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000346Instead, put your ifdefs at the top of your .c file (or in a header), and
347conditionally define 'static inline' functions, (or *maybe* macros), which are
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000348used in the code.
Mark Whitleyd58ff872000-11-22 19:25:39 +0000349
350 Don't do this:
351
352 ret = my_func(bar, baz);
353 if (!ret)
354 return -1;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000355 #ifdef CONFIG_FEATURE_FUNKY
Mark Whitleyd58ff872000-11-22 19:25:39 +0000356 maybe_do_funky_stuff(bar, baz);
357 #endif
358
359 Do this instead:
360
361 (in .h header file)
362
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000363 #ifdef CONFIG_FEATURE_FUNKY
Mark Whitleyd238a7b2001-02-09 00:28:59 +0000364 static inline void maybe_do_funky_stuff (int bar, int baz)
365 {
366 /* lotsa code in here */
367 }
368 #else
Mark Whitleyd58ff872000-11-22 19:25:39 +0000369 static inline void maybe_do_funky_stuff (int bar, int baz) {}
370 #endif
371
372 (in the .c source file)
373
374 ret = my_func(bar, baz);
375 if (!ret)
376 return -1;
377 maybe_do_funky_stuff(bar, baz);
378
379The great thing about this approach is that the compiler will optimize away
Mark Whitleyd238a7b2001-02-09 00:28:59 +0000380the "no-op" case (the empty function) when the feature is turned off.
Mark Whitleyd58ff872000-11-22 19:25:39 +0000381
382Note also the use of the word 'maybe' in the function name to indicate
383conditional execution.
384
385
386
387Notes on Strings
388----------------
389
390Strings in C can get a little thorny. Here's some guidelines for dealing with
391strings in Busybox. (There is surely more that could be added to this
392section.)
393
394
395String Files
396~~~~~~~~~~~~
397
398Put all help/usage messages in usage.c. Put other strings in messages.c.
399Putting these strings into their own file is a calculated decision designed to
400confine spelling errors to a single place and aid internationalization
401efforts, if needed. (Side Note: we might want to use a single file - maybe
402called 'strings.c' - instead of two, food for thought).
403
404
405Testing String Equivalence
406~~~~~~~~~~~~~~~~~~~~~~~~~~
407
408There's a right way and a wrong way to test for sting equivalence with
409strcmp():
410
411 The wrong way:
412
413 if (!strcmp(string, "foo")) {
414 ...
415
416 The right way:
417
418 if (strcmp(string, "foo") == 0){
419 ...
420
421The use of the "equals" (==) operator in the latter example makes it much more
422obvious that you are testing for equivalence. The former example with the
423"not" (!) operator makes it look like you are testing for an error. In a more
424perfect world, we would have a streq() function in the string library, but
425that ain't the world we're living in.
426
427
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000428Avoid Dangerous String Functions
429~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
430
431Unfortunately, the way C handles strings makes them prone to overruns when
432certain library functions are (mis)used. The following table offers a summary
433of some of the more notorious troublemakers:
434
435function overflows preferred
436----------------------------------------
437strcpy dest string strncpy
438strcat dest string strncat
439gets string it gets fgets
440getwd buf string getcwd
441[v]sprintf str buffer [v]snprintf
442realpath path buffer use with pathconf
443[vf]scanf its arguments just avoid it
444
445
446The above is by no means a complete list. Be careful out there.
447
448
449
450Avoid Big Static Buffers
451------------------------
452
453First, some background to put this discussion in context: Static buffers look
454like this in code:
455
456 /* in a .c file outside any functions */
Rob Landley6a249762005-12-20 15:23:20 +0000457 static char buffer[BUFSIZ]; /* happily used by any function in this file,
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000458 but ick! big! */
459
460The problem with these is that any time any busybox app is run, you pay a
461memory penalty for this buffer, even if the applet that uses said buffer is
462not run. This can be fixed, thusly:
463
Eric Andersend35c2152001-01-25 23:49:09 +0000464 static char *buffer;
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000465 ...
466 other_func()
467 {
468 strcpy(buffer, lotsa_chars); /* happily uses global *buffer */
469 ...
470 foo_main()
471 {
472 buffer = xmalloc(sizeof(char)*BUFSIZ);
473 ...
474
475However, this approach trades bss segment for text segment. Rather than
476mallocing the buffers (and thus growing the text size), buffers can be
477declared on the stack in the *_main() function and made available globally by
478assigning them to a global pointer thusly:
479
Eric Andersend35c2152001-01-25 23:49:09 +0000480 static char *pbuffer;
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000481 ...
482 other_func()
483 {
484 strcpy(pbuffer, lotsa_chars); /* happily uses global *pbuffer */
485 ...
486 foo_main()
487 {
488 char *buffer[BUFSIZ]; /* declared locally, on stack */
489 pbuffer = buffer; /* but available globally */
490 ...
491
Eric Andersend35c2152001-01-25 23:49:09 +0000492This last approach has some advantages (low code size, space not used until
493it's needed), but can be a problem in some low resource machines that have
Mark Whitleyd238a7b2001-02-09 00:28:59 +0000494very limited stack space (e.g., uCLinux).
495
496A macro is declared in busybox.h that implements compile-time selection
497between xmalloc() and stack creation, so you can code the line in question as
498
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000499 RESERVE_CONFIG_BUFFER(buffer, BUFSIZ);
Mark Whitleyd238a7b2001-02-09 00:28:59 +0000500
501and the right thing will happen, based on your configuration.
Mark Whitleya5b55ca2001-01-24 00:18:13 +0000502
503
Mark Whitleyd58ff872000-11-22 19:25:39 +0000504
505Miscellaneous Coding Guidelines
506-------------------------------
507
508The following are important items that don't fit into any of the above
509sections.
510
511
512Model Busybox Applets After GNU Counterparts
513~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
514
515When in doubt about the proper behavior of a Busybox program (output,
516formatting, options, etc.), model it after the equivalent GNU program.
517Doesn't matter how that program behaves on some other flavor of *NIX; doesn't
518matter what the POSIX standard says or doesn't say, just model Busybox
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000519programs after their GNU counterparts and it will make life easier on (nearly)
520everyone.
Mark Whitleyd58ff872000-11-22 19:25:39 +0000521
522The only time we deviate from emulating the GNU behavior is when:
523
524 - We are deliberately not supporting a feature (such as a command line
525 switch)
526 - Emulating the GNU behavior is prohibitively expensive (lots more code
527 would be required, lots more memory would be used, etc.)
Eric Andersen07309432000-11-29 22:12:19 +0000528 - The difference is minor or cosmetic
Mark Whitleyd58ff872000-11-22 19:25:39 +0000529
530A note on the 'cosmetic' case: Output differences might be considered
531cosmetic, but if the output is significant enough to break other scripts that
532use the output, it should really be fixed.
533
534
535Scope
536~~~~~
537
538If a const variable is used only in a single source file, put it in the source
539file and not in a header file. Likewise, if a const variable is used in only
540one function, do not make it global to the file. Instead, declare it inside
Eric Andersen07309432000-11-29 22:12:19 +0000541the function body. Bottom line: Make a conscious effort to limit declarations
Mark Whitleyd58ff872000-11-22 19:25:39 +0000542to the smallest scope possible.
543
544Inside applet files, all functions should be declared static so as to keep the
545global name space clean. The only exception to this rule is the "applet_main"
546function which must be declared extern.
547
548If you write a function that performs a task that could be useful outside the
549immediate file, turn it into a general-purpose function with no ties to any
550applet and put it in the utility.c file instead.
551
552
553Brackets Are Your Friends
554~~~~~~~~~~~~~~~~~~~~~~~~~
555
556Please use brackets on all if and else statements, even if it is only one
557line. Example:
Mark Whitley40bfc762000-07-24 22:36:06 +0000558
559 Don't do this:
560
561 if (foo)
Mark Whitley3680c582000-12-20 22:35:12 +0000562 stmt1;
563 stmt2
564 stmt3;
Mark Whitley40bfc762000-07-24 22:36:06 +0000565
566 Do this instead:
567
568 if (foo) {
Mark Whitley3680c582000-12-20 22:35:12 +0000569 stmt1;
Mark Whitley40bfc762000-07-24 22:36:06 +0000570 }
Mark Whitley3680c582000-12-20 22:35:12 +0000571 stmt2
572 stmt3;
Mark Whitley40bfc762000-07-24 22:36:06 +0000573
Mark Whitleyd58ff872000-11-22 19:25:39 +0000574The "bracketless" approach is error prone because someday you might add a line
575like this:
Mark Whitley40bfc762000-07-24 22:36:06 +0000576
577 if (foo)
Mark Whitley3680c582000-12-20 22:35:12 +0000578 stmt1;
Mark Whitley40bfc762000-07-24 22:36:06 +0000579 new_line();
Mark Whitley3680c582000-12-20 22:35:12 +0000580 stmt2
581 stmt3;
Mark Whitley40bfc762000-07-24 22:36:06 +0000582
Mark Whitleyd58ff872000-11-22 19:25:39 +0000583And the resulting behavior of your program would totally bewilder you. (Don't
584laugh, it happens to us all.) Remember folks, this is C, not Python.
585
586
587Function Declarations
588~~~~~~~~~~~~~~~~~~~~~
589
590Do not use old-style function declarations that declare variable types between
591the parameter list and opening bracket. Example:
592
593 Don't do this:
594
595 int foo(parm1, parm2)
596 char parm1;
597 float parm2;
598 {
599 ....
600
601 Do this instead:
602
603 int foo(char parm1, float parm2)
604 {
605 ....
606
607The only time you would ever need to use the old declaration syntax is to
Eric Andersen07309432000-11-29 22:12:19 +0000608support ancient, antediluvian compilers. To our good fortune, we have access
Mark Whitleyd58ff872000-11-22 19:25:39 +0000609to more modern compilers and the old declaration syntax is neither necessary
610nor desired.
611
Mark Whitley3680c582000-12-20 22:35:12 +0000612
613Emphasizing Logical Blocks
614~~~~~~~~~~~~~~~~~~~~~~~~~~
615
616Organization and readability are improved by putting extra newlines around
617blocks of code that perform a single task. These are typically blocks that
618begin with a C keyword, but not always.
619
620Furthermore, you should put a single comment (not necessarily one line, just
621one comment) before the block, rather than commenting each and every line.
Mike Frysinger434ccd92006-07-05 22:19:21 +0000622There is an optimal amount of commenting that a program can have; you can
Mark Whitley3680c582000-12-20 22:35:12 +0000623comment too much as well as too little.
624
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000625A picture is really worth a thousand words here, the following example
626illustrates how to emphasize logical blocks:
Mark Whitley3680c582000-12-20 22:35:12 +0000627
628 while (line = get_line_from_file(fp)) {
629
630 /* eat the newline, if any */
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000631 chomp(line);
Mark Whitley3680c582000-12-20 22:35:12 +0000632
633 /* ignore blank lines */
634 if (strlen(file_to_act_on) == 0) {
635 continue;
636 }
637
638 /* if the search string is in this line, print it,
639 * unless we were told to be quiet */
640 if (strstr(line, search) && !be_quiet) {
641 puts(line);
642 }
643
644 /* clean up */
645 free(line);
646 }
Mark Whitley9ead6892001-03-03 00:44:55 +0000647
648
649Processing Options with getopt
650~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
651
652If your applet needs to process command-line switches, please use getopt() to
653do so. Numerous examples can be seen in many of the existing applets, but
654basically it boils down to two things: at the top of the .c file, have this
655line in the midst of your #includes:
656
657 #include <getopt.h>
658
659And a code block similar to the following near the top of your applet_main()
660routine:
661
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000662 while ((opt = getopt(argc, argv, "abc")) > 0) {
Mark Whitley9ead6892001-03-03 00:44:55 +0000663 switch (opt) {
664 case 'a':
665 do_a_opt = 1;
666 break;
667 case 'b':
668 do_b_opt = 1;
669 break;
670 case 'c':
671 do_c_opt = 1;
672 break;
673 default:
674 show_usage(); /* in utility.c */
675 }
676 }
677
678If your applet takes no options (such as 'init'), there should be a line
679somewhere in the file reads:
680
681 /* no options, no getopt */
682
683That way, when people go grepping to see which applets need to be converted to
684use getopt, they won't get false positives.
685
686Additional Note: Do not use the getopt_long library function and do not try to
687hand-roll your own long option parsing. Busybox applets should only support
Mark Whitley7ddaf7c2001-03-14 21:04:53 +0000688short options. Explanations and examples of the short options should be
689documented in usage.h.