style revision 1.56
11.56Slukem/* $NetBSD: style,v 1.56 2020/08/01 02:45:35 lukem Exp $ */ 21.6Sthorpej 31.1Scgd/* 41.12Slukem * The revision control tag appears first, with a blank line after it. 51.12Slukem * Copyright text appears after the revision control tag. 61.12Slukem */ 71.12Slukem 81.12Slukem/* 91.12Slukem * The NetBSD source code style guide. 101.12Slukem * (Previously known as KNF - Kernel Normal Form). 111.1Scgd * 121.2Scgd * from: @(#)style 1.12 (Berkeley) 3/18/94 131.10Sscottr */ 141.10Sscottr/* 151.10Sscottr * An indent(1) profile approximating the style outlined in 161.10Sscottr * this document lives in /usr/share/misc/indent.pro. It is a 171.10Sscottr * useful tool to assist in converting code to KNF, but indent(1) 181.10Sscottr * output generated using this profile must not be considered to 191.10Sscottr * be an authoritative reference. 201.1Scgd */ 211.1Scgd 221.1Scgd/* 231.12Slukem * Source code revision control identifiers appear after any copyright 241.12Slukem * text. Use the appropriate macros from <sys/cdefs.h>. Usually only one 251.12Slukem * source file per program contains a __COPYRIGHT() section. 261.12Slukem * Historic Berkeley code may also have an __SCCSID() section. 271.12Slukem * Only one instance of each of these macros can occur in each file. 281.43Slukem * Don't use newlines in the identifiers. 291.12Slukem */ 301.12Slukem#include <sys/cdefs.h> 311.43Slukem__COPYRIGHT("@(#) Copyright (c) 2008\ 321.43Slukem The NetBSD Foundation, inc. All rights reserved."); 331.56Slukem__RCSID("$NetBSD: style,v 1.56 2020/08/01 02:45:35 lukem Exp $"); 341.12Slukem 351.12Slukem/* 361.1Scgd * VERY important single-line comments look like this. 371.1Scgd */ 381.1Scgd 391.1Scgd/* Most single-line comments look like this. */ 401.1Scgd 411.1Scgd/* 421.1Scgd * Multi-line comments look like this. Make them real sentences. Fill 431.1Scgd * them so they look like real paragraphs. 441.1Scgd */ 451.1Scgd 461.2Scgd/* 471.12Slukem * Attempt to wrap lines longer than 80 characters appropriately. 481.12Slukem * Refer to the examples below for more information. 491.12Slukem */ 501.12Slukem 511.12Slukem/* 521.12Slukem * EXAMPLE HEADER FILE: 531.12Slukem * 541.12Slukem * A header file should protect itself against multiple inclusion. 551.12Slukem * E.g, <sys/socket.h> would contain something like: 561.12Slukem */ 571.12Slukem#ifndef _SYS_SOCKET_H_ 581.12Slukem#define _SYS_SOCKET_H_ 591.12Slukem/* 601.12Slukem * Contents of #include file go between the #ifndef and the #endif at the end. 611.12Slukem */ 621.12Slukem#endif /* !_SYS_SOCKET_H_ */ 631.12Slukem/* 641.12Slukem * END OF EXAMPLE HEADER FILE. 651.12Slukem */ 661.12Slukem 671.12Slukem/* 681.39Sdarcy * If a header file requires structures, defines, typedefs, etc. from 691.39Sdarcy * another header file it should include that header file and not depend 701.39Sdarcy * on the including file for that header including both. If there are 711.39Sdarcy * exceptions to this for specific headers it should be clearly documented 721.39Sdarcy * in the headers and, if appropriate, the documentation. Nothing in this 731.39Sdarcy * rule should suggest relaxation of the multiple inclusion rule and the 741.39Sdarcy * application programmer should be free to include both regardless. 751.39Sdarcy */ 761.39Sdarcy 771.39Sdarcy/* 781.12Slukem * Kernel include files come first. 791.2Scgd */ 801.50Sriastrad#include <sys/param.h> /* <sys/param.h> first, */ 811.50Sriastrad#include <sys/types.h> /* <sys/types.h> next, */ 821.50Sriastrad#include <sys/ioctl.h> /* and then the rest, */ 831.50Sriastrad#include <sys/socket.h> /* sorted lexicographically. */ 841.50Sriastrad#include <sys/stat.h> 851.50Sriastrad#include <sys/wait.h> /* Non-local includes in brackets. */ 861.2Scgd 871.12Slukem/* 881.12Slukem * If it's a network program, put the network include files next. 891.12Slukem * Group the includes files by subdirectory. 901.12Slukem */ 911.2Scgd#include <net/if.h> 921.2Scgd#include <net/if_dl.h> 931.2Scgd#include <net/route.h> 941.2Scgd#include <netinet/in.h> 951.2Scgd#include <protocols/rwhod.h> 961.2Scgd 971.2Scgd/* 981.2Scgd * Then there's a blank line, followed by the /usr include files. 991.50Sriastrad * The /usr include files should be sorted lexicographically! 1001.2Scgd */ 1011.20Skleink#include <assert.h> 1021.25Slukem#include <errno.h> 1031.36Sbriggs#include <inttypes.h> 1041.2Scgd#include <stdio.h> 1051.18Scgd#include <stdlib.h> 1061.1Scgd 1071.1Scgd/* 1081.1Scgd * Global pathnames are defined in /usr/include/paths.h. Pathnames local 1091.1Scgd * to the program go in pathnames.h in the local directory. 1101.1Scgd */ 1111.2Scgd#include <paths.h> 1121.2Scgd 1131.2Scgd/* Then, there's a blank line, and the user include files. */ 1141.12Slukem#include "pathnames.h" /* Local includes in double quotes. */ 1151.1Scgd 1161.1Scgd/* 1171.2Scgd * ANSI function declarations for private functions (i.e. functions not used 1181.45Sdholland * elsewhere) and the main() function go at the top of the source module. 1191.12Slukem * Don't associate a name with the types. I.e. use: 1201.12Slukem * void function(int); 1211.12Slukem * Use your discretion on indenting between the return type and the name, and 1221.12Slukem * how to wrap a prototype too long for a single line. In the latter case, 1231.15Slukem * lining up under the initial left parenthesis may be more readable. 1241.12Slukem * In any case, consistency is important! 1251.12Slukem */ 1261.12Slukemstatic char *function(int, int, float, int); 1271.12Slukemstatic int dirinfo(const char *, struct stat *, struct dirent *, 1281.12Slukem struct statfs *, int *, char **[]); 1291.47Schristosstatic void usage(void) __dead; /* declare functions that don't return dead */ 1301.1Scgd 1311.1Scgd/* 1321.1Scgd * Macros are capitalized, parenthesized, and should avoid side-effects. 1331.22Sjhawk * Spacing before and after the macro name may be any whitespace, though 1341.22Sjhawk * use of TABs should be consistent through a file. 1351.1Scgd * If they are an inline expansion of a function, the function is defined 1361.12Slukem * all in lowercase, the macro has the same name all in uppercase. 1371.12Slukem * If the macro is an expression, wrap the expression in parenthesis. 1381.12Slukem * If the macro is more than a single statement, use ``do { ... } while (0)'', 1391.12Slukem * so that a trailing semicolon works. Right-justify the backslashes; it 1401.13Slukem * makes it easier to read. The CONSTCOND comment is to satisfy lint(1). 1411.12Slukem */ 1421.12Slukem#define MACRO(v, w, x, y) \ 1431.12Slukemdo { \ 1441.12Slukem v = (x) + (y); \ 1451.12Slukem w = (y) + 2; \ 1461.12Slukem} while (/* CONSTCOND */ 0) 1471.12Slukem 1481.15Slukem#define DOUBLE(x) ((x) * 2) 1491.12Slukem 1501.55Srillig/* Enum constants are capitalized. No comma on the last element. */ 1511.12Slukemenum enumtype { 1521.12Slukem ONE, 1531.12Slukem TWO 1541.12Slukem} et; 1551.12Slukem 1561.12Slukem/* 1571.54Schristos * Sometimes we want a macro to be conditionally defined for debugging 1581.54Schristos * and expand to nothing (but still as statement) when we are not debugging: 1591.54Schristos */ 1601.54Schristos#ifdef FOO_DEBUG 1611.54Schristos# define DPRINTF(...) printf(__VA_ARGS__) 1621.54Schristos#else 1631.54Schristos# define DPRINTF(...) __nothing 1641.54Schristos#endif 1651.54Schristos 1661.54Schristos/* 1671.16Senami * When declaring variables in structures, declare them organized by use in 1681.16Senami * a manner to attempt to minimize memory wastage because of compiler alignment 1691.12Slukem * issues, then by size, and then by alphabetical order. E.g, don't use 1701.12Slukem * ``int a; char *b; int c; char *d''; use ``int a; int b; char *c; char *d''. 1711.12Slukem * Each variable gets its own type and line, although an exception can be made 1721.12Slukem * when declaring bitfields (to clarify that it's part of the one bitfield). 1731.12Slukem * Note that the use of bitfields in general is discouraged. 1741.1Scgd * 1751.2Scgd * Major structures should be declared at the top of the file in which they 1761.2Scgd * are used, or in separate header files, if they are used in multiple 1771.2Scgd * source files. Use of the structures should be by separate declarations 1781.1Scgd * and should be "extern" if they are declared in a header file. 1791.12Slukem * 1801.12Slukem * It may be useful to use a meaningful prefix for each member name. 1811.12Slukem * E.g, for ``struct softc'' the prefix could be ``sc_''. 1821.1Scgd */ 1831.1Scgdstruct foo { 1841.12Slukem struct foo *next; /* List of active foo */ 1851.12Slukem struct mumble amumble; /* Comment for mumble */ 1861.12Slukem int bar; 1871.12Slukem unsigned int baz:1, /* Bitfield; line up entries if desired */ 1881.12Slukem fuz:5, 1891.12Slukem zap:2; 1901.27Ssimonb uint8_t flag; 1911.1Scgd}; 1921.1Scgdstruct foo *foohead; /* Head of global foo list */ 1931.2Scgd 1941.2Scgd/* Make the structure name match the typedef. */ 1951.12Slukemtypedef struct BAR { 1961.12Slukem int level; 1971.2Scgd} BAR; 1981.12Slukem 1991.32Sjunyoung/* C99 uintN_t is preferred over u_intN_t. */ 2001.32Sjunyounguint32_t zero; 2011.32Sjunyoung 2021.1Scgd/* 2031.1Scgd * All major routines should have a comment briefly describing what 2041.2Scgd * they do. The comment before the "main" routine should describe 2051.1Scgd * what the program does. 2061.1Scgd */ 2071.2Scgdint 2081.12Slukemmain(int argc, char *argv[]) 2091.1Scgd{ 2101.1Scgd long num; 2111.1Scgd int ch; 2121.1Scgd char *ep; 2131.1Scgd 2141.1Scgd /* 2151.17Scgd * At the start of main(), call setprogname() to set the program 2161.17Scgd * name. This does nothing on NetBSD, but increases portability 2171.17Scgd * to other systems. 2181.17Scgd */ 2191.17Scgd setprogname(argv[0]); 2201.17Scgd 2211.17Scgd /* 2221.37Swiz * For consistency, getopt should be used to parse options. 2231.37Swiz * Options should be sorted in the getopt call and the switch 2241.37Swiz * statement, unless parts of the switch cascade. For the 2251.37Swiz * sorting order, see the usage() example below. Don't forget 2261.37Swiz * to add option descriptions to the usage and the manpage. 2271.37Swiz * Elements in a switch statement that cascade should have a 2281.37Swiz * FALLTHROUGH comment. Numerical arguments should be checked 2291.37Swiz * for accuracy. Code that cannot be reached should have a 2301.37Swiz * NOTREACHED comment. 2311.1Scgd */ 2321.41Splunky while ((ch = getopt(argc, argv, "abn:")) != -1) { 2331.1Scgd switch (ch) { /* Indent the switch. */ 2341.1Scgd case 'a': /* Don't indent the case. */ 2351.1Scgd aflag = 1; 2361.1Scgd /* FALLTHROUGH */ 2371.1Scgd case 'b': 2381.1Scgd bflag = 1; 2391.1Scgd break; 2401.1Scgd case 'n': 2411.25Slukem errno = 0; 2421.1Scgd num = strtol(optarg, &ep, 10); 2431.25Slukem if (num <= 0 || *ep != '\0' || (errno == ERANGE && 2441.56Slukem (num == LONG_MAX || num == LONG_MIN)) ) { 2451.12Slukem errx(1, "illegal number -- %s", optarg); 2461.56Slukem } 2471.1Scgd break; 2481.1Scgd case '?': 2491.1Scgd default: 2501.1Scgd usage(); 2511.2Scgd /* NOTREACHED */ 2521.1Scgd } 2531.12Slukem } 2541.1Scgd argc -= optind; 2551.1Scgd argv += optind; 2561.1Scgd 2571.1Scgd /* 2581.56Slukem * Space after keywords (while, for, return, switch). 2591.56Slukem * Braces are preferred for control statements 2601.56Slukem * with only a single statement. 2611.1Scgd * 2621.1Scgd * Forever loops are done with for's, not while's. 2631.1Scgd */ 2641.56Slukem for (p = buf; *p != '\0'; ++p) { 2651.12Slukem continue; /* Explicit no-op */ 2661.56Slukem } 2671.56Slukem for (;;) { 2681.1Scgd stmt; 2691.56Slukem } 2701.12Slukem 2711.1Scgd /* 2721.38Schristos * Braces are required for control statements with a single statement 2731.38Schristos * that may expand to nothing. 2741.38Schristos */ 2751.38Schristos#ifdef DEBUG_FOO 2761.40Schristos#define DPRINTF(a) printf a 2771.40Schristos#else 2781.38Schristos#define DPRINTF(a) 2791.38Schristos#endif 2801.38Schristos if (broken) { 2811.38Schristos DPRINTF(("broken is %d\n", broken)); 2821.38Schristos } 2831.38Schristos 2841.38Schristos /* 2851.2Scgd * Parts of a for loop may be left empty. Don't put declarations 2861.2Scgd * inside blocks unless the routine is unusually complicated. 2871.1Scgd */ 2881.1Scgd for (; cnt < 15; cnt++) { 2891.1Scgd stmt1; 2901.1Scgd stmt2; 2911.1Scgd } 2921.1Scgd 2931.2Scgd /* Second level indents are four spaces. */ 2941.56Slukem while (cnt < 20) { 2951.40Schristos z = a + really + long + statement + that + needs + two + lines + 2961.1Scgd gets + indented + four + spaces + on + the + second + 2971.7Senami and + subsequent + lines; 2981.56Slukem } 2991.1Scgd 3001.1Scgd /* 3011.2Scgd * Closing and opening braces go on the same line as the else. 3021.1Scgd */ 3031.12Slukem if (test) { 3041.12Slukem /* 3051.12Slukem * I have a long comment here. 3061.12Slukem */ 3071.12Slukem#ifdef zorro 3081.12Slukem z = 1; 3091.12Slukem#else 3101.12Slukem b = 3; 3111.12Slukem#endif 3121.12Slukem } else if (bar) { 3131.1Scgd stmt; 3141.1Scgd stmt; 3151.56Slukem } else { 3161.1Scgd stmt; 3171.56Slukem } 3181.12Slukem 3191.2Scgd /* No spaces after function names. */ 3201.56Slukem if ((result = function(a1, a2, a3, a4)) == NULL) { 3211.12Slukem exit(1); 3221.56Slukem } 3231.1Scgd 3241.1Scgd /* 3251.12Slukem * Unary operators don't require spaces, binary operators do. 3261.12Slukem * Don't excessively use parenthesis, but they should be used if 3271.9Slukem * statement is really confusing without them, such as: 3281.9Slukem * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1; 3291.1Scgd */ 3301.9Slukem a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1); 3311.2Scgd k = !(l & FLAGS); 3321.1Scgd 3331.1Scgd /* 3341.26Sjmmv * Exits should be EXIT_SUCCESS on success, and EXIT_FAILURE on 3351.26Sjmmv * failure. Don't denote all the possible exit points, using the 3361.29Schristos * integers 1 through 127. Avoid obvious comments such as "Exit 3371.29Schristos * 0 on success.". Since main is a function that returns an int, 3381.29Schristos * prefer returning from it, than calling exit. 3391.1Scgd */ 3401.29Schristos return EXIT_SUCCESS; 3411.1Scgd} 3421.1Scgd 3431.1Scgd/* 3441.8Ssimonb * The function type must be declared on a line by itself 3451.16Senami * preceding the function. 3461.1Scgd */ 3471.1Scgdstatic char * 3481.12Slukemfunction(int a1, int a2, float fl, int a4) 3491.1Scgd{ 3501.1Scgd /* 3511.1Scgd * When declaring variables in functions declare them sorted by size, 3521.12Slukem * then in alphabetical order; multiple ones per line are okay. 3531.12Slukem * Function prototypes should go in the include file "extern.h". 3541.1Scgd * If a line overflows reuse the type keyword. 3551.1Scgd * 3561.52Schristos * Avoid initializing variables in the declarations; move 3571.52Schristos * declarations next to their first use, and initialize 3581.52Schristos * opportunistically. This avoids over-initialization and 3591.52Schristos * accidental bugs caused by declaration reordering. 3601.1Scgd */ 3611.1Scgd extern u_char one; 3621.1Scgd extern char two; 3631.1Scgd struct foo three, *four; 3641.1Scgd double five; 3651.12Slukem int *six, seven; 3661.12Slukem char *eight, *nine, ten, eleven, twelve, thirteen; 3671.12Slukem char fourteen, fifteen, sixteen; 3681.1Scgd 3691.1Scgd /* 3701.1Scgd * Casts and sizeof's are not followed by a space. NULL is any 3711.1Scgd * pointer type, and doesn't need to be cast, so use NULL instead 3721.1Scgd * of (struct foo *)0 or (struct foo *)NULL. Also, test pointers 3731.12Slukem * against NULL. I.e. use: 3741.1Scgd * 3751.12Slukem * (p = f()) == NULL 3761.1Scgd * not: 3771.1Scgd * !(p = f()) 3781.2Scgd * 3791.51Schristos * The notable exception here is variadic functions. Since our 3801.49Schristos * code is designed to compile and work on different environments 3811.49Schristos * where we don't have control over the NULL definition (on NetBSD 3821.49Schristos * it is defined as ((void *)0), but on other systems it can be 3831.49Schristos * defined as (0) and both definitions are valid under ANSI C), it 3841.53Ssalazar * it advised to cast NULL to a pointer on variadic functions, 3851.49Schristos * because on machines where sizeof(pointer) != sizeof(int) and in 3861.49Schristos * the absence of a prototype in scope, passing an un-casted NULL, 3871.49Schristos * will result in passing an int on the stack instead of a pointer. 3881.49Schristos * 3891.12Slukem * Don't use `!' for tests unless it's a boolean. 3901.12Slukem * E.g. use "if (*p == '\0')", not "if (!*p)". 3911.12Slukem * 3921.31Schristos * Routines returning ``void *'' should not have their return 3931.31Schristos * values cast to more specific pointer types. 3941.2Scgd * 3951.46Schristos * Prefer sizeof(*var) over sizeof(type) because if type changes, 3961.46Schristos * the change needs to be done in one place. 3971.46Schristos * 3981.2Scgd * Use err/warn(3), don't roll your own! 3991.1Scgd */ 4001.56Slukem if ((four = malloc(sizeof(*four))) == NULL) { 4011.2Scgd err(1, NULL); 4021.56Slukem } 4031.56Slukem if ((six = (int *)overflow()) == NULL) { 4041.2Scgd errx(1, "Number overflowed."); 4051.56Slukem } 4061.23Sfvdl 4071.23Sfvdl /* No parentheses are needed around the return value. */ 4081.23Sfvdl return eight; 4091.1Scgd} 4101.1Scgd 4111.2Scgd/* 4121.12Slukem * Use ANSI function declarations. ANSI function braces look like 4131.12Slukem * old-style (K&R) function braces. 4141.12Slukem * As per the wrapped prototypes, use your discretion on how to format 4151.12Slukem * the subsequent lines. 4161.12Slukem */ 4171.12Slukemstatic int 4181.12Slukemdirinfo(const char *p, struct stat *sb, struct dirent *de, struct statfs *sf, 4191.12Slukem int *rargc, char **rargv[]) 4201.12Slukem{ /* Insert an empty line if the function has no local variables. */ 4211.19Skleink 4221.19Skleink /* 4231.19Skleink * In system libraries, catch obviously invalid function arguments 4241.19Skleink * using _DIAGASSERT(3). 4251.19Skleink */ 4261.19Skleink _DIAGASSERT(p != NULL); 4271.19Skleink _DIAGASSERT(filedesc != -1); 4281.12Slukem 4291.56Slukem if (stat(p, sb) < 0) { 4301.14Slukem err(1, "Unable to stat %s", p); 4311.56Slukem } 4321.14Slukem 4331.14Slukem /* 4341.53Ssalazar * To printf quantities that might be larger than "long", include 4351.36Sbriggs * <inttypes.h>, cast quantities to intmax_t or uintmax_t and use 4361.42Sapb * PRI?MAX constants. 4371.36Sbriggs */ 4381.36Sbriggs (void)printf("The size of %s is %" PRIdMAX " (%#" PRIxMAX ")\n", p, 4391.36Sbriggs (intmax_t)sb->st_size, (uintmax_t)sb->st_size); 4401.36Sbriggs 4411.36Sbriggs /* 4421.36Sbriggs * To printf quantities of known bit-width, use the corresponding 4431.36Sbriggs * defines (generally only done within NetBSD for quantities that 4441.36Sbriggs * exceed 32-bits). 4451.36Sbriggs */ 4461.36Sbriggs (void)printf("%s uses %" PRId64 " blocks and has flags %#" PRIx32 "\n", 4471.36Sbriggs p, sb->st_blocks, sb->st_flags); 4481.36Sbriggs 4491.36Sbriggs /* 4501.36Sbriggs * There are similar constants that should be used with the *scanf(3) 4511.36Sbriggs * family of functions: SCN?MAX, SCN?64, etc. 4521.14Slukem */ 4531.2Scgd} 4541.2Scgd 4551.12Slukem/* 4561.12Slukem * Functions that support variable numbers of arguments should look like this. 4571.12Slukem * (With the #include <stdarg.h> appearing at the top of the file with the 4581.44Sjschauma * other include files.) 4591.12Slukem */ 4601.2Scgd#include <stdarg.h> 4611.2Scgd 4621.2Scgdvoid 4631.2Scgdvaf(const char *fmt, ...) 4641.2Scgd{ 4651.2Scgd va_list ap; 4661.12Slukem 4671.2Scgd va_start(ap, fmt); 4681.2Scgd STUFF; 4691.45Sdholland va_end(ap); 4701.12Slukem /* No return needed for void functions. */ 4711.1Scgd} 4721.1Scgd 4731.1Scgdstatic void 4741.12Slukemusage(void) 4751.12Slukem{ 4761.1Scgd 4771.1Scgd /* 4781.1Scgd * Use printf(3), not fputs/puts/putchar/whatever, it's faster and 4791.1Scgd * usually cleaner, not to mention avoiding stupid bugs. 4801.12Slukem * Use snprintf(3) or strlcpy(3)/strlcat(3) instead of sprintf(3); 4811.12Slukem * again to avoid stupid bugs. 4821.1Scgd * 4831.37Swiz * Usage statements should look like the manual pages. 4841.37Swiz * Options w/o operands come first, in alphabetical order 4851.37Swiz * inside a single set of braces, upper case before lower case 4861.37Swiz * (AaBbCc...). Next are options with operands, in the same 4871.37Swiz * order, each in braces. Then required arguments in the 4881.37Swiz * order they are specified, followed by optional arguments in 4891.37Swiz * the order they are specified. A bar (`|') separates 4901.37Swiz * either/or options/arguments, and multiple options/arguments 4911.37Swiz * which are specified together are placed in a single set of 4921.37Swiz * braces. 4931.1Scgd * 4941.17Scgd * Use getprogname() instead of hardcoding the program name. 4951.12Slukem * 4961.37Swiz * "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n" 4971.1Scgd * "usage: f [-a | -b] [-c [-de] [-n number]]\n" 4981.1Scgd */ 4991.17Scgd (void)fprintf(stderr, "usage: %s [-ab]\n", getprogname()); 5001.33Srillig exit(EXIT_FAILURE); 5011.1Scgd} 502