style revision 1.77
11.77Srin/* $NetBSD: style,v 1.77 2024/01/29 05:42:41 rin 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.77Srin__RCSID("$NetBSD: style,v 1.77 2024/01/29 05:42:41 rin 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.69Sriastrad 601.69Sriastrad/* 611.75Sriastrad * Include other header files only as necessary, mainly for type 621.75Sriastrad * definitions or macros that are necessary to use in this header file. 631.75Sriastrad * 641.75Sriastrad * Avoid relying on transitive inclusions. 651.75Sriastrad * 661.75Sriastrad * Avoid header files dependencies just for struct and union types that 671.77Srin * are used in pointer types, which don't require type definitions. 681.75Sriastrad * Instead, use forward declarations of the struct or union tag. 691.75Sriastrad */ 701.75Sriastrad#include <sys/foobar.h> 711.75Sriastrad 721.75Sriastrad/* 731.75Sriastrad * Forward declarations for struct and union tags that don't need 741.75Sriastrad * definitions go next. 751.75Sriastrad */ 761.75Sriastradstruct dirent; 771.75Sriastrad 781.75Sriastrad/* 791.75Sriastrad * Define public structs and unions, only if they are user-allocated or 801.75Sriastrad * otherwise exposed to users for a good reason; otherwise keep them 811.75Sriastrad * private to .c files or `_impl.h' or `_private.h' files. 821.75Sriastrad * 831.75Sriastrad * Do not create a typedef like `typedef struct example example_t;' or 841.75Sriastrad * `typedef struct example *example_t;'. Use `struct example' or 851.75Sriastrad * `struct example *' in the public API; that way, other header files 861.75Sriastrad * which declare functions or define struct or union types that involve 871.75Sriastrad * only pointers to `struct example' need not pull in unnecessary 881.75Sriastrad * header files. 891.75Sriastrad */ 901.75Sriastradstruct example { 911.75Sriastrad struct data *p; 921.75Sriastrad int x; 931.75Sriastrad char y; 941.75Sriastrad}; 951.75Sriastrad 961.75Sriastrad/* 971.75Sriastrad * Use typedefs judiciously. 981.75Sriastrad * 991.75Sriastrad * Function or function pointer types: 1001.75Sriastrad */ 1011.75Sriastradtypedef void sighandler_t(int); 1021.75Sriastrad 1031.75Sriastrad/* 1041.75Sriastrad * Aliases for arithmetic types: 1051.75Sriastrad */ 1061.75Sriastradtypedef uint16_t nlink_t; 1071.75Sriastrad 1081.75Sriastrad/* 1091.75Sriastrad * Types that might be defined differently in some contexts, like 1101.75Sriastrad * uint8_t on one port, a pointer to a struct on another port, and an 1111.75Sriastrad * in-line struct larger than a pointer on a third port: 1121.75Sriastrad */ 1131.75Sriastradtypedef uint8_t foo_t; /* Hypothetical leg26 definition */ 1141.75Sriastradtypedef struct foo *foo_t; /* Hypothetical i786 definition */ 1151.75Sriastradtypedef struct { /* Hypothetical risc72 definition */ 1161.75Sriastrad uint32_t p; 1171.75Sriastrad uint32_t q; 1181.75Sriastrad uint8_t t; 1191.75Sriastrad} foo_t; 1201.75Sriastrad 1211.75Sriastrad/* 1221.75Sriastrad * For opaque data structures that are always represented by a pointer 1231.75Sriastrad * when stored in other data structures or passed to functions, don't 1241.75Sriastrad * use a type `foo_t' with `typedef void *foo_t'. Use `struct foo *' 1251.75Sriastrad * with no public definition for `struct foo', so the compiler can 1261.75Sriastrad * detect type errors, and other header files can use `struct foo *' 1271.75Sriastrad * without creating header file dependencies. 1281.75Sriastrad */ 1291.75Sriastrad 1301.75Sriastrad/* 1311.69Sriastrad * extern declarations must only appear in header files, not in .c 1321.69Sriastrad * files, so the same declaration is used by the .c file defining it 1331.69Sriastrad * and the .c file using it, giving the compiler the opportunity to 1341.69Sriastrad * detect type errors. 1351.69Sriastrad * 1361.69Sriastrad * extern function declarations should not use the extern keyword, 1371.69Sriastrad * which is unnecessary. 1381.69Sriastrad * 1391.69Sriastrad * Exception: A subroutine written in assembly in an adjacent .S file, 1401.69Sriastrad * which is used only in one .c file, may be declared in the .c file. 1411.69Sriastrad */ 1421.69Sriastradextern int frotz; 1431.69Sriastrad 1441.75Sriastradint frobnicate(const char *, struct dirent *, foobar_t); 1451.69Sriastrad 1461.12Slukem/* 1471.12Slukem * Contents of #include file go between the #ifndef and the #endif at the end. 1481.12Slukem */ 1491.12Slukem#endif /* !_SYS_SOCKET_H_ */ 1501.12Slukem/* 1511.12Slukem * END OF EXAMPLE HEADER FILE. 1521.12Slukem */ 1531.12Slukem 1541.12Slukem/* 1551.39Sdarcy * If a header file requires structures, defines, typedefs, etc. from 1561.39Sdarcy * another header file it should include that header file and not depend 1571.39Sdarcy * on the including file for that header including both. If there are 1581.39Sdarcy * exceptions to this for specific headers it should be clearly documented 1591.39Sdarcy * in the headers and, if appropriate, the documentation. Nothing in this 1601.39Sdarcy * rule should suggest relaxation of the multiple inclusion rule and the 1611.39Sdarcy * application programmer should be free to include both regardless. 1621.39Sdarcy */ 1631.39Sdarcy 1641.39Sdarcy/* 1651.12Slukem * Kernel include files come first. 1661.2Scgd */ 1671.50Sriastrad#include <sys/param.h> /* <sys/param.h> first, */ 1681.50Sriastrad#include <sys/types.h> /* <sys/types.h> next, */ 1691.50Sriastrad#include <sys/ioctl.h> /* and then the rest, */ 1701.50Sriastrad#include <sys/socket.h> /* sorted lexicographically. */ 1711.50Sriastrad#include <sys/stat.h> 1721.50Sriastrad#include <sys/wait.h> /* Non-local includes in brackets. */ 1731.2Scgd 1741.12Slukem/* 1751.12Slukem * If it's a network program, put the network include files next. 1761.60Srillig * Group the include files by subdirectory. 1771.12Slukem */ 1781.2Scgd#include <net/if.h> 1791.2Scgd#include <net/if_dl.h> 1801.2Scgd#include <net/route.h> 1811.2Scgd#include <netinet/in.h> 1821.2Scgd#include <protocols/rwhod.h> 1831.2Scgd 1841.2Scgd/* 1851.2Scgd * Then there's a blank line, followed by the /usr include files. 1861.50Sriastrad * The /usr include files should be sorted lexicographically! 1871.2Scgd */ 1881.20Skleink#include <assert.h> 1891.25Slukem#include <errno.h> 1901.36Sbriggs#include <inttypes.h> 1911.2Scgd#include <stdio.h> 1921.18Scgd#include <stdlib.h> 1931.1Scgd 1941.1Scgd/* 1951.1Scgd * Global pathnames are defined in /usr/include/paths.h. Pathnames local 1961.1Scgd * to the program go in pathnames.h in the local directory. 1971.1Scgd */ 1981.2Scgd#include <paths.h> 1991.2Scgd 2001.2Scgd/* Then, there's a blank line, and the user include files. */ 2011.12Slukem#include "pathnames.h" /* Local includes in double quotes. */ 2021.1Scgd 2031.1Scgd/* 2041.74Srillig * Declarations for file-static functions go at the top of the file. 2051.74Srillig * Don't associate a name with the parameter types. I.e. use: 2061.12Slukem * void function(int); 2071.12Slukem * Use your discretion on indenting between the return type and the name, and 2081.12Slukem * how to wrap a prototype too long for a single line. In the latter case, 2091.15Slukem * lining up under the initial left parenthesis may be more readable. 2101.12Slukem * In any case, consistency is important! 2111.12Slukem */ 2121.12Slukemstatic char *function(int, int, float, int); 2131.12Slukemstatic int dirinfo(const char *, struct stat *, struct dirent *, 2141.12Slukem struct statfs *, int *, char **[]); 2151.47Schristosstatic void usage(void) __dead; /* declare functions that don't return dead */ 2161.1Scgd 2171.1Scgd/* 2181.1Scgd * Macros are capitalized, parenthesized, and should avoid side-effects. 2191.22Sjhawk * Spacing before and after the macro name may be any whitespace, though 2201.22Sjhawk * use of TABs should be consistent through a file. 2211.1Scgd * If they are an inline expansion of a function, the function is defined 2221.12Slukem * all in lowercase, the macro has the same name all in uppercase. 2231.72Srillig * If the macro is an expression, wrap the expression in parentheses. 2241.64Srillig * If the macro is more than a single statement, use ``do { ... } while (0)'' 2251.64Srillig * or ``do { ... } while (false)'', so that a trailing semicolon works. 2261.64Srillig * Right-justify the backslashes; it makes it easier to read. 2271.12Slukem */ 2281.12Slukem#define MACRO(v, w, x, y) \ 2291.12Slukemdo { \ 2301.12Slukem v = (x) + (y); \ 2311.12Slukem w = (y) + 2; \ 2321.64Srillig} while (0) 2331.12Slukem 2341.15Slukem#define DOUBLE(x) ((x) * 2) 2351.12Slukem 2361.55Srillig/* Enum constants are capitalized. No comma on the last element. */ 2371.12Slukemenum enumtype { 2381.12Slukem ONE, 2391.12Slukem TWO 2401.63Srillig}; 2411.12Slukem 2421.12Slukem/* 2431.54Schristos * Sometimes we want a macro to be conditionally defined for debugging 2441.54Schristos * and expand to nothing (but still as statement) when we are not debugging: 2451.54Schristos */ 2461.54Schristos#ifdef FOO_DEBUG 2471.54Schristos# define DPRINTF(...) printf(__VA_ARGS__) 2481.54Schristos#else 2491.54Schristos# define DPRINTF(...) __nothing 2501.54Schristos#endif 2511.54Schristos 2521.54Schristos/* 2531.16Senami * When declaring variables in structures, declare them organized by use in 2541.16Senami * a manner to attempt to minimize memory wastage because of compiler alignment 2551.12Slukem * issues, then by size, and then by alphabetical order. E.g, don't use 2561.12Slukem * ``int a; char *b; int c; char *d''; use ``int a; int b; char *c; char *d''. 2571.12Slukem * Each variable gets its own type and line, although an exception can be made 2581.12Slukem * when declaring bitfields (to clarify that it's part of the one bitfield). 2591.12Slukem * Note that the use of bitfields in general is discouraged. 2601.1Scgd * 2611.2Scgd * Major structures should be declared at the top of the file in which they 2621.2Scgd * are used, or in separate header files, if they are used in multiple 2631.2Scgd * source files. Use of the structures should be by separate declarations 2641.1Scgd * and should be "extern" if they are declared in a header file. 2651.12Slukem * 2661.12Slukem * It may be useful to use a meaningful prefix for each member name. 2671.12Slukem * E.g, for ``struct softc'' the prefix could be ``sc_''. 2681.75Sriastrad * 2691.75Sriastrad * Don't create typedef aliases for struct or union types. That way, 2701.75Sriastrad * other header files can use pointer types to them without the header 2711.75Sriastrad * file defining the typedef. 2721.1Scgd */ 2731.1Scgdstruct foo { 2741.12Slukem struct foo *next; /* List of active foo */ 2751.12Slukem struct mumble amumble; /* Comment for mumble */ 2761.12Slukem int bar; 2771.12Slukem unsigned int baz:1, /* Bitfield; line up entries if desired */ 2781.12Slukem fuz:5, 2791.12Slukem zap:2; 2801.27Ssimonb uint8_t flag; 2811.1Scgd}; 2821.1Scgdstruct foo *foohead; /* Head of global foo list */ 2831.2Scgd 2841.32Sjunyoung/* C99 uintN_t is preferred over u_intN_t. */ 2851.32Sjunyounguint32_t zero; 2861.32Sjunyoung 2871.1Scgd/* 2881.1Scgd * All major routines should have a comment briefly describing what 2891.2Scgd * they do. The comment before the "main" routine should describe 2901.1Scgd * what the program does. 2911.1Scgd */ 2921.2Scgdint 2931.12Slukemmain(int argc, char *argv[]) 2941.1Scgd{ 2951.1Scgd long num; 2961.1Scgd int ch; 2971.1Scgd char *ep; 2981.1Scgd 2991.1Scgd /* 3001.17Scgd * At the start of main(), call setprogname() to set the program 3011.17Scgd * name. This does nothing on NetBSD, but increases portability 3021.17Scgd * to other systems. 3031.17Scgd */ 3041.17Scgd setprogname(argv[0]); 3051.17Scgd 3061.17Scgd /* 3071.37Swiz * For consistency, getopt should be used to parse options. 3081.37Swiz * Options should be sorted in the getopt call and the switch 3091.37Swiz * statement, unless parts of the switch cascade. For the 3101.37Swiz * sorting order, see the usage() example below. Don't forget 3111.37Swiz * to add option descriptions to the usage and the manpage. 3121.37Swiz * Elements in a switch statement that cascade should have a 3131.37Swiz * FALLTHROUGH comment. Numerical arguments should be checked 3141.37Swiz * for accuracy. Code that cannot be reached should have a 3151.37Swiz * NOTREACHED comment. 3161.1Scgd */ 3171.41Splunky while ((ch = getopt(argc, argv, "abn:")) != -1) { 3181.1Scgd switch (ch) { /* Indent the switch. */ 3191.1Scgd case 'a': /* Don't indent the case. */ 3201.1Scgd aflag = 1; 3211.1Scgd /* FALLTHROUGH */ 3221.1Scgd case 'b': 3231.1Scgd bflag = 1; 3241.1Scgd break; 3251.1Scgd case 'n': 3261.25Slukem errno = 0; 3271.1Scgd num = strtol(optarg, &ep, 10); 3281.25Slukem if (num <= 0 || *ep != '\0' || (errno == ERANGE && 3291.58Sriastrad (num == LONG_MAX || num == LONG_MIN)) ) { 3301.12Slukem errx(1, "illegal number -- %s", optarg); 3311.58Sriastrad } 3321.1Scgd break; 3331.1Scgd case '?': 3341.1Scgd default: 3351.1Scgd usage(); 3361.2Scgd /* NOTREACHED */ 3371.1Scgd } 3381.12Slukem } 3391.1Scgd argc -= optind; 3401.1Scgd argv += optind; 3411.1Scgd 3421.1Scgd /* 3431.58Sriastrad * Space after keywords (while, for, return, switch). 3441.58Sriastrad * 3451.58Sriastrad * Braces around single-line bodies are optional; use discretion. 3461.1Scgd * 3471.66Sjkoshy * Use narrow scopes for loop variables where possible. 3481.1Scgd */ 3491.66Sjkoshy for (char *p = buf; *p != '\0'; ++p) 3501.12Slukem continue; /* Explicit no-op */ 3511.67Sjkoshy 3521.67Sjkoshy /* 3531.67Sjkoshy * Forever loops are done with for's, not while's. 3541.67Sjkoshy */ 3551.57Slukem for (;;) 3561.1Scgd stmt; 3571.12Slukem 3581.1Scgd /* 3591.2Scgd * Parts of a for loop may be left empty. Don't put declarations 3601.2Scgd * inside blocks unless the routine is unusually complicated. 3611.1Scgd */ 3621.1Scgd for (; cnt < 15; cnt++) { 3631.1Scgd stmt1; 3641.1Scgd stmt2; 3651.1Scgd } 3661.1Scgd 3671.2Scgd /* Second level indents are four spaces. */ 3681.58Sriastrad while (cnt < 20) { 3691.40Schristos z = a + really + long + statement + that + needs + two + lines + 3701.1Scgd gets + indented + four + spaces + on + the + second + 3711.7Senami and + subsequent + lines; 3721.58Sriastrad } 3731.1Scgd 3741.1Scgd /* 3751.2Scgd * Closing and opening braces go on the same line as the else. 3761.1Scgd */ 3771.12Slukem if (test) { 3781.12Slukem /* 3791.12Slukem * I have a long comment here. 3801.12Slukem */ 3811.12Slukem#ifdef zorro 3821.12Slukem z = 1; 3831.12Slukem#else 3841.12Slukem b = 3; 3851.12Slukem#endif 3861.12Slukem } else if (bar) { 3871.1Scgd stmt; 3881.1Scgd stmt; 3891.58Sriastrad } else { 3901.1Scgd stmt; 3911.58Sriastrad } 3921.12Slukem 3931.2Scgd /* No spaces after function names. */ 3941.57Slukem if ((result = function(a1, a2, a3, a4)) == NULL) 3951.68Sjschauma exit(EXIT_FAILURE); 3961.1Scgd 3971.1Scgd /* 3981.12Slukem * Unary operators don't require spaces, binary operators do. 3991.72Srillig * Don't excessively use parentheses, but they should be used if a 4001.9Slukem * statement is really confusing without them, such as: 4011.9Slukem * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1; 4021.1Scgd */ 4031.9Slukem a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1); 4041.2Scgd k = !(l & FLAGS); 4051.1Scgd 4061.1Scgd /* 4071.26Sjmmv * Exits should be EXIT_SUCCESS on success, and EXIT_FAILURE on 4081.26Sjmmv * failure. Don't denote all the possible exit points, using the 4091.29Schristos * integers 1 through 127. Avoid obvious comments such as "Exit 4101.29Schristos * 0 on success.". Since main is a function that returns an int, 4111.29Schristos * prefer returning from it, than calling exit. 4121.1Scgd */ 4131.29Schristos return EXIT_SUCCESS; 4141.1Scgd} 4151.1Scgd 4161.1Scgd/* 4171.8Ssimonb * The function type must be declared on a line by itself 4181.16Senami * preceding the function. 4191.1Scgd */ 4201.1Scgdstatic char * 4211.12Slukemfunction(int a1, int a2, float fl, int a4) 4221.1Scgd{ 4231.1Scgd /* 4241.71Srillig * When declaring variables in functions, multiple variables per line 4251.71Srillig * are okay. If a line overflows reuse the type keyword. 4261.71Srillig * 4271.73Sdholland * Function prototypes and external data declarations should go in a 4281.73Sdholland * suitable include file. 4291.1Scgd * 4301.52Schristos * Avoid initializing variables in the declarations; move 4311.52Schristos * declarations next to their first use, and initialize 4321.52Schristos * opportunistically. This avoids over-initialization and 4331.52Schristos * accidental bugs caused by declaration reordering. 4341.1Scgd */ 4351.1Scgd struct foo three, *four; 4361.1Scgd double five; 4371.12Slukem int *six, seven; 4381.12Slukem char *eight, *nine, ten, eleven, twelve, thirteen; 4391.12Slukem char fourteen, fifteen, sixteen; 4401.1Scgd 4411.1Scgd /* 4421.62Schristos * Casts and sizeof's are not followed by a space. 4431.62Schristos * 4441.62Schristos * We parenthesize sizeof expressions to clarify their precedence: 4451.62Schristos * 4461.62Schristos * sizeof(e) + 4 4471.62Schristos * not: 4481.62Schristos * sizeof e + 4 4491.62Schristos * 4501.62Schristos * We don't put a space before the parenthesis so that it looks like 4511.62Schristos * a function call. We always parenthesize the sizeof expression for 4521.62Schristos * consistency. 4531.62Schristos * 4541.62Schristos * On the other hand, we don't parenthesize the return statement 4551.62Schristos * because there is never a precedence ambiguity situation (it is 4561.62Schristos * a single statement). 4571.62Schristos * 4581.62Schristos * NULL is any pointer type, and doesn't need to be cast, so use 4591.62Schristos * NULL instead of (struct foo *)0 or (struct foo *)NULL. Also, 4601.62Schristos * test pointers against NULL because it indicates the type of the 4611.62Schristos * expression to the user. I.e. use: 4621.1Scgd * 4631.12Slukem * (p = f()) == NULL 4641.1Scgd * not: 4651.1Scgd * !(p = f()) 4661.2Scgd * 4671.51Schristos * The notable exception here is variadic functions. Since our 4681.49Schristos * code is designed to compile and work on different environments 4691.49Schristos * where we don't have control over the NULL definition (on NetBSD 4701.49Schristos * it is defined as ((void *)0), but on other systems it can be 4711.74Srillig * defined as (0) and both definitions are valid), it 4721.76Srin * is advised to cast NULL to a pointer on variadic functions, 4731.49Schristos * because on machines where sizeof(pointer) != sizeof(int) and in 4741.49Schristos * the absence of a prototype in scope, passing an un-casted NULL, 4751.49Schristos * will result in passing an int on the stack instead of a pointer. 4761.49Schristos * 4771.12Slukem * Don't use `!' for tests unless it's a boolean. 4781.12Slukem * E.g. use "if (*p == '\0')", not "if (!*p)". 4791.12Slukem * 4801.31Schristos * Routines returning ``void *'' should not have their return 4811.31Schristos * values cast to more specific pointer types. 4821.2Scgd * 4831.46Schristos * Prefer sizeof(*var) over sizeof(type) because if type changes, 4841.46Schristos * the change needs to be done in one place. 4851.46Schristos * 4861.2Scgd * Use err/warn(3), don't roll your own! 4871.61Schristos * 4881.61Schristos * Prefer EXIT_FAILURE instead of random error codes. 4891.1Scgd */ 4901.57Slukem if ((four = malloc(sizeof(*four))) == NULL) 4911.61Schristos err(EXIT_FAILURE, NULL); 4921.57Slukem if ((six = (int *)overflow()) == NULL) 4931.61Schristos errx(EXIT_FAILURE, "Number overflowed."); 4941.23Sfvdl 4951.23Sfvdl /* No parentheses are needed around the return value. */ 4961.23Sfvdl return eight; 4971.1Scgd} 4981.1Scgd 4991.2Scgd/* 5001.74Srillig * Place the opening brace of a function body in column 1. 5011.12Slukem * As per the wrapped prototypes, use your discretion on how to format 5021.12Slukem * the subsequent lines. 5031.12Slukem */ 5041.12Slukemstatic int 5051.12Slukemdirinfo(const char *p, struct stat *sb, struct dirent *de, struct statfs *sf, 5061.12Slukem int *rargc, char **rargv[]) 5071.12Slukem{ /* Insert an empty line if the function has no local variables. */ 5081.19Skleink 5091.19Skleink /* 5101.19Skleink * In system libraries, catch obviously invalid function arguments 5111.19Skleink * using _DIAGASSERT(3). 5121.19Skleink */ 5131.19Skleink _DIAGASSERT(p != NULL); 5141.19Skleink _DIAGASSERT(filedesc != -1); 5151.12Slukem 5161.61Schristos /* Prefer checking syscalls against -1 instead of < 0 */ 5171.61Schristos if (stat(p, sb) == -1) 5181.61Schristos err(EXIT_FAILURE, "Unable to stat %s", p); 5191.14Slukem 5201.14Slukem /* 5211.61Schristos * To printf quantities that might be larger than "long", 5221.65Sjkoshy * cast quantities to intmax_t or uintmax_t and use %j. 5231.36Sbriggs */ 5241.61Schristos (void)printf("The size of %s is %jd (%#ju)\n", p, 5251.36Sbriggs (intmax_t)sb->st_size, (uintmax_t)sb->st_size); 5261.36Sbriggs 5271.36Sbriggs /* 5281.61Schristos * To printf quantities of known bit-width, include <inttypes.h> and 5291.61Schristos * use the corresponding defines (generally only done within NetBSD 5301.61Schristos * for quantities that exceed 32-bits). 5311.36Sbriggs */ 5321.36Sbriggs (void)printf("%s uses %" PRId64 " blocks and has flags %#" PRIx32 "\n", 5331.36Sbriggs p, sb->st_blocks, sb->st_flags); 5341.36Sbriggs 5351.36Sbriggs /* 5361.36Sbriggs * There are similar constants that should be used with the *scanf(3) 5371.36Sbriggs * family of functions: SCN?MAX, SCN?64, etc. 5381.14Slukem */ 5391.2Scgd} 5401.2Scgd 5411.12Slukem/* 5421.12Slukem * Functions that support variable numbers of arguments should look like this. 5431.12Slukem * (With the #include <stdarg.h> appearing at the top of the file with the 5441.44Sjschauma * other include files.) 5451.12Slukem */ 5461.2Scgd#include <stdarg.h> 5471.2Scgd 5481.2Scgdvoid 5491.2Scgdvaf(const char *fmt, ...) 5501.2Scgd{ 5511.2Scgd va_list ap; 5521.12Slukem 5531.2Scgd va_start(ap, fmt); 5541.2Scgd STUFF; 5551.45Sdholland va_end(ap); 5561.12Slukem /* No return needed for void functions. */ 5571.1Scgd} 5581.1Scgd 5591.1Scgdstatic void 5601.12Slukemusage(void) 5611.12Slukem{ 5621.1Scgd 5631.1Scgd /* 5641.1Scgd * Use printf(3), not fputs/puts/putchar/whatever, it's faster and 5651.1Scgd * usually cleaner, not to mention avoiding stupid bugs. 5661.12Slukem * Use snprintf(3) or strlcpy(3)/strlcat(3) instead of sprintf(3); 5671.12Slukem * again to avoid stupid bugs. 5681.1Scgd * 5691.37Swiz * Usage statements should look like the manual pages. 5701.37Swiz * Options w/o operands come first, in alphabetical order 5711.37Swiz * inside a single set of braces, upper case before lower case 5721.37Swiz * (AaBbCc...). Next are options with operands, in the same 5731.37Swiz * order, each in braces. Then required arguments in the 5741.37Swiz * order they are specified, followed by optional arguments in 5751.37Swiz * the order they are specified. A bar (`|') separates 5761.37Swiz * either/or options/arguments, and multiple options/arguments 5771.37Swiz * which are specified together are placed in a single set of 5781.37Swiz * braces. 5791.1Scgd * 5801.17Scgd * Use getprogname() instead of hardcoding the program name. 5811.12Slukem * 5821.37Swiz * "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n" 5831.1Scgd * "usage: f [-a | -b] [-c [-de] [-n number]]\n" 5841.1Scgd */ 5851.17Scgd (void)fprintf(stderr, "usage: %s [-ab]\n", getprogname()); 5861.33Srillig exit(EXIT_FAILURE); 5871.1Scgd} 588