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