autoupdate
[gnulib.git] / doc / standards.texi
1 \input texinfo @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename standards.info
4 @settitle GNU Coding Standards
5 @c This date is automagically updated when you save this file:
6 @set lastupdate February 23, 2008
7 @c %**end of header
8
9 @dircategory GNU organization
10 @direntry
11 * Standards: (standards).       GNU coding standards.
12 @end direntry
13
14 @c @setchapternewpage odd
15 @setchapternewpage off
16
17 @c Put everything in one index (arbitrarily chosen to be the concept index).
18 @syncodeindex fn cp
19 @syncodeindex ky cp
20 @syncodeindex pg cp
21 @syncodeindex vr cp
22
23 @c This is used by a cross ref in make-stds.texi
24 @set CODESTD  1
25
26 @copying
27 The GNU coding standards, last updated @value{lastupdate}.
28
29 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
30 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
31 Foundation, Inc.
32
33 Permission is granted to copy, distribute and/or modify this document
34 under the terms of the GNU Free Documentation License, Version 1.2
35 or any later version published by the Free Software Foundation;
36 with no Invariant Sections, with no
37 Front-Cover Texts, and with no Back-Cover Texts.
38 A copy of the license is included in the section entitled ``GNU
39 Free Documentation License''.
40 @end copying
41
42 @titlepage
43 @title GNU Coding Standards
44 @author Richard Stallman, et al.
45 @author last updated @value{lastupdate}
46 @page
47 @vskip 0pt plus 1filll
48 @insertcopying
49 @end titlepage
50
51 @contents
52
53 @ifnottex
54 @node Top, Preface, (dir), (dir)
55 @top Version
56
57 @insertcopying
58 @end ifnottex
59
60 @menu
61 * Preface::                     About the GNU Coding Standards.
62 * Legal Issues::                Keeping free software free.
63 * Design Advice::               General program design.
64 * Program Behavior::            Program behavior for all programs
65 * Writing C::                   Making the best use of C.
66 * Documentation::               Documenting programs.
67 * Managing Releases::           The release process.
68 * References::                  Mentioning non-free software or documentation.
69 * GNU Free Documentation License::  Copying and sharing this manual.
70 * Index::
71
72 @end menu
73
74 @node Preface
75 @chapter About the GNU Coding Standards
76
77 The GNU Coding Standards were written by Richard Stallman and other GNU
78 Project volunteers.  Their purpose is to make the GNU system clean,
79 consistent, and easy to install.  This document can also be read as a
80 guide to writing portable, robust and reliable programs.  It focuses on
81 programs written in C, but many of the rules and principles are useful
82 even if you write in another programming language.  The rules often
83 state reasons for writing in a certain way.
84
85 This release of the GNU Coding Standards was last updated
86 @value{lastupdate}.
87
88 @cindex where to obtain @code{standards.texi}
89 @cindex downloading this manual
90 If you did not obtain this file directly from the GNU project and
91 recently, please check for a newer version.  You can get the GNU
92 Coding Standards from the GNU web server in many
93 different formats, including the Texinfo source, PDF, HTML, DVI, plain
94 text, and more, at: @uref{http://www.gnu.org/prep/standards/}.
95
96 Corrections or suggestions for this document should be sent to
97 @email{bug-standards@@gnu.org}.  If you make a suggestion, please include a
98 suggested new wording for it; our time is limited.  We prefer a context
99 diff to the @file{standards.texi} or @file{make-stds.texi} files, but if
100 you don't have those files, please mail your suggestion anyway.
101
102 These standards cover the minimum of what is important when writing a
103 GNU package.  Likely, the need for additional standards will come up.
104 Sometimes, you might suggest that such standards be added to this
105 document.  If you think your standards would be generally useful, please
106 do suggest them.
107
108 You should also set standards for your package on many questions not
109 addressed or not firmly specified here.  The most important point is to
110 be self-consistent---try to stick to the conventions you pick, and try
111 to document them as much as possible.  That way, your program will be
112 more maintainable by others.
113
114 The GNU Hello program serves as an example of how to follow the GNU
115 coding standards for a trivial program.
116 @uref{http://www.gnu.org/software/hello/hello.html}.
117
118 @node Legal Issues
119 @chapter Keeping Free Software Free
120 @cindex legal aspects
121
122 This chapter discusses how you can make sure that GNU software
123 avoids legal difficulties, and other related issues.
124
125 @menu
126 * Reading Non-Free Code::       Referring to proprietary programs.
127 * Contributions::               Accepting contributions.
128 * Trademarks::                  How we deal with trademark issues.
129 @end menu
130
131 @node Reading Non-Free Code
132 @section Referring to Proprietary Programs
133 @cindex proprietary programs
134 @cindex avoiding proprietary code
135
136 Don't in any circumstances refer to Unix source code for or during
137 your work on GNU!  (Or to any other proprietary programs.)
138
139 If you have a vague recollection of the internals of a Unix program,
140 this does not absolutely mean you can't write an imitation of it, but
141 do try to organize the imitation internally along different lines,
142 because this is likely to make the details of the Unix version
143 irrelevant and dissimilar to your results.
144
145 For example, Unix utilities were generally optimized to minimize
146 memory use; if you go for speed instead, your program will be very
147 different.  You could keep the entire input file in memory and scan it
148 there instead of using stdio.  Use a smarter algorithm discovered more
149 recently than the Unix program.  Eliminate use of temporary files.  Do
150 it in one pass instead of two (we did this in the assembler).
151
152 Or, on the contrary, emphasize simplicity instead of speed.  For some
153 applications, the speed of today's computers makes simpler algorithms
154 adequate.
155
156 Or go for generality.  For example, Unix programs often have static
157 tables or fixed-size strings, which make for arbitrary limits; use
158 dynamic allocation instead.  Make sure your program handles NULs and
159 other funny characters in the input files.  Add a programming language
160 for extensibility and write part of the program in that language.
161
162 Or turn some parts of the program into independently usable libraries.
163 Or use a simple garbage collector instead of tracking precisely when
164 to free memory, or use a new GNU facility such as obstacks.
165
166 @node Contributions
167 @section Accepting Contributions
168 @cindex legal papers
169 @cindex accepting contributions
170
171 If the program you are working on is copyrighted by the Free Software
172 Foundation, then when someone else sends you a piece of code to add to
173 the program, we need legal papers to use it---just as we asked you to
174 sign papers initially.  @emph{Each} person who makes a nontrivial
175 contribution to a program must sign some sort of legal papers in order
176 for us to have clear title to the program; the main author alone is not
177 enough.
178
179 So, before adding in any contributions from other people, please tell
180 us, so we can arrange to get the papers.  Then wait until we tell you
181 that we have received the signed papers, before you actually use the
182 contribution.
183
184 This applies both before you release the program and afterward.  If
185 you receive diffs to fix a bug, and they make significant changes, we
186 need legal papers for that change.
187
188 This also applies to comments and documentation files.  For copyright
189 law, comments and code are just text.  Copyright applies to all kinds of
190 text, so we need legal papers for all kinds.
191
192 We know it is frustrating to ask for legal papers; it's frustrating for
193 us as well.  But if you don't wait, you are going out on a limb---for
194 example, what if the contributor's employer won't sign a disclaimer?
195 You might have to take that code out again!
196
197 You don't need papers for changes of a few lines here or there, since
198 they are not significant for copyright purposes.  Also, you don't need
199 papers if all you get from the suggestion is some ideas, not actual code
200 which you use.  For example, if someone sent you one implementation, but
201 you write a different implementation of the same idea, you don't need to
202 get papers.
203
204 The very worst thing is if you forget to tell us about the other
205 contributor.  We could be very embarrassed in court some day as a
206 result.
207
208 We have more detailed advice for maintainers of programs; if you have
209 reached the stage of actually maintaining a program for GNU (whether
210 released or not), please ask us for a copy.  It is also available
211 online for your perusal: @uref{http://www.gnu.org/prep/maintain/}.
212
213 @node Trademarks
214 @section Trademarks
215 @cindex trademarks
216
217 Please do not include any trademark acknowledgements in GNU software
218 packages or documentation.
219
220 Trademark acknowledgements are the statements that such-and-such is a
221 trademark of so-and-so.  The GNU Project has no objection to the basic
222 idea of trademarks, but these acknowledgements feel like kowtowing,
223 and there is no legal requirement for them, so we don't use them.
224
225 What is legally required, as regards other people's trademarks, is to
226 avoid using them in ways which a reader might reasonably understand as
227 naming or labeling our own programs or activities.  For example, since
228 ``Objective C'' is (or at least was) a trademark, we made sure to say
229 that we provide a ``compiler for the Objective C language'' rather
230 than an ``Objective C compiler''.  The latter would have been meant as
231 a shorter way of saying the former, but it does not explicitly state
232 the relationship, so it could be misinterpreted as using ``Objective
233 C'' as a label for the compiler rather than for the language.
234
235 Please don't use ``win'' as an abbreviation for Microsoft Windows in
236 GNU software or documentation.  In hacker terminology, calling
237 something a ``win'' is a form of praise.  If you wish to praise
238 Microsoft Windows when speaking on your own, by all means do so, but
239 not in GNU software.  Usually we write the name ``Windows'' in full,
240 but when brevity is very important (as in file names and sometimes
241 symbol names), we abbreviate it to ``w''.  For instance, the files and
242 functions in Emacs that deal with Windows start with @samp{w32}.
243
244 @node Design Advice
245 @chapter General Program Design
246 @cindex program design
247
248 This chapter discusses some of the issues you should take into
249 account when designing your program.
250
251 @c                         Standard or ANSI C
252 @c
253 @c In 1989 the American National Standards Institute (ANSI) standardized
254 @c C   as  standard  X3.159-1989.    In  December   of  that   year  the
255 @c International Standards Organization ISO  adopted the ANSI C standard
256 @c making  minor changes.   In 1990  ANSI then  re-adopted  ISO standard
257 @c C. This version of C is known as either ANSI C or Standard C.
258
259 @c A major revision of the C Standard appeared in 1999.
260
261 @menu
262 * Source Language::             Which languages to use.
263 * Compatibility::               Compatibility with other implementations.
264 * Using Extensions::            Using non-standard features.
265 * Standard C::                  Using standard C features.
266 * Conditional Compilation::     Compiling code only if a conditional is true.
267 @end menu
268
269 @node Source Language
270 @section Which Languages to Use
271 @cindex programming languages
272
273 When you want to use a language that gets compiled and runs at high
274 speed, the best language to use is C.  Using another language is like
275 using a non-standard feature: it will cause trouble for users.  Even if
276 GCC supports the other language, users may find it inconvenient to have
277 to install the compiler for that other language in order to build your
278 program.  For example, if you write your program in C++, people will
279 have to install the GNU C++ compiler in order to compile your program.
280
281 C has one other advantage over C++ and other compiled languages: more
282 people know C, so more people will find it easy to read and modify the
283 program if it is written in C.
284
285 So in general it is much better to use C, rather than the
286 comparable alternatives.
287
288 But there are two exceptions to that conclusion:
289
290 @itemize @bullet
291 @item
292 It is no problem to use another language to write a tool specifically
293 intended for use with that language.  That is because the only people
294 who want to build the tool will be those who have installed the other
295 language anyway.
296
297 @item
298 If an application is of interest only to a narrow part of the community,
299 then the question of which language it is written in has less effect on
300 other people, so you may as well please yourself.
301 @end itemize
302
303 Many programs are designed to be extensible: they include an interpreter
304 for a language that is higher level than C.  Often much of the program
305 is written in that language, too.  The Emacs editor pioneered this
306 technique.
307
308 @cindex GUILE
309 The standard extensibility interpreter for GNU software is GUILE
310 (@uref{http://www.gnu.org/software/guile/}), which implements the
311 language Scheme (an especially clean and simple dialect of Lisp).  We
312 don't reject programs written in other ``scripting languages'' such as
313 Perl and Python, but using GUILE is very important for the overall
314 consistency of the GNU system.
315
316 @node Compatibility
317 @section Compatibility with Other Implementations
318 @cindex compatibility with C and @sc{posix} standards
319 @cindex @sc{posix} compatibility
320
321 With occasional exceptions, utility programs and libraries for GNU
322 should be upward compatible with those in Berkeley Unix, and upward
323 compatible with Standard C if Standard C specifies their
324 behavior, and upward compatible with @sc{posix} if @sc{posix} specifies
325 their behavior.
326
327 When these standards conflict, it is useful to offer compatibility
328 modes for each of them.
329
330 @cindex options for compatibility
331 Standard C and @sc{posix} prohibit many kinds of extensions.  Feel
332 free to make the extensions anyway, and include a @samp{--ansi},
333 @samp{--posix}, or @samp{--compatible} option to turn them off.
334 However, if the extension has a significant chance of breaking any real
335 programs or scripts, then it is not really upward compatible.  So you
336 should try to redesign its interface to make it upward compatible.
337
338 @cindex @code{POSIXLY_CORRECT}, environment variable
339 Many GNU programs suppress extensions that conflict with @sc{posix} if the
340 environment variable @code{POSIXLY_CORRECT} is defined (even if it is
341 defined with a null value).  Please make your program recognize this
342 variable if appropriate.
343
344 When a feature is used only by users (not by programs or command
345 files), and it is done poorly in Unix, feel free to replace it
346 completely with something totally different and better.  (For example,
347 @code{vi} is replaced with Emacs.)  But it is nice to offer a compatible
348 feature as well.  (There is a free @code{vi} clone, so we offer it.)
349
350 Additional useful features are welcome regardless of whether
351 there is any precedent for them.
352
353 @node Using Extensions
354 @section Using Non-standard Features
355 @cindex non-standard extensions
356
357 Many GNU facilities that already exist support a number of convenient
358 extensions over the comparable Unix facilities.  Whether to use these
359 extensions in implementing your program is a difficult question.
360
361 On the one hand, using the extensions can make a cleaner program.
362 On the other hand, people will not be able to build the program
363 unless the other GNU tools are available.  This might cause the
364 program to work on fewer kinds of machines.
365
366 With some extensions, it might be easy to provide both alternatives.
367 For example, you can define functions with a ``keyword'' @code{INLINE}
368 and define that as a macro to expand into either @code{inline} or
369 nothing, depending on the compiler.
370
371 In general, perhaps it is best not to use the extensions if you can
372 straightforwardly do without them, but to use the extensions if they
373 are a big improvement.
374
375 An exception to this rule are the large, established programs (such as
376 Emacs) which run on a great variety of systems.  Using GNU extensions in
377 such programs would make many users unhappy, so we don't do that.
378
379 Another exception is for programs that are used as part of compilation:
380 anything that must be compiled with other compilers in order to
381 bootstrap the GNU compilation facilities.  If these require the GNU
382 compiler, then no one can compile them without having them installed
383 already.  That would be extremely troublesome in certain cases.
384
385 @node Standard C
386 @section Standard C and Pre-Standard C
387 @cindex @sc{ansi} C standard
388
389 1989 Standard C is widespread enough now that it is ok to use its
390 features in new programs.  There is one exception: do not ever use the
391 ``trigraph'' feature of Standard C.
392
393 1999 Standard C is not widespread yet, so please do not require its
394 features in programs.  It is ok to use its features if they are present.
395
396 However, it is easy to support pre-standard compilers in most programs,
397 so if you know how to do that, feel free.  If a program you are
398 maintaining has such support, you should try to keep it working.
399
400 @cindex function prototypes
401 To support pre-standard C, instead of writing function definitions in
402 standard prototype form,
403
404 @example
405 int
406 foo (int x, int y)
407 @dots{}
408 @end example
409
410 @noindent
411 write the definition in pre-standard style like this,
412
413 @example
414 int
415 foo (x, y)
416      int x, y;
417 @dots{}
418 @end example
419
420 @noindent
421 and use a separate declaration to specify the argument prototype:
422
423 @example
424 int foo (int, int);
425 @end example
426
427 You need such a declaration anyway, in a header file, to get the benefit
428 of prototypes in all the files where the function is called.  And once
429 you have the declaration, you normally lose nothing by writing the
430 function definition in the pre-standard style.
431
432 This technique does not work for integer types narrower than @code{int}.
433 If you think of an argument as being of a type narrower than @code{int},
434 declare it as @code{int} instead.
435
436 There are a few special cases where this technique is hard to use.  For
437 example, if a function argument needs to hold the system type
438 @code{dev_t}, you run into trouble, because @code{dev_t} is shorter than
439 @code{int} on some machines; but you cannot use @code{int} instead,
440 because @code{dev_t} is wider than @code{int} on some machines.  There
441 is no type you can safely use on all machines in a non-standard
442 definition.  The only way to support non-standard C and pass such an
443 argument is to check the width of @code{dev_t} using Autoconf and choose
444 the argument type accordingly.  This may not be worth the trouble.
445
446 In order to support pre-standard compilers that do not recognize
447 prototypes, you may want to use a preprocessor macro like this:
448
449 @example
450 /* Declare the prototype for a general external function.  */
451 #if defined (__STDC__) || defined (WINDOWSNT)
452 #define P_(proto) proto
453 #else
454 #define P_(proto) ()
455 #endif
456 @end example
457
458 @node Conditional Compilation
459 @section Conditional Compilation
460
461 When supporting configuration options already known when building your
462 program we prefer using @code{if (... )} over conditional compilation,
463 as in the former case the compiler is able to perform more extensive
464 checking of all possible code paths.
465
466 For example, please write
467
468 @smallexample
469   if (HAS_FOO)
470     ...
471   else
472     ...
473 @end smallexample
474
475 @noindent
476 instead of:
477
478 @smallexample
479   #ifdef HAS_FOO
480     ...
481   #else
482     ...
483   #endif
484 @end smallexample
485
486 A modern compiler such as GCC will generate exactly the same code in
487 both cases, and we have been using similar techniques with good success
488 in several projects.  Of course, the former method assumes that
489 @code{HAS_FOO} is defined as either 0 or 1.
490
491 While this is not a silver bullet solving all portability problems,
492 and is not always appropriate, following this policy would have saved
493 GCC developers many hours, or even days, per year.
494
495 In the case of function-like macros like @code{REVERSIBLE_CC_MODE} in
496 GCC which cannot be simply used in @code{if( ...)} statements, there is
497 an easy workaround.  Simply introduce another macro
498 @code{HAS_REVERSIBLE_CC_MODE} as in the following example:
499
500 @smallexample
501   #ifdef REVERSIBLE_CC_MODE
502   #define HAS_REVERSIBLE_CC_MODE 1
503   #else
504   #define HAS_REVERSIBLE_CC_MODE 0
505   #endif
506 @end smallexample
507
508 @node Program Behavior
509 @chapter Program Behavior for All Programs
510
511 This chapter describes conventions for writing robust
512 software.  It also describes general standards for error messages, the
513 command line interface, and how libraries should behave.
514
515 @menu
516 * Non-GNU Standards::           We consider standards such as POSIX;
517                                   we don't "obey" them.
518 * Semantics::                   Writing robust programs.
519 * Libraries::                   Library behavior.
520 * Errors::                      Formatting error messages.
521 * User Interfaces::             Standards about interfaces generally.
522 * Graphical Interfaces::        Standards for graphical interfaces.
523 * Command-Line Interfaces::     Standards for command line interfaces.
524 * Option Table::                Table of long options.
525 * Memory Usage::                When and how to care about memory needs.
526 * File Usage::                  Which files to use, and where.
527 @end menu
528
529 @node Non-GNU Standards
530 @section Non-GNU Standards
531
532 The GNU Project regards standards published by other organizations as
533 suggestions, not orders.  We consider those standards, but we do not
534 ``obey'' them.  In developing a GNU program, you should implement
535 an outside standard's specifications when that makes the GNU system
536 better overall in an objective sense.  When it doesn't, you shouldn't.
537
538 In most cases, following published standards is convenient for
539 users---it means that their programs or scripts will work more
540 portably.  For instance, GCC implements nearly all the features of
541 Standard C as specified by that standard.  C program developers would
542 be unhappy if it did not.  And GNU utilities mostly follow
543 specifications of POSIX.2; shell script writers and users would be
544 unhappy if our programs were incompatible.
545
546 But we do not follow either of these specifications rigidly, and there
547 are specific points on which we decided not to follow them, so as to
548 make the GNU system better for users.
549
550 For instance, Standard C says that nearly all extensions to C are
551 prohibited.  How silly!  GCC implements many extensions, some of which
552 were later adopted as part of the standard.  If you want these
553 constructs to give an error message as ``required'' by the standard,
554 you must specify @samp{--pedantic}, which was implemented only so that
555 we can say ``GCC is a 100% implementation of the standard,'' not
556 because there is any reason to actually use it.
557
558 POSIX.2 specifies that @samp{df} and @samp{du} must output sizes by
559 default in units of 512 bytes.  What users want is units of 1k, so
560 that is what we do by default.  If you want the ridiculous behavior
561 ``required'' by POSIX, you must set the environment variable
562 @samp{POSIXLY_CORRECT} (which was originally going to be named
563 @samp{POSIX_ME_HARDER}).
564
565 GNU utilities also depart from the letter of the POSIX.2 specification
566 when they support long-named command-line options, and intermixing
567 options with ordinary arguments.  This minor incompatibility with
568 POSIX is never a problem in practice, and it is very useful.
569
570 In particular, don't reject a new feature, or remove an old one,
571 merely because a standard says it is ``forbidden'' or ``deprecated.''
572
573 @node Semantics
574 @section Writing Robust Programs
575
576 @cindex arbitrary limits on data
577 Avoid arbitrary limits on the length or number of @emph{any} data
578 structure, including file names, lines, files, and symbols, by allocating
579 all data structures dynamically.  In most Unix utilities, ``long lines
580 are silently truncated''.  This is not acceptable in a GNU utility.
581
582 @cindex @code{NUL} characters
583 Utilities reading files should not drop NUL characters, or any other
584 nonprinting characters @emph{including those with codes above 0177}.
585 The only sensible exceptions would be utilities specifically intended
586 for interface to certain types of terminals or printers
587 that can't handle those characters.
588 Whenever possible, try to make programs work properly with
589 sequences of bytes that represent multibyte characters, using encodings
590 such as UTF-8 and others.
591
592 @cindex error messages
593 Check every system call for an error return, unless you know you wish to
594 ignore errors.  Include the system error text (from @code{perror} or
595 equivalent) in @emph{every} error message resulting from a failing
596 system call, as well as the name of the file if any and the name of the
597 utility.  Just ``cannot open foo.c'' or ``stat failed'' is not
598 sufficient.
599
600 @cindex @code{malloc} return value
601 @cindex memory allocation failure
602 Check every call to @code{malloc} or @code{realloc} to see if it
603 returned zero.  Check @code{realloc} even if you are making the block
604 smaller; in a system that rounds block sizes to a power of 2,
605 @code{realloc} may get a different block if you ask for less space.
606
607 In Unix, @code{realloc} can destroy the storage block if it returns
608 zero.  GNU @code{realloc} does not have this bug: if it fails, the
609 original block is unchanged.  Feel free to assume the bug is fixed.  If
610 you wish to run your program on Unix, and wish to avoid lossage in this
611 case, you can use the GNU @code{malloc}.
612
613 You must expect @code{free} to alter the contents of the block that was
614 freed.  Anything you want to fetch from the block, you must fetch before
615 calling @code{free}.
616
617 If @code{malloc} fails in a noninteractive program, make that a fatal
618 error.  In an interactive program (one that reads commands from the
619 user), it is better to abort the command and return to the command
620 reader loop.  This allows the user to kill other processes to free up
621 virtual memory, and then try the command again.
622
623 @cindex command-line arguments, decoding
624 Use @code{getopt_long} to decode arguments, unless the argument syntax
625 makes this unreasonable.
626
627 When static storage is to be written in during program execution, use
628 explicit C code to initialize it.  Reserve C initialized declarations
629 for data that will not be changed.
630 @c ADR: why?
631
632 Try to avoid low-level interfaces to obscure Unix data structures (such
633 as file directories, utmp, or the layout of kernel memory), since these
634 are less likely to work compatibly.  If you need to find all the files
635 in a directory, use @code{readdir} or some other high-level interface.
636 These are supported compatibly by GNU.
637
638 @cindex signal handling
639 The preferred signal handling facilities are the BSD variant of
640 @code{signal}, and the @sc{posix} @code{sigaction} function; the
641 alternative USG @code{signal} interface is an inferior design.
642
643 Nowadays, using the @sc{posix} signal functions may be the easiest way
644 to make a program portable.  If you use @code{signal}, then on GNU/Linux
645 systems running GNU libc version 1, you should include
646 @file{bsd/signal.h} instead of @file{signal.h}, so as to get BSD
647 behavior.  It is up to you whether to support systems where
648 @code{signal} has only the USG behavior, or give up on them.
649
650 @cindex impossible conditions
651 In error checks that detect ``impossible'' conditions, just abort.
652 There is usually no point in printing any message.  These checks
653 indicate the existence of bugs.  Whoever wants to fix the bugs will have
654 to read the source code and run a debugger.  So explain the problem with
655 comments in the source.  The relevant data will be in variables, which
656 are easy to examine with the debugger, so there is no point moving them
657 elsewhere.
658
659 Do not use a count of errors as the exit status for a program.
660 @emph{That does not work}, because exit status values are limited to 8
661 bits (0 through 255).  A single run of the program might have 256
662 errors; if you try to return 256 as the exit status, the parent process
663 will see 0 as the status, and it will appear that the program succeeded.
664
665 @cindex temporary files
666 @cindex @code{TMPDIR} environment variable
667 If you make temporary files, check the @code{TMPDIR} environment
668 variable; if that variable is defined, use the specified directory
669 instead of @file{/tmp}.
670
671 In addition, be aware that there is a possible security problem when
672 creating temporary files in world-writable directories.  In C, you can
673 avoid this problem by creating temporary files in this manner:
674
675 @example
676 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
677 @end example
678
679 @noindent
680 or by using the @code{mkstemps} function from libiberty.
681
682 In bash, use @code{set -C} to avoid this problem.
683
684 @node Libraries
685 @section Library Behavior
686 @cindex libraries
687
688 Try to make library functions reentrant.  If they need to do dynamic
689 storage allocation, at least try to avoid any nonreentrancy aside from
690 that of @code{malloc} itself.
691
692 Here are certain name conventions for libraries, to avoid name
693 conflicts.
694
695 Choose a name prefix for the library, more than two characters long.
696 All external function and variable names should start with this
697 prefix.  In addition, there should only be one of these in any given
698 library member.  This usually means putting each one in a separate
699 source file.
700
701 An exception can be made when two external symbols are always used
702 together, so that no reasonable program could use one without the
703 other; then they can both go in the same file.
704
705 External symbols that are not documented entry points for the user
706 should have names beginning with @samp{_}.  The @samp{_} should be
707 followed by the chosen name prefix for the library, to prevent
708 collisions with other libraries.  These can go in the same files with
709 user entry points if you like.
710
711 Static functions and variables can be used as you like and need not
712 fit any naming convention.
713
714 @node Errors
715 @section Formatting Error Messages
716 @cindex formatting error messages
717 @cindex error messages, formatting
718
719 Error messages from compilers should look like this:
720
721 @example
722 @var{source-file-name}:@var{lineno}: @var{message}
723 @end example
724
725 @noindent
726 If you want to mention the column number, use one of these formats:
727
728 @example
729 @var{source-file-name}:@var{lineno}:@var{column}: @var{message}
730 @var{source-file-name}:@var{lineno}.@var{column}: @var{message}
731
732 @end example
733
734 @noindent
735 Line numbers should start from 1 at the beginning of the file, and
736 column numbers should start from 1 at the beginning of the line.  (Both
737 of these conventions are chosen for compatibility.)  Calculate column
738 numbers assuming that space and all ASCII printing characters have
739 equal width, and assuming tab stops every 8 columns.
740
741 The error message can also give both the starting and ending positions
742 of the erroneous text.  There are several formats so that you can
743 avoid redundant information such as a duplicate line number.
744 Here are the possible formats:
745
746 @example
747 @var{source-file-name}:@var{lineno-1}.@var{column-1}-@var{lineno-2}.@var{column-2}: @var{message}
748 @var{source-file-name}:@var{lineno-1}.@var{column-1}-@var{column-2}: @var{message}
749 @var{source-file-name}:@var{lineno-1}-@var{lineno-2}: @var{message}
750 @end example
751
752 @noindent
753 When an error is spread over several files, you can use this format:
754
755 @example
756 @var{file-1}:@var{lineno-1}.@var{column-1}-@var{file-2}:@var{lineno-2}.@var{column-2}: @var{message}
757 @end example
758
759 Error messages from other noninteractive programs should look like this:
760
761 @example
762 @var{program}:@var{source-file-name}:@var{lineno}: @var{message}
763 @end example
764
765 @noindent
766 when there is an appropriate source file, or like this:
767
768 @example
769 @var{program}: @var{message}
770 @end example
771
772 @noindent
773 when there is no relevant source file.
774
775 If you want to mention the column number, use this format:
776
777 @example
778 @var{program}:@var{source-file-name}:@var{lineno}:@var{column}: @var{message}
779 @end example
780
781 In an interactive program (one that is reading commands from a
782 terminal), it is better not to include the program name in an error
783 message.  The place to indicate which program is running is in the
784 prompt or with the screen layout.  (When the same program runs with
785 input from a source other than a terminal, it is not interactive and
786 would do best to print error messages using the noninteractive style.)
787
788 The string @var{message} should not begin with a capital letter when
789 it follows a program name and/or file name, because that isn't the
790 beginning of a sentence.  (The sentence conceptually starts at the
791 beginning of the line.)  Also, it should not end with a period.
792
793 Error messages from interactive programs, and other messages such as
794 usage messages, should start with a capital letter.  But they should not
795 end with a period.
796
797 @node User Interfaces
798 @section Standards for Interfaces Generally
799
800 @cindex program name and its behavior
801 @cindex behavior, dependent on program's name
802 Please don't make the behavior of a utility depend on the name used
803 to invoke it.  It is useful sometimes to make a link to a utility
804 with a different name, and that should not change what it does.
805
806 Instead, use a run time option or a compilation switch or both
807 to select among the alternate behaviors.
808
809 @cindex output device and program's behavior
810 Likewise, please don't make the behavior of the program depend on the
811 type of output device it is used with.  Device independence is an
812 important principle of the system's design; do not compromise it merely
813 to save someone from typing an option now and then.  (Variation in error
814 message syntax when using a terminal is ok, because that is a side issue
815 that people do not depend on.)
816
817 If you think one behavior is most useful when the output is to a
818 terminal, and another is most useful when the output is a file or a
819 pipe, then it is usually best to make the default behavior the one that
820 is useful with output to a terminal, and have an option for the other
821 behavior.
822
823 Compatibility requires certain programs to depend on the type of output
824 device.  It would be disastrous if @code{ls} or @code{sh} did not do so
825 in the way all users expect.  In some of these cases, we supplement the
826 program with a preferred alternate version that does not depend on the
827 output device type.  For example, we provide a @code{dir} program much
828 like @code{ls} except that its default output format is always
829 multi-column format.
830
831
832 @node Graphical Interfaces
833 @section Standards for Graphical Interfaces
834 @cindex graphical user interface
835
836 @cindex gtk+
837 When you write a program that provides a graphical user interface,
838 please make it work with X Windows and the GTK+ toolkit unless the
839 functionality specifically requires some alternative (for example,
840 ``displaying jpeg images while in console mode'').
841
842 In addition, please provide a command-line interface to control the
843 functionality.  (In many cases, the graphical user interface can be a
844 separate program which invokes the command-line program.)  This is
845 so that the same jobs can be done from scripts.
846
847 @cindex corba
848 @cindex gnome
849 Please also consider providing a CORBA interface (for use from GNOME), a
850 library interface (for use from C), and perhaps a keyboard-driven
851 console interface (for use by users from console mode).  Once you are
852 doing the work to provide the functionality and the graphical interface,
853 these won't be much extra work.
854
855
856 @node Command-Line Interfaces
857 @section Standards for Command Line Interfaces
858 @cindex command-line interface
859
860 @findex getopt
861 It is a good idea to follow the @sc{posix} guidelines for the
862 command-line options of a program.  The easiest way to do this is to use
863 @code{getopt} to parse them.  Note that the GNU version of @code{getopt}
864 will normally permit options anywhere among the arguments unless the
865 special argument @samp{--} is used.  This is not what @sc{posix}
866 specifies; it is a GNU extension.
867
868 @cindex long-named options
869 Please define long-named options that are equivalent to the
870 single-letter Unix-style options.  We hope to make GNU more user
871 friendly this way.  This is easy to do with the GNU function
872 @code{getopt_long}.
873
874 One of the advantages of long-named options is that they can be
875 consistent from program to program.  For example, users should be able
876 to expect the ``verbose'' option of any GNU program which has one, to be
877 spelled precisely @samp{--verbose}.  To achieve this uniformity, look at
878 the table of common long-option names when you choose the option names
879 for your program (@pxref{Option Table}).
880
881 It is usually a good idea for file names given as ordinary arguments to
882 be input files only; any output files would be specified using options
883 (preferably @samp{-o} or @samp{--output}).  Even if you allow an output
884 file name as an ordinary argument for compatibility, try to provide an
885 option as another way to specify it.  This will lead to more consistency
886 among GNU utilities, and fewer idiosyncrasies for users to remember.
887
888 @cindex standard command-line options
889 @cindex options, standard command-line
890 @cindex CGI programs, standard options for
891 @cindex PATH_INFO, specifying standard options as
892 All programs should support two standard options: @samp{--version}
893 and @samp{--help}.  CGI programs should accept these as command-line
894 options, and also if given as the @env{PATH_INFO}; for instance,
895 visiting @url{http://example.org/p.cgi/--help} in a browser should
896 output the same information as invoking @samp{p.cgi --help} from the
897 command line.
898
899 @menu
900 * --version::       The standard output for --version.
901 * --help::          The standard output for --help.
902 @end menu
903
904 @node --version
905 @subsection @option{--version}
906
907 @cindex @samp{--version} output
908
909 The standard @code{--version} option should direct the program to
910 print information about its name, version, origin and legal status,
911 all on standard output, and then exit successfully.  Other options and
912 arguments should be ignored once this is seen, and the program should
913 not perform its normal function.
914
915 @cindex canonical name of a program
916 @cindex program's canonical name
917 The first line is meant to be easy for a program to parse; the version
918 number proper starts after the last space.  In addition, it contains
919 the canonical name for this program, in this format:
920
921 @example
922 GNU Emacs 19.30
923 @end example
924
925 @noindent
926 The program's name should be a constant string; @emph{don't} compute it
927 from @code{argv[0]}.  The idea is to state the standard or canonical
928 name for the program, not its file name.  There are other ways to find
929 out the precise file name where a command is found in @code{PATH}.
930
931 If the program is a subsidiary part of a larger package, mention the
932 package name in parentheses, like this:
933
934 @example
935 emacsserver (GNU Emacs) 19.30
936 @end example
937
938 @noindent
939 If the package has a version number which is different from this
940 program's version number, you can mention the package version number
941 just before the close-parenthesis.
942
943 If you @emph{need} to mention the version numbers of libraries which
944 are distributed separately from the package which contains this program,
945 you can do so by printing an additional line of version info for each
946 library you want to mention.  Use the same format for these lines as for
947 the first line.
948
949 Please do not mention all of the libraries that the program uses ``just
950 for completeness''---that would produce a lot of unhelpful clutter.
951 Please mention library version numbers only if you find in practice that
952 they are very important to you in debugging.
953
954 The following line, after the version number line or lines, should be a
955 copyright notice.  If more than one copyright notice is called for, put
956 each on a separate line.
957
958 Next should follow a line stating the license, preferably using one of
959 abbrevations below, and a brief statement that the program is free
960 software, and that users are free to copy and change it.  Also mention
961 that there is no warranty, to the extent permitted by law.  See
962 recommended wording below.
963
964 It is ok to finish the output with a list of the major authors of the
965 program, as a way of giving credit.
966
967 Here's an example of output that follows these rules:
968
969 @smallexample
970 GNU hello 2.3
971 Copyright (C) 2007 Free Software Foundation, Inc.
972 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
973 This is free software: you are free to change and redistribute it.
974 There is NO WARRANTY, to the extent permitted by law.
975 @end smallexample
976
977 You should adapt this to your program, of course, filling in the proper
978 year, copyright holder, name of program, and the references to
979 distribution terms, and changing the rest of the wording as necessary.
980
981 This copyright notice only needs to mention the most recent year in
982 which changes were made---there's no need to list the years for previous
983 versions' changes.  You don't have to mention the name of the program in
984 these notices, if that is inconvenient, since it appeared in the first
985 line.  (The rules are different for copyright notices in source files;
986 @pxref{Copyright Notices,,,maintain,Information for GNU Maintainers}.)
987
988 Translations of the above lines must preserve the validity of the
989 copyright notices (@pxref{Internationalization}).  If the translation's
990 character set supports it, the @samp{(C)} should be replaced with the
991 copyright symbol, as follows:
992
993 @ifinfo
994 (the official copyright symbol, which is the letter C in a circle);
995 @end ifinfo
996 @ifnotinfo
997 @copyright{}
998 @end ifnotinfo
999
1000 Write the word ``Copyright'' exactly like that, in English.  Do not
1001 translate it into another language.  International treaties recognize
1002 the English word ``Copyright''; translations into other languages do not
1003 have legal significance.
1004
1005 Finally, here is the table of our suggested license abbreviations.
1006 Any abbreviation can be followed by @samp{v@var{version}[+]}, meaning
1007 that particular version, or later versions with the @samp{+}, as shown
1008 above.
1009
1010 In the case of exceptions for extra permissions with the GPL, we use
1011 @samp{/} for a separator; the version number can follow the license
1012 abbreviation as usual, as in the examples below.
1013
1014 @table @asis
1015 @item GPL
1016 GNU General Public License, @url{http://www.gnu.org/licenses/gpl.html}.
1017
1018 @item LGPL
1019 GNU Lesser General Public License, @url{http://www.gnu.org/licenses/lgpl.html}.
1020
1021 @item GPL/Guile
1022 GNU GPL with the exception for Guile; for example, GPLv3+/Guile means
1023 the GNU GPL version 3 or later, with the extra exception for Guile.
1024
1025 GNU GPL with the exception for Ada.
1026
1027 @item Apache
1028 The Apache Software Foundation license,
1029 @url{http://www.apache.org/licenses}.
1030
1031 @item Artistic
1032 The Artistic license used for Perl, @url{http://www.perlfoundation.org/legal}.
1033
1034 @item Expat
1035 The Expat license, @url{http://www.jclark.com/xml/copying.txt}.
1036
1037 @item MPL
1038 The Mozilla Public License, @url{http://www.mozilla.org/MPL/}.
1039
1040 @item OBSD
1041 The original (4-clause) BSD license, incompatible with the GNU GPL
1042 @url{http://www.xfree86.org/3.3.6/COPYRIGHT2.html#6}.
1043
1044 @item PHP
1045 The license used for PHP, @url{http://www.php.net/license/}.
1046
1047 @item public domain
1048 The non-license that is being in the public domain,
1049 @url{http://www.gnu.org/licenses/license-list.html#PublicDomain}.
1050
1051 @item Python
1052 The license for Python, @url{http://www.python.org/2.0.1/license.html}.
1053
1054 @item RBSD
1055 The revised (3-clause) BSD, compatible with the GNU GPL,
1056 @url{http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5}.
1057
1058 @item X11
1059 The simple non-copyleft license used for most versions of the X Window
1060 system, @url{http://www.xfree86.org/3.3.6/COPYRIGHT2.html#3}.
1061
1062 @item Zlib
1063 The license for Zlib, @url{http://www.gzip.org/zlib/zlib_license.html}.
1064
1065 @end table
1066
1067 More information about these licenses and many more are on the GNU
1068 licensing web pages,
1069 @url{http://www.gnu.org/licenses/license-list.html}.
1070
1071
1072 @node --help
1073 @subsection @option{--help}
1074
1075 @cindex @samp{--help} output
1076
1077 The standard @code{--help} option should output brief documentation
1078 for how to invoke the program, on standard output, then exit
1079 successfully.  Other options and arguments should be ignored once this
1080 is seen, and the program should not perform its normal function.
1081
1082 @cindex address for bug reports
1083 @cindex bug reports
1084 Near the end of the @samp{--help} option's output there should be a line
1085 that says where to mail bug reports.  It should have this format:
1086
1087 @example
1088 Report bugs to @var{mailing-address}.
1089 @end example
1090
1091
1092 @node Option Table
1093 @section Table of Long Options
1094 @cindex long option names
1095 @cindex table of long options
1096
1097 Here is a table of long options used by GNU programs.  It is surely
1098 incomplete, but we aim to list all the options that a new program might
1099 want to be compatible with.  If you use names not already in the table,
1100 please send @email{bug-standards@@gnu.org} a list of them, with their
1101 meanings, so we can update the table.
1102
1103 @c Please leave newlines between items in this table; it's much easier
1104 @c to update when it isn't completely squashed together and unreadable.
1105 @c When there is more than one short option for a long option name, put
1106 @c a semicolon between the lists of the programs that use them, not a
1107 @c period.   --friedman
1108
1109 @table @samp
1110 @item after-date
1111 @samp{-N} in @code{tar}.
1112
1113 @item all
1114 @samp{-a} in @code{du}, @code{ls}, @code{nm}, @code{stty}, @code{uname},
1115 and @code{unexpand}.
1116
1117 @item all-text
1118 @samp{-a} in @code{diff}.
1119
1120 @item almost-all
1121 @samp{-A} in @code{ls}.
1122
1123 @item append
1124 @samp{-a} in @code{etags}, @code{tee}, @code{time};
1125 @samp{-r} in @code{tar}.
1126
1127 @item archive
1128 @samp{-a} in @code{cp}.
1129
1130 @item archive-name
1131 @samp{-n} in @code{shar}.
1132
1133 @item arglength
1134 @samp{-l} in @code{m4}.
1135
1136 @item ascii
1137 @samp{-a} in @code{diff}.
1138
1139 @item assign
1140 @samp{-v} in @code{gawk}.
1141
1142 @item assume-new
1143 @samp{-W} in Make.
1144
1145 @item assume-old
1146 @samp{-o} in Make.
1147
1148 @item auto-check
1149 @samp{-a} in @code{recode}.
1150
1151 @item auto-pager
1152 @samp{-a} in @code{wdiff}.
1153
1154 @item auto-reference
1155 @samp{-A} in @code{ptx}.
1156
1157 @item avoid-wraps
1158 @samp{-n} in @code{wdiff}.
1159
1160 @item background
1161 For server programs, run in the background.
1162
1163 @item backward-search
1164 @samp{-B} in @code{ctags}.
1165
1166 @item basename
1167 @samp{-f} in @code{shar}.
1168
1169 @item batch
1170 Used in GDB.
1171
1172 @item baud
1173 Used in GDB.
1174
1175 @item before
1176 @samp{-b} in @code{tac}.
1177
1178 @item binary
1179 @samp{-b} in @code{cpio} and @code{diff}.
1180
1181 @item bits-per-code
1182 @samp{-b} in @code{shar}.
1183
1184 @item block-size
1185 Used in @code{cpio} and @code{tar}.
1186
1187 @item blocks
1188 @samp{-b} in @code{head} and @code{tail}.
1189
1190 @item break-file
1191 @samp{-b} in @code{ptx}.
1192
1193 @item brief
1194 Used in various programs to make output shorter.
1195
1196 @item bytes
1197 @samp{-c} in @code{head}, @code{split}, and @code{tail}.
1198
1199 @item c@t{++}
1200 @samp{-C} in @code{etags}.
1201
1202 @item catenate
1203 @samp{-A} in @code{tar}.
1204
1205 @item cd
1206 Used in various programs to specify the directory to use.
1207
1208 @item changes
1209 @samp{-c} in @code{chgrp} and @code{chown}.
1210
1211 @item classify
1212 @samp{-F} in @code{ls}.
1213
1214 @item colons
1215 @samp{-c} in @code{recode}.
1216
1217 @item command
1218 @samp{-c} in @code{su};
1219 @samp{-x} in GDB.
1220
1221 @item compare
1222 @samp{-d} in @code{tar}.
1223
1224 @item compat
1225 Used in @code{gawk}.
1226
1227 @item compress
1228 @samp{-Z} in @code{tar} and @code{shar}.
1229
1230 @item concatenate
1231 @samp{-A} in @code{tar}.
1232
1233 @item confirmation
1234 @samp{-w} in @code{tar}.
1235
1236 @item context
1237 Used in @code{diff}.
1238
1239 @item copyleft
1240 @samp{-W copyleft} in @code{gawk}.
1241
1242 @item copyright
1243 @samp{-C} in @code{ptx}, @code{recode}, and @code{wdiff};
1244 @samp{-W copyright} in @code{gawk}.
1245
1246 @item core
1247 Used in GDB.
1248
1249 @item count
1250 @samp{-q} in @code{who}.
1251
1252 @item count-links
1253 @samp{-l} in @code{du}.
1254
1255 @item create
1256 Used in @code{tar} and @code{cpio}.
1257
1258 @item cut-mark
1259 @samp{-c} in @code{shar}.
1260
1261 @item cxref
1262 @samp{-x} in @code{ctags}.
1263
1264 @item date
1265 @samp{-d} in @code{touch}.
1266
1267 @item debug
1268 @samp{-d} in Make and @code{m4};
1269 @samp{-t} in Bison.
1270
1271 @item define
1272 @samp{-D} in @code{m4}.
1273
1274 @item defines
1275 @samp{-d} in Bison and @code{ctags}.
1276
1277 @item delete
1278 @samp{-D} in @code{tar}.
1279
1280 @item dereference
1281 @samp{-L} in @code{chgrp}, @code{chown}, @code{cpio}, @code{du},
1282 @code{ls}, and @code{tar}.
1283
1284 @item dereference-args
1285 @samp{-D} in @code{du}.
1286
1287 @item device
1288 Specify an I/O device (special file name).
1289
1290 @item diacritics
1291 @samp{-d} in @code{recode}.
1292
1293 @item dictionary-order
1294 @samp{-d} in @code{look}.
1295
1296 @item diff
1297 @samp{-d} in @code{tar}.
1298
1299 @item digits
1300 @samp{-n} in @code{csplit}.
1301
1302 @item directory
1303 Specify the directory to use, in various programs.  In @code{ls}, it
1304 means to show directories themselves rather than their contents.  In
1305 @code{rm} and @code{ln}, it means to not treat links to directories
1306 specially.
1307
1308 @item discard-all
1309 @samp{-x} in @code{strip}.
1310
1311 @item discard-locals
1312 @samp{-X} in @code{strip}.
1313
1314 @item dry-run
1315 @samp{-n} in Make.
1316
1317 @item ed
1318 @samp{-e} in @code{diff}.
1319
1320 @item elide-empty-files
1321 @samp{-z} in @code{csplit}.
1322
1323 @item end-delete
1324 @samp{-x} in @code{wdiff}.
1325
1326 @item end-insert
1327 @samp{-z} in @code{wdiff}.
1328
1329 @item entire-new-file
1330 @samp{-N} in @code{diff}.
1331
1332 @item environment-overrides
1333 @samp{-e} in Make.
1334
1335 @item eof
1336 @samp{-e} in @code{xargs}.
1337
1338 @item epoch
1339 Used in GDB.
1340
1341 @item error-limit
1342 Used in @code{makeinfo}.
1343
1344 @item error-output
1345 @samp{-o} in @code{m4}.
1346
1347 @item escape
1348 @samp{-b} in @code{ls}.
1349
1350 @item exclude-from
1351 @samp{-X} in @code{tar}.
1352
1353 @item exec
1354 Used in GDB.
1355
1356 @item exit
1357 @samp{-x} in @code{xargs}.
1358
1359 @item exit-0
1360 @samp{-e} in @code{unshar}.
1361
1362 @item expand-tabs
1363 @samp{-t} in @code{diff}.
1364
1365 @item expression
1366 @samp{-e} in @code{sed}.
1367
1368 @item extern-only
1369 @samp{-g} in @code{nm}.
1370
1371 @item extract
1372 @samp{-i} in @code{cpio};
1373 @samp{-x} in @code{tar}.
1374
1375 @item faces
1376 @samp{-f} in @code{finger}.
1377
1378 @item fast
1379 @samp{-f} in @code{su}.
1380
1381 @item fatal-warnings
1382 @samp{-E} in @code{m4}.
1383
1384 @item file
1385 @samp{-f} in @code{info}, @code{gawk}, Make, @code{mt}, and @code{tar};
1386 @samp{-n} in @code{sed};
1387 @samp{-r} in @code{touch}.
1388
1389 @item field-separator
1390 @samp{-F} in @code{gawk}.
1391
1392 @item file-prefix
1393 @samp{-b} in Bison.
1394
1395 @item file-type
1396 @samp{-F} in @code{ls}.
1397
1398 @item files-from
1399 @samp{-T} in @code{tar}.
1400
1401 @item fill-column
1402 Used in @code{makeinfo}.
1403
1404 @item flag-truncation
1405 @samp{-F} in @code{ptx}.
1406
1407 @item fixed-output-files
1408 @samp{-y} in Bison.
1409
1410 @item follow
1411 @samp{-f} in @code{tail}.
1412
1413 @item footnote-style
1414 Used in @code{makeinfo}.
1415
1416 @item force
1417 @samp{-f} in @code{cp}, @code{ln}, @code{mv}, and @code{rm}.
1418
1419 @item force-prefix
1420 @samp{-F} in @code{shar}.
1421
1422 @item foreground
1423 For server programs, run in the foreground;
1424 in other words, don't do anything special to run the server
1425 in the background.
1426
1427 @item format
1428 Used in @code{ls}, @code{time}, and @code{ptx}.
1429
1430 @item freeze-state
1431 @samp{-F} in @code{m4}.
1432
1433 @item fullname
1434 Used in GDB.
1435
1436 @item gap-size
1437 @samp{-g} in @code{ptx}.
1438
1439 @item get
1440 @samp{-x} in @code{tar}.
1441
1442 @item graphic
1443 @samp{-i} in @code{ul}.
1444
1445 @item graphics
1446 @samp{-g} in @code{recode}.
1447
1448 @item group
1449 @samp{-g} in @code{install}.
1450
1451 @item gzip
1452 @samp{-z} in @code{tar} and @code{shar}.
1453
1454 @item hashsize
1455 @samp{-H} in @code{m4}.
1456
1457 @item header
1458 @samp{-h} in @code{objdump} and @code{recode}
1459
1460 @item heading
1461 @samp{-H} in @code{who}.
1462
1463 @item help
1464 Used to ask for brief usage information.
1465
1466 @item here-delimiter
1467 @samp{-d} in @code{shar}.
1468
1469 @item hide-control-chars
1470 @samp{-q} in @code{ls}.
1471
1472 @item html
1473 In @code{makeinfo}, output HTML.
1474
1475 @item idle
1476 @samp{-u} in @code{who}.
1477
1478 @item ifdef
1479 @samp{-D} in @code{diff}.
1480
1481 @item ignore
1482 @samp{-I} in @code{ls};
1483 @samp{-x} in @code{recode}.
1484
1485 @item ignore-all-space
1486 @samp{-w} in @code{diff}.
1487
1488 @item ignore-backups
1489 @samp{-B} in @code{ls}.
1490
1491 @item ignore-blank-lines
1492 @samp{-B} in @code{diff}.
1493
1494 @item ignore-case
1495 @samp{-f} in @code{look} and @code{ptx};
1496 @samp{-i} in @code{diff} and @code{wdiff}.
1497
1498 @item ignore-errors
1499 @samp{-i} in Make.
1500
1501 @item ignore-file
1502 @samp{-i} in @code{ptx}.
1503
1504 @item ignore-indentation
1505 @samp{-I} in @code{etags}.
1506
1507 @item ignore-init-file
1508 @samp{-f} in Oleo.
1509
1510 @item ignore-interrupts
1511 @samp{-i} in @code{tee}.
1512
1513 @item ignore-matching-lines
1514 @samp{-I} in @code{diff}.
1515
1516 @item ignore-space-change
1517 @samp{-b} in @code{diff}.
1518
1519 @item ignore-zeros
1520 @samp{-i} in @code{tar}.
1521
1522 @item include
1523 @samp{-i} in @code{etags};
1524 @samp{-I} in @code{m4}.
1525
1526 @item include-dir
1527 @samp{-I} in Make.
1528
1529 @item incremental
1530 @samp{-G} in @code{tar}.
1531
1532 @item info
1533 @samp{-i}, @samp{-l}, and @samp{-m} in Finger.
1534
1535 @item init-file
1536 In some programs, specify the name of the file to read as the user's
1537 init file.
1538
1539 @item initial
1540 @samp{-i} in @code{expand}.
1541
1542 @item initial-tab
1543 @samp{-T} in @code{diff}.
1544
1545 @item inode
1546 @samp{-i} in @code{ls}.
1547
1548 @item interactive
1549 @samp{-i} in @code{cp}, @code{ln}, @code{mv}, @code{rm};
1550 @samp{-e} in @code{m4};
1551 @samp{-p} in @code{xargs};
1552 @samp{-w} in @code{tar}.
1553
1554 @item intermix-type
1555 @samp{-p} in @code{shar}.
1556
1557 @item iso-8601
1558 Used in @code{date}
1559
1560 @item jobs
1561 @samp{-j} in Make.
1562
1563 @item just-print
1564 @samp{-n} in Make.
1565
1566 @item keep-going
1567 @samp{-k} in Make.
1568
1569 @item keep-files
1570 @samp{-k} in @code{csplit}.
1571
1572 @item kilobytes
1573 @samp{-k} in @code{du} and @code{ls}.
1574
1575 @item language
1576 @samp{-l} in @code{etags}.
1577
1578 @item less-mode
1579 @samp{-l} in @code{wdiff}.
1580
1581 @item level-for-gzip
1582 @samp{-g} in @code{shar}.
1583
1584 @item line-bytes
1585 @samp{-C} in @code{split}.
1586
1587 @item lines
1588 Used in @code{split}, @code{head}, and @code{tail}.
1589
1590 @item link
1591 @samp{-l} in @code{cpio}.
1592
1593 @item lint
1594 @itemx lint-old
1595 Used in @code{gawk}.
1596
1597 @item list
1598 @samp{-t} in @code{cpio};
1599 @samp{-l} in @code{recode}.
1600
1601 @item list
1602 @samp{-t} in @code{tar}.
1603
1604 @item literal
1605 @samp{-N} in @code{ls}.
1606
1607 @item load-average
1608 @samp{-l} in Make.
1609
1610 @item login
1611 Used in @code{su}.
1612
1613 @item machine
1614 Used in @code{uname}.
1615
1616 @item macro-name
1617 @samp{-M} in @code{ptx}.
1618
1619 @item mail
1620 @samp{-m} in @code{hello} and @code{uname}.
1621
1622 @item make-directories
1623 @samp{-d} in @code{cpio}.
1624
1625 @item makefile
1626 @samp{-f} in Make.
1627
1628 @item mapped
1629 Used in GDB.
1630
1631 @item max-args
1632 @samp{-n} in @code{xargs}.
1633
1634 @item max-chars
1635 @samp{-n} in @code{xargs}.
1636
1637 @item max-lines
1638 @samp{-l} in @code{xargs}.
1639
1640 @item max-load
1641 @samp{-l} in Make.
1642
1643 @item max-procs
1644 @samp{-P} in @code{xargs}.
1645
1646 @item mesg
1647 @samp{-T} in @code{who}.
1648
1649 @item message
1650 @samp{-T} in @code{who}.
1651
1652 @item minimal
1653 @samp{-d} in @code{diff}.
1654
1655 @item mixed-uuencode
1656 @samp{-M} in @code{shar}.
1657
1658 @item mode
1659 @samp{-m} in @code{install}, @code{mkdir}, and @code{mkfifo}.
1660
1661 @item modification-time
1662 @samp{-m} in @code{tar}.
1663
1664 @item multi-volume
1665 @samp{-M} in @code{tar}.
1666
1667 @item name-prefix
1668 @samp{-a} in Bison.
1669
1670 @item nesting-limit
1671 @samp{-L} in @code{m4}.
1672
1673 @item net-headers
1674 @samp{-a} in @code{shar}.
1675
1676 @item new-file
1677 @samp{-W} in Make.
1678
1679 @item no-builtin-rules
1680 @samp{-r} in Make.
1681
1682 @item no-character-count
1683 @samp{-w} in @code{shar}.
1684
1685 @item no-check-existing
1686 @samp{-x} in @code{shar}.
1687
1688 @item no-common
1689 @samp{-3} in @code{wdiff}.
1690
1691 @item no-create
1692 @samp{-c} in @code{touch}.
1693
1694 @item no-defines
1695 @samp{-D} in @code{etags}.
1696
1697 @item no-deleted
1698 @samp{-1} in @code{wdiff}.
1699
1700 @item no-dereference
1701 @samp{-d} in @code{cp}.
1702
1703 @item no-inserted
1704 @samp{-2} in @code{wdiff}.
1705
1706 @item no-keep-going
1707 @samp{-S} in Make.
1708
1709 @item no-lines
1710 @samp{-l} in Bison.
1711
1712 @item no-piping
1713 @samp{-P} in @code{shar}.
1714
1715 @item no-prof
1716 @samp{-e} in @code{gprof}.
1717
1718 @item no-regex
1719 @samp{-R} in @code{etags}.
1720
1721 @item no-sort
1722 @samp{-p} in @code{nm}.
1723
1724 @item no-splash
1725 Don't print a startup splash screen.
1726
1727 @item no-split
1728 Used in @code{makeinfo}.
1729
1730 @item no-static
1731 @samp{-a} in @code{gprof}.
1732
1733 @item no-time
1734 @samp{-E} in @code{gprof}.
1735
1736 @item no-timestamp
1737 @samp{-m} in @code{shar}.
1738
1739 @item no-validate
1740 Used in @code{makeinfo}.
1741
1742 @item no-wait
1743 Used in @code{emacsclient}.
1744
1745 @item no-warn
1746 Used in various programs to inhibit warnings.
1747
1748 @item node
1749 @samp{-n} in @code{info}.
1750
1751 @item nodename
1752 @samp{-n} in @code{uname}.
1753
1754 @item nonmatching
1755 @samp{-f} in @code{cpio}.
1756
1757 @item nstuff
1758 @samp{-n} in @code{objdump}.
1759
1760 @item null
1761 @samp{-0} in @code{xargs}.
1762
1763 @item number
1764 @samp{-n} in @code{cat}.
1765
1766 @item number-nonblank
1767 @samp{-b} in @code{cat}.
1768
1769 @item numeric-sort
1770 @samp{-n} in @code{nm}.
1771
1772 @item numeric-uid-gid
1773 @samp{-n} in @code{cpio} and @code{ls}.
1774
1775 @item nx
1776 Used in GDB.
1777
1778 @item old-archive
1779 @samp{-o} in @code{tar}.
1780
1781 @item old-file
1782 @samp{-o} in Make.
1783
1784 @item one-file-system
1785 @samp{-l} in @code{tar}, @code{cp}, and @code{du}.
1786
1787 @item only-file
1788 @samp{-o} in @code{ptx}.
1789
1790 @item only-prof
1791 @samp{-f} in @code{gprof}.
1792
1793 @item only-time
1794 @samp{-F} in @code{gprof}.
1795
1796 @item options
1797 @samp{-o} in @code{getopt}, @code{fdlist}, @code{fdmount},
1798 @code{fdmountd}, and @code{fdumount}.
1799
1800 @item output
1801 In various programs, specify the output file name.
1802
1803 @item output-prefix
1804 @samp{-o} in @code{shar}.
1805
1806 @item override
1807 @samp{-o} in @code{rm}.
1808
1809 @item overwrite
1810 @samp{-c} in @code{unshar}.
1811
1812 @item owner
1813 @samp{-o} in @code{install}.
1814
1815 @item paginate
1816 @samp{-l} in @code{diff}.
1817
1818 @item paragraph-indent
1819 Used in @code{makeinfo}.
1820
1821 @item parents
1822 @samp{-p} in @code{mkdir} and @code{rmdir}.
1823
1824 @item pass-all
1825 @samp{-p} in @code{ul}.
1826
1827 @item pass-through
1828 @samp{-p} in @code{cpio}.
1829
1830 @item port
1831 @samp{-P} in @code{finger}.
1832
1833 @item portability
1834 @samp{-c} in @code{cpio} and @code{tar}.
1835
1836 @item posix
1837 Used in @code{gawk}.
1838
1839 @item prefix-builtins
1840 @samp{-P} in @code{m4}.
1841
1842 @item prefix
1843 @samp{-f} in @code{csplit}.
1844
1845 @item preserve
1846 Used in @code{tar} and @code{cp}.
1847
1848 @item preserve-environment
1849 @samp{-p} in @code{su}.
1850
1851 @item preserve-modification-time
1852 @samp{-m} in @code{cpio}.
1853
1854 @item preserve-order
1855 @samp{-s} in @code{tar}.
1856
1857 @item preserve-permissions
1858 @samp{-p} in @code{tar}.
1859
1860 @item print
1861 @samp{-l} in @code{diff}.
1862
1863 @item print-chars
1864 @samp{-L} in @code{cmp}.
1865
1866 @item print-data-base
1867 @samp{-p} in Make.
1868
1869 @item print-directory
1870 @samp{-w} in Make.
1871
1872 @item print-file-name
1873 @samp{-o} in @code{nm}.
1874
1875 @item print-symdefs
1876 @samp{-s} in @code{nm}.
1877
1878 @item printer
1879 @samp{-p} in @code{wdiff}.
1880
1881 @item prompt
1882 @samp{-p} in @code{ed}.
1883
1884 @item proxy
1885 Specify an HTTP proxy.
1886
1887 @item query-user
1888 @samp{-X} in @code{shar}.
1889
1890 @item question
1891 @samp{-q} in Make.
1892
1893 @item quiet
1894 Used in many programs to inhibit the usual output.  Every
1895 program accepting @samp{--quiet} should accept @samp{--silent} as a
1896 synonym.
1897
1898 @item quiet-unshar
1899 @samp{-Q} in @code{shar}
1900
1901 @item quote-name
1902 @samp{-Q} in @code{ls}.
1903
1904 @item rcs
1905 @samp{-n} in @code{diff}.
1906
1907 @item re-interval
1908 Used in @code{gawk}.
1909
1910 @item read-full-blocks
1911 @samp{-B} in @code{tar}.
1912
1913 @item readnow
1914 Used in GDB.
1915
1916 @item recon
1917 @samp{-n} in Make.
1918
1919 @item record-number
1920 @samp{-R} in @code{tar}.
1921
1922 @item recursive
1923 Used in @code{chgrp}, @code{chown}, @code{cp}, @code{ls}, @code{diff},
1924 and @code{rm}.
1925
1926 @item reference-limit
1927 Used in @code{makeinfo}.
1928
1929 @item references
1930 @samp{-r} in @code{ptx}.
1931
1932 @item regex
1933 @samp{-r} in @code{tac} and @code{etags}.
1934
1935 @item release
1936 @samp{-r} in @code{uname}.
1937
1938 @item reload-state
1939 @samp{-R} in @code{m4}.
1940
1941 @item relocation
1942 @samp{-r} in @code{objdump}.
1943
1944 @item rename
1945 @samp{-r} in @code{cpio}.
1946
1947 @item replace
1948 @samp{-i} in @code{xargs}.
1949
1950 @item report-identical-files
1951 @samp{-s} in @code{diff}.
1952
1953 @item reset-access-time
1954 @samp{-a} in @code{cpio}.
1955
1956 @item reverse
1957 @samp{-r} in @code{ls} and @code{nm}.
1958
1959 @item reversed-ed
1960 @samp{-f} in @code{diff}.
1961
1962 @item right-side-defs
1963 @samp{-R} in @code{ptx}.
1964
1965 @item same-order
1966 @samp{-s} in @code{tar}.
1967
1968 @item same-permissions
1969 @samp{-p} in @code{tar}.
1970
1971 @item save
1972 @samp{-g} in @code{stty}.
1973
1974 @item se
1975 Used in GDB.
1976
1977 @item sentence-regexp
1978 @samp{-S} in @code{ptx}.
1979
1980 @item separate-dirs
1981 @samp{-S} in @code{du}.
1982
1983 @item separator
1984 @samp{-s} in @code{tac}.
1985
1986 @item sequence
1987 Used by @code{recode} to chose files or pipes for sequencing passes.
1988
1989 @item shell
1990 @samp{-s} in @code{su}.
1991
1992 @item show-all
1993 @samp{-A} in @code{cat}.
1994
1995 @item show-c-function
1996 @samp{-p} in @code{diff}.
1997
1998 @item show-ends
1999 @samp{-E} in @code{cat}.
2000
2001 @item show-function-line
2002 @samp{-F} in @code{diff}.
2003
2004 @item show-tabs
2005 @samp{-T} in @code{cat}.
2006
2007 @item silent
2008 Used in many programs to inhibit the usual output.
2009 Every program accepting
2010 @samp{--silent} should accept @samp{--quiet} as a synonym.
2011
2012 @item size
2013 @samp{-s} in @code{ls}.
2014
2015 @item socket
2016 Specify a file descriptor for a network server to use for its socket,
2017 instead of opening and binding a new socket.  This provides a way to
2018 run, in a non-privileged process, a server that normally needs a
2019 reserved port number.
2020
2021 @item sort
2022 Used in @code{ls}.
2023
2024 @item source
2025 @samp{-W source} in @code{gawk}.
2026
2027 @item sparse
2028 @samp{-S} in @code{tar}.
2029
2030 @item speed-large-files
2031 @samp{-H} in @code{diff}.
2032
2033 @item split-at
2034 @samp{-E} in @code{unshar}.
2035
2036 @item split-size-limit
2037 @samp{-L} in @code{shar}.
2038
2039 @item squeeze-blank
2040 @samp{-s} in @code{cat}.
2041
2042 @item start-delete
2043 @samp{-w} in @code{wdiff}.
2044
2045 @item start-insert
2046 @samp{-y} in @code{wdiff}.
2047
2048 @item starting-file
2049 Used in @code{tar} and @code{diff} to specify which file within
2050 a directory to start processing with.
2051
2052 @item statistics
2053 @samp{-s} in @code{wdiff}.
2054
2055 @item stdin-file-list
2056 @samp{-S} in @code{shar}.
2057
2058 @item stop
2059 @samp{-S} in Make.
2060
2061 @item strict
2062 @samp{-s} in @code{recode}.
2063
2064 @item strip
2065 @samp{-s} in @code{install}.
2066
2067 @item strip-all
2068 @samp{-s} in @code{strip}.
2069
2070 @item strip-debug
2071 @samp{-S} in @code{strip}.
2072
2073 @item submitter
2074 @samp{-s} in @code{shar}.
2075
2076 @item suffix
2077 @samp{-S} in @code{cp}, @code{ln}, @code{mv}.
2078
2079 @item suffix-format
2080 @samp{-b} in @code{csplit}.
2081
2082 @item sum
2083 @samp{-s} in @code{gprof}.
2084
2085 @item summarize
2086 @samp{-s} in @code{du}.
2087
2088 @item symbolic
2089 @samp{-s} in @code{ln}.
2090
2091 @item symbols
2092 Used in GDB and @code{objdump}.
2093
2094 @item synclines
2095 @samp{-s} in @code{m4}.
2096
2097 @item sysname
2098 @samp{-s} in @code{uname}.
2099
2100 @item tabs
2101 @samp{-t} in @code{expand} and @code{unexpand}.
2102
2103 @item tabsize
2104 @samp{-T} in @code{ls}.
2105
2106 @item terminal
2107 @samp{-T} in @code{tput} and @code{ul}.
2108 @samp{-t} in @code{wdiff}.
2109
2110 @item text
2111 @samp{-a} in @code{diff}.
2112
2113 @item text-files
2114 @samp{-T} in @code{shar}.
2115
2116 @item time
2117 Used in @code{ls} and @code{touch}.
2118
2119 @item timeout
2120 Specify how long to wait before giving up on some operation.
2121
2122 @item to-stdout
2123 @samp{-O} in @code{tar}.
2124
2125 @item total
2126 @samp{-c} in @code{du}.
2127
2128 @item touch
2129 @samp{-t} in Make, @code{ranlib}, and @code{recode}.
2130
2131 @item trace
2132 @samp{-t} in @code{m4}.
2133
2134 @item traditional
2135 @samp{-t} in @code{hello};
2136 @samp{-W traditional} in @code{gawk};
2137 @samp{-G} in @code{ed}, @code{m4}, and @code{ptx}.
2138
2139 @item tty
2140 Used in GDB.
2141
2142 @item typedefs
2143 @samp{-t} in @code{ctags}.
2144
2145 @item typedefs-and-c++
2146 @samp{-T} in @code{ctags}.
2147
2148 @item typeset-mode
2149 @samp{-t} in @code{ptx}.
2150
2151 @item uncompress
2152 @samp{-z} in @code{tar}.
2153
2154 @item unconditional
2155 @samp{-u} in @code{cpio}.
2156
2157 @item undefine
2158 @samp{-U} in @code{m4}.
2159
2160 @item undefined-only
2161 @samp{-u} in @code{nm}.
2162
2163 @item update
2164 @samp{-u} in @code{cp}, @code{ctags}, @code{mv}, @code{tar}.
2165
2166 @item usage
2167 Used in @code{gawk}; same as @samp{--help}.
2168
2169 @item uuencode
2170 @samp{-B} in @code{shar}.
2171
2172 @item vanilla-operation
2173 @samp{-V} in @code{shar}.
2174
2175 @item verbose
2176 Print more information about progress.  Many programs support this.
2177
2178 @item verify
2179 @samp{-W} in @code{tar}.
2180
2181 @item version
2182 Print the version number.
2183
2184 @item version-control
2185 @samp{-V} in @code{cp}, @code{ln}, @code{mv}.
2186
2187 @item vgrind
2188 @samp{-v} in @code{ctags}.
2189
2190 @item volume
2191 @samp{-V} in @code{tar}.
2192
2193 @item what-if
2194 @samp{-W} in Make.
2195
2196 @item whole-size-limit
2197 @samp{-l} in @code{shar}.
2198
2199 @item width
2200 @samp{-w} in @code{ls} and @code{ptx}.
2201
2202 @item word-regexp
2203 @samp{-W} in @code{ptx}.
2204
2205 @item writable
2206 @samp{-T} in @code{who}.
2207
2208 @item zeros
2209 @samp{-z} in @code{gprof}.
2210 @end table
2211
2212 @node Memory Usage
2213 @section Memory Usage
2214 @cindex memory usage
2215
2216 If a program typically uses just a few meg of memory, don't bother making any
2217 effort to reduce memory usage.  For example, if it is impractical for
2218 other reasons to operate on files more than a few meg long, it is
2219 reasonable to read entire input files into memory to operate on them.
2220
2221 However, for programs such as @code{cat} or @code{tail}, that can
2222 usefully operate on very large files, it is important to avoid using a
2223 technique that would artificially limit the size of files it can handle.
2224 If a program works by lines and could be applied to arbitrary
2225 user-supplied input files, it should keep only a line in memory, because
2226 this is not very hard and users will want to be able to operate on input
2227 files that are bigger than will fit in memory all at once.
2228
2229 If your program creates complicated data structures, just make them in
2230 memory and give a fatal error if @code{malloc} returns zero.
2231
2232 @node File Usage
2233 @section File Usage
2234 @cindex file usage
2235
2236 Programs should be prepared to operate when @file{/usr} and @file{/etc}
2237 are read-only file systems.  Thus, if the program manages log files,
2238 lock files, backup files, score files, or any other files which are
2239 modified for internal purposes, these files should not be stored in
2240 @file{/usr} or @file{/etc}.
2241
2242 There are two exceptions.  @file{/etc} is used to store system
2243 configuration information; it is reasonable for a program to modify
2244 files in @file{/etc} when its job is to update the system configuration.
2245 Also, if the user explicitly asks to modify one file in a directory, it
2246 is reasonable for the program to store other files in the same
2247 directory.
2248
2249 @node Writing C
2250 @chapter Making The Best Use of C
2251
2252 This chapter provides advice on how best to use the C language
2253 when writing GNU software.
2254
2255 @menu
2256 * Formatting::                  Formatting your source code.
2257 * Comments::                    Commenting your work.
2258 * Syntactic Conventions::       Clean use of C constructs.
2259 * Names::                       Naming variables, functions, and files.
2260 * System Portability::          Portability among different operating systems.
2261 * CPU Portability::             Supporting the range of CPU types.
2262 * System Functions::            Portability and ``standard'' library functions.
2263 * Internationalization::        Techniques for internationalization.
2264 * Character Set::               Use ASCII by default.
2265 * Quote Characters::            Use `...' in the C locale.
2266 * Mmap::                        How you can safely use @code{mmap}.
2267 @end menu
2268
2269 @node Formatting
2270 @section Formatting Your Source Code
2271 @cindex formatting source code
2272
2273 @cindex open brace
2274 @cindex braces, in C source
2275 It is important to put the open-brace that starts the body of a C
2276 function in column one, so that they will start a defun.  Several
2277 tools look for open-braces in column one to find the beginnings of C
2278 functions.  These tools will not work on code not formatted that way.
2279
2280 Avoid putting open-brace, open-parenthesis or open-bracket in column
2281 one when they are inside a function, so that they won't start a defun.
2282 The open-brace that starts a @code{struct} body can go in column one
2283 if you find it useful to treat that definition as a defun.
2284
2285 It is also important for function definitions to start the name of the
2286 function in column one.  This helps people to search for function
2287 definitions, and may also help certain tools recognize them.  Thus,
2288 using Standard C syntax, the format is this:
2289
2290 @example
2291 static char *
2292 concat (char *s1, char *s2)
2293 @{
2294   @dots{}
2295 @}
2296 @end example
2297
2298 @noindent
2299 or, if you want to use traditional C syntax, format the definition like
2300 this:
2301
2302 @example
2303 static char *
2304 concat (s1, s2)        /* Name starts in column one here */
2305      char *s1, *s2;
2306 @{                     /* Open brace in column one here */
2307   @dots{}
2308 @}
2309 @end example
2310
2311 In Standard C, if the arguments don't fit nicely on one line,
2312 split it like this:
2313
2314 @example
2315 int
2316 lots_of_args (int an_integer, long a_long, short a_short,
2317               double a_double, float a_float)
2318 @dots{}
2319 @end example
2320
2321 The rest of this section gives our recommendations for other aspects of
2322 C formatting style, which is also the default style of the @code{indent}
2323 program in version 1.2 and newer.  It corresponds to the options
2324
2325 @smallexample
2326 -nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2
2327 -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -psl -nsc -nsob
2328 @end smallexample
2329
2330 We don't think of these recommendations as requirements, because it
2331 causes no problems for users if two different programs have different
2332 formatting styles.
2333
2334 But whatever style you use, please use it consistently, since a mixture
2335 of styles within one program tends to look ugly.  If you are
2336 contributing changes to an existing program, please follow the style of
2337 that program.
2338
2339 For the body of the function, our recommended style looks like this:
2340
2341 @example
2342 if (x < foo (y, z))
2343   haha = bar[4] + 5;
2344 else
2345   @{
2346     while (z)
2347       @{
2348         haha += foo (z, z);
2349         z--;
2350       @}
2351     return ++x + bar ();
2352   @}
2353 @end example
2354
2355 @cindex spaces before open-paren
2356 We find it easier to read a program when it has spaces before the
2357 open-parentheses and after the commas.  Especially after the commas.
2358
2359 When you split an expression into multiple lines, split it
2360 before an operator, not after one.  Here is the right way:
2361
2362 @cindex expressions, splitting
2363 @example
2364 if (foo_this_is_long && bar > win (x, y, z)
2365     && remaining_condition)
2366 @end example
2367
2368 Try to avoid having two operators of different precedence at the same
2369 level of indentation.  For example, don't write this:
2370
2371 @example
2372 mode = (inmode[j] == VOIDmode
2373         || GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j])
2374         ? outmode[j] : inmode[j]);
2375 @end example
2376
2377 Instead, use extra parentheses so that the indentation shows the nesting:
2378
2379 @example
2380 mode = ((inmode[j] == VOIDmode
2381          || (GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j])))
2382         ? outmode[j] : inmode[j]);
2383 @end example
2384
2385 Insert extra parentheses so that Emacs will indent the code properly.
2386 For example, the following indentation looks nice if you do it by hand,
2387
2388 @example
2389 v = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000
2390     + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000;
2391 @end example
2392
2393 @noindent
2394 but Emacs would alter it.  Adding a set of parentheses produces
2395 something that looks equally nice, and which Emacs will preserve:
2396
2397 @example
2398 v = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000
2399      + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000);
2400 @end example
2401
2402 Format do-while statements like this:
2403
2404 @example
2405 do
2406   @{
2407     a = foo (a);
2408   @}
2409 while (a > 0);
2410 @end example
2411
2412 @cindex formfeed
2413 @cindex control-L
2414 Please use formfeed characters (control-L) to divide the program into
2415 pages at logical places (but not within a function).  It does not matter
2416 just how long the pages are, since they do not have to fit on a printed
2417 page.  The formfeeds should appear alone on lines by themselves.
2418
2419 @node Comments
2420 @section Commenting Your Work
2421 @cindex commenting
2422
2423 Every program should start with a comment saying briefly what it is for.
2424 Example: @samp{fmt - filter for simple filling of text}.  This comment
2425 should be at the top of the source file containing the @samp{main}
2426 function of the program.
2427
2428 Also, please write a brief comment at the start of each source file,
2429 with the file name and a line or two about the overall purpose of the
2430 file.
2431
2432 Please write the comments in a GNU program in English, because English
2433 is the one language that nearly all programmers in all countries can
2434 read.  If you do not write English well, please write comments in
2435 English as well as you can, then ask other people to help rewrite them.
2436 If you can't write comments in English, please find someone to work with
2437 you and translate your comments into English.
2438
2439 Please put a comment on each function saying what the function does,
2440 what sorts of arguments it gets, and what the possible values of
2441 arguments mean and are used for.  It is not necessary to duplicate in
2442 words the meaning of the C argument declarations, if a C type is being
2443 used in its customary fashion.  If there is anything nonstandard about
2444 its use (such as an argument of type @code{char *} which is really the
2445 address of the second character of a string, not the first), or any
2446 possible values that would not work the way one would expect (such as,
2447 that strings containing newlines are not guaranteed to work), be sure
2448 to say so.
2449
2450 Also explain the significance of the return value, if there is one.
2451
2452 Please put two spaces after the end of a sentence in your comments, so
2453 that the Emacs sentence commands will work.  Also, please write
2454 complete sentences and capitalize the first word.  If a lower-case
2455 identifier comes at the beginning of a sentence, don't capitalize it!
2456 Changing the spelling makes it a different identifier.  If you don't
2457 like starting a sentence with a lower case letter, write the sentence
2458 differently (e.g., ``The identifier lower-case is @dots{}'').
2459
2460 The comment on a function is much clearer if you use the argument
2461 names to speak about the argument values.  The variable name itself
2462 should be lower case, but write it in upper case when you are speaking
2463 about the value rather than the variable itself.  Thus, ``the inode
2464 number NODE_NUM'' rather than ``an inode''.
2465
2466 There is usually no purpose in restating the name of the function in
2467 the comment before it, because the reader can see that for himself.
2468 There might be an exception when the comment is so long that the function
2469 itself would be off the bottom of the screen.
2470
2471 There should be a comment on each static variable as well, like this:
2472
2473 @example
2474 /* Nonzero means truncate lines in the display;
2475    zero means continue them.  */
2476 int truncate_lines;
2477 @end example
2478
2479 @cindex conditionals, comments for
2480 @cindex @code{#endif}, commenting
2481 Every @samp{#endif} should have a comment, except in the case of short
2482 conditionals (just a few lines) that are not nested.  The comment should
2483 state the condition of the conditional that is ending, @emph{including
2484 its sense}.  @samp{#else} should have a comment describing the condition
2485 @emph{and sense} of the code that follows.  For example:
2486
2487 @example
2488 @group
2489 #ifdef foo
2490   @dots{}
2491 #else /* not foo */
2492   @dots{}
2493 #endif /* not foo */
2494 @end group
2495 @group
2496 #ifdef foo
2497   @dots{}
2498 #endif /* foo */
2499 @end group
2500 @end example
2501
2502 @noindent
2503 but, by contrast, write the comments this way for a @samp{#ifndef}:
2504
2505 @example
2506 @group
2507 #ifndef foo
2508   @dots{}
2509 #else /* foo */
2510   @dots{}
2511 #endif /* foo */
2512 @end group
2513 @group
2514 #ifndef foo
2515   @dots{}
2516 #endif /* not foo */
2517 @end group
2518 @end example
2519
2520 @node Syntactic Conventions
2521 @section Clean Use of C Constructs
2522 @cindex syntactic conventions
2523
2524 @cindex implicit @code{int}
2525 @cindex function argument, declaring
2526 Please explicitly declare the types of all objects.  For example, you
2527 should explicitly declare all arguments to functions, and you should
2528 declare functions to return @code{int} rather than omitting the
2529 @code{int}.
2530
2531 @cindex compiler warnings
2532 @cindex @samp{-Wall} compiler option
2533 Some programmers like to use the GCC @samp{-Wall} option, and change the
2534 code whenever it issues a warning.  If you want to do this, then do.
2535 Other programmers prefer not to use @samp{-Wall}, because it gives
2536 warnings for valid and legitimate code which they do not want to change.
2537 If you want to do this, then do.  The compiler should be your servant,
2538 not your master.
2539
2540 Declarations of external functions and functions to appear later in the
2541 source file should all go in one place near the beginning of the file
2542 (somewhere before the first function definition in the file), or else
2543 should go in a header file.  Don't put @code{extern} declarations inside
2544 functions.
2545
2546 @cindex temporary variables
2547 It used to be common practice to use the same local variables (with
2548 names like @code{tem}) over and over for different values within one
2549 function.  Instead of doing this, it is better to declare a separate local
2550 variable for each distinct purpose, and give it a name which is
2551 meaningful.  This not only makes programs easier to understand, it also
2552 facilitates optimization by good compilers.  You can also move the
2553 declaration of each local variable into the smallest scope that includes
2554 all its uses.  This makes the program even cleaner.
2555
2556 Don't use local variables or parameters that shadow global identifiers.
2557
2558 @cindex multiple variables in a line
2559 Don't declare multiple variables in one declaration that spans lines.
2560 Start a new declaration on each line, instead.  For example, instead
2561 of this:
2562
2563 @example
2564 @group
2565 int    foo,
2566        bar;
2567 @end group
2568 @end example
2569
2570 @noindent
2571 write either this:
2572
2573 @example
2574 int foo, bar;
2575 @end example
2576
2577 @noindent
2578 or this:
2579
2580 @example
2581 int foo;
2582 int bar;
2583 @end example
2584
2585 @noindent
2586 (If they are global variables, each should have a comment preceding it
2587 anyway.)
2588
2589 When you have an @code{if}-@code{else} statement nested in another
2590 @code{if} statement, always put braces around the @code{if}-@code{else}.
2591 Thus, never write like this:
2592
2593 @example
2594 if (foo)
2595   if (bar)
2596     win ();
2597   else
2598     lose ();
2599 @end example
2600
2601 @noindent
2602 always like this:
2603
2604 @example
2605 if (foo)
2606   @{
2607     if (bar)
2608       win ();
2609     else
2610       lose ();
2611   @}
2612 @end example
2613
2614 If you have an @code{if} statement nested inside of an @code{else}
2615 statement, either write @code{else if} on one line, like this,
2616
2617 @example
2618 if (foo)
2619   @dots{}
2620 else if (bar)
2621   @dots{}
2622 @end example
2623
2624 @noindent
2625 with its @code{then}-part indented like the preceding @code{then}-part,
2626 or write the nested @code{if} within braces like this:
2627
2628 @example
2629 if (foo)
2630   @dots{}
2631 else
2632   @{
2633     if (bar)
2634       @dots{}
2635   @}
2636 @end example
2637
2638 Don't declare both a structure tag and variables or typedefs in the
2639 same declaration.  Instead, declare the structure tag separately
2640 and then use it to declare the variables or typedefs.
2641
2642 Try to avoid assignments inside @code{if}-conditions (assignments
2643 inside @code{while}-conditions are ok).  For example, don't write
2644 this:
2645
2646 @example
2647 if ((foo = (char *) malloc (sizeof *foo)) == 0)
2648   fatal ("virtual memory exhausted");
2649 @end example
2650
2651 @noindent
2652 instead, write this:
2653
2654 @example
2655 foo = (char *) malloc (sizeof *foo);
2656 if (foo == 0)
2657   fatal ("virtual memory exhausted");
2658 @end example
2659
2660 @pindex lint
2661 Don't make the program ugly to placate @code{lint}.  Please don't insert any
2662 casts to @code{void}.  Zero without a cast is perfectly fine as a null
2663 pointer constant, except when calling a varargs function.
2664
2665 @node Names
2666 @section Naming Variables, Functions, and Files
2667
2668 @cindex names of variables, functions, and files
2669 The names of global variables and functions in a program serve as
2670 comments of a sort.  So don't choose terse names---instead, look for
2671 names that give useful information about the meaning of the variable or
2672 function.  In a GNU program, names should be English, like other
2673 comments.
2674
2675 Local variable names can be shorter, because they are used only within
2676 one context, where (presumably) comments explain their purpose.
2677
2678 Try to limit your use of abbreviations in symbol names.  It is ok to
2679 make a few abbreviations, explain what they mean, and then use them
2680 frequently, but don't use lots of obscure abbreviations.
2681
2682 Please use underscores to separate words in a name, so that the Emacs
2683 word commands can be useful within them.  Stick to lower case; reserve
2684 upper case for macros and @code{enum} constants, and for name-prefixes
2685 that follow a uniform convention.
2686
2687 For example, you should use names like @code{ignore_space_change_flag};
2688 don't use names like @code{iCantReadThis}.
2689
2690 Variables that indicate whether command-line options have been
2691 specified should be named after the meaning of the option, not after
2692 the option-letter.  A comment should state both the exact meaning of
2693 the option and its letter.  For example,
2694
2695 @example
2696 @group
2697 /* Ignore changes in horizontal whitespace (-b).  */
2698 int ignore_space_change_flag;
2699 @end group
2700 @end example
2701
2702 When you want to define names with constant integer values, use
2703 @code{enum} rather than @samp{#define}.  GDB knows about enumeration
2704 constants.
2705
2706 @cindex file-name limitations
2707 @pindex doschk
2708 You might want to make sure that none of the file names would conflict
2709 if the files were loaded onto an MS-DOS file system which shortens the
2710 names.  You can use the program @code{doschk} to test for this.
2711
2712 Some GNU programs were designed to limit themselves to file names of 14
2713 characters or less, to avoid file name conflicts if they are read into
2714 older System V systems.  Please preserve this feature in the existing
2715 GNU programs that have it, but there is no need to do this in new GNU
2716 programs.  @code{doschk} also reports file names longer than 14
2717 characters.
2718
2719 @node System Portability
2720 @section Portability between System Types
2721 @cindex portability, between system types
2722
2723 In the Unix world, ``portability'' refers to porting to different Unix
2724 versions.  For a GNU program, this kind of portability is desirable, but
2725 not paramount.
2726
2727 The primary purpose of GNU software is to run on top of the GNU kernel,
2728 compiled with the GNU C compiler, on various types of @sc{cpu}.  So the
2729 kinds of portability that are absolutely necessary are quite limited.
2730 But it is important to support Linux-based GNU systems, since they
2731 are the form of GNU that is popular.
2732
2733 Beyond that, it is good to support the other free operating systems
2734 (*BSD), and it is nice to support other Unix-like systems if you want
2735 to.  Supporting a variety of Unix-like systems is desirable, although
2736 not paramount.  It is usually not too hard, so you may as well do it.
2737 But you don't have to consider it an obligation, if it does turn out to
2738 be hard.
2739
2740 @pindex autoconf
2741 The easiest way to achieve portability to most Unix-like systems is to
2742 use Autoconf.  It's unlikely that your program needs to know more
2743 information about the host platform than Autoconf can provide, simply
2744 because most of the programs that need such knowledge have already been
2745 written.
2746
2747 Avoid using the format of semi-internal data bases (e.g., directories)
2748 when there is a higher-level alternative (@code{readdir}).
2749
2750 @cindex non-@sc{posix} systems, and portability
2751 As for systems that are not like Unix, such as MSDOS, Windows, VMS, MVS,
2752 and older Macintosh systems, supporting them is often a lot of work.
2753 When that is the case, it is better to spend your time adding features
2754 that will be useful on GNU and GNU/Linux, rather than on supporting
2755 other incompatible systems.
2756
2757 If you do support Windows, please do not abbreviate it as ``win''.  In
2758 hacker terminology, calling something a ``win'' is a form of praise.
2759 You're free to praise Microsoft Windows on your own if you want, but
2760 please don't do this in GNU packages.  Instead of abbreviating
2761 ``Windows'' to ``win'', you can write it in full or abbreviate it to
2762 ``woe'' or ``w''.  In GNU Emacs, for instance, we use @samp{w32} in
2763 file names of Windows-specific files, but the macro for Windows
2764 conditionals is called @code{WINDOWSNT}.
2765
2766 It is a good idea to define the ``feature test macro''
2767 @code{_GNU_SOURCE} when compiling your C files.  When you compile on GNU
2768 or GNU/Linux, this will enable the declarations of GNU library extension
2769 functions, and that will usually give you a compiler error message if
2770 you define the same function names in some other way in your program.
2771 (You don't have to actually @emph{use} these functions, if you prefer
2772 to make the program more portable to other systems.)
2773
2774 But whether or not you use these GNU extensions, you should avoid
2775 using their names for any other meanings.  Doing so would make it hard
2776 to move your code into other GNU programs.
2777
2778 @node CPU Portability
2779 @section Portability between @sc{cpu}s
2780
2781 @cindex data types, and portability
2782 @cindex portability, and data types
2783 Even GNU systems will differ because of differences among @sc{cpu}
2784 types---for example, difference in byte ordering and alignment
2785 requirements.  It is absolutely essential to handle these differences.
2786 However, don't make any effort to cater to the possibility that an
2787 @code{int} will be less than 32 bits.  We don't support 16-bit machines
2788 in GNU.
2789
2790 Similarly, don't make any effort to cater to the possibility that
2791 @code{long} will be smaller than predefined types like @code{size_t}.
2792 For example, the following code is ok:
2793
2794 @example
2795 printf ("size = %lu\n", (unsigned long) sizeof array);
2796 printf ("diff = %ld\n", (long) (pointer2 - pointer1));
2797 @end example
2798
2799 1989 Standard C requires this to work, and we know of only one
2800 counterexample: 64-bit programs on Microsoft Windows.  We will
2801 leave it to those who want to port GNU programs to that environment
2802 to figure out how to do it.
2803
2804 Predefined file-size types like @code{off_t} are an exception: they are
2805 longer than @code{long} on many platforms, so code like the above won't
2806 work with them.  One way to print an @code{off_t} value portably is to
2807 print its digits yourself, one by one.
2808
2809 Don't assume that the address of an @code{int} object is also the
2810 address of its least-significant byte.  This is false on big-endian
2811 machines.  Thus, don't make the following mistake:
2812
2813 @example
2814 int c;
2815 @dots{}
2816 while ((c = getchar ()) != EOF)
2817   write (file_descriptor, &c, 1);
2818 @end example
2819
2820 @noindent Instead, use @code{unsigned char} as follows.  (The @code{unsigned}
2821 is for portability to unusual systems where @code{char} is signed and
2822 where there is integer overflow checking.)
2823
2824 @example
2825 int c;
2826 while ((c = getchar ()) != EOF)
2827   @{
2828     unsigned char u = c;
2829     write (file_descriptor, &u, 1);
2830   @}
2831 @end example
2832
2833 It used to be ok to not worry about the difference between pointers
2834 and integers when passing arguments to functions.  However, on most
2835 modern 64-bit machines pointers are wider than @code{int}.
2836 Conversely, integer types like @code{long long int} and @code{off_t}
2837 are wider than pointers on most modern 32-bit machines.  Hence it's
2838 often better nowadays to use prototypes to define functions whose
2839 argument types are not trivial.
2840
2841 In particular, if functions accept varying argument counts or types
2842 they should be declared using prototypes containing @samp{...} and
2843 defined using @file{stdarg.h}.  For an example of this, please see the
2844 @uref{http://www.gnu.org/software/gnulib/, Gnulib} error module, which
2845 declares and defines the following function:
2846
2847 @example
2848 /* Print a message with `fprintf (stderr, FORMAT, ...)';
2849    if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
2850    If STATUS is nonzero, terminate the program with `exit (STATUS)'.  */
2851
2852 void error (int status, int errnum, const char *format, ...);
2853 @end example
2854
2855 A simple way to use the Gnulib error module is to obtain the two
2856 source files @file{error.c} and @file{error.h} from the Gnulib library
2857 source code repository at
2858 @uref{http://savannah.gnu.org/cgi-bin/viewcvs/gnulib/gnulib/lib/}.
2859 Here's a sample use:
2860
2861 @example
2862 #include "error.h"
2863 #include <errno.h>
2864 #include <stdio.h>
2865
2866 char *program_name = "myprogram";
2867
2868 FILE *
2869 xfopen (char const *name)
2870 @{
2871   FILE *fp = fopen (name, "r");
2872   if (! fp)
2873     error (1, errno, "cannot read %s", name);
2874   return fp;
2875 @}
2876 @end example
2877
2878 @cindex casting pointers to integers
2879 Avoid casting pointers to integers if you can.  Such casts greatly
2880 reduce portability, and in most programs they are easy to avoid.  In the
2881 cases where casting pointers to integers is essential---such as, a Lisp
2882 interpreter which stores type information as well as an address in one
2883 word---you'll have to make explicit provisions to handle different word
2884 sizes.  You will also need to make provision for systems in which the
2885 normal range of addresses you can get from @code{malloc} starts far away
2886 from zero.
2887
2888 @node System Functions
2889 @section Calling System Functions
2890 @cindex library functions, and portability
2891 @cindex portability, and library functions
2892
2893 C implementations differ substantially.  Standard C reduces but does
2894 not eliminate the incompatibilities; meanwhile, many GNU packages still
2895 support pre-standard compilers because this is not hard to do.  This
2896 chapter gives recommendations for how to use the more-or-less standard C
2897 library functions to avoid unnecessary loss of portability.
2898
2899 @itemize @bullet
2900 @item
2901 Don't use the return value of @code{sprintf}.  It returns the number of
2902 characters written on some systems, but not on all systems.
2903
2904 @item
2905 Be aware that @code{vfprintf} is not always available.
2906
2907 @item
2908 @code{main} should be declared to return type @code{int}.  It should
2909 terminate either by calling @code{exit} or by returning the integer
2910 status code; make sure it cannot ever return an undefined value.
2911
2912 @cindex declaration for system functions
2913 @item
2914 Don't declare system functions explicitly.
2915
2916 Almost any declaration for a system function is wrong on some system.
2917 To minimize conflicts, leave it to the system header files to declare
2918 system functions.  If the headers don't declare a function, let it
2919 remain undeclared.
2920
2921 While it may seem unclean to use a function without declaring it, in
2922 practice this works fine for most system library functions on the
2923 systems where this really happens; thus, the disadvantage is only
2924 theoretical.  By contrast, actual declarations have frequently caused
2925 actual conflicts.
2926
2927 @item
2928 If you must declare a system function, don't specify the argument types.
2929 Use an old-style declaration, not a Standard C prototype.  The more you
2930 specify about the function, the more likely a conflict.
2931
2932 @item
2933 In particular, don't unconditionally declare @code{malloc} or
2934 @code{realloc}.
2935
2936 Most GNU programs use those functions just once, in functions
2937 conventionally named @code{xmalloc} and @code{xrealloc}.  These
2938 functions call @code{malloc} and @code{realloc}, respectively, and
2939 check the results.
2940
2941 Because @code{xmalloc} and @code{xrealloc} are defined in your program,
2942 you can declare them in other files without any risk of type conflict.
2943
2944 On most systems, @code{int} is the same length as a pointer; thus, the
2945 calls to @code{malloc} and @code{realloc} work fine.  For the few
2946 exceptional systems (mostly 64-bit machines), you can use
2947 @strong{conditionalized} declarations of @code{malloc} and
2948 @code{realloc}---or put these declarations in configuration files
2949 specific to those systems.
2950
2951 @cindex string library functions
2952 @item
2953 The string functions require special treatment.  Some Unix systems have
2954 a header file @file{string.h}; others have @file{strings.h}.  Neither
2955 file name is portable.  There are two things you can do: use Autoconf to
2956 figure out which file to include, or don't include either file.
2957
2958 @item
2959 If you don't include either strings file, you can't get declarations for
2960 the string functions from the header file in the usual way.
2961
2962 That causes less of a problem than you might think.  The newer standard
2963 string functions should be avoided anyway because many systems still
2964 don't support them.  The string functions you can use are these:
2965
2966 @example
2967 strcpy   strncpy   strcat   strncat
2968 strlen   strcmp    strncmp
2969 strchr   strrchr
2970 @end example
2971
2972 The copy and concatenate functions work fine without a declaration as
2973 long as you don't use their values.  Using their values without a
2974 declaration fails on systems where the width of a pointer differs from
2975 the width of @code{int}, and perhaps in other cases.  It is trivial to
2976 avoid using their values, so do that.
2977
2978 The compare functions and @code{strlen} work fine without a declaration
2979 on most systems, possibly all the ones that GNU software runs on.
2980 You may find it necessary to declare them @strong{conditionally} on a
2981 few systems.
2982
2983 The search functions must be declared to return @code{char *}.  Luckily,
2984 there is no variation in the data type they return.  But there is
2985 variation in their names.  Some systems give these functions the names
2986 @code{index} and @code{rindex}; other systems use the names
2987 @code{strchr} and @code{strrchr}.  Some systems support both pairs of
2988 names, but neither pair works on all systems.
2989
2990 You should pick a single pair of names and use it throughout your
2991 program.  (Nowadays, it is better to choose @code{strchr} and
2992 @code{strrchr} for new programs, since those are the standard
2993 names.)  Declare both of those names as functions returning @code{char
2994 *}.  On systems which don't support those names, define them as macros
2995 in terms of the other pair.  For example, here is what to put at the
2996 beginning of your file (or in a header) if you want to use the names
2997 @code{strchr} and @code{strrchr} throughout:
2998
2999 @example
3000 #ifndef HAVE_STRCHR
3001 #define strchr index
3002 #endif
3003 #ifndef HAVE_STRRCHR
3004 #define strrchr rindex
3005 #endif
3006
3007 char *strchr ();
3008 char *strrchr ();
3009 @end example
3010 @end itemize
3011
3012 Here we assume that @code{HAVE_STRCHR} and @code{HAVE_STRRCHR} are
3013 macros defined in systems where the corresponding functions exist.
3014 One way to get them properly defined is to use Autoconf.
3015
3016 @node Internationalization
3017 @section Internationalization
3018 @cindex internationalization
3019
3020 @pindex gettext
3021 GNU has a library called GNU gettext that makes it easy to translate the
3022 messages in a program into various languages.  You should use this
3023 library in every program.  Use English for the messages as they appear
3024 in the program, and let gettext provide the way to translate them into
3025 other languages.
3026
3027 Using GNU gettext involves putting a call to the @code{gettext} macro
3028 around each string that might need translation---like this:
3029
3030 @example
3031 printf (gettext ("Processing file `%s'..."));
3032 @end example
3033
3034 @noindent
3035 This permits GNU gettext to replace the string @code{"Processing file
3036 `%s'..."} with a translated version.
3037
3038 Once a program uses gettext, please make a point of writing calls to
3039 @code{gettext} when you add new strings that call for translation.
3040
3041 Using GNU gettext in a package involves specifying a @dfn{text domain
3042 name} for the package.  The text domain name is used to separate the
3043 translations for this package from the translations for other packages.
3044 Normally, the text domain name should be the same as the name of the
3045 package---for example, @samp{coreutils} for the GNU core utilities.
3046
3047 @cindex message text, and internationalization
3048 To enable gettext to work well, avoid writing code that makes
3049 assumptions about the structure of words or sentences.  When you want
3050 the precise text of a sentence to vary depending on the data, use two or
3051 more alternative string constants each containing a complete sentences,
3052 rather than inserting conditionalized words or phrases into a single
3053 sentence framework.
3054
3055 Here is an example of what not to do:
3056
3057 @smallexample
3058 printf ("%s is full", capacity > 5000000 ? "disk" : "floppy disk");
3059 @end smallexample
3060
3061 If you apply gettext to all strings, like this,
3062
3063 @smallexample
3064 printf (gettext ("%s is full"),
3065         capacity > 5000000 ? gettext ("disk") : gettext ("floppy disk"));
3066 @end smallexample
3067
3068 @noindent
3069 the translator will hardly know that "disk" and "floppy disk" are meant to
3070 be substituted in the other string.  Worse, in some languages (like French)
3071 the construction will not work: the translation of the word "full" depends
3072 on the gender of the first part of the sentence; it happens to be not the
3073 same for "disk" as for "floppy disk".
3074
3075 Complete sentences can be translated without problems:
3076
3077 @example
3078 printf (capacity > 5000000 ? gettext ("disk is full")
3079         : gettext ("floppy disk is full"));
3080 @end example
3081
3082 A similar problem appears at the level of sentence structure with this
3083 code:
3084
3085 @example
3086 printf ("#  Implicit rule search has%s been done.\n",
3087         f->tried_implicit ? "" : " not");
3088 @end example
3089
3090 @noindent
3091 Adding @code{gettext} calls to this code cannot give correct results for
3092 all languages, because negation in some languages requires adding words
3093 at more than one place in the sentence.  By contrast, adding
3094 @code{gettext} calls does the job straightforwardly if the code starts
3095 out like this:
3096
3097 @example
3098 printf (f->tried_implicit
3099         ? "#  Implicit rule search has been done.\n",
3100         : "#  Implicit rule search has not been done.\n");
3101 @end example
3102
3103 Another example is this one:
3104
3105 @example
3106 printf ("%d file%s processed", nfiles,
3107         nfiles != 1 ? "s" : "");
3108 @end example
3109
3110 @noindent
3111 The problem with this example is that it assumes that plurals are made
3112 by adding `s'.  If you apply gettext to the format string, like this,
3113
3114 @example
3115 printf (gettext ("%d file%s processed"), nfiles,
3116         nfiles != 1 ? "s" : "");
3117 @end example
3118
3119 @noindent
3120 the message can use different words, but it will still be forced to use
3121 `s' for the plural.  Here is a better way, with gettext being applied to
3122 the two strings independently:
3123
3124 @example
3125 printf ((nfiles != 1 ? gettext ("%d files processed")
3126          : gettext ("%d file processed")),
3127         nfiles);
3128 @end example
3129
3130 @noindent
3131 But this still doesn't work for languages like Polish, which has three
3132 plural forms: one for nfiles == 1, one for nfiles == 2, 3, 4, 22, 23, 24, ...
3133 and one for the rest.  The GNU @code{ngettext} function solves this problem:
3134
3135 @example
3136 printf (ngettext ("%d files processed", "%d file processed", nfiles),
3137         nfiles);
3138 @end example
3139
3140
3141 @node Character Set
3142 @section Character Set
3143 @cindex character set
3144 @cindex encodings
3145 @cindex ASCII characters
3146 @cindex non-ASCII characters
3147
3148 Sticking to the ASCII character set (plain text, 7-bit characters) is
3149 preferred in GNU source code comments, text documents, and other
3150 contexts, unless there is good reason to do something else because of
3151 the application domain.  For example, if source code deals with the
3152 French Revolutionary calendar, it is OK if its literal strings contain
3153 accented characters in month names like ``Flor@'eal''.  Also, it is OK
3154 to use non-ASCII characters to represent proper names of contributors in
3155 change logs (@pxref{Change Logs}).
3156
3157 If you need to use non-ASCII characters, you should normally stick with
3158 one encoding, as one cannot in general mix encodings reliably.
3159
3160
3161 @node Quote Characters
3162 @section Quote Characters
3163 @cindex quote characters
3164 @cindex locale-specific quote characters
3165 @cindex left quote
3166 @cindex grave accent
3167
3168 In the C locale, GNU programs should stick to plain ASCII for quotation
3169 characters in messages to users: preferably 0x60 (@samp{`}) for left
3170 quotes and 0x27 (@samp{'}) for right quotes.  It is ok, but not
3171 required, to use locale-specific quotes in other locales.
3172
3173 The @uref{http://www.gnu.org/software/gnulib/, Gnulib} @code{quote} and
3174 @code{quotearg} modules provide a reasonably straightforward way to
3175 support locale-specific quote characters, as well as taking care of
3176 other issues, such as quoting a filename that itself contains a quote
3177 character.  See the Gnulib documentation for usage details.
3178
3179 In any case, the documentation for your program should clearly specify
3180 how it does quoting, if different than the preferred method of @samp{`}
3181 and @samp{'}.  This is especially important if the output of your
3182 program is ever likely to be parsed by another program.
3183
3184 Quotation characters are a difficult area in the computing world at
3185 this time: there are no true left or right quote characters in Latin1;
3186 the @samp{`} character we use was standardized there as a grave
3187 accent.  Moreover, Latin1 is still not universally usable.
3188
3189 Unicode contains the unambiguous quote characters required, and its
3190 common encoding UTF-8 is upward compatible with Latin1.  However,
3191 Unicode and UTF-8 are not universally well-supported, either.
3192
3193 This may change over the next few years, and then we will revisit
3194 this.
3195
3196
3197 @node Mmap
3198 @section Mmap
3199 @findex mmap
3200
3201 Don't assume that @code{mmap} either works on all files or fails
3202 for all files.  It may work on some files and fail on others.
3203
3204 The proper way to use @code{mmap} is to try it on the specific file for
3205 which you want to use it---and if @code{mmap} doesn't work, fall back on
3206 doing the job in another way using @code{read} and @code{write}.
3207
3208 The reason this precaution is needed is that the GNU kernel (the HURD)
3209 provides a user-extensible file system, in which there can be many
3210 different kinds of ``ordinary files.''  Many of them support
3211 @code{mmap}, but some do not.  It is important to make programs handle
3212 all these kinds of files.
3213
3214 @node Documentation
3215 @chapter Documenting Programs
3216 @cindex documentation
3217
3218 A GNU program should ideally come with full free documentation, adequate
3219 for both reference and tutorial purposes.  If the package can be
3220 programmed or extended, the documentation should cover programming or
3221 extending it, as well as just using it.
3222
3223 @menu
3224 * GNU Manuals::                 Writing proper manuals.
3225 * Doc Strings and Manuals::     Compiling doc strings doesn't make a manual.
3226 * Manual Structure Details::    Specific structure conventions.
3227 * License for Manuals::         Writing the distribution terms for a manual.
3228 * Manual Credits::              Giving credit to documentation contributors.
3229 * Printed Manuals::             Mentioning the printed manual.
3230 * NEWS File::                   NEWS files supplement manuals.
3231 * Change Logs::                 Recording changes.
3232 * Man Pages::                   Man pages are secondary.
3233 * Reading other Manuals::       How far you can go in learning
3234                                 from other manuals.
3235 @end menu
3236
3237 @node GNU Manuals
3238 @section GNU Manuals
3239
3240 The preferred document format for the GNU system is the Texinfo
3241 formatting language.  Every GNU package should (ideally) have
3242 documentation in Texinfo both for reference and for learners.  Texinfo
3243 makes it possible to produce a good quality formatted book, using
3244 @TeX{}, and to generate an Info file.  It is also possible to generate
3245 HTML output from Texinfo source.  See the Texinfo manual, either the
3246 hardcopy, or the on-line version available through @code{info} or the
3247 Emacs Info subsystem (@kbd{C-h i}).
3248
3249 Nowadays some other formats such as Docbook and Sgmltexi can be
3250 converted automatically into Texinfo.  It is ok to produce the Texinfo
3251 documentation by conversion this way, as long as it gives good results.
3252
3253 Make sure your manual is clear to a reader who knows nothing about the
3254 topic and reads it straight through.  This means covering basic topics
3255 at the beginning, and advanced topics only later.  This also means
3256 defining every specialized term when it is first used.
3257
3258 Programmers tend to carry over the structure of the program as the
3259 structure for its documentation.  But this structure is not
3260 necessarily good for explaining how to use the program; it may be
3261 irrelevant and confusing for a user.
3262
3263 Instead, the right way to structure documentation is according to the
3264 concepts and questions that a user will have in mind when reading it.
3265 This principle applies at every level, from the lowest (ordering
3266 sentences in a paragraph) to the highest (ordering of chapter topics
3267 within the manual).  Sometimes this structure of ideas matches the
3268 structure of the implementation of the software being documented---but
3269 often they are different.  An important part of learning to write good
3270 documentation is to learn to notice when you have unthinkingly
3271 structured the documentation like the implementation, stop yourself,
3272 and look for better alternatives.
3273
3274 For example, each program in the GNU system probably ought to be
3275 documented in one manual; but this does not mean each program should
3276 have its own manual.  That would be following the structure of the
3277 implementation, rather than the structure that helps the user
3278 understand.
3279
3280 Instead, each manual should cover a coherent @emph{topic}.  For example,
3281 instead of a manual for @code{diff} and a manual for @code{diff3}, we
3282 have one manual for ``comparison of files'' which covers both of those
3283 programs, as well as @code{cmp}.  By documenting these programs
3284 together, we can make the whole subject clearer.
3285
3286 The manual which discusses a program should certainly document all of
3287 the program's command-line options and all of its commands.  It should
3288 give examples of their use.  But don't organize the manual as a list
3289 of features.  Instead, organize it logically, by subtopics.  Address
3290 the questions that a user will ask when thinking about the job that
3291 the program does.  Don't just tell the reader what each feature can
3292 do---say what jobs it is good for, and show how to use it for those
3293 jobs.  Explain what is recommended usage, and what kinds of usage
3294 users should avoid.
3295
3296 In general, a GNU manual should serve both as tutorial and reference.
3297 It should be set up for convenient access to each topic through Info,
3298 and for reading straight through (appendixes aside).  A GNU manual
3299 should give a good introduction to a beginner reading through from the
3300 start, and should also provide all the details that hackers want.
3301 The Bison manual is a good example of this---please take a look at it
3302 to see what we mean.
3303
3304 That is not as hard as it first sounds.  Arrange each chapter as a
3305 logical breakdown of its topic, but order the sections, and write their
3306 text, so that reading the chapter straight through makes sense.  Do
3307 likewise when structuring the book into chapters, and when structuring a
3308 section into paragraphs.  The watchword is, @emph{at each point, address
3309 the most fundamental and important issue raised by the preceding text.}
3310
3311 If necessary, add extra chapters at the beginning of the manual which
3312 are purely tutorial and cover the basics of the subject.  These provide
3313 the framework for a beginner to understand the rest of the manual.  The
3314 Bison manual provides a good example of how to do this.
3315
3316 To serve as a reference, a manual should have an Index that list all the
3317 functions, variables, options, and important concepts that are part of
3318 the program.  One combined Index should do for a short manual, but
3319 sometimes for a complex package it is better to use multiple indices.
3320 The Texinfo manual includes advice on preparing good index entries, see
3321 @ref{Index Entries, , Making Index Entries, texinfo, GNU Texinfo}, and
3322 see @ref{Indexing Commands, , Defining the Entries of an
3323 Index, texinfo, GNU Texinfo}.
3324
3325 Don't use Unix man pages as a model for how to write GNU documentation;
3326 most of them are terse, badly structured, and give inadequate
3327 explanation of the underlying concepts.  (There are, of course, some
3328 exceptions.)  Also, Unix man pages use a particular format which is
3329 different from what we use in GNU manuals.
3330
3331 Please include an email address in the manual for where to report
3332 bugs @emph{in the text of the manual}.
3333
3334 Please do not use the term ``pathname'' that is used in Unix
3335 documentation; use ``file name'' (two words) instead.  We use the term
3336 ``path'' only for search paths, which are lists of directory names.
3337
3338 Please do not use the term ``illegal'' to refer to erroneous input to
3339 a computer program.  Please use ``invalid'' for this, and reserve the
3340 term ``illegal'' for activities prohibited by law.
3341
3342 Please do not write @samp{()} after a function name just to indicate
3343 it is a function.  @code{foo ()} is not a function, it is a function
3344 call with no arguments.
3345
3346 @node Doc Strings and Manuals
3347 @section Doc Strings and Manuals
3348
3349 Some programming systems, such as Emacs, provide a documentation string
3350 for each function, command or variable.  You may be tempted to write a
3351 reference manual by compiling the documentation strings and writing a
3352 little additional text to go around them---but you must not do it.  That
3353 approach is a fundamental mistake.  The text of well-written
3354 documentation strings will be entirely wrong for a manual.
3355
3356 A documentation string needs to stand alone---when it appears on the
3357 screen, there will be no other text to introduce or explain it.
3358 Meanwhile, it can be rather informal in style.
3359
3360 The text describing a function or variable in a manual must not stand
3361 alone; it appears in the context of a section or subsection.  Other text
3362 at the beginning of the section should explain some of the concepts, and
3363 should often make some general points that apply to several functions or
3364 variables.  The previous descriptions of functions and variables in the
3365 section will also have given information about the topic.  A description
3366 written to stand alone would repeat some of that information; this
3367 redundancy looks bad.  Meanwhile, the informality that is acceptable in
3368 a documentation string is totally unacceptable in a manual.
3369
3370 The only good way to use documentation strings in writing a good manual
3371 is to use them as a source of information for writing good text.
3372
3373 @node Manual Structure Details
3374 @section Manual Structure Details
3375 @cindex manual structure
3376
3377 The title page of the manual should state the version of the programs or
3378 packages documented in the manual.  The Top node of the manual should
3379 also contain this information.  If the manual is changing more
3380 frequently than or independent of the program, also state a version
3381 number for the manual in both of these places.
3382
3383 Each program documented in the manual should have a node named
3384 @samp{@var{program} Invocation} or @samp{Invoking @var{program}}.  This
3385 node (together with its subnodes, if any) should describe the program's
3386 command line arguments and how to run it (the sort of information people
3387 would look for in a man page).  Start with an @samp{@@example}
3388 containing a template for all the options and arguments that the program
3389 uses.
3390
3391 Alternatively, put a menu item in some menu whose item name fits one of
3392 the above patterns.  This identifies the node which that item points to
3393 as the node for this purpose, regardless of the node's actual name.
3394
3395 The @samp{--usage} feature of the Info reader looks for such a node
3396 or menu item in order to find the relevant text, so it is essential
3397 for every Texinfo file to have one.
3398
3399 If one manual describes several programs, it should have such a node for
3400 each program described in the manual.
3401
3402 @node License for Manuals
3403 @section License for Manuals
3404 @cindex license for manuals
3405
3406 Please use the GNU Free Documentation License for all GNU manuals that
3407 are more than a few pages long.  Likewise for a collection of short
3408 documents---you only need one copy of the GNU FDL for the whole
3409 collection.  For a single short document, you can use a very permissive
3410 non-copyleft license, to avoid taking up space with a long license.
3411
3412 See @uref{http://www.gnu.org/copyleft/fdl-howto.html} for more explanation
3413 of how to employ the GFDL.
3414
3415 Note that it is not obligatory to include a copy of the GNU GPL or GNU
3416 LGPL in a manual whose license is neither the GPL nor the LGPL.  It can
3417 be a good idea to include the program's license in a large manual; in a
3418 short manual, whose size would be increased considerably by including
3419 the program's license, it is probably better not to include it.
3420
3421 @node Manual Credits
3422 @section Manual Credits
3423 @cindex credits for manuals
3424
3425 Please credit the principal human writers of the manual as the authors,
3426 on the title page of the manual.  If a company sponsored the work, thank
3427 the company in a suitable place in the manual, but do not cite the
3428 company as an author.
3429
3430 @node Printed Manuals
3431 @section Printed Manuals
3432
3433 The FSF publishes some GNU manuals in printed form.  To encourage sales
3434 of these manuals, the on-line versions of the manual should mention at
3435 the very start that the printed manual is available and should point at
3436 information for getting it---for instance, with a link to the page
3437 @url{http://www.gnu.org/order/order.html}.  This should not be included
3438 in the printed manual, though, because there it is redundant.
3439
3440 It is also useful to explain in the on-line forms of the manual how the
3441 user can print out the manual from the sources.
3442
3443 @node NEWS File
3444 @section The NEWS File
3445 @cindex @file{NEWS} file
3446
3447 In addition to its manual, the package should have a file named
3448 @file{NEWS} which contains a list of user-visible changes worth
3449 mentioning.  In each new release, add items to the front of the file and
3450 identify the version they pertain to.  Don't discard old items; leave
3451 them in the file after the newer items.  This way, a user upgrading from
3452 any previous version can see what is new.
3453
3454 If the @file{NEWS} file gets very long, move some of the older items
3455 into a file named @file{ONEWS} and put a note at the end referring the
3456 user to that file.
3457
3458 @node Change Logs
3459 @section Change Logs
3460 @cindex change logs
3461
3462 Keep a change log to describe all the changes made to program source
3463 files.  The purpose of this is so that people investigating bugs in the
3464 future will know about the changes that might have introduced the bug.
3465 Often a new bug can be found by looking at what was recently changed.
3466 More importantly, change logs can help you eliminate conceptual
3467 inconsistencies between different parts of a program, by giving you a
3468 history of how the conflicting concepts arose and who they came from.
3469
3470 @menu
3471 * Change Log Concepts::
3472 * Style of Change Logs::
3473 * Simple Changes::
3474 * Conditional Changes::
3475 * Indicating the Part Changed::
3476 @end menu
3477
3478 @node Change Log Concepts
3479 @subsection Change Log Concepts
3480
3481 You can think of the change log as a conceptual ``undo list'' which
3482 explains how earlier versions were different from the current version.
3483 People can see the current version; they don't need the change log
3484 to tell them what is in it.  What they want from a change log is a
3485 clear explanation of how the earlier version differed.
3486
3487 The change log file is normally called @file{ChangeLog} and covers an
3488 entire directory.  Each directory can have its own change log, or a
3489 directory can use the change log of its parent directory--it's up to
3490 you.
3491
3492 Another alternative is to record change log information with a version
3493 control system such as RCS or CVS.  This can be converted automatically
3494 to a @file{ChangeLog} file using @code{rcs2log}; in Emacs, the command
3495 @kbd{C-x v a} (@code{vc-update-change-log}) does the job.
3496
3497 There's no need to describe the full purpose of the changes or how they
3498 work together.  If you think that a change calls for explanation, you're
3499 probably right.  Please do explain it---but please put the explanation
3500 in comments in the code, where people will see it whenever they see the
3501 code.  For example, ``New function'' is enough for the change log when
3502 you add a function, because there should be a comment before the
3503 function definition to explain what it does.
3504
3505 In the past, we recommended not mentioning changes in non-software
3506 files (manuals, help files, etc.) in change logs.  However, we've been
3507 advised that it is a good idea to include them, for the sake of
3508 copyright records.
3509
3510 However, sometimes it is useful to write one line to describe the
3511 overall purpose of a batch of changes.
3512
3513 The easiest way to add an entry to @file{ChangeLog} is with the Emacs
3514 command @kbd{M-x add-change-log-entry}.  An entry should have an
3515 asterisk, the name of the changed file, and then in parentheses the name
3516 of the changed functions, variables or whatever, followed by a colon.
3517 Then describe the changes you made to that function or variable.
3518
3519 @node Style of Change Logs
3520 @subsection Style of Change Logs
3521 @cindex change logs, style
3522
3523 Here are some simple examples of change log entries, starting with the
3524 header line that says who made the change and when it was installed,
3525 followed by descriptions of specific changes.  (These examples are
3526 drawn from Emacs and GCC.)
3527
3528 @example
3529 1998-08-17  Richard Stallman  <rms@@gnu.org>
3530
3531 * register.el (insert-register): Return nil.
3532 (jump-to-register): Likewise.
3533
3534 * sort.el (sort-subr): Return nil.
3535
3536 * tex-mode.el (tex-bibtex-file, tex-file, tex-region):
3537 Restart the tex shell if process is gone or stopped.
3538 (tex-shell-running): New function.
3539
3540 * expr.c (store_one_arg): Round size up for move_block_to_reg.
3541 (expand_call): Round up when emitting USE insns.
3542 * stmt.c (assign_parms): Round size up for move_block_from_reg.
3543 @end example
3544
3545 It's important to name the changed function or variable in full.  Don't
3546 abbreviate function or variable names, and don't combine them.
3547 Subsequent maintainers will often search for a function name to find all
3548 the change log entries that pertain to it; if you abbreviate the name,
3549 they won't find it when they search.
3550
3551 For example, some people are tempted to abbreviate groups of function
3552 names by writing @samp{* register.el (@{insert,jump-to@}-register)};
3553 this is not a good idea, since searching for @code{jump-to-register} or
3554 @code{insert-register} would not find that entry.
3555
3556 Separate unrelated change log entries with blank lines.  When two
3557 entries represent parts of the same change, so that they work together,
3558 then don't put blank lines between them.  Then you can omit the file
3559 name and the asterisk when successive entries are in the same file.
3560
3561 Break long lists of function names by closing continued lines with
3562 @samp{)}, rather than @samp{,}, and opening the continuation with
3563 @samp{(} as in this example:
3564
3565 @example
3566 * keyboard.c (menu_bar_items, tool_bar_items)
3567 (Fexecute_extended_command): Deal with `keymap' property.
3568 @end example
3569
3570 When you install someone else's changes, put the contributor's name in
3571 the change log entry rather than in the text of the entry.  In other
3572 words, write this:
3573
3574 @example
3575 2002-07-14  John Doe  <jdoe@@gnu.org>
3576
3577         * sewing.c: Make it sew.
3578 @end example
3579
3580 @noindent
3581 rather than this:
3582
3583 @example
3584 2002-07-14  Usual Maintainer  <usual@@gnu.org>
3585
3586         * sewing.c: Make it sew.  Patch by jdoe@@gnu.org.
3587 @end example
3588
3589 As for the date, that should be the date you applied the change.
3590
3591 @node Simple Changes
3592 @subsection Simple Changes
3593
3594 Certain simple kinds of changes don't need much detail in the change
3595 log.
3596
3597 When you change the calling sequence of a function in a simple fashion,
3598 and you change all the callers of the function to use the new calling
3599 sequence, there is no need to make individual entries for all the
3600 callers that you changed.  Just write in the entry for the function
3601 being called, ``All callers changed''---like this:
3602
3603 @example
3604 * keyboard.c (Fcommand_execute): New arg SPECIAL.
3605 All callers changed.
3606 @end example
3607
3608 When you change just comments or doc strings, it is enough to write an
3609 entry for the file, without mentioning the functions.  Just ``Doc
3610 fixes'' is enough for the change log.
3611
3612 There's no technical need to make change log entries for documentation
3613 files.  This is because documentation is not susceptible to bugs that
3614 are hard to fix.  Documentation does not consist of parts that must
3615 interact in a precisely engineered fashion.  To correct an error, you
3616 need not know the history of the erroneous passage; it is enough to
3617 compare what the documentation says with the way the program actually
3618 works.
3619
3620 However, you should keep change logs for documentation files when the
3621 project gets copyright assignments from its contributors, so as to
3622 make the records of authorship more accurate.
3623
3624 @node Conditional Changes
3625 @subsection Conditional Changes
3626 @cindex conditional changes, and change logs
3627 @cindex change logs, conditional changes
3628
3629 C programs often contain compile-time @code{#if} conditionals.  Many
3630 changes are conditional; sometimes you add a new definition which is
3631 entirely contained in a conditional.  It is very useful to indicate in
3632 the change log the conditions for which the change applies.
3633
3634 Our convention for indicating conditional changes is to use square
3635 brackets around the name of the condition.
3636
3637 Here is a simple example, describing a change which is conditional but
3638 does not have a function or entity name associated with it:
3639
3640 @example
3641 * xterm.c [SOLARIS2]: Include string.h.
3642 @end example
3643
3644 Here is an entry describing a new definition which is entirely
3645 conditional.  This new definition for the macro @code{FRAME_WINDOW_P} is
3646 used only when @code{HAVE_X_WINDOWS} is defined:
3647
3648 @example
3649 * frame.h [HAVE_X_WINDOWS] (FRAME_WINDOW_P): Macro defined.
3650 @end example
3651
3652 Here is an entry for a change within the function @code{init_display},
3653 whose definition as a whole is unconditional, but the changes themselves
3654 are contained in a @samp{#ifdef HAVE_LIBNCURSES} conditional:
3655
3656 @example
3657 * dispnew.c (init_display) [HAVE_LIBNCURSES]: If X, call tgetent.
3658 @end example
3659
3660 Here is an entry for a change that takes affect only when
3661 a certain macro is @emph{not} defined:
3662
3663 @example
3664 (gethostname) [!HAVE_SOCKETS]: Replace with winsock version.
3665 @end example
3666
3667 @node Indicating the Part Changed
3668 @subsection Indicating the Part Changed
3669
3670 Indicate the part of a function which changed by using angle brackets
3671 enclosing an indication of what the changed part does.  Here is an entry
3672 for a change in the part of the function @code{sh-while-getopts} that
3673 deals with @code{sh} commands:
3674
3675 @example
3676 * progmodes/sh-script.el (sh-while-getopts) <sh>: Handle case that
3677 user-specified option string is empty.
3678 @end example
3679
3680
3681 @node Man Pages
3682 @section Man Pages
3683 @cindex man pages
3684
3685 In the GNU project, man pages are secondary.  It is not necessary or
3686 expected for every GNU program to have a man page, but some of them do.
3687 It's your choice whether to include a man page in your program.
3688
3689 When you make this decision, consider that supporting a man page
3690 requires continual effort each time the program is changed.  The time
3691 you spend on the man page is time taken away from more useful work.
3692
3693 For a simple program which changes little, updating the man page may be
3694 a small job.  Then there is little reason not to include a man page, if
3695 you have one.
3696
3697 For a large program that changes a great deal, updating a man page may
3698 be a substantial burden.  If a user offers to donate a man page, you may
3699 find this gift costly to accept.  It may be better to refuse the man
3700 page unless the same person agrees to take full responsibility for
3701 maintaining it---so that you can wash your hands of it entirely.  If
3702 this volunteer later ceases to do the job, then don't feel obliged to
3703 pick it up yourself; it may be better to withdraw the man page from the
3704 distribution until someone else agrees to update it.
3705
3706 When a program changes only a little, you may feel that the
3707 discrepancies are small enough that the man page remains useful without
3708 updating.  If so, put a prominent note near the beginning of the man
3709 page explaining that you don't maintain it and that the Texinfo manual
3710 is more authoritative.  The note should say how to access the Texinfo
3711 documentation.
3712
3713 Be sure that man pages include a copyright statement and free
3714 license.  The simple all-permissive license is appropriate for simple
3715 man pages:
3716
3717 @example
3718 Copying and distribution of this file, with or without modification,
3719 are permitted in any medium without royalty provided the copyright
3720 notice and this notice are preserved.
3721 @end example
3722
3723 For long man pages, with enough explanation and documentation that
3724 they can be considered true manuals, use the GFDL (@pxref{License for
3725 Manuals}).
3726
3727 Finally, the GNU help2man program
3728 (@uref{http://www.gnu.org/software/help2man/}) is one way to automate
3729 generation of a man page, in this case from @option{--help} output.
3730 This is sufficient in many cases.
3731
3732 @node Reading other Manuals
3733 @section Reading other Manuals
3734
3735 There may be non-free books or documentation files that describe the
3736 program you are documenting.
3737
3738 It is ok to use these documents for reference, just as the author of a
3739 new algebra textbook can read other books on algebra.  A large portion
3740 of any non-fiction book consists of facts, in this case facts about how
3741 a certain program works, and these facts are necessarily the same for
3742 everyone who writes about the subject.  But be careful not to copy your
3743 outline structure, wording, tables or examples from preexisting non-free
3744 documentation.  Copying from free documentation may be ok; please check
3745 with the FSF about the individual case.
3746
3747 @node Managing Releases
3748 @chapter The Release Process
3749 @cindex releasing
3750
3751 Making a release is more than just bundling up your source files in a
3752 tar file and putting it up for FTP.  You should set up your software so
3753 that it can be configured to run on a variety of systems.  Your Makefile
3754 should conform to the GNU standards described below, and your directory
3755 layout should also conform to the standards discussed below.  Doing so
3756 makes it easy to include your package into the larger framework of
3757 all GNU software.
3758
3759 @menu
3760 * Configuration::               How configuration of GNU packages should work.
3761 * Makefile Conventions::        Makefile conventions.
3762 * Releases::                    Making releases
3763 @end menu
3764
3765 @node Configuration
3766 @section How Configuration Should Work
3767 @cindex program configuration
3768
3769 @pindex configure
3770 Each GNU distribution should come with a shell script named
3771 @code{configure}.  This script is given arguments which describe the
3772 kind of machine and system you want to compile the program for.
3773
3774 The @code{configure} script must record the configuration options so
3775 that they affect compilation.
3776
3777 One way to do this is to make a link from a standard name such as
3778 @file{config.h} to the proper configuration file for the chosen system.
3779 If you use this technique, the distribution should @emph{not} contain a
3780 file named @file{config.h}.  This is so that people won't be able to
3781 build the program without configuring it first.
3782
3783 Another thing that @code{configure} can do is to edit the Makefile.  If
3784 you do this, the distribution should @emph{not} contain a file named
3785 @file{Makefile}.  Instead, it should include a file @file{Makefile.in} which
3786 contains the input used for editing.  Once again, this is so that people
3787 won't be able to build the program without configuring it first.
3788
3789 If @code{configure} does write the @file{Makefile}, then @file{Makefile}
3790 should have a target named @file{Makefile} which causes @code{configure}
3791 to be rerun, setting up the same configuration that was set up last
3792 time.  The files that @code{configure} reads should be listed as
3793 dependencies of @file{Makefile}.
3794
3795 All the files which are output from the @code{configure} script should
3796 have comments at the beginning explaining that they were generated
3797 automatically using @code{configure}.  This is so that users won't think
3798 of trying to edit them by hand.
3799
3800 The @code{configure} script should write a file named @file{config.status}
3801 which describes which configuration options were specified when the
3802 program was last configured.  This file should be a shell script which,
3803 if run, will recreate the same configuration.
3804
3805 The @code{configure} script should accept an option of the form
3806 @samp{--srcdir=@var{dirname}} to specify the directory where sources are found
3807 (if it is not the current directory).  This makes it possible to build
3808 the program in a separate directory, so that the actual source directory
3809 is not modified.
3810
3811 If the user does not specify @samp{--srcdir}, then @code{configure} should
3812 check both @file{.} and @file{..} to see if it can find the sources.  If
3813 it finds the sources in one of these places, it should use them from
3814 there.  Otherwise, it should report that it cannot find the sources, and
3815 should exit with nonzero status.
3816
3817 Usually the easy way to support @samp{--srcdir} is by editing a
3818 definition of @code{VPATH} into the Makefile.  Some rules may need to
3819 refer explicitly to the specified source directory.  To make this
3820 possible, @code{configure} can add to the Makefile a variable named
3821 @code{srcdir} whose value is precisely the specified directory.
3822
3823 In addition, the @samp{configure} script should take options
3824 corresponding to most of the standard directory variables
3825 (@pxref{Directory Variables}).  Here is the list:
3826
3827 @example
3828 --prefix --exec-prefix --bindir --sbindir --libexecdir --sysconfdir
3829 --sharedstatedir --localstatedir --libdir --includedir --oldincludedir
3830 --datarootdir --datadir --infodir --localedir --mandir --docdir
3831 --htmldir --dvidir --pdfdir --psdir
3832 @end example
3833
3834 The @code{configure} script should also take an argument which specifies the
3835 type of system to build the program for.  This argument should look like
3836 this:
3837
3838 @example
3839 @var{cpu}-@var{company}-@var{system}
3840 @end example
3841
3842 For example, an Athlon-based GNU/Linux system might be
3843 @samp{i686-pc-linux-gnu}.
3844
3845 The @code{configure} script needs to be able to decode all plausible
3846 alternatives for how to describe a machine.  Thus,
3847 @samp{athlon-pc-gnu/linux} would be a valid alias.  There is a shell
3848 script called
3849 @uref{http://savannah.gnu.org/@/cgi-bin/@/viewcvs/@/*checkout*/@/config/@/config/@/config.sub,
3850 @file{config.sub}} that you can use as a subroutine to validate system
3851 types and canonicalize aliases.
3852
3853 The @code{configure} script should also take the option
3854 @option{--build=@var{buildtype}}, which should be equivalent to a
3855 plain @var{buildtype} argument.  For example, @samp{configure
3856 --build=i686-pc-linux-gnu} is equivalent to @samp{configure
3857 i686-pc-linux-gnu}.  When the build type is not specified by an option
3858 or argument, the @code{configure} script should normally guess it using
3859 the shell script
3860 @uref{http://savannah.gnu.org/@/cgi-bin/@/viewcvs/@/*checkout*/@/config/@/config/@/config.guess,
3861 @file{config.guess}}.
3862
3863 @cindex optional features, configure-time
3864 Other options are permitted to specify in more detail the software
3865 or hardware present on the machine, to include or exclude optional parts
3866 of the package, or to adjust the name of some tools or arguments to them:
3867
3868 @table @samp
3869 @item --enable-@var{feature}@r{[}=@var{parameter}@r{]}
3870 Configure the package to build and install an optional user-level
3871 facility called @var{feature}.  This allows users to choose which
3872 optional features to include.  Giving an optional @var{parameter} of
3873 @samp{no} should omit @var{feature}, if it is built by default.
3874
3875 No @samp{--enable} option should @strong{ever} cause one feature to
3876 replace another.  No @samp{--enable} option should ever substitute one
3877 useful behavior for another useful behavior.  The only proper use for
3878 @samp{--enable} is for questions of whether to build part of the program
3879 or exclude it.
3880
3881 @item --with-@var{package}
3882 @c @r{[}=@var{parameter}@r{]}
3883 The package @var{package} will be installed, so configure this package
3884 to work with @var{package}.
3885
3886 @c  Giving an optional @var{parameter} of
3887 @c @samp{no} should omit @var{package}, if it is used by default.
3888
3889 Possible values of @var{package} include
3890 @samp{gnu-as} (or @samp{gas}), @samp{gnu-ld}, @samp{gnu-libc},
3891 @samp{gdb},
3892 @samp{x},
3893 and
3894 @samp{x-toolkit}.
3895
3896 Do not use a @samp{--with} option to specify the file name to use to
3897 find certain files.  That is outside the scope of what @samp{--with}
3898 options are for.
3899
3900 @item @var{variable}=@var{value}
3901 Set the value of the variable @var{variable} to @var{value}.  This is
3902 used to override the default values of commands or arguments in the
3903 build process.  For example, the user could issue @samp{configure
3904 CFLAGS=-g CXXFLAGS=-g} to build with debugging information and without
3905 the default optimization.
3906
3907 Specifying variables as arguments to @code{configure}, like this:
3908 @example
3909 ./configure CC=gcc
3910 @end example
3911 is preferable to setting them in environment variables:
3912 @example
3913 CC=gcc ./configure
3914 @end example
3915 as it helps to recreate the same configuration later with
3916 @file{config.status}.
3917 @end table
3918
3919 All @code{configure} scripts should accept all of the ``detail''
3920 options and the variable settings, whether or not they make any
3921 difference to the particular package at hand.  In particular, they
3922 should accept any option that starts with @samp{--with-} or
3923 @samp{--enable-}.  This is so users will be able to configure an
3924 entire GNU source tree at once with a single set of options.
3925
3926 You will note that the categories @samp{--with-} and @samp{--enable-}
3927 are narrow: they @strong{do not} provide a place for any sort of option
3928 you might think of.  That is deliberate.  We want to limit the possible
3929 configuration options in GNU software.  We do not want GNU programs to
3930 have idiosyncratic configuration options.
3931
3932 Packages that perform part of the compilation process may support
3933 cross-compilation.  In such a case, the host and target machines for the
3934 program may be different.
3935
3936 The @code{configure} script should normally treat the specified type of
3937 system as both the host and the target, thus producing a program which
3938 works for the same type of machine that it runs on.
3939
3940 To compile a program to run on a host type that differs from the build
3941 type, use the configure option @option{--host=@var{hosttype}}, where
3942 @var{hosttype} uses the same syntax as @var{buildtype}.  The host type
3943 normally defaults to the build type.
3944
3945 To configure a cross-compiler, cross-assembler, or what have you, you
3946 should specify a target different from the host, using the configure
3947 option @samp{--target=@var{targettype}}.  The syntax for
3948 @var{targettype} is the same as for the host type.  So the command would
3949 look like this:
3950
3951 @example
3952 ./configure --host=@var{hosttype} --target=@var{targettype}
3953 @end example
3954
3955 The target type normally defaults to the host type.
3956 Programs for which cross-operation is not meaningful need not accept the
3957 @samp{--target} option, because configuring an entire operating system for
3958 cross-operation is not a meaningful operation.
3959
3960 Some programs have ways of configuring themselves automatically.  If
3961 your program is set up to do this, your @code{configure} script can simply
3962 ignore most of its arguments.
3963
3964 @comment The makefile standards are in a separate file that is also
3965 @comment included by make.texinfo.  Done by roland@gnu.ai.mit.edu on 1/6/93.
3966 @comment For this document, turn chapters into sections, etc.
3967 @lowersections
3968 @include make-stds.texi
3969 @raisesections
3970
3971 @node Releases
3972 @section Making Releases
3973 @cindex packaging
3974
3975 You should identify each release with a pair of version numbers, a
3976 major version and a minor.  We have no objection to using more than
3977 two numbers, but it is very unlikely that you really need them.
3978
3979 Package the distribution of @code{Foo version 69.96} up in a gzipped tar
3980 file with the name @file{foo-69.96.tar.gz}.  It should unpack into a
3981 subdirectory named @file{foo-69.96}.
3982
3983 Building and installing the program should never modify any of the files
3984 contained in the distribution.  This means that all the files that form
3985 part of the program in any way must be classified into @dfn{source
3986 files} and @dfn{non-source files}.  Source files are written by humans
3987 and never changed automatically; non-source files are produced from
3988 source files by programs under the control of the Makefile.
3989
3990 @cindex @file{README} file
3991 The distribution should contain a file named @file{README} which gives
3992 the name of the package, and a general description of what it does.  It
3993 is also good to explain the purpose of each of the first-level
3994 subdirectories in the package, if there are any.  The @file{README} file
3995 should either state the version number of the package, or refer to where
3996 in the package it can be found.
3997
3998 The @file{README} file should refer to the file @file{INSTALL}, which
3999 should contain an explanation of the installation procedure.
4000
4001 The @file{README} file should also refer to the file which contains the
4002 copying conditions.  The GNU GPL, if used, should be in a file called
4003 @file{COPYING}.  If the GNU LGPL is used, it should be in a file called
4004 @file{COPYING.LIB}.
4005
4006 Naturally, all the source files must be in the distribution.  It is okay
4007 to include non-source files in the distribution, provided they are
4008 up-to-date and machine-independent, so that building the distribution
4009 normally will never modify them.  We commonly include non-source files
4010 produced by Bison, @code{lex}, @TeX{}, and @code{makeinfo}; this helps avoid
4011 unnecessary dependencies between our distributions, so that users can
4012 install whichever packages they want to install.
4013
4014 Non-source files that might actually be modified by building and
4015 installing the program should @strong{never} be included in the
4016 distribution.  So if you do distribute non-source files, always make
4017 sure they are up to date when you make a new distribution.
4018
4019 Make sure that the directory into which the distribution unpacks (as
4020 well as any subdirectories) are all world-writable (octal mode 777).
4021 This is so that old versions of @code{tar} which preserve the
4022 ownership and permissions of the files from the tar archive will be
4023 able to extract all the files even if the user is unprivileged.
4024
4025 Make sure that all the files in the distribution are world-readable.
4026
4027 Don't include any symbolic links in the distribution itself.  If the tar
4028 file contains symbolic links, then people cannot even unpack it on
4029 systems that don't support symbolic links.  Also, don't use multiple
4030 names for one file in different directories, because certain file
4031 systems cannot handle this and that prevents unpacking the
4032 distribution.
4033
4034 Try to make sure that all the file names will be unique on MS-DOS.  A
4035 name on MS-DOS consists of up to 8 characters, optionally followed by a
4036 period and up to three characters.  MS-DOS will truncate extra
4037 characters both before and after the period.  Thus,
4038 @file{foobarhacker.c} and @file{foobarhacker.o} are not ambiguous; they
4039 are truncated to @file{foobarha.c} and @file{foobarha.o}, which are
4040 distinct.
4041
4042 @cindex @file{texinfo.tex}, in a distribution
4043 Include in your distribution a copy of the @file{texinfo.tex} you used
4044 to test print any @file{*.texinfo} or @file{*.texi} files.
4045
4046 Likewise, if your program uses small GNU software packages like regex,
4047 getopt, obstack, or termcap, include them in the distribution file.
4048 Leaving them out would make the distribution file a little smaller at
4049 the expense of possible inconvenience to a user who doesn't know what
4050 other files to get.
4051
4052 @node References
4053 @chapter References to Non-Free Software and Documentation
4054 @cindex references to non-free material
4055
4056 A GNU program should not recommend, promote, or grant legitimacy to
4057 the use of any non-free program.  Proprietary software is a social and
4058 ethical problem, and our aim is to put an end to that problem.  We
4059 can't stop some people from writing proprietary programs, or stop
4060 other people from using them, but we can and should refuse to
4061 advertise them to new potential customers, or to give the public the
4062 idea that their existence is ethical.
4063
4064 The GNU definition of free software is found on the GNU web site at
4065 @url{http://www.gnu.org/philosophy/free-sw.html}, and the definition
4066 of free documentation is found at
4067 @url{http://www.gnu.org/philosophy/free-doc.html}.  The terms ``free''
4068 and ``non-free'', used in this document, refer to those definitions.
4069
4070 A list of important licenses and whether they qualify as free is in
4071 @url{http://www.gnu.org/@/licenses/@/license-list.html}.  If it is not
4072 clear whether a license qualifies as free, please ask the GNU Project
4073 by writing to @email{licensing@@gnu.org}.  We will answer, and if the
4074 license is an important one, we will add it to the list.
4075
4076 When a non-free program or system is well known, you can mention it in
4077 passing---that is harmless, since users who might want to use it
4078 probably already know about it.  For instance, it is fine to explain
4079 how to build your package on top of some widely used non-free
4080 operating system, or how to use it together with some widely used
4081 non-free program.
4082
4083 However, you should give only the necessary information to help those
4084 who already use the non-free program to use your program with
4085 it---don't give, or refer to, any further information about the
4086 proprietary program, and don't imply that the proprietary program
4087 enhances your program, or that its existence is in any way a good
4088 thing.  The goal should be that people already using the proprietary
4089 program will get the advice they need about how to use your free
4090 program with it, while people who don't already use the proprietary
4091 program will not see anything likely to lead them to take an interest
4092 in it.
4093
4094 If a non-free program or system is obscure in your program's domain,
4095 your program should not mention or support it at all, since doing so
4096 would tend to popularize the non-free program more than it popularizes
4097 your program.  (You cannot hope to find many additional users for your
4098 program among the users of Foobar, if the existence of Foobar is not
4099 generally known among people who might want to use your program.)
4100
4101 Sometimes a program is free software in itself but depends on a
4102 non-free platform in order to run.  For instance, many Java programs
4103 depend on some non-free Java libraries.  To recommend or promote such
4104 a program is to promote the other programs it needs.  This is why we
4105 are careful about listing Java programs in the Free Software
4106 Directory: we don't want to promote the non-free Java libraries.
4107
4108 We hope this particular problem with Java will be gone by and by, as
4109 we replace the remaining non-free standard Java libraries with free
4110 software, but the general principle will remain the same: don't
4111 recommend, promote or legitimize programs that depend on non-free
4112 software to run.
4113
4114 Some free programs strongly encourage the use of non-free software.  A
4115 typical example is @command{mplayer}.  It is free software in itself,
4116 and the free code can handle some kinds of files.  However,
4117 @command{mplayer} recommends use of non-free codecs for other kinds of
4118 files, and users that install @command{mplayer} are very likely to
4119 install those codecs along with it.  To recommend @command{mplayer}
4120 is, in effect, to promote use of the non-free codecs.
4121
4122 Thus, you should not recommend programs that strongly encourage the
4123 use of non-free software.  This is why we do not list
4124 @command{mplayer} in the Free Software Directory.
4125
4126 A GNU package should not refer the user to any non-free documentation
4127 for free software.  Free documentation that can be included in free
4128 operating systems is essential for completing the GNU system, or any
4129 free operating system, so encouraging it is a priority; to recommend
4130 use of documentation that we are not allowed to include undermines the
4131 impetus for the community to produce documentation that we can
4132 include.  So GNU packages should never recommend non-free
4133 documentation.
4134
4135 By contrast, it is ok to refer to journal articles and textbooks in
4136 the comments of a program for explanation of how it functions, even
4137 though they are non-free.  This is because we don't include such
4138 things in the GNU system even they are free---they are outside the
4139 scope of what a software distribution needs to include.
4140
4141 Referring to a web site that describes or recommends a non-free
4142 program is promoting that program, so please do not make links (or
4143 mention by name) web sites that contain such material.  This policy is
4144 relevant particularly for the web pages for a GNU package.
4145
4146 Following links from nearly any web site can lead eventually to
4147 non-free software; this is inherent in the nature of the web.  So it
4148 makes no sense to criticize a site for having such links.  As long as
4149 the site does not itself recommend a non-free program, there is no
4150 need to consider the question of the sites that it links to for other
4151 reasons.
4152
4153 Thus, for example, you should not refer to AT&T's web site if that
4154 recommends AT&T's non-free software packages; you should not refer to
4155 a site that links to AT&T's site presenting it as a place to get some
4156 non-free program, because that link recommends and legitimizes the
4157 non-free program.  However, that a site contains a link to AT&T's web
4158 site for some other purpose (such as long-distance telephone service)
4159 is not an objection against it.
4160
4161 @node GNU Free Documentation License
4162 @appendix GNU Free Documentation License
4163
4164 @cindex FDL, GNU Free Documentation License
4165 @include fdl.texi
4166
4167 @node Index
4168 @unnumbered Index
4169 @printindex cp
4170
4171 @bye
4172
4173 Local variables:
4174 eval: (add-hook 'write-file-hooks 'time-stamp)
4175 time-stamp-start: "@set lastupdate "
4176 time-stamp-end: "$"
4177 time-stamp-format: "%:b %:d, %:y"
4178 compile-command: "make just-standards"
4179 End: