4 This document describes the coding style conventions used in Busybox. If you
5 add a new file to Busybox or are editing an existing file, please format your
6 code according to this style. If you are the maintainer of a file that does
7 not follow these guidelines, please -- at your own convenience -- modify the
8 file(s) you maintain to bring them into conformance with this style guide.
9 Please note that this is a low priority task.
11 To help you format the whitespace of your programs, an ".indent.pro" file is
12 included in the main Busybox source directory that contains option flags to
13 format code as per this style guide. This way you can run GNU indent on your
14 files by typing 'indent myfile.c myfile.h' and it will magically apply all the
15 right formatting rules to your file. Please _do_not_ run this on all the files
16 in the directory, just your own.
22 Here is the order in which code should be laid out in a file:
24 - commented program name and one-line description
25 - commented author name and email address(es)
26 - commented GPL boilerplate
27 - commented longer description / notes for the program (if needed)
28 - #includes and #defines
29 - const and global variables
30 - function declarations (if necessary)
31 - function implementations
37 This is everybody's favorite flame topic so let's get it out of the way right
41 Tabs vs. Spaces in Line Indentation
42 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44 The preference in Busybox is to indent lines with tabs. Do not indent lines
45 with spaces and do not indents lines using a mixture of tabs and spaces. (The
46 indentation style in the Apache and Postfix source does this sort of thing:
47 \s\s\s\sif (expr) {\n\tstmt; --ick.) The only exception to this rule is
48 multi-line comments that use an asterisk at the beginning of each line, i.e.:
51 /t * This is a block comment.
52 /t * Note that it has multiple lines
53 /t * and that the beginning of each line has a tab plus a space
54 /t * except for the opening '/*' line where the slash
55 /t * is used instead of a space.
58 Furthermore, The preference is that tabs be set to display at four spaces
59 wide, but the beauty of using only tabs (and not spaces) at the beginning of
60 lines is that you can set your editor to display tabs at *whatever* number of
61 spaces is desired and the code will still look fine.
67 Put spaces between terms and operators. Example:
71 for(i=0;i<num_items;i++){
75 for (i = 0; i < num_items; i++) {
77 While it extends the line a bit longer, the spaced version is more
78 readable. An allowable exception to this rule is the situation where
79 excluding the spacing makes it more obvious that we are dealing with a
80 single term (even if it is a compound term) such as:
82 if (str[idx] == '/' && str[idx-1] != '\\')
86 if ((argc-1) - (optind+1) > 0)
92 If an opening bracket starts a function, it should be on the
93 next line with no spacing before it. However, if a bracket follows an opening
94 control block, it should be on the same line with a single space (not a tab)
95 between it and the opening control block statement. Examples:
105 Don't do this either:
119 Put a space between C keywords and left parens, but not between
120 function names and the left paren that starts it's parameter list (whether it
121 is being declared or called). Examples:
126 for(i = 0; i < n; i++) {
131 for (i = 0; i < n; i++) {
133 But do functions like this:
135 static int my_func(int foo, char bar)
143 Also, please "cuddle" your else statements by putting the else keyword on the
144 same line after the right bracket that closes an 'if' statement.
163 The exception to this rule is if you want to include a comment before the else
169 /* otherwise, we're just kidding ourselves, so re-frob the input */
175 Variable and Function Names
176 ---------------------------
178 Use the K&R style with names in all lower-case and underscores occasionally
179 used to separate words (e.g., "variable_name" and "numchars" are both
180 acceptable). Using underscores makes variable and function names more readable
181 because it looks like whitespace; using lower-case is easy on the eyes.
183 Note: The Busybox codebase is very much a mixture of code gathered from a
184 variety of sources. This explains why the current codebase contains such a
185 hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird,
186 etc.). The K&R guideline explained above should therefore be used on new files
187 that are added to the repository. Furthermore, the maintainer of an existing
188 file that uses alternate naming conventions should -- at his own convenience --
189 convert those names over to K&R style; converting variable names is a very low
190 priority task. Perhaps in the future we will include some magical Perl script
191 that can go through and convert files -- left as an exercise to the reader for
198 The following are simple coding guidelines that should be followed:
200 - When in doubt about the proper behavior of a Busybox program (output,
201 formatting, options, etc.), model it after the equivalent GNU program.
202 Doesn't matter how that program behaves on some other flavor of *NIX;
203 doesn't matter what the POSIX standard says or doesn't say, just model
204 Busybox programs after their GNU counterparts and nobody has to get hurt.
206 - Don't use a '#define var 80' when you can use 'static const int var 80'
207 instead. This makes the compiler do type checking for you (rather than
208 relying on the more error-prone preprocessor) and it makes debugging
209 programs much easier since the value of the variable can be easily
212 - If a const variable is used in only one function, do not make it global to
213 the file. Instead, declare it inside the function body.
215 - Inside applet files, all functions should be declared static so as to keep
216 the global name space clean. The only exception to this rule is the
217 "applet_main" function which must be declared extern.
219 - If you write a function that performs a task that could be useful outside
220 the immediate file, turn it into a general-purpose function with no ties to
221 any applet and put it in the utility.c file instead.
223 - Put all help/usage messages in usage.c. Put other strings in messages.c.
224 Putting these strings into their own file is a calculated decision designed
225 to confine spelling errors to a single place and aid internationalization
226 efforts, if needed. (Side Note: we might want to use a single file instead
227 of two, food for thought).
229 - There's a right way and a wrong way to test for sting equivalence with
234 if (!strcmp(string, "foo")) {
239 if (strcmp(string, "foo") == 0){
242 The use of the "equals" (==) operator in the latter example makes it much
243 more obvious that you are testing for equivalence. The former example with
244 the "not" (!) operator makes it look like you are testing for an error. In
245 a more perfect world, we would have a streq() function in the string
246 library, but that ain't the world we're living in.
248 - Do not use old-style function declarations that declare variable types
249 between the parameter list and opening bracket. Example:
253 int foo(parm1, parm2)
261 int foo(char parm1, float parm2)
265 - Please use brackets on all if and else statements, even if it is only one
283 The "bracketless" approach is error prone because someday you might add a
292 And the resulting behavior of your program would totally bewilder you.
293 (Don't laugh, it happens to us all.) Remember folks, this is C, not