style revision 1.1
1/* 2 * Style guide for BSD's KNF (Kernel Normal Form). 3 * 4 * from: @(#)style 1.10 (Berkeley) 2/11/92 5 * $Id: style,v 1.1 1993/08/06 07:30:52 cgd Exp $ 6 */ 7 8/* 9 * VERY important single-line comments look like this. 10 */ 11 12/* Most single-line comments look like this. */ 13 14/* 15 * Multi-line comments look like this. Make them real sentences. Fill 16 * them so they look like real paragraphs. 17 */ 18 19/* Include files go at the top of the source module. */ 20#include <stdio.h> /* Non-local includes in brackets. */ 21 22/* 23 * Global pathnames are defined in /usr/include/paths.h. Pathnames local 24 * to the program go in pathnames.h in the local directory. 25 */ 26#include <paths.h> /* Non-local includes in brackets. */ 27#include "pathnames.h" /* Local includes in quotes. */ 28 29/* 30 * All ANSI function decls go at the top of the source module. Use the 31 * __P macro from include file <sys/cdefs.h>. Only the kernel has a name 32 * associated with the types, i.e. in the kernel use: 33 * 34 * void function __P((int a)); 35 * 36 * in user land use: 37 * 38 * void function __P((int)); 39 */ 40void function __P((int, const char *)); 41 42/* 43 * Macros are capitalized, parenthesized, and should avoid side-effects. 44 * If they are an inline expansion of a function, the function is defined 45 * all in lowercase, the macro has the same name all in uppercase. If the 46 * macro needs more than a single line, use braces. Put a space before 47 * the backslashes. 48 */ 49#define MACRO(x, y) { \ 50 variable = (x) + (y); \ 51 line two; \ 52} 53 54/* Enum types are capitalized. */ 55enum enumtype { ONE, TWO } et; 56 57/* 58 * When declaring variables in structures, declare them sorted by use, then 59 * by size, and then by alphabetical order. The first category normally 60 * doesn't apply, but there are exceptions. Each one gets its own line. 61 * Put a tab after the first word, i.e. use "int^Ix;" and "struct^Ifoo *x;". 62 * 63 * Major structures should be declared at the top of the file they are 64 * used in, or in separate header files, if they are used in multiple 65 * source files. Use of the structures should be by separate declarations 66 * and should be "extern" if they are declared in a header file. 67 */ 68struct foo { 69 struct foo *next; /* List of active foo */ 70 struct mumble amumble; /* Comment for mumble */ 71 int bar; 72}; 73struct foo *foohead; /* Head of global foo list */ 74 75/* 76 * All major routines should have a comment briefly describing what 77 * they do. The comment before the "main" routine should describe 78 * what the program does. 79 */ 80main(argc, argv) 81 int argc; 82 char *argv[]; 83{ 84 extern char *optarg; 85 extern int optind; 86 long num; 87 int ch; 88 char *ep; 89 90 /* 91 * For consistency, getopt should be used to parse options. 92 * Options should be sorted in the getopt call and the switch 93 * statement, unless they fall through. Elements in a switch 94 * statement that fall through should have a FALLTHROUGH comment. 95 * Numerical arguments should be checked for accuracy. 96 */ 97 while ((ch = getopt(argc, argv, "abn")) != EOF) 98 switch (ch) { /* Indent the switch. */ 99 case 'a': /* Don't indent the case. */ 100 aflag = 1; 101 /* FALLTHROUGH */ 102 case 'b': 103 bflag = 1; 104 break; 105 case 'n': 106 num = strtol(optarg, &ep, 10); 107 if (num <= 0 || *ep) 108 err("illegal number -- %s", optarg); 109 break; 110 case '?': 111 default: 112 usage(); 113 } 114 argc -= optind; 115 argv += optind; 116 117 /* 118 * Space after keywords (while, for, return, switch). No braces are 119 * used for single statement block. 120 * 121 * Forever loops are done with for's, not while's. 122 */ 123 for (;;) 124 stmt; 125 126 /* 127 * Parts of a for loop may be left empty. Avoid declarations in 128 * blocks unless the routine is unusually complicated. 129 */ 130 for (; cnt < 15; cnt++) { 131 stmt1; 132 stmt2; 133 } 134 135 while (cnt < 20) { 136 stmt1; /* Second level indents are four spaces. */ 137 z = a + really + long + statment + that + needs + two lines + 138 gets + indented + four + spaces + on + the + second + 139 and + subsequent + lines. 140 } 141 142 /* 143 * Try to put shorter part first. The closing and opening braces 144 * go on the same line as the else. 145 */ 146 if (test) 147 stmt; 148 else if (bar) { 149 stmt; 150 stmt; 151 } else 152 stmt; 153 154 /* No space after function names. */ 155 if (error = function(a1, a2)) 156 exit(error); 157 158 /* 159 * Unary operators do not require spaces, binary operators do. 160 * Try not to use too many parenthesis unless the statement is 161 * really confusing without them. 162 */ 163 a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1; 164 k = l & FLAGS; 165 166 /* 167 * Exits should be 0 on success, and 1 on failure. Don't denote 168 * all the possible exit points, using the integers 1 through 300. 169 */ 170 exit(0); /* Avoid obvious comments such as "Exit 0 on success." */ 171} 172 173/* 174 * If a function type is declared, it should be on a line 175 * by itself preceeding the function. 176 */ 177static char * 178function(a1, a2, a3, a4) 179 int a1, a2, a4; /* Declare ints too. */ 180 float a3; /* List in order declared, as much as possible. */ 181{ 182 /* 183 * When declaring variables in functions declare them sorted by size, 184 * then in alphabetical order; multiple ones per line are okay. Old 185 * style function declarations can go on the same line. ANSI style 186 * function declarations should go in the include file "externs.h". 187 * If a line overflows reuse the type keyword. 188 * 189 * Try not to initialize variables in the declarations. 190 */ 191 extern u_char one; 192 extern char two; 193 struct foo three, *four; 194 double five; 195 int *six, seven, eight(); 196 char *nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen; 197 char *overflow __P((void)); 198 void *mymalloc __P((u_int)); 199 200 /* 201 * Casts and sizeof's are not followed by a space. NULL is any 202 * pointer type, and doesn't need to be cast, so use NULL instead 203 * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers 204 * against NULL, i.e. use: 205 * 206 * (p = f()) == NULL 207 * not: 208 * !(p = f()) 209 * 210 * Routines returning void * should not have their return values cast 211 * to any pointer type. 212 */ 213 if ((four = malloc(sizeof(struct foo))) == NULL) 214 return (NULL); 215 if ((six = (int *)overflow()) == NULL) 216 return (NULL); 217 return (eight); 218} 219 220/* ANSI function braces look like regular function braces. */ 221function(int a1, int a2) 222{ 223 ... 224} 225 226static void 227usage() 228{ /* Insert an empty line if the function has no local variables. */ 229 230 /* 231 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and 232 * usually cleaner, not to mention avoiding stupid bugs. 233 * 234 * Usage statements should look like the manual pages. Options w/o 235 * operands come first, in alphabetical order inside a single set of 236 * braces. Followed by options with operands, in alphabetical order, 237 * each in braces. Followed by required arguments in the order they 238 * are specified, followed by optional arguments in the order they 239 * are specified. A bar ('|') separates either/or options/arguments, 240 * and multiple options/arguments which are specified together are 241 * placed in a single set of braces. 242 * 243 * "usage: f [-ade] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n" 244 * "usage: f [-a | -b] [-c [-de] [-n number]]\n" 245 */ 246 (void)fprintf(stderr, "usage: f [-ab]\n"); 247 exit(1); 248} 249