1.\" Copyright (c) 1995-2001 FreeBSD Inc. 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.\" ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE 17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23.\" SUCH DAMAGE. 24.\" 25.\" 26.Dd December 7, 2001 27.Dt STYLE 9 28.Os 29.Sh NAME 30.Nm style 31.Nd "kernel source file style guide" 32.Sh DESCRIPTION 33This file specifies the preferred style for kernel source files in the 34.Fx 35source tree. 36It is also a guide for the preferred userland code style. 37Many of the style rules are implicit in the examples. 38Be careful to check the examples before assuming that 39.Nm 40is silent on an issue. 41.Bd -literal 42/* 43 * Style guide for FreeBSD. Based on the CSRG's KNF (Kernel Normal Form). 44 * 45 * @(#)style 1.14 (Berkeley) 4/28/95 46 * $FreeBSD$ 47 */ 48 49/* 50 * VERY important single-line comments look like this. 51 */ 52 53/* Most single-line comments look like this. */ 54 55/* 56 * Multi-line comments look like this. Make them real sentences. Fill 57 * them so they look like real paragraphs. 58 */ 59.Ed 60.Pp 61After any copyright header, there is a blank line, and the 62.Va rcsid 63for source files. 64Version control system ID tags should only exist once in a file 65(unlike in this one). 66Non-C/C++ source files follow the example above, while C/C++ source files 67follow the one below. 68All VCS (version control system) revision identification in files obtained 69from elsewhere should be maintained, including, where applicable, multiple IDs 70showing a file's history. 71In general, do not edit foreign IDs or their infrastructure. 72Unless otherwise wrapped (such as 73.Dq Li "#if defined(LIBC_SCCS)" ) , 74enclose both in 75.Dq Li "#if 0 ... #endif" 76to hide any uncompilable bits 77and to keep the IDs out of object files. 78Only add 79.Dq Li "From: " 80in front of foreign VCS IDs if the file is renamed. 81.Bd -literal 82#if 0 83#ifndef lint 84static char sccsid[] = "@(#)style 1.14 (Berkeley) 4/28/95"; 85#endif /* not lint */ 86#endif 87 88#include <sys/cdefs.h> 89__FBSDID("$FreeBSD$"); 90.Ed 91.Pp 92Leave another blank line before the header files. 93.Pp 94Kernel include files (i.e.\& 95.Pa sys/*.h ) 96come first; normally, include 97.Aq Pa sys/types.h 98OR 99.Aq Pa sys/param.h , 100but not both. 101.Aq Pa sys/types.h 102includes 103.Aq Pa sys/cdefs.h , 104and it is okay to depend on that. 105.Bd -literal 106#include <sys/types.h> /* Non-local includes in angle brackets. */ 107.Ed 108.Pp 109For a network program, put the network include files next. 110.Bd -literal 111#include <net/if.h> 112#include <net/if_dl.h> 113#include <net/route.h> 114#include <netinet/in.h> 115#include <protocols/rwhod.h> 116.Ed 117.Pp 118Do not use files in 119.Pa /usr/include 120for files in the kernel. 121.Pp 122Leave a blank line before the next group, the 123.Pa /usr 124include files, 125which should be sorted alphabetically by name. 126.Bd -literal 127#include <stdio.h> 128.Ed 129.Pp 130Global pathnames are defined in 131.Aq Pa paths.h . 132Pathnames local 133to the program go in 134.Qq Pa pathnames.h 135in the local directory. 136.Bd -literal 137#include <paths.h> 138.Ed 139.Pp 140Leave another blank line before the user include files. 141.Bd -literal 142#include "pathnames.h" /* Local includes in double quotes. */ 143.Ed 144.Pp 145Do not 146.Ic #define 147or declare names in the implementation namespace except 148for implementing application interfaces. 149.Pp 150The names of 151.Dq unsafe 152macros (ones that have side effects), and the names of macros for 153manifest constants, are all in uppercase. 154The expansions of expression-like macros are either a single token 155or have outer parentheses. 156Put a single tab character between the 157.Ic #define 158and the macro name. 159If a macro is an inline expansion of a function, the function name is 160all in lowercase and the macro has the same name all in uppercase. 161.\" XXX the above conflicts with ANSI style where the names are the 162.\" same and you #undef the macro (if any) to get the function. 163.\" It is not followed for MALLOC(), and not very common if inline 164.\" functions are used. 165Right-justify the 166backslashes; it makes it easier to read. 167If the macro encapsulates a compound statement, enclose it in a 168.Ic do 169loop, 170so that it can safely be used in 171.Ic if 172statements. 173Any final statement-terminating semicolon should be 174supplied by the macro invocation rather than the macro, to make parsing easier 175for pretty-printers and editors. 176.Bd -literal 177#define MACRO(x, y) do { \e 178 variable = (x) + (y); \e 179 (y) += 2; \e 180} while (0) 181.Ed 182.Pp 183When code is conditionally compiled using 184.Ic #ifdef 185or 186.Ic #if , 187a comment may be added following the matching 188.Ic #endif 189or 190.Ic #else 191to permit the reader to easily discern where conditionally compiled code 192regions end. 193This comment should be used only for (subjectively) long regions, regions 194greater than 20 lines, or where a series of nested 195.Ic #ifdef 's 196may be confusing to the reader. 197Exceptions may be made for cases where code is conditionally not compiled for 198the purposes of lint, even though the uncompiled region may be small. 199The comment should be separated from the 200.Ic #endif 201or 202.Ic #else 203by a single space. 204For short conditionally compiled regions, a closing comment should not be 205used. 206.Pp 207The comment for 208.Ic #endif 209should match the expression used in the corresponding 210.Ic #if 211or 212.Ic #ifdef . 213The comment for 214.Ic #else 215and 216.Ic #elif 217should match the inverse of the expression(s) used in the preceding 218.Ic #if 219and/or 220.Ic #elif 221statements. 222In the comments, the subexpression 223.Dq Li defined(FOO) 224is abbreviated as 225.Dq Li FOO . 226For the purposes of comments, 227.Dq Ic #ifndef Li FOO 228is treated as 229.Dq Ic #if Li !defined(FOO) . 230.Bd -literal 231#ifdef KTRACE 232#include <sys/ktrace.h> 233#endif 234 235#ifdef COMPAT_43 236/* A large region here, or other conditional code. */ 237#else /* !COMPAT_43 */ 238/* Or here. */ 239#endif /* COMPAT_43 */ 240 241#ifndef COMPAT_43 242/* Yet another large region here, or other conditional code. */ 243#else /* COMPAT_43 */ 244/* Or here. */ 245#endif /* !COMPAT_43*/ 246.Ed 247.Pp 248Enumeration values are all uppercase. 249.Bd -literal 250enum enumtype { ONE, TWO } et; 251.Ed 252.Pp 253In declarations, do not put any whitespace between asterisks and 254adjacent tokens, except for tokens that are identifiers related to 255types. (These identifiers are the names of basic types, type 256qualifiers, and typedef-names other than the one being declared.) 257Separate these identifers from asterisks using a single space. 258.Pp 259When declaring variables in structures, declare them sorted by use, then 260by size, and then in alphabetical order. 261The first category normally does not apply, but there are exceptions. 262Each one gets its own line. 263Try to make the structure 264readable by aligning the member names using either one or two tabs 265depending upon your judgment. 266You should use one tab only if it suffices to align at least 90% of 267the member names. 268Names following extremely long types 269should be separated by a single space. 270.Pp 271Major structures should be declared at the top of the file in which they 272are used, or in separate header files if they are used in multiple 273source files. 274Use of the structures should be by separate declarations 275and should be 276.Ic extern 277if they are declared in a header file. 278.Bd -literal 279struct foo { 280 struct foo *next; /* List of active foo. */ 281 struct mumble amumble; /* Comment for mumble. */ 282 int bar; /* Try to align the comments. */ 283 struct verylongtypename *baz; /* Won't fit in 2 tabs. */ 284}; 285struct foo *foohead; /* Head of global foo list. */ 286.Ed 287.Pp 288Use 289.Xr queue 3 290macros rather than rolling your own lists, whenever possible. 291Thus, 292the previous example would be better written: 293.Bd -literal 294#include <sys/queue.h> 295 296struct foo { 297 LIST_ENTRY(foo) link; /* Use queue macros for foo lists. */ 298 struct mumble amumble; /* Comment for mumble. */ 299 int bar; /* Try to align the comments. */ 300 struct verylongtypename *baz; /* Won't fit in 2 tabs. */ 301}; 302LIST_HEAD(, foo) foohead; /* Head of global foo list. */ 303.Ed 304.Pp 305Avoid using typedefs for structure types. 306This makes it impossible 307for applications to use pointers to such a structure opaquely, which 308is both possible and beneficial when using an ordinary struct tag. 309When convention requires a 310.Ic typedef , 311make its name match the struct tag. 312Avoid typedefs ending in 313.Dq Li _t , 314except as specified in Standard C or by \*[Px]. 315.Bd -literal 316/* Make the structure name match the typedef. */ 317typedef struct bar { 318 int level; 319} BAR; 320typedef int foo; /* This is foo. */ 321typedef const long baz; /* This is baz. */ 322.Ed 323.Pp 324All functions are prototyped somewhere. 325.Pp 326Function prototypes for private functions (i.e. functions not used 327elsewhere) go at the top of the first source module. 328Functions 329local to one source module should be declared 330.Ic static . 331.Pp 332Functions used from other parts of the kernel are prototyped in the 333relevant include file. 334Function prototypes should be listed in a logical order, preferably 335alphabetical unless there is a compelling reason to use a different 336ordering. 337.Pp 338Functions that are used locally in more than one module go into a 339separate header file, e.g.\& 340.Qq Pa extern.h . 341.Pp 342Do not use the 343.Dv __P 344macro. 345.Pp 346In general code can be considered 347.Dq "new code" 348when it makes up about 50% or more of the file(s) involved. 349This is enough 350to break precedents in the existing code and use the current 351.Nm 352guidelines. 353.Pp 354The kernel has a name associated with parameter types, e.g., in the kernel 355use: 356.Bd -literal 357void function(int fd); 358.Ed 359.Pp 360In header files visible to userland applications, prototypes that are 361visible must use either 362.Dq protected 363names (ones beginning with an underscore) 364or no names with the types. 365It is preferable to use protected names. 366E.g., use: 367.Bd -literal 368void function(int); 369.Ed 370.Pp 371or: 372.Bd -literal 373void function(int _fd); 374.Ed 375.Pp 376Prototypes may have an extra space after a tab to enable function names 377to line up: 378.Bd -literal 379static char *function(int _arg, const char *_arg2, struct foo *_arg3, 380 struct bar *_arg4); 381static void usage(void); 382 383/* 384 * All major routines should have a comment briefly describing what 385 * they do. The comment before the "main" routine should describe 386 * what the program does. 387 */ 388int 389main(int argc, char *argv[]) 390{ 391 char *ep; 392 long num; 393 int ch; 394.Ed 395.Pp 396For consistency, 397.Xr getopt 3 398should be used to parse options. 399Options 400should be sorted in the 401.Xr getopt 3 402call and the 403.Ic switch 404statement, unless 405parts of the 406.Ic switch 407cascade. 408Elements in a 409.Ic switch 410statement that cascade should have a 411.Li FALLTHROUGH 412comment. 413Numerical arguments should be checked for accuracy. 414Code that cannot be reached should have a 415.Li NOTREACHED 416comment. 417.Bd -literal 418 while ((ch = getopt(argc, argv, "abn:")) != -1) 419 switch (ch) { /* Indent the switch. */ 420 case 'a': /* Don't indent the case. */ 421 aflag = 1; 422 /* FALLTHROUGH */ 423 case 'b': 424 bflag = 1; 425 break; 426 case 'n': 427 num = strtol(optarg, &ep, 10); 428 if (num <= 0 || *ep != '\e0') { 429 warnx("illegal number, -n argument -- %s", 430 optarg); 431 usage(); 432 } 433 break; 434 case '?': 435 default: 436 usage(); 437 /* NOTREACHED */ 438 } 439 argc -= optind; 440 argv += optind; 441.Ed 442.Pp 443Space after keywords 444.Pq Ic if , while , for , return , switch . 445No braces 446.Ql ( \&{ 447and 448.Ql \&} ) 449are 450used for control statements with zero or only a single statement unless that 451statement is more than a single line in which case they are permitted. 452Forever loops are done with 453.Ic for Ns 's , 454not 455.Ic while Ns 's . 456.Bd -literal 457 for (p = buf; *p != '\e0'; ++p) 458 ; /* nothing */ 459 for (;;) 460 stmt; 461 for (;;) { 462 z = a + really + long + statement + that + needs + 463 two + lines + gets + indented + four + spaces + 464 on + the + second + and + subsequent + lines; 465 } 466 for (;;) { 467 if (cond) 468 stmt; 469 } 470 if (val != NULL) 471 val = realloc(val, newsize); 472.Ed 473.Pp 474Parts of a 475.Ic for 476loop may be left empty. 477Do not put declarations 478inside blocks unless the routine is unusually complicated. 479.Bd -literal 480 for (; cnt < 15; cnt++) { 481 stmt1; 482 stmt2; 483 } 484.Ed 485.Pp 486Indentation is an 8 character tab. 487Second level indents are four spaces. 488If you have to wrap a long statement, put the operator at the end of the 489line. 490.Bd -literal 491 while (cnt < 20 && this_variable_name_is_too_long && 492 ep != NULL) 493 z = a + really + long + statement + that + needs + 494 two + lines + gets + indented + four + spaces + 495 on + the + second + and + subsequent + lines; 496.Ed 497.Pp 498Do not add whitespace at the end of a line, and only use tabs 499followed by spaces 500to form the indentation. 501Do not use more spaces than a tab will produce 502and do not use spaces in front of tabs. 503.Pp 504Closing and opening braces go on the same line as the 505.Ic else . 506Braces that are not necessary may be left out. 507.Bd -literal 508 if (test) 509 stmt; 510 else if (bar) { 511 stmt; 512 stmt; 513 } else 514 stmt; 515.Ed 516.Pp 517No spaces after function names. 518Commas have a space after them. 519No spaces 520after 521.Ql \&( 522or 523.Ql \&[ 524or preceding 525.Ql \&] 526or 527.Ql \&) 528characters. 529.Bd -literal 530 error = function(a1, a2); 531 if (error != 0) 532 exit(error); 533.Ed 534.Pp 535Unary operators do not require spaces, binary operators do. 536Do not use parentheses unless they are required for precedence or unless the 537statement is confusing without them. 538Remember that other people may 539confuse easier than you. 540Do YOU understand the following? 541.Bd -literal 542 a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1; 543 k = !(l & FLAGS); 544.Ed 545.Pp 546Exits should be 0 on success, or according to the predefined 547values in 548.Xr sysexits 3 . 549.Bd -literal 550 exit(EX_OK); /* 551 * Avoid obvious comments such as 552 * "Exit 0 on success." 553 */ 554} 555.Ed 556.Pp 557The function type should be on a line by itself 558preceding the function. 559The opening brace of the function body should be 560on a line by itself. 561.Bd -literal 562static char * 563function(int a1, int a2, float fl, int a4) 564{ 565.Ed 566.Pp 567When declaring variables in functions declare them sorted by size, 568then in alphabetical order; multiple ones per line are okay. 569If a line overflows reuse the type keyword. 570.Pp 571Be careful to not obfuscate the code by initializing variables in 572the declarations. 573Use this feature only thoughtfully. 574DO NOT use function calls in initializers. 575.Bd -literal 576 struct foo one, *two; 577 double three; 578 int *four, five; 579 char *six, seven, eight, nine, ten, eleven, twelve; 580 581 four = myfunction(); 582.Ed 583.Pp 584Do not declare functions inside other functions; ANSI C says that 585such declarations have file scope regardless of the nesting of the 586declaration. 587Hiding file declarations in what appears to be a local 588scope is undesirable and will elicit complaints from a good compiler. 589.Pp 590Casts and 591.Ic sizeof Ns 's 592are not followed by a space. 593Note that 594.Xr indent 1 595does not understand this rule. 596.Ic sizeof Ns 's 597are written with parenthesis always. 598The redundant parenthesis rules do not apply to 599.Fn sizeof var 600instances. 601.Pp 602.Dv NULL 603is the preferred null pointer constant. 604Use 605.Dv NULL 606instead of 607.Vt ( "type *" ) Ns 0 608or 609.Vt ( "type *" ) Ns Dv NULL 610in contexts where the compiler knows the 611type, e.g., in assignments. 612Use 613.Vt ( "type *" ) Ns Dv NULL 614in other contexts, 615in particular for all function args. 616(Casting is essential for 617variadic args and is necessary for other args if the function prototype 618might not be in scope.) 619Test pointers against 620.Dv NULL , 621e.g., use: 622.Pp 623.Bd -literal 624(p = f()) == NULL 625.Ed 626.Pp 627not: 628.Bd -literal 629!(p = f()) 630.Ed 631.Pp 632Do not use 633.Ic \&! 634for tests unless it is a boolean, e.g. use 635.Bd -literal 636if (*p == '\e0') 637.Ed 638.Pp 639not 640.Bd -literal 641if (!*p) 642.Ed 643.Pp 644Routines returning 645.Vt "void *" 646should not have their return values cast 647to any pointer type. 648.Pp 649Values in 650.Ic return 651statements should be enclosed in parentheses. 652.Pp 653Use 654.Xr err 3 655or 656.Xr warn 3 , 657do not roll your own. 658.Bd -literal 659 if ((four = malloc(sizeof(struct foo))) == NULL) 660 err(1, (char *)NULL); 661 if ((six = (int *)overflow()) == NULL) 662 errx(1, "number overflowed"); 663 return (eight); 664} 665.Ed 666.Pp 667Old-style function declarations look like this: 668.Bd -literal 669static char * 670function(a1, a2, fl, a4) 671 int a1, a2; /* Declare ints, too, don't default them. */ 672 float fl; /* Beware double vs. float prototype differences. */ 673 int a4; /* List in order declared. */ 674{ 675.Ed 676.Pp 677Use ANSI function declarations unless you explicitly need K&R compatibility. 678Long parameter lists are wrapped with a normal four space indent. 679.Pp 680Variable numbers of arguments should look like this. 681.Bd -literal 682#include <stdarg.h> 683 684void 685vaf(const char *fmt, ...) 686{ 687 va_list ap; 688 689 va_start(ap, fmt); 690 STUFF; 691 va_end(ap); 692 /* No return needed for void functions. */ 693} 694 695static void 696usage() 697{ 698 /* Insert an empty line if the function has no local variables. */ 699.Ed 700.Pp 701Use 702.Xr printf 3 , 703not 704.Xr fputs 3 , 705.Xr puts 3 , 706.Xr putchar 3 , 707whatever; it is faster and usually cleaner, not 708to mention avoiding stupid bugs. 709.Pp 710Usage statements should look like the manual pages 711.Sx SYNOPSIS . 712The usage statement should be structured in the following order: 713.Bl -enum 714.It 715Options without operands come first, 716in alphabetical order, 717inside a single set of brackets 718.Ql ( \&[ 719and 720.Ql \&] ) . 721.It 722Options with operands come next, 723also in alphabetical order, 724with each option and its argument inside its own pair of brackets. 725.It 726Required arguments 727(if any) 728are next, 729listed in the order they should be specified on the command line. 730.It 731Finally, 732any optional arguments should be listed, 733listed in the order they should be specified, 734and all inside brackets. 735.El 736.Pp 737A bar 738.Pq Ql \&| 739separates 740.Dq either-or 741options/arguments, 742and multiple options/arguments which are specified together are 743placed in a single set of brackets. 744.Bd -literal -offset 4n 745"usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\en" 746"usage: f [-a | -b] [-c [-dEe] [-n number]]\en" 747.Ed 748.Bd -literal 749 (void)fprintf(stderr, "usage: f [-ab]\en"); 750 exit(EX_USAGE); 751} 752.Ed 753.Pp 754Note that the manual page options description should list the options in 755pure alphabetical order. 756That is, without regard to whether an option takes arguments or not. 757The alphabetical ordering should take into account the case ordering 758shown above. 759.Pp 760New core kernel code should be reasonably compliant with the 761.Nm 762guides. 763The guidelines for third-party maintained modules and device drivers are more 764relaxed but at a minimum should be internally consistent with their style. 765.Pp 766Stylistic changes (including whitespace changes) are hard on the source 767repository and are to be avoided without good reason. 768Code that is approximately 769.Fx 770KNF 771.Nm 772compliant in the repository must not diverge from compliance. 773.Pp 774Whenever possible, code should be run through a code checker 775(e.g., 776.Xr lint 1 777or 778.Nm gcc Fl Wall ) 779and produce minimal warnings. 780.Sh SEE ALSO 781.Xr indent 1 , 782.Xr lint 1 , 783.Xr err 3 , 784.Xr sysexits 3 , 785.Xr warn 3 786.Sh HISTORY 787This man page is largely based on the 788.Pa src/admin/style/style 789file from the 790.Bx 4.4 Lite2 791release, with occasional updates to reflect the current practice and 792desire of the 793.Fx 794project. 795