Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 1 | Busybox Style Guide |
| 2 | =================== |
| 3 | |
| 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. |
| 10 | |
| 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. |
| 17 | |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 18 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 19 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 20 | Declaration Order |
| 21 | ----------------- |
| 22 | |
| 23 | Here is the order in which code should be laid out in a file: |
| 24 | |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 25 | - commented program name and one-line description |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 26 | - commented author name and email address(es) |
| 27 | - commented GPL boilerplate |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 28 | - commented longer description / notes for the program (if needed) |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 29 | - #includes and #defines |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 30 | - const and global variables |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 31 | - function declarations (if necessary) |
| 32 | - function implementations |
| 33 | |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 34 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 35 | |
| 36 | Whitespace and Formatting |
| 37 | ------------------------- |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 38 | |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 39 | This is everybody's favorite flame topic so let's get it out of the way right |
| 40 | up front. |
| 41 | |
| 42 | |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 43 | Tabs vs. Spaces in Line Indentation |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 44 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 45 | |
| 46 | The preference in Busybox is to indent lines with tabs. Do not indent lines |
| 47 | with spaces and do not indents lines using a mixture of tabs and spaces. (The |
| 48 | indentation style in the Apache and Postfix source does this sort of thing: |
| 49 | \s\s\s\sif (expr) {\n\tstmt; --ick.) The only exception to this rule is |
| 50 | multi-line comments that use an asterisk at the beginning of each line, i.e.: |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 51 | |
| 52 | /t/* |
| 53 | /t * This is a block comment. |
| 54 | /t * Note that it has multiple lines |
| 55 | /t * and that the beginning of each line has a tab plus a space |
| 56 | /t * except for the opening '/*' line where the slash |
| 57 | /t * is used instead of a space. |
| 58 | /t */ |
| 59 | |
| 60 | Furthermore, The preference is that tabs be set to display at four spaces |
| 61 | wide, but the beauty of using only tabs (and not spaces) at the beginning of |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 62 | lines is that you can set your editor to display tabs at *whatever* number of |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 63 | spaces is desired and the code will still look fine. |
| 64 | |
| 65 | |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 66 | Operator Spacing |
| 67 | ~~~~~~~~~~~~~~~~ |
| 68 | |
| 69 | Put spaces between terms and operators. Example: |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 70 | |
| 71 | Don't do this: |
| 72 | |
| 73 | for(i=0;i<num_items;i++){ |
| 74 | |
| 75 | Do this instead: |
| 76 | |
| 77 | for (i = 0; i < num_items; i++) { |
| 78 | |
| 79 | While it extends the line a bit longer, the spaced version is more |
| 80 | readable. An allowable exception to this rule is the situation where |
| 81 | excluding the spacing makes it more obvious that we are dealing with a |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 82 | single term (even if it is a compound term) such as: |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 83 | |
| 84 | if (str[idx] == '/' && str[idx-1] != '\\') |
| 85 | |
| 86 | or |
| 87 | |
| 88 | if ((argc-1) - (optind+1) > 0) |
| 89 | |
| 90 | |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 91 | Bracket Spacing |
| 92 | ~~~~~~~~~~~~~~~ |
| 93 | |
| 94 | If an opening bracket starts a function, it should be on the |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 95 | next line with no spacing before it. However, if a bracket follows an opening |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 96 | control block, it should be on the same line with a single space (not a tab) |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 97 | between it and the opening control block statement. Examples: |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 98 | |
| 99 | Don't do this: |
| 100 | |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 101 | while (!done) |
| 102 | { |
| 103 | |
| 104 | do |
| 105 | { |
| 106 | |
| 107 | Don't do this either: |
| 108 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 109 | while (!done){ |
Mark Whitley | 925edb8 | 2001-02-03 00:20:14 +0000 | [diff] [blame^] | 110 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 111 | do{ |
| 112 | |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 113 | And for heaven's sake, don't do this: |
| 114 | |
| 115 | while (!done) |
| 116 | { |
Mark Whitley | 925edb8 | 2001-02-03 00:20:14 +0000 | [diff] [blame^] | 117 | |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 118 | do |
| 119 | { |
| 120 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 121 | Do this instead: |
| 122 | |
| 123 | while (!done) { |
Mark Whitley | 925edb8 | 2001-02-03 00:20:14 +0000 | [diff] [blame^] | 124 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 125 | do { |
| 126 | |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 127 | |
Mark Whitley | 925edb8 | 2001-02-03 00:20:14 +0000 | [diff] [blame^] | 128 | Spacing around Parentheses |
| 129 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 130 | |
| 131 | Put a space between C keywords and left parens, but not between |
| 132 | function names and the left paren that starts it's parameter list (whether it |
| 133 | is being declared or called). Examples: |
| 134 | |
| 135 | Don't do this: |
| 136 | |
| 137 | while(foo) { |
| 138 | for(i = 0; i < n; i++) { |
| 139 | |
| 140 | Do this instead: |
| 141 | |
| 142 | while (foo) { |
| 143 | for (i = 0; i < n; i++) { |
| 144 | |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 145 | But do functions like this: |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 146 | |
| 147 | static int my_func(int foo, char bar) |
| 148 | ... |
| 149 | baz = my_func(1, 2); |
| 150 | |
Mark Whitley | 925edb8 | 2001-02-03 00:20:14 +0000 | [diff] [blame^] | 151 | Also, don't put a space between the left paren and the first term, nor between |
| 152 | the last arg and the right paren. |
| 153 | |
| 154 | Don't do this: |
| 155 | |
| 156 | if ( x < 1 ) |
| 157 | strcmp( thisstr, thatstr ) |
| 158 | |
| 159 | Do this instead: |
| 160 | |
| 161 | if (x < 1) |
| 162 | strcmp(thisstr, thatstr) |
| 163 | |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 164 | |
| 165 | Cuddled Elses |
| 166 | ~~~~~~~~~~~~~ |
| 167 | |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 168 | Also, please "cuddle" your else statements by putting the else keyword on the |
| 169 | same line after the right bracket that closes an 'if' statement. |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 170 | |
| 171 | Don't do this: |
| 172 | |
| 173 | if (foo) { |
| 174 | stmt; |
| 175 | } |
| 176 | else { |
| 177 | stmt; |
| 178 | } |
| 179 | |
| 180 | Do this instead: |
| 181 | |
| 182 | if (foo) { |
| 183 | stmt; |
| 184 | } else { |
| 185 | stmt; |
| 186 | } |
| 187 | |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 188 | The exception to this rule is if you want to include a comment before the else |
| 189 | block. Example: |
| 190 | |
| 191 | if (foo) { |
| 192 | stmts... |
| 193 | } |
| 194 | /* otherwise, we're just kidding ourselves, so re-frob the input */ |
| 195 | else { |
| 196 | other_stmts... |
| 197 | } |
| 198 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 199 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 200 | |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 201 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 202 | Variable and Function Names |
| 203 | --------------------------- |
| 204 | |
| 205 | Use the K&R style with names in all lower-case and underscores occasionally |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 206 | used to separate words (e.g., "variable_name" and "numchars" are both |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 207 | acceptable). Using underscores makes variable and function names more readable |
| 208 | because it looks like whitespace; using lower-case is easy on the eyes. |
| 209 | |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 210 | Frowned upon: |
| 211 | |
| 212 | hitList |
| 213 | TotalChars |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 214 | szFileName |
| 215 | pf_Nfol_TriState |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 216 | |
| 217 | Preferred: |
| 218 | |
| 219 | hit_list |
| 220 | total_chars |
| 221 | file_name |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 222 | sensible_name |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 223 | |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 224 | Exceptions: |
| 225 | |
| 226 | - Enums, macros, and constant variables should all be in upper-case with |
| 227 | words optionally seperatedy by underscores (i.e. FIFOTYPE, ISBLKDEV()). |
| 228 | |
| 229 | - Nobody is going to get mad at you for using 'pvar' as the name of a |
| 230 | variable that is a pointer to 'var'. |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 231 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 232 | Note: The Busybox codebase is very much a mixture of code gathered from a |
Mark Whitley | 9028e2c | 2000-11-17 21:28:39 +0000 | [diff] [blame] | 233 | variety of sources. This explains why the current codebase contains such a |
| 234 | hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird, |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 235 | etc.). The K&R guideline explained above should therefore be used on new files |
| 236 | that are added to the repository. Furthermore, the maintainer of an existing |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 237 | file that uses alternate naming conventions should -- at his own convenience |
| 238 | -- convert those names over to K&R style; converting variable names is a very |
| 239 | low priority task. Perhaps in the future we will include some magical Perl |
| 240 | script that can go through and convert variable names, left as an exercise for |
| 241 | the reader for now. |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 242 | |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 243 | For the time being, if you want to do a search-and-replace of a variable name |
| 244 | in different files, do the following in the busybox directory: |
| 245 | |
| 246 | $ perl -pi -e 's/\bOldVar\b/new_var/g' *.[ch] |
| 247 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 248 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 249 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 250 | Avoid The Preprocessor |
| 251 | ---------------------- |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 252 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 253 | At best, the preprocessor is a necessary evil, helping us account for platform |
| 254 | and architecture differences. Using the preprocessor unnecessarily is just |
| 255 | plain evil. |
Mark Whitley | 2368a38 | 2000-08-22 00:20:21 +0000 | [diff] [blame] | 256 | |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 257 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 258 | The Folly of #define |
| 259 | ~~~~~~~~~~~~~~~~~~~~ |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 260 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 261 | Use 'const <type> var' for declaring constants. |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 262 | |
| 263 | Don't do this: |
| 264 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 265 | #define var 80 |
| 266 | |
| 267 | Do this instead, when the variable is in a header file and will be used in |
| 268 | several source files: |
| 269 | |
| 270 | const int var = 80; |
| 271 | |
| 272 | Or do this when the variable is used only in a single source file: |
| 273 | |
| 274 | static const int var = 80; |
| 275 | |
| 276 | Declaring variables as '[static] const' gives variables an actual type and |
| 277 | makes the compiler do type checking for you; the preprocessor does _no_ type |
| 278 | checking whatsoever, making it much more error prone. Declaring variables with |
| 279 | '[static] const' also makes debugging programs much easier since the value of |
| 280 | the variable can be easily queried and displayed. |
| 281 | |
| 282 | |
| 283 | The Folly of Macros |
| 284 | ~~~~~~~~~~~~~~~~~~~ |
| 285 | |
| 286 | Use 'static inline' instead of a macro. |
| 287 | |
| 288 | Don't do this: |
| 289 | |
| 290 | #define mini_func(param1, param2) (param1 << param2) |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 291 | |
| 292 | Do this instead: |
| 293 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 294 | static inline int mini_func(int param1, param2) |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 295 | { |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 296 | return (param1 << param2); |
| 297 | } |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 298 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 299 | Static inline functions are greatly preferred over macros. They provide type |
| 300 | safety, have no length limitations, no formatting limitations, and under gcc |
| 301 | they are as cheap as macros. Besides, really long macros with backslashes at |
| 302 | the end of each line are ugly as sin. |
| 303 | |
| 304 | |
| 305 | The Folly of #ifdef |
| 306 | ~~~~~~~~~~~~~~~~~~~ |
| 307 | |
| 308 | Code cluttered with ifdefs is difficult to read and maintain. Don't do it. |
| 309 | Instead, put your ifdefs in a header, and conditionally define 'static inline' |
| 310 | functions, (or *maybe* macros), which are used in the code. |
| 311 | |
| 312 | Don't do this: |
| 313 | |
| 314 | ret = my_func(bar, baz); |
| 315 | if (!ret) |
| 316 | return -1; |
| 317 | #ifdef BB_FEATURE_FUNKY |
| 318 | maybe_do_funky_stuff(bar, baz); |
| 319 | #endif |
| 320 | |
| 321 | Do this instead: |
| 322 | |
| 323 | (in .h header file) |
| 324 | |
| 325 | #ifndef BB_FEATURE_FUNKY |
| 326 | static inline void maybe_do_funky_stuff (int bar, int baz) {} |
| 327 | #endif |
| 328 | |
| 329 | (in the .c source file) |
| 330 | |
| 331 | ret = my_func(bar, baz); |
| 332 | if (!ret) |
| 333 | return -1; |
| 334 | maybe_do_funky_stuff(bar, baz); |
| 335 | |
| 336 | The great thing about this approach is that the compiler will optimize away |
| 337 | the "no-op" case when the feature is turned off. |
| 338 | |
| 339 | Note also the use of the word 'maybe' in the function name to indicate |
| 340 | conditional execution. |
| 341 | |
| 342 | |
| 343 | |
| 344 | Notes on Strings |
| 345 | ---------------- |
| 346 | |
| 347 | Strings in C can get a little thorny. Here's some guidelines for dealing with |
| 348 | strings in Busybox. (There is surely more that could be added to this |
| 349 | section.) |
| 350 | |
| 351 | |
| 352 | String Files |
| 353 | ~~~~~~~~~~~~ |
| 354 | |
| 355 | Put all help/usage messages in usage.c. Put other strings in messages.c. |
| 356 | Putting these strings into their own file is a calculated decision designed to |
| 357 | confine spelling errors to a single place and aid internationalization |
| 358 | efforts, if needed. (Side Note: we might want to use a single file - maybe |
| 359 | called 'strings.c' - instead of two, food for thought). |
| 360 | |
| 361 | |
| 362 | Testing String Equivalence |
| 363 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 364 | |
| 365 | There's a right way and a wrong way to test for sting equivalence with |
| 366 | strcmp(): |
| 367 | |
| 368 | The wrong way: |
| 369 | |
| 370 | if (!strcmp(string, "foo")) { |
| 371 | ... |
| 372 | |
| 373 | The right way: |
| 374 | |
| 375 | if (strcmp(string, "foo") == 0){ |
| 376 | ... |
| 377 | |
| 378 | The use of the "equals" (==) operator in the latter example makes it much more |
| 379 | obvious that you are testing for equivalence. The former example with the |
| 380 | "not" (!) operator makes it look like you are testing for an error. In a more |
| 381 | perfect world, we would have a streq() function in the string library, but |
| 382 | that ain't the world we're living in. |
| 383 | |
| 384 | |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 385 | Avoid Dangerous String Functions |
| 386 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 387 | |
| 388 | Unfortunately, the way C handles strings makes them prone to overruns when |
| 389 | certain library functions are (mis)used. The following table offers a summary |
| 390 | of some of the more notorious troublemakers: |
| 391 | |
| 392 | function overflows preferred |
| 393 | ---------------------------------------- |
| 394 | strcpy dest string strncpy |
| 395 | strcat dest string strncat |
| 396 | gets string it gets fgets |
| 397 | getwd buf string getcwd |
| 398 | [v]sprintf str buffer [v]snprintf |
| 399 | realpath path buffer use with pathconf |
| 400 | [vf]scanf its arguments just avoid it |
| 401 | |
| 402 | |
| 403 | The above is by no means a complete list. Be careful out there. |
| 404 | |
| 405 | |
| 406 | |
| 407 | Avoid Big Static Buffers |
| 408 | ------------------------ |
| 409 | |
| 410 | First, some background to put this discussion in context: Static buffers look |
| 411 | like this in code: |
| 412 | |
| 413 | /* in a .c file outside any functions */ |
| 414 | static char *buffer[BUFSIZ]; /* happily used by any function in this file, |
| 415 | but ick! big! */ |
| 416 | |
| 417 | The problem with these is that any time any busybox app is run, you pay a |
| 418 | memory penalty for this buffer, even if the applet that uses said buffer is |
| 419 | not run. This can be fixed, thusly: |
| 420 | |
Eric Andersen | d35c215 | 2001-01-25 23:49:09 +0000 | [diff] [blame] | 421 | static char *buffer; |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 422 | ... |
| 423 | other_func() |
| 424 | { |
| 425 | strcpy(buffer, lotsa_chars); /* happily uses global *buffer */ |
| 426 | ... |
| 427 | foo_main() |
| 428 | { |
| 429 | buffer = xmalloc(sizeof(char)*BUFSIZ); |
| 430 | ... |
| 431 | |
| 432 | However, this approach trades bss segment for text segment. Rather than |
| 433 | mallocing the buffers (and thus growing the text size), buffers can be |
| 434 | declared on the stack in the *_main() function and made available globally by |
| 435 | assigning them to a global pointer thusly: |
| 436 | |
Eric Andersen | d35c215 | 2001-01-25 23:49:09 +0000 | [diff] [blame] | 437 | static char *pbuffer; |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 438 | ... |
| 439 | other_func() |
| 440 | { |
| 441 | strcpy(pbuffer, lotsa_chars); /* happily uses global *pbuffer */ |
| 442 | ... |
| 443 | foo_main() |
| 444 | { |
| 445 | char *buffer[BUFSIZ]; /* declared locally, on stack */ |
| 446 | pbuffer = buffer; /* but available globally */ |
| 447 | ... |
| 448 | |
Eric Andersen | d35c215 | 2001-01-25 23:49:09 +0000 | [diff] [blame] | 449 | This last approach has some advantages (low code size, space not used until |
| 450 | it's needed), but can be a problem in some low resource machines that have |
| 451 | very limited stack space (e.g., uCLinux). busybox.h declares a macro that |
| 452 | implements compile-time selection between xmalloc() and stack creation, so |
| 453 | you can code the line in question as |
| 454 | RESERVE_BB_BUFFER(buffer, BUFSIZ); |
| 455 | and the right thing will happen, based on the customer's configuration. |
Mark Whitley | a5b55ca | 2001-01-24 00:18:13 +0000 | [diff] [blame] | 456 | |
| 457 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 458 | |
| 459 | Miscellaneous Coding Guidelines |
| 460 | ------------------------------- |
| 461 | |
| 462 | The following are important items that don't fit into any of the above |
| 463 | sections. |
| 464 | |
| 465 | |
| 466 | Model Busybox Applets After GNU Counterparts |
| 467 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 468 | |
| 469 | When in doubt about the proper behavior of a Busybox program (output, |
| 470 | formatting, options, etc.), model it after the equivalent GNU program. |
| 471 | Doesn't matter how that program behaves on some other flavor of *NIX; doesn't |
| 472 | matter what the POSIX standard says or doesn't say, just model Busybox |
| 473 | programs after their GNU counterparts and nobody has to get hurt. |
| 474 | |
| 475 | The only time we deviate from emulating the GNU behavior is when: |
| 476 | |
| 477 | - We are deliberately not supporting a feature (such as a command line |
| 478 | switch) |
| 479 | - Emulating the GNU behavior is prohibitively expensive (lots more code |
| 480 | would be required, lots more memory would be used, etc.) |
Eric Andersen | 0730943 | 2000-11-29 22:12:19 +0000 | [diff] [blame] | 481 | - The difference is minor or cosmetic |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 482 | |
| 483 | A note on the 'cosmetic' case: Output differences might be considered |
| 484 | cosmetic, but if the output is significant enough to break other scripts that |
| 485 | use the output, it should really be fixed. |
| 486 | |
| 487 | |
| 488 | Scope |
| 489 | ~~~~~ |
| 490 | |
| 491 | If a const variable is used only in a single source file, put it in the source |
| 492 | file and not in a header file. Likewise, if a const variable is used in only |
| 493 | one function, do not make it global to the file. Instead, declare it inside |
Eric Andersen | 0730943 | 2000-11-29 22:12:19 +0000 | [diff] [blame] | 494 | the function body. Bottom line: Make a conscious effort to limit declarations |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 495 | to the smallest scope possible. |
| 496 | |
| 497 | Inside applet files, all functions should be declared static so as to keep the |
| 498 | global name space clean. The only exception to this rule is the "applet_main" |
| 499 | function which must be declared extern. |
| 500 | |
| 501 | If you write a function that performs a task that could be useful outside the |
| 502 | immediate file, turn it into a general-purpose function with no ties to any |
| 503 | applet and put it in the utility.c file instead. |
| 504 | |
| 505 | |
| 506 | Brackets Are Your Friends |
| 507 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 508 | |
| 509 | Please use brackets on all if and else statements, even if it is only one |
| 510 | line. Example: |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 511 | |
| 512 | Don't do this: |
| 513 | |
| 514 | if (foo) |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 515 | stmt1; |
| 516 | stmt2 |
| 517 | stmt3; |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 518 | |
| 519 | Do this instead: |
| 520 | |
| 521 | if (foo) { |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 522 | stmt1; |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 523 | } |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 524 | stmt2 |
| 525 | stmt3; |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 526 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 527 | The "bracketless" approach is error prone because someday you might add a line |
| 528 | like this: |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 529 | |
| 530 | if (foo) |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 531 | stmt1; |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 532 | new_line(); |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 533 | stmt2 |
| 534 | stmt3; |
Mark Whitley | 40bfc76 | 2000-07-24 22:36:06 +0000 | [diff] [blame] | 535 | |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 536 | And the resulting behavior of your program would totally bewilder you. (Don't |
| 537 | laugh, it happens to us all.) Remember folks, this is C, not Python. |
| 538 | |
| 539 | |
| 540 | Function Declarations |
| 541 | ~~~~~~~~~~~~~~~~~~~~~ |
| 542 | |
| 543 | Do not use old-style function declarations that declare variable types between |
| 544 | the parameter list and opening bracket. Example: |
| 545 | |
| 546 | Don't do this: |
| 547 | |
| 548 | int foo(parm1, parm2) |
| 549 | char parm1; |
| 550 | float parm2; |
| 551 | { |
| 552 | .... |
| 553 | |
| 554 | Do this instead: |
| 555 | |
| 556 | int foo(char parm1, float parm2) |
| 557 | { |
| 558 | .... |
| 559 | |
| 560 | The only time you would ever need to use the old declaration syntax is to |
Eric Andersen | 0730943 | 2000-11-29 22:12:19 +0000 | [diff] [blame] | 561 | support ancient, antediluvian compilers. To our good fortune, we have access |
Mark Whitley | d58ff87 | 2000-11-22 19:25:39 +0000 | [diff] [blame] | 562 | to more modern compilers and the old declaration syntax is neither necessary |
| 563 | nor desired. |
| 564 | |
Mark Whitley | 3680c58 | 2000-12-20 22:35:12 +0000 | [diff] [blame] | 565 | |
| 566 | Emphasizing Logical Blocks |
| 567 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 568 | |
| 569 | Organization and readability are improved by putting extra newlines around |
| 570 | blocks of code that perform a single task. These are typically blocks that |
| 571 | begin with a C keyword, but not always. |
| 572 | |
| 573 | Furthermore, you should put a single comment (not necessarily one line, just |
| 574 | one comment) before the block, rather than commenting each and every line. |
| 575 | There is an optimal ammount of commenting that a program can have; you can |
| 576 | comment too much as well as too little. |
| 577 | |
| 578 | A picture is really worth a thousand words here, so here is an example that |
| 579 | illustrates emphasizing logical blocks: |
| 580 | |
| 581 | while (line = get_line_from_file(fp)) { |
| 582 | |
| 583 | /* eat the newline, if any */ |
| 584 | if (line[strlen(line)-1] == '\n') { |
| 585 | line[strlen(line)-1] = '\0'; |
| 586 | } |
| 587 | |
| 588 | /* ignore blank lines */ |
| 589 | if (strlen(file_to_act_on) == 0) { |
| 590 | continue; |
| 591 | } |
| 592 | |
| 593 | /* if the search string is in this line, print it, |
| 594 | * unless we were told to be quiet */ |
| 595 | if (strstr(line, search) && !be_quiet) { |
| 596 | puts(line); |
| 597 | } |
| 598 | |
| 599 | /* clean up */ |
| 600 | free(line); |
| 601 | } |
Mark Whitley | 925edb8 | 2001-02-03 00:20:14 +0000 | [diff] [blame^] | 602 | |
| 603 | |
| 604 | Testing Guidelines |
| 605 | ~~~~~~~~~~~~~~~~~~ |
| 606 | |
| 607 | It's considered good form to test your new feature before you submit a patch |
| 608 | to the mailing list, and especially before you commit a change to CVS. Here |
| 609 | are some guildlines on testing your changes. |
| 610 | |
| 611 | - Always test busybox grep against GNU grep and make sure the behavior / |
| 612 | output is identical between the two. |
| 613 | |
| 614 | - Try several different permutations and combinations of the features you're |
| 615 | adding and make sure they all work. (Make sure one feature does not |
| 616 | interfere with another, etc.) |
| 617 | |
| 618 | - Make sure you test compiling against the source both with the feature |
| 619 | turned on and turned off in Config.h and make sure busybox compiles cleanly |
| 620 | both ways. |
| 621 | |