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.
21
Here is the order in which code should be laid out in a file:
23
- commented author name and email address(es)
24
- commented GPL boilerplate
25
- commented description of program
26
- #includes and #defines
27
- const and globals variables
28
- function declarations (if necessary)
29
- function implementations
34
Tabs vs Spaces in Line Indentation: The preference in Busybox is to indent
35
lines with tabs. Do not indent lines with spaces and do not indents lines
36
using a mixture of tabs and spaces. (The indentation style in the Apache and
37
Postfix source does this sort of thing: \s\s\s\sif (expr) {\n\tstmt; --ick.)
38
The only exception to this rule is multi-line comments that use an asterisk at
39
the beginning of each line, i.e.:
42
/t * This is a block comment.
43
/t * Note that it has multiple lines
44
/t * and that the beginning of each line has a tab plus a space
45
/t * except for the opening '/*' line where the slash
46
/t * is used instead of a space.
49
Furthermore, The preference is that tabs be set to display at four spaces
50
wide, but the beauty of using only tabs (and not spaces) at the beginning of
51
lines is that you can set your editor to display tabs at *watever* number of
52
spaces is desired and the code will still look fine.
55
Operator Spacing: Put spaces between terms and operators. Example:
59
for(i=0;i<num_items;i++){
63
for (i = 0; i < num_items; i++) {
65
While it extends the line a bit longer, the spaced version is more
66
readable. An allowable exception to this rule is the situation where
67
excluding the spacing makes it more obvious that we are dealing with a
68
single term (even if it is a compund term) such as:
70
if (str[idx] == '/' && str[idx-1] != '\\')
74
if ((argc-1) - (optind+1) > 0)
77
Bracket Spacing: If an opening bracket starts a function, it should be on the
78
next line with no spacing before it. However, if a bracet follows an opening
79
control block, it should be on the same line with a single space (not a tab)
80
between it and the opening control block statment. Examples:
92
Also, please "cuddle" your else statments by putting the else keyword on the
93
same line after the right bracket that closes an 'if' statment.
113
Paren Spacing: Put a space between C keywords and left parens, but not between
114
function names and the left paren that starts it's parameter list (whether it
115
is being declared or called). Examples:
120
for(i = 0; i < n; i++) {
125
for (i = 0; i < n; i++) {
127
Do functions like this:
129
static int my_func(int foo, char bar)
133
Variable and Function Names
134
---------------------------
136
Use the K&R style with names in all lower-case and underscores occasionally
137
used to seperate words (e.g. "variable_name" and "numchars" are both
138
acceptable). Using underscores makes variable and function names more readable
139
because it looks like whitespace; using lower-case is easy on the eyes.
141
Note: The Busybox codebase is very much a mixture of code gathered from a
142
variety of locations. This explains why the current codebase contains such a
143
plethora of different naming styles (Java, Pascal, K&R, just-plain-weird,
144
etc.). The K&R guideline explained above should therefore be used on new files
145
that are added to the repository. Furthermore, the maintainer of an existing
146
file that uses alternate naming conventions should -- at his own convenience
147
-- convert those names over to K&R style; converting variable names is a very
148
low priority task. Perhaps in the future we will include some magical Perl
149
script that can go through and convert files--left as an exersize to the
156
The following are simple coding guidelines that should be followed:
158
- Don't use a '#define var 80' when you can use 'static const int var 80'
159
instead. This makes the compiler do typechecking for you (rather than
160
relying on the more error-prone preprocessor) and it makes debugging
161
programs much easier since the value of the variable can be easily queried.
163
- If a const variable is used in only one function, do not make it global to
164
the file. Instead, declare it inside the function body.
166
- Inside applet files, all functions should be declared static so as to keep
167
the global namespace clean. The only exception to this rule is the
168
"applet_main" function which must be declared extern.
170
- If you write a function that performs a task that could be useful outside
171
the immediate file, turn it into a general-purpose function with no ties to
172
any applet and put it in the utility.c file instead.
174
- Put all help/usage messages in usage.c. Put other strings in messages.c
175
(Side Note: we might want to use a single file instead of two, food for
178
- Do not use old-style function declarations that declare variable types
179
between the parameter list and opening bracket. Example:
183
int foo(parm1, parm2)
191
int foo(char parm1, float parm2)
195
- Please use brackets on all if and else statements, even if it is only one
213
The "bracketless" approach is error prone because someday you might add a
222
And the resulting behavior of your program would totally bewilder you.
223
(Don't laugh, it happens to us all.) Remember folks, this is C, not