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