Mon Apr 17 00:37:04 2023 UTC ()
share/misc/style: Don't prescribe using "extern.h" for all prototypes.

That's very 80s.

No objection (or even response, amazingly enough) on tech-userlevel in
two weeks.


(dholland)
diff -r1.72 -r1.73 src/share/misc/style

cvs diff -r1.72 -r1.73 src/share/misc/style (switch to unified diff)

--- src/share/misc/style 2023/04/15 12:22:37 1.72
+++ src/share/misc/style 2023/04/17 00:37:04 1.73
@@ -1,519 +1,520 @@ @@ -1,519 +1,520 @@
1/* $NetBSD: style,v 1.72 2023/04/15 12:22:37 rillig Exp $ */ 1/* $NetBSD: style,v 1.73 2023/04/17 00:37:04 dholland Exp $ */
2 2
3/* 3/*
4 * The revision control tag appears first, with a blank line after it. 4 * The revision control tag appears first, with a blank line after it.
5 * Copyright text appears after the revision control tag. 5 * Copyright text appears after the revision control tag.
6 */ 6 */
7 7
8/* 8/*
9 * The NetBSD source code style guide. 9 * The NetBSD source code style guide.
10 * (Previously known as KNF - Kernel Normal Form). 10 * (Previously known as KNF - Kernel Normal Form).
11 * 11 *
12 * from: @(#)style 1.12 (Berkeley) 3/18/94 12 * from: @(#)style 1.12 (Berkeley) 3/18/94
13 */ 13 */
14/* 14/*
15 * An indent(1) profile approximating the style outlined in 15 * An indent(1) profile approximating the style outlined in
16 * this document lives in /usr/share/misc/indent.pro. It is a 16 * this document lives in /usr/share/misc/indent.pro. It is a
17 * useful tool to assist in converting code to KNF, but indent(1) 17 * useful tool to assist in converting code to KNF, but indent(1)
18 * output generated using this profile must not be considered to 18 * output generated using this profile must not be considered to
19 * be an authoritative reference. 19 * be an authoritative reference.
20 */ 20 */
21 21
22/* 22/*
23 * Source code revision control identifiers appear after any copyright 23 * Source code revision control identifiers appear after any copyright
24 * text. Use the appropriate macros from <sys/cdefs.h>. Usually only one 24 * text. Use the appropriate macros from <sys/cdefs.h>. Usually only one
25 * source file per program contains a __COPYRIGHT() section. 25 * source file per program contains a __COPYRIGHT() section.
26 * Historic Berkeley code may also have an __SCCSID() section. 26 * Historic Berkeley code may also have an __SCCSID() section.
27 * Only one instance of each of these macros can occur in each file. 27 * Only one instance of each of these macros can occur in each file.
28 * Don't use newlines in the identifiers. 28 * Don't use newlines in the identifiers.
29 */ 29 */
30#include <sys/cdefs.h> 30#include <sys/cdefs.h>
31__COPYRIGHT("@(#) Copyright (c) 2008\ 31__COPYRIGHT("@(#) Copyright (c) 2008\
32 The NetBSD Foundation, inc. All rights reserved."); 32 The NetBSD Foundation, inc. All rights reserved.");
33__RCSID("$NetBSD: style,v 1.72 2023/04/15 12:22:37 rillig Exp $"); 33__RCSID("$NetBSD: style,v 1.73 2023/04/17 00:37:04 dholland Exp $");
34 34
35/* 35/*
36 * VERY important single-line comments look like this. 36 * VERY important single-line comments look like this.
37 */ 37 */
38 38
39/* Most single-line comments look like this. */ 39/* Most single-line comments look like this. */
40 40
41/* 41/*
42 * Multi-line comments look like this. Make them real sentences. Fill 42 * Multi-line comments look like this. Make them real sentences. Fill
43 * them so they look like real paragraphs. 43 * them so they look like real paragraphs.
44 */ 44 */
45 45
46/* 46/*
47 * Attempt to wrap lines longer than 80 characters appropriately. 47 * Attempt to wrap lines longer than 80 characters appropriately.
48 * Refer to the examples below for more information. 48 * Refer to the examples below for more information.
49 */ 49 */
50 50
51/* 51/*
52 * EXAMPLE HEADER FILE: 52 * EXAMPLE HEADER FILE:
53 * 53 *
54 * A header file should protect itself against multiple inclusion. 54 * A header file should protect itself against multiple inclusion.
55 * E.g, <sys/socket.h> would contain something like: 55 * E.g, <sys/socket.h> would contain something like:
56 */ 56 */
57#ifndef _SYS_SOCKET_H_ 57#ifndef _SYS_SOCKET_H_
58#define _SYS_SOCKET_H_ 58#define _SYS_SOCKET_H_
59 59
60/* 60/*
61 * extern declarations must only appear in header files, not in .c 61 * extern declarations must only appear in header files, not in .c
62 * files, so the same declaration is used by the .c file defining it 62 * files, so the same declaration is used by the .c file defining it
63 * and the .c file using it, giving the compiler the opportunity to 63 * and the .c file using it, giving the compiler the opportunity to
64 * detect type errors. 64 * detect type errors.
65 * 65 *
66 * extern function declarations should not use the extern keyword, 66 * extern function declarations should not use the extern keyword,
67 * which is unnecessary. 67 * which is unnecessary.
68 * 68 *
69 * Exception: A subroutine written in assembly in an adjacent .S file, 69 * Exception: A subroutine written in assembly in an adjacent .S file,
70 * which is used only in one .c file, may be declared in the .c file. 70 * which is used only in one .c file, may be declared in the .c file.
71 */ 71 */
72extern int frotz; 72extern int frotz;
73 73
74int frobnicate(const char *); 74int frobnicate(const char *);
75 75
76/* 76/*
77 * Contents of #include file go between the #ifndef and the #endif at the end. 77 * Contents of #include file go between the #ifndef and the #endif at the end.
78 */ 78 */
79#endif /* !_SYS_SOCKET_H_ */ 79#endif /* !_SYS_SOCKET_H_ */
80/* 80/*
81 * END OF EXAMPLE HEADER FILE. 81 * END OF EXAMPLE HEADER FILE.
82 */ 82 */
83 83
84/* 84/*
85 * If a header file requires structures, defines, typedefs, etc. from 85 * If a header file requires structures, defines, typedefs, etc. from
86 * another header file it should include that header file and not depend 86 * another header file it should include that header file and not depend
87 * on the including file for that header including both. If there are 87 * on the including file for that header including both. If there are
88 * exceptions to this for specific headers it should be clearly documented 88 * exceptions to this for specific headers it should be clearly documented
89 * in the headers and, if appropriate, the documentation. Nothing in this 89 * in the headers and, if appropriate, the documentation. Nothing in this
90 * rule should suggest relaxation of the multiple inclusion rule and the 90 * rule should suggest relaxation of the multiple inclusion rule and the
91 * application programmer should be free to include both regardless. 91 * application programmer should be free to include both regardless.
92 */ 92 */
93 93
94/* 94/*
95 * Kernel include files come first. 95 * Kernel include files come first.
96 */ 96 */
97#include <sys/param.h> /* <sys/param.h> first, */ 97#include <sys/param.h> /* <sys/param.h> first, */
98#include <sys/types.h> /* <sys/types.h> next, */ 98#include <sys/types.h> /* <sys/types.h> next, */
99#include <sys/ioctl.h> /* and then the rest, */ 99#include <sys/ioctl.h> /* and then the rest, */
100#include <sys/socket.h> /* sorted lexicographically. */ 100#include <sys/socket.h> /* sorted lexicographically. */
101#include <sys/stat.h> 101#include <sys/stat.h>
102#include <sys/wait.h> /* Non-local includes in brackets. */ 102#include <sys/wait.h> /* Non-local includes in brackets. */
103 103
104/* 104/*
105 * If it's a network program, put the network include files next. 105 * If it's a network program, put the network include files next.
106 * Group the include files by subdirectory. 106 * Group the include files by subdirectory.
107 */ 107 */
108#include <net/if.h> 108#include <net/if.h>
109#include <net/if_dl.h> 109#include <net/if_dl.h>
110#include <net/route.h> 110#include <net/route.h>
111#include <netinet/in.h> 111#include <netinet/in.h>
112#include <protocols/rwhod.h> 112#include <protocols/rwhod.h>
113 113
114/* 114/*
115 * Then there's a blank line, followed by the /usr include files. 115 * Then there's a blank line, followed by the /usr include files.
116 * The /usr include files should be sorted lexicographically! 116 * The /usr include files should be sorted lexicographically!
117 */ 117 */
118#include <assert.h> 118#include <assert.h>
119#include <errno.h> 119#include <errno.h>
120#include <inttypes.h> 120#include <inttypes.h>
121#include <stdio.h> 121#include <stdio.h>
122#include <stdlib.h> 122#include <stdlib.h>
123 123
124/* 124/*
125 * Global pathnames are defined in /usr/include/paths.h. Pathnames local 125 * Global pathnames are defined in /usr/include/paths.h. Pathnames local
126 * to the program go in pathnames.h in the local directory. 126 * to the program go in pathnames.h in the local directory.
127 */ 127 */
128#include <paths.h> 128#include <paths.h>
129 129
130/* Then, there's a blank line, and the user include files. */ 130/* Then, there's a blank line, and the user include files. */
131#include "pathnames.h" /* Local includes in double quotes. */ 131#include "pathnames.h" /* Local includes in double quotes. */
132 132
133/* 133/*
134 * ANSI function declarations for private functions (i.e. functions not used 134 * ANSI function declarations for private functions (i.e. functions not used
135 * elsewhere) and the main() function go at the top of the source module. 135 * elsewhere) and the main() function go at the top of the source module.
136 * Don't associate a name with the types. I.e. use: 136 * Don't associate a name with the types. I.e. use:
137 * void function(int); 137 * void function(int);
138 * Use your discretion on indenting between the return type and the name, and 138 * Use your discretion on indenting between the return type and the name, and
139 * how to wrap a prototype too long for a single line. In the latter case, 139 * how to wrap a prototype too long for a single line. In the latter case,
140 * lining up under the initial left parenthesis may be more readable. 140 * lining up under the initial left parenthesis may be more readable.
141 * In any case, consistency is important! 141 * In any case, consistency is important!
142 */ 142 */
143static char *function(int, int, float, int); 143static char *function(int, int, float, int);
144static int dirinfo(const char *, struct stat *, struct dirent *, 144static int dirinfo(const char *, struct stat *, struct dirent *,
145 struct statfs *, int *, char **[]); 145 struct statfs *, int *, char **[]);
146static void usage(void) __dead; /* declare functions that don't return dead */ 146static void usage(void) __dead; /* declare functions that don't return dead */
147 147
148/* 148/*
149 * Macros are capitalized, parenthesized, and should avoid side-effects. 149 * Macros are capitalized, parenthesized, and should avoid side-effects.
150 * Spacing before and after the macro name may be any whitespace, though 150 * Spacing before and after the macro name may be any whitespace, though
151 * use of TABs should be consistent through a file. 151 * use of TABs should be consistent through a file.
152 * If they are an inline expansion of a function, the function is defined 152 * If they are an inline expansion of a function, the function is defined
153 * all in lowercase, the macro has the same name all in uppercase. 153 * all in lowercase, the macro has the same name all in uppercase.
154 * If the macro is an expression, wrap the expression in parentheses. 154 * If the macro is an expression, wrap the expression in parentheses.
155 * If the macro is more than a single statement, use ``do { ... } while (0)'' 155 * If the macro is more than a single statement, use ``do { ... } while (0)''
156 * or ``do { ... } while (false)'', so that a trailing semicolon works. 156 * or ``do { ... } while (false)'', so that a trailing semicolon works.
157 * Right-justify the backslashes; it makes it easier to read. 157 * Right-justify the backslashes; it makes it easier to read.
158 */ 158 */
159#define MACRO(v, w, x, y) \ 159#define MACRO(v, w, x, y) \
160do { \ 160do { \
161 v = (x) + (y); \ 161 v = (x) + (y); \
162 w = (y) + 2; \ 162 w = (y) + 2; \
163} while (0) 163} while (0)
164 164
165#define DOUBLE(x) ((x) * 2) 165#define DOUBLE(x) ((x) * 2)
166 166
167/* Enum constants are capitalized. No comma on the last element. */ 167/* Enum constants are capitalized. No comma on the last element. */
168enum enumtype { 168enum enumtype {
169 ONE, 169 ONE,
170 TWO 170 TWO
171}; 171};
172 172
173/* 173/*
174 * Sometimes we want a macro to be conditionally defined for debugging 174 * Sometimes we want a macro to be conditionally defined for debugging
175 * and expand to nothing (but still as statement) when we are not debugging: 175 * and expand to nothing (but still as statement) when we are not debugging:
176 */ 176 */
177#ifdef FOO_DEBUG 177#ifdef FOO_DEBUG
178# define DPRINTF(...) printf(__VA_ARGS__) 178# define DPRINTF(...) printf(__VA_ARGS__)
179#else 179#else
180# define DPRINTF(...) __nothing 180# define DPRINTF(...) __nothing
181#endif 181#endif
182 182
183/* 183/*
184 * When declaring variables in structures, declare them organized by use in 184 * When declaring variables in structures, declare them organized by use in
185 * a manner to attempt to minimize memory wastage because of compiler alignment 185 * a manner to attempt to minimize memory wastage because of compiler alignment
186 * issues, then by size, and then by alphabetical order. E.g, don't use 186 * issues, then by size, and then by alphabetical order. E.g, don't use
187 * ``int a; char *b; int c; char *d''; use ``int a; int b; char *c; char *d''. 187 * ``int a; char *b; int c; char *d''; use ``int a; int b; char *c; char *d''.
188 * Each variable gets its own type and line, although an exception can be made 188 * Each variable gets its own type and line, although an exception can be made
189 * when declaring bitfields (to clarify that it's part of the one bitfield). 189 * when declaring bitfields (to clarify that it's part of the one bitfield).
190 * Note that the use of bitfields in general is discouraged. 190 * Note that the use of bitfields in general is discouraged.
191 * 191 *
192 * Major structures should be declared at the top of the file in which they 192 * Major structures should be declared at the top of the file in which they
193 * are used, or in separate header files, if they are used in multiple 193 * are used, or in separate header files, if they are used in multiple
194 * source files. Use of the structures should be by separate declarations 194 * source files. Use of the structures should be by separate declarations
195 * and should be "extern" if they are declared in a header file. 195 * and should be "extern" if they are declared in a header file.
196 * 196 *
197 * It may be useful to use a meaningful prefix for each member name. 197 * It may be useful to use a meaningful prefix for each member name.
198 * E.g, for ``struct softc'' the prefix could be ``sc_''. 198 * E.g, for ``struct softc'' the prefix could be ``sc_''.
199 */ 199 */
200struct foo { 200struct foo {
201 struct foo *next; /* List of active foo */ 201 struct foo *next; /* List of active foo */
202 struct mumble amumble; /* Comment for mumble */ 202 struct mumble amumble; /* Comment for mumble */
203 int bar; 203 int bar;
204 unsigned int baz:1, /* Bitfield; line up entries if desired */ 204 unsigned int baz:1, /* Bitfield; line up entries if desired */
205 fuz:5, 205 fuz:5,
206 zap:2; 206 zap:2;
207 uint8_t flag; 207 uint8_t flag;
208}; 208};
209struct foo *foohead; /* Head of global foo list */ 209struct foo *foohead; /* Head of global foo list */
210 210
211/* Make the structure name match the typedef. */ 211/* Make the structure name match the typedef. */
212typedef struct BAR { 212typedef struct BAR {
213 int level; 213 int level;
214} BAR; 214} BAR;
215 215
216/* C99 uintN_t is preferred over u_intN_t. */ 216/* C99 uintN_t is preferred over u_intN_t. */
217uint32_t zero; 217uint32_t zero;
218 218
219/* 219/*
220 * All major routines should have a comment briefly describing what 220 * All major routines should have a comment briefly describing what
221 * they do. The comment before the "main" routine should describe 221 * they do. The comment before the "main" routine should describe
222 * what the program does. 222 * what the program does.
223 */ 223 */
224int 224int
225main(int argc, char *argv[]) 225main(int argc, char *argv[])
226{ 226{
227 long num; 227 long num;
228 int ch; 228 int ch;
229 char *ep; 229 char *ep;
230 230
231 /* 231 /*
232 * At the start of main(), call setprogname() to set the program 232 * At the start of main(), call setprogname() to set the program
233 * name. This does nothing on NetBSD, but increases portability 233 * name. This does nothing on NetBSD, but increases portability
234 * to other systems. 234 * to other systems.
235 */ 235 */
236 setprogname(argv[0]); 236 setprogname(argv[0]);
237 237
238 /* 238 /*
239 * For consistency, getopt should be used to parse options. 239 * For consistency, getopt should be used to parse options.
240 * Options should be sorted in the getopt call and the switch 240 * Options should be sorted in the getopt call and the switch
241 * statement, unless parts of the switch cascade. For the 241 * statement, unless parts of the switch cascade. For the
242 * sorting order, see the usage() example below. Don't forget 242 * sorting order, see the usage() example below. Don't forget
243 * to add option descriptions to the usage and the manpage. 243 * to add option descriptions to the usage and the manpage.
244 * Elements in a switch statement that cascade should have a 244 * Elements in a switch statement that cascade should have a
245 * FALLTHROUGH comment. Numerical arguments should be checked 245 * FALLTHROUGH comment. Numerical arguments should be checked
246 * for accuracy. Code that cannot be reached should have a 246 * for accuracy. Code that cannot be reached should have a
247 * NOTREACHED comment. 247 * NOTREACHED comment.
248 */ 248 */
249 while ((ch = getopt(argc, argv, "abn:")) != -1) { 249 while ((ch = getopt(argc, argv, "abn:")) != -1) {
250 switch (ch) { /* Indent the switch. */ 250 switch (ch) { /* Indent the switch. */
251 case 'a': /* Don't indent the case. */ 251 case 'a': /* Don't indent the case. */
252 aflag = 1; 252 aflag = 1;
253 /* FALLTHROUGH */ 253 /* FALLTHROUGH */
254 case 'b': 254 case 'b':
255 bflag = 1; 255 bflag = 1;
256 break; 256 break;
257 case 'n': 257 case 'n':
258 errno = 0; 258 errno = 0;
259 num = strtol(optarg, &ep, 10); 259 num = strtol(optarg, &ep, 10);
260 if (num <= 0 || *ep != '\0' || (errno == ERANGE && 260 if (num <= 0 || *ep != '\0' || (errno == ERANGE &&
261 (num == LONG_MAX || num == LONG_MIN)) ) { 261 (num == LONG_MAX || num == LONG_MIN)) ) {
262 errx(1, "illegal number -- %s", optarg); 262 errx(1, "illegal number -- %s", optarg);
263 } 263 }
264 break; 264 break;
265 case '?': 265 case '?':
266 default: 266 default:
267 usage(); 267 usage();
268 /* NOTREACHED */ 268 /* NOTREACHED */
269 } 269 }
270 } 270 }
271 argc -= optind; 271 argc -= optind;
272 argv += optind; 272 argv += optind;
273 273
274 /* 274 /*
275 * Space after keywords (while, for, return, switch). 275 * Space after keywords (while, for, return, switch).
276 * 276 *
277 * Braces around single-line bodies are optional; use discretion. 277 * Braces around single-line bodies are optional; use discretion.
278 * 278 *
279 * Use narrow scopes for loop variables where possible. 279 * Use narrow scopes for loop variables where possible.
280 */ 280 */
281 for (char *p = buf; *p != '\0'; ++p) 281 for (char *p = buf; *p != '\0'; ++p)
282 continue; /* Explicit no-op */ 282 continue; /* Explicit no-op */
283 283
284 /* 284 /*
285 * Forever loops are done with for's, not while's. 285 * Forever loops are done with for's, not while's.
286 */ 286 */
287 for (;;) 287 for (;;)
288 stmt; 288 stmt;
289 289
290 /* 290 /*
291 * Parts of a for loop may be left empty. Don't put declarations 291 * Parts of a for loop may be left empty. Don't put declarations
292 * inside blocks unless the routine is unusually complicated. 292 * inside blocks unless the routine is unusually complicated.
293 */ 293 */
294 for (; cnt < 15; cnt++) { 294 for (; cnt < 15; cnt++) {
295 stmt1; 295 stmt1;
296 stmt2; 296 stmt2;
297 } 297 }
298 298
299 /* Second level indents are four spaces. */ 299 /* Second level indents are four spaces. */
300 while (cnt < 20) { 300 while (cnt < 20) {
301 z = a + really + long + statement + that + needs + two + lines + 301 z = a + really + long + statement + that + needs + two + lines +
302 gets + indented + four + spaces + on + the + second + 302 gets + indented + four + spaces + on + the + second +
303 and + subsequent + lines; 303 and + subsequent + lines;
304 } 304 }
305 305
306 /* 306 /*
307 * Closing and opening braces go on the same line as the else. 307 * Closing and opening braces go on the same line as the else.
308 */ 308 */
309 if (test) { 309 if (test) {
310 /* 310 /*
311 * I have a long comment here. 311 * I have a long comment here.
312 */ 312 */
313#ifdef zorro 313#ifdef zorro
314 z = 1; 314 z = 1;
315#else 315#else
316 b = 3; 316 b = 3;
317#endif 317#endif
318 } else if (bar) { 318 } else if (bar) {
319 stmt; 319 stmt;
320 stmt; 320 stmt;
321 } else { 321 } else {
322 stmt; 322 stmt;
323 } 323 }
324 324
325 /* No spaces after function names. */ 325 /* No spaces after function names. */
326 if ((result = function(a1, a2, a3, a4)) == NULL) 326 if ((result = function(a1, a2, a3, a4)) == NULL)
327 exit(EXIT_FAILURE); 327 exit(EXIT_FAILURE);
328 328
329 /* 329 /*
330 * Unary operators don't require spaces, binary operators do. 330 * Unary operators don't require spaces, binary operators do.
331 * Don't excessively use parentheses, but they should be used if a 331 * Don't excessively use parentheses, but they should be used if a
332 * statement is really confusing without them, such as: 332 * statement is really confusing without them, such as:
333 * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1; 333 * a = b->c[0] + ~d == (e || f) || g && h ? i : j >> 1;
334 */ 334 */
335 a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1); 335 a = ((b->c[0] + ~d == (e || f)) || (g && h)) ? i : (j >> 1);
336 k = !(l & FLAGS); 336 k = !(l & FLAGS);
337 337
338 /* 338 /*
339 * Exits should be EXIT_SUCCESS on success, and EXIT_FAILURE on 339 * Exits should be EXIT_SUCCESS on success, and EXIT_FAILURE on
340 * failure. Don't denote all the possible exit points, using the 340 * failure. Don't denote all the possible exit points, using the
341 * integers 1 through 127. Avoid obvious comments such as "Exit 341 * integers 1 through 127. Avoid obvious comments such as "Exit
342 * 0 on success.". Since main is a function that returns an int, 342 * 0 on success.". Since main is a function that returns an int,
343 * prefer returning from it, than calling exit. 343 * prefer returning from it, than calling exit.
344 */ 344 */
345 return EXIT_SUCCESS; 345 return EXIT_SUCCESS;
346} 346}
347 347
348/* 348/*
349 * The function type must be declared on a line by itself 349 * The function type must be declared on a line by itself
350 * preceding the function. 350 * preceding the function.
351 */ 351 */
352static char * 352static char *
353function(int a1, int a2, float fl, int a4) 353function(int a1, int a2, float fl, int a4)
354{ 354{
355 /* 355 /*
356 * When declaring variables in functions, multiple variables per line 356 * When declaring variables in functions, multiple variables per line
357 * are okay. If a line overflows reuse the type keyword. 357 * are okay. If a line overflows reuse the type keyword.
358 * 358 *
359 * Function prototypes should go in the include file "extern.h". 359 * Function prototypes and external data declarations should go in a
 360 * suitable include file.
360 * 361 *
361 * Avoid initializing variables in the declarations; move 362 * Avoid initializing variables in the declarations; move
362 * declarations next to their first use, and initialize 363 * declarations next to their first use, and initialize
363 * opportunistically. This avoids over-initialization and 364 * opportunistically. This avoids over-initialization and
364 * accidental bugs caused by declaration reordering. 365 * accidental bugs caused by declaration reordering.
365 */ 366 */
366 struct foo three, *four; 367 struct foo three, *four;
367 double five; 368 double five;
368 int *six, seven; 369 int *six, seven;
369 char *eight, *nine, ten, eleven, twelve, thirteen; 370 char *eight, *nine, ten, eleven, twelve, thirteen;
370 char fourteen, fifteen, sixteen; 371 char fourteen, fifteen, sixteen;
371 372
372 /* 373 /*
373 * Casts and sizeof's are not followed by a space. 374 * Casts and sizeof's are not followed by a space.
374 * 375 *
375 * We parenthesize sizeof expressions to clarify their precedence: 376 * We parenthesize sizeof expressions to clarify their precedence:
376 * 377 *
377 * sizeof(e) + 4 378 * sizeof(e) + 4
378 * not: 379 * not:
379 * sizeof e + 4 380 * sizeof e + 4
380 * 381 *
381 * We don't put a space before the parenthesis so that it looks like 382 * We don't put a space before the parenthesis so that it looks like
382 * a function call. We always parenthesize the sizeof expression for 383 * a function call. We always parenthesize the sizeof expression for
383 * consistency. 384 * consistency.
384 * 385 *
385 * On the other hand, we don't parenthesize the return statement 386 * On the other hand, we don't parenthesize the return statement
386 * because there is never a precedence ambiguity situation (it is 387 * because there is never a precedence ambiguity situation (it is
387 * a single statement). 388 * a single statement).
388 * 389 *
389 * NULL is any pointer type, and doesn't need to be cast, so use 390 * NULL is any pointer type, and doesn't need to be cast, so use
390 * NULL instead of (struct foo *)0 or (struct foo *)NULL. Also, 391 * NULL instead of (struct foo *)0 or (struct foo *)NULL. Also,
391 * test pointers against NULL because it indicates the type of the 392 * test pointers against NULL because it indicates the type of the
392 * expression to the user. I.e. use: 393 * expression to the user. I.e. use:
393 * 394 *
394 * (p = f()) == NULL 395 * (p = f()) == NULL
395 * not: 396 * not:
396 * !(p = f()) 397 * !(p = f())
397 * 398 *
398 * The notable exception here is variadic functions. Since our 399 * The notable exception here is variadic functions. Since our
399 * code is designed to compile and work on different environments 400 * code is designed to compile and work on different environments
400 * where we don't have control over the NULL definition (on NetBSD 401 * where we don't have control over the NULL definition (on NetBSD
401 * it is defined as ((void *)0), but on other systems it can be 402 * it is defined as ((void *)0), but on other systems it can be
402 * defined as (0) and both definitions are valid under ANSI C), it 403 * defined as (0) and both definitions are valid under ANSI C), it
403 * it advised to cast NULL to a pointer on variadic functions, 404 * it advised to cast NULL to a pointer on variadic functions,
404 * because on machines where sizeof(pointer) != sizeof(int) and in 405 * because on machines where sizeof(pointer) != sizeof(int) and in
405 * the absence of a prototype in scope, passing an un-casted NULL, 406 * the absence of a prototype in scope, passing an un-casted NULL,
406 * will result in passing an int on the stack instead of a pointer. 407 * will result in passing an int on the stack instead of a pointer.
407 * 408 *
408 * Don't use `!' for tests unless it's a boolean. 409 * Don't use `!' for tests unless it's a boolean.
409 * E.g. use "if (*p == '\0')", not "if (!*p)". 410 * E.g. use "if (*p == '\0')", not "if (!*p)".
410 * 411 *
411 * Routines returning ``void *'' should not have their return 412 * Routines returning ``void *'' should not have their return
412 * values cast to more specific pointer types. 413 * values cast to more specific pointer types.
413 * 414 *
414 * Prefer sizeof(*var) over sizeof(type) because if type changes, 415 * Prefer sizeof(*var) over sizeof(type) because if type changes,
415 * the change needs to be done in one place. 416 * the change needs to be done in one place.
416 * 417 *
417 * Use err/warn(3), don't roll your own! 418 * Use err/warn(3), don't roll your own!
418 * 419 *
419 * Prefer EXIT_FAILURE instead of random error codes. 420 * Prefer EXIT_FAILURE instead of random error codes.
420 */ 421 */
421 if ((four = malloc(sizeof(*four))) == NULL) 422 if ((four = malloc(sizeof(*four))) == NULL)
422 err(EXIT_FAILURE, NULL); 423 err(EXIT_FAILURE, NULL);
423 if ((six = (int *)overflow()) == NULL) 424 if ((six = (int *)overflow()) == NULL)
424 errx(EXIT_FAILURE, "Number overflowed."); 425 errx(EXIT_FAILURE, "Number overflowed.");
425 426
426 /* No parentheses are needed around the return value. */ 427 /* No parentheses are needed around the return value. */
427 return eight; 428 return eight;
428} 429}
429 430
430/* 431/*
431 * Use ANSI function declarations. ANSI function braces look like 432 * Use ANSI function declarations. ANSI function braces look like
432 * old-style (K&R) function braces. 433 * old-style (K&R) function braces.
433 * As per the wrapped prototypes, use your discretion on how to format 434 * As per the wrapped prototypes, use your discretion on how to format
434 * the subsequent lines. 435 * the subsequent lines.
435 */ 436 */
436static int 437static int
437dirinfo(const char *p, struct stat *sb, struct dirent *de, struct statfs *sf, 438dirinfo(const char *p, struct stat *sb, struct dirent *de, struct statfs *sf,
438 int *rargc, char **rargv[]) 439 int *rargc, char **rargv[])
439{ /* Insert an empty line if the function has no local variables. */ 440{ /* Insert an empty line if the function has no local variables. */
440 441
441 /* 442 /*
442 * In system libraries, catch obviously invalid function arguments 443 * In system libraries, catch obviously invalid function arguments
443 * using _DIAGASSERT(3). 444 * using _DIAGASSERT(3).
444 */ 445 */
445 _DIAGASSERT(p != NULL); 446 _DIAGASSERT(p != NULL);
446 _DIAGASSERT(filedesc != -1); 447 _DIAGASSERT(filedesc != -1);
447 448
448 /* Prefer checking syscalls against -1 instead of < 0 */ 449 /* Prefer checking syscalls against -1 instead of < 0 */
449 if (stat(p, sb) == -1) 450 if (stat(p, sb) == -1)
450 err(EXIT_FAILURE, "Unable to stat %s", p); 451 err(EXIT_FAILURE, "Unable to stat %s", p);
451 452
452 /* 453 /*
453 * To printf quantities that might be larger than "long", 454 * To printf quantities that might be larger than "long",
454 * cast quantities to intmax_t or uintmax_t and use %j. 455 * cast quantities to intmax_t or uintmax_t and use %j.
455 */ 456 */
456 (void)printf("The size of %s is %jd (%#ju)\n", p, 457 (void)printf("The size of %s is %jd (%#ju)\n", p,
457 (intmax_t)sb->st_size, (uintmax_t)sb->st_size); 458 (intmax_t)sb->st_size, (uintmax_t)sb->st_size);
458 459
459 /* 460 /*
460 * To printf quantities of known bit-width, include <inttypes.h> and 461 * To printf quantities of known bit-width, include <inttypes.h> and
461 * use the corresponding defines (generally only done within NetBSD 462 * use the corresponding defines (generally only done within NetBSD
462 * for quantities that exceed 32-bits). 463 * for quantities that exceed 32-bits).
463 */ 464 */
464 (void)printf("%s uses %" PRId64 " blocks and has flags %#" PRIx32 "\n", 465 (void)printf("%s uses %" PRId64 " blocks and has flags %#" PRIx32 "\n",
465 p, sb->st_blocks, sb->st_flags); 466 p, sb->st_blocks, sb->st_flags);
466 467
467 /* 468 /*
468 * There are similar constants that should be used with the *scanf(3) 469 * There are similar constants that should be used with the *scanf(3)
469 * family of functions: SCN?MAX, SCN?64, etc. 470 * family of functions: SCN?MAX, SCN?64, etc.
470 */ 471 */
471} 472}
472 473
473/* 474/*
474 * Functions that support variable numbers of arguments should look like this. 475 * Functions that support variable numbers of arguments should look like this.
475 * (With the #include <stdarg.h> appearing at the top of the file with the 476 * (With the #include <stdarg.h> appearing at the top of the file with the
476 * other include files.) 477 * other include files.)
477 */ 478 */
478#include <stdarg.h> 479#include <stdarg.h>
479 480
480void 481void
481vaf(const char *fmt, ...) 482vaf(const char *fmt, ...)
482{ 483{
483 va_list ap; 484 va_list ap;
484 485
485 va_start(ap, fmt); 486 va_start(ap, fmt);
486 STUFF; 487 STUFF;
487 va_end(ap); 488 va_end(ap);
488 /* No return needed for void functions. */ 489 /* No return needed for void functions. */
489} 490}
490 491
491static void 492static void
492usage(void) 493usage(void)
493{ 494{
494 495
495 /* 496 /*
496 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and 497 * Use printf(3), not fputs/puts/putchar/whatever, it's faster and
497 * usually cleaner, not to mention avoiding stupid bugs. 498 * usually cleaner, not to mention avoiding stupid bugs.
498 * Use snprintf(3) or strlcpy(3)/strlcat(3) instead of sprintf(3); 499 * Use snprintf(3) or strlcpy(3)/strlcat(3) instead of sprintf(3);
499 * again to avoid stupid bugs. 500 * again to avoid stupid bugs.
500 * 501 *
501 * Usage statements should look like the manual pages. 502 * Usage statements should look like the manual pages.
502 * Options w/o operands come first, in alphabetical order 503 * Options w/o operands come first, in alphabetical order
503 * inside a single set of braces, upper case before lower case 504 * inside a single set of braces, upper case before lower case
504 * (AaBbCc...). Next are options with operands, in the same 505 * (AaBbCc...). Next are options with operands, in the same
505 * order, each in braces. Then required arguments in the 506 * order, each in braces. Then required arguments in the
506 * order they are specified, followed by optional arguments in 507 * order they are specified, followed by optional arguments in
507 * the order they are specified. A bar (`|') separates 508 * the order they are specified. A bar (`|') separates
508 * either/or options/arguments, and multiple options/arguments 509 * either/or options/arguments, and multiple options/arguments
509 * which are specified together are placed in a single set of 510 * which are specified together are placed in a single set of
510 * braces. 511 * braces.
511 * 512 *
512 * Use getprogname() instead of hardcoding the program name. 513 * Use getprogname() instead of hardcoding the program name.
513 * 514 *
514 * "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n" 515 * "usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
515 * "usage: f [-a | -b] [-c [-de] [-n number]]\n" 516 * "usage: f [-a | -b] [-c [-de] [-n number]]\n"
516 */ 517 */
517 (void)fprintf(stderr, "usage: %s [-ab]\n", getprogname()); 518 (void)fprintf(stderr, "usage: %s [-ab]\n", getprogname());
518 exit(EXIT_FAILURE); 519 exit(EXIT_FAILURE);
519} 520}