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