Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/etc/standards.texi

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    44@settitle GNU Coding Standards
    55@c This date is automagically updated when you save this file:
    6 @set lastupdate March 13, 1998
     6@set lastupdate February 14, 2002
    77@c %**end of header
    88
     
    1717@c @setchapternewpage odd
    1818@setchapternewpage off
     19
     20@c Put everything in one index (arbitrarily chosen to be the concept index).
     21@syncodeindex fn cp
     22@syncodeindex ky cp
     23@syncodeindex pg cp
     24@syncodeindex vr cp
    1925
    2026@c This is used by a cross ref in make-stds.texi
     
    2935@ifinfo
    3036GNU Coding Standards
    31 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
    32 
    33 Permission is granted to make and distribute verbatim copies of
    34 this manual provided the copyright notice and this permission notice
    35 are preserved on all copies.
    36 
    37 @ignore
    38 Permission is granted to process this file through TeX and print the
    39 results, provided the printed document carries copying permission
    40 notice identical to this one except for the removal of this paragraph
    41 (this paragraph not being relevant to the printed manual).
    42 @end ignore
    43 
    44 Permission is granted to copy and distribute modified versions of this
    45 manual under the conditions for verbatim copying, provided that the entire
    46 resulting derived work is distributed under the terms of a permission
    47 notice identical to this one.
    48 
    49 Permission is granted to copy and distribute translations of this manual
    50 into another language, under the above conditions for modified versions,
    51 except that this permission notice may be stated in a translation approved
    52 by the Free Software Foundation.
     37Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
     38
     39Permission is granted to copy, distribute and/or modify this document
     40under the terms of the GNU Free Documentation License, Version 1.1
     41or any later version published by the Free Software Foundation;
     42with no Invariant Sections, with no
     43Front-Cover Texts, and with no Back-Cover Texts.
     44A copy of the license is included in the section entitled ``GNU
     45Free Documentation License''.
    5346@end ifinfo
    5447
    5548@titlepage
    5649@title GNU Coding Standards
    57 @author Richard Stallman
     50@author Richard Stallman, et al.
    5851@author last updated @value{lastupdate}
    5952@page
    6053
    6154@vskip 0pt plus 1filll
    62 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
    63 
    64 Permission is granted to make and distribute verbatim copies of
    65 this manual provided the copyright notice and this permission notice
    66 are preserved on all copies.
    67 
    68 Permission is granted to copy and distribute modified versions of this
    69 manual under the conditions for verbatim copying, provided that the entire
    70 resulting derived work is distributed under the terms of a permission
    71 notice identical to this one.
    72 
    73 Permission is granted to copy and distribute translations of this manual
    74 into another language, under the above conditions for modified versions,
    75 except that this permission notice may be stated in a translation approved
    76 by the Free Software Foundation.
     55Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
     56
     57Permission is granted to copy, distribute and/or modify this document
     58under the terms of the GNU Free Documentation License, Version 1.1
     59or any later version published by the Free Software Foundation;
     60with no Invariant Sections, with no
     61Front-Cover Texts, and with no Back-Cover Texts.
     62A copy of the license is included in the section entitled ``GNU
     63Free Documentation License''.
    7764@end titlepage
    7865
     
    8673@menu
    8774* Preface::                     About the GNU Coding Standards
    88 * Intellectual Property::       Keeping Free Software Free
     75* Legal Issues::                Keeping Free Software Free
    8976* Design Advice::               General Program Design
    9077* Program Behavior::            Program Behavior for All Programs
     
    9279* Documentation::               Documenting Programs
    9380* Managing Releases::           The Release Process
     81* References::                  References to Non-Free Software or Documentation
     82* Copying This Manual::         How to Make Copies of This Manual
     83* Index::                       
     84
    9485@end menu
    9586
     
    10596state reasons for writing in a certain way.
    10697
     98This release of the GNU Coding Standards was last updated
     99@value{lastupdate}.
     100
     101@cindex where to obtain @code{standards.texi}
     102@cindex downloading this manual
     103If you did not obtain this file directly from the GNU project and
     104recently, please check for a newer version.  You can ftp the GNU
     105Coding Standards from any GNU FTP host in the directory
     106@file{/pub/gnu/standards/}.  The GNU Coding Standards are available
     107there in several different formats: @file{standards.text},
     108@file{standards.info}, and @file{standards.dvi}, as well as the
     109Texinfo ``source'' which is divided in two files:
     110@file{standards.texi} and @file{make-stds.texi}.  The GNU Coding
     111Standards are also available on the GNU World Wide Web server:
     112@uref{http://www.gnu.org/prep/standards_toc.html}.
     113
    107114Corrections or suggestions for this document should be sent to
    108 @email{gnu@@gnu.org}.  If you make a suggestion, please include a
     115@email{bug-standards@@gnu.org}.  If you make a suggestion, please include a
    109116suggested new wording for it; our time is limited.  We prefer a context
    110117diff to the @file{standards.texi} or @file{make-stds.texi} files, but if
    111118you don't have those files, please mail your suggestion anyway.
    112119
    113 This release of the GNU Coding Standards was last updated
    114 @value{lastupdate}.
    115 
    116 @node Intellectual Property
     120These standards cover the minimum of what is important when writing a
     121GNU package.  Likely, the needs for additional standards will come up.
     122Sometimes, you might suggest that such standards be added to this
     123document.  If you think your standards would be generally useful, please
     124do suggest them.
     125
     126You should also set standards for your package on many questions not
     127addressed or not firmly specified here.  The most important point is to
     128be self-consistent---try to stick to the conventions you pick, and try
     129to document them as much as possible.  That way, your program will be
     130more maintainable by others.
     131
     132@node Legal Issues
    117133@chapter Keeping Free Software Free
     134@cindex legal aspects
    118135
    119136This @value{CHAPTER} discusses how you can make sure that GNU software
    120 remains unencumbered.
     137avoids legal difficulties, and other related issues.
    121138
    122139@menu
    123140* Reading Non-Free Code::       Referring to Proprietary Programs
    124141* Contributions::               Accepting Contributions
     142* Trademarks::                  How We Deal with Trademark Issues
    125143@end menu
    126144
    127145@node Reading Non-Free Code
    128146@section Referring to Proprietary Programs
     147@cindex proprietary programs
     148@cindex avoiding proprietary code
    129149
    130150Don't in any circumstances refer to Unix source code for or during
     
    158178to free memory, or use a new GNU facility such as obstacks.
    159179
    160 
    161180@node Contributions
    162181@section Accepting Contributions
    163 
    164 If someone else sends you a piece of code to add to the program you are
    165 working on, we need legal papers to use it---the same sort of legal
    166 papers we will need to get from you.  @emph{Each} significant
    167 contributor to a program must sign some sort of legal papers in order
    168 for us to have clear title to the program.  The main author alone is not
     182@cindex legal papers
     183@cindex accepting contributions
     184
     185If the program you are working on is copyrighted by the Free Software
     186Foundation, then when someone else sends you a piece of code to add to
     187the program, we need legal papers to use it---just as we asked you to
     188sign papers initially.  @emph{Each} person who makes a nontrivial
     189contribution to a program must sign some sort of legal papers in order
     190for us to have clear title to the program; the main author alone is not
    169191enough.
    170192
     
    182204text, so we need legal papers for all kinds.
    183205
     206We know it is frustrating to ask for legal papers; it's frustrating for
     207us as well.  But if you don't wait, you are going out on a limb---for
     208example, what if the contributor's employer won't sign a disclaimer?
     209You might have to take that code out again!
     210
    184211You don't need papers for changes of a few lines here or there, since
    185212they are not significant for copyright purposes.  Also, you don't need
    186213papers if all you get from the suggestion is some ideas, not actual code
    187 which you use.  For example, if you write a different solution to the
    188 problem, you don't need to get papers.
    189 
    190 We know this is frustrating; it's frustrating for us as well.  But if
    191 you don't wait, you are going out on a limb---for example, what if the
    192 contributor's employer won't sign a disclaimer?  You might have to take
    193 that code out again!
     214which you use.  For example, if someone send you one implementation, but
     215you write a different implementation of the same idea, you don't need to
     216get papers.
    194217
    195218The very worst thing is if you forget to tell us about the other
     
    201224released or not), please ask us for a copy.
    202225
     226@node Trademarks
     227@section Trademarks
     228@cindex trademarks
     229
     230Please do not include any trademark acknowledgements in GNU software
     231packages or documentation.
     232
     233Trademark acknowledgements are the statements that such-and-such is a
     234trademark of so-and-so.  The GNU Project has no objection to the basic
     235idea of trademarks, but these acknowledgements feel like kowtowing, so
     236we don't use them.  There is no legal requirement for them.
     237
     238What is legally required, as regards other people's trademarks, is to
     239avoid using them in ways which a reader might read as naming or labeling
     240our own programs or activities.  For example, since ``Objective C'' is
     241(or at least was) a trademark, we made sure to say that we provide a
     242``compiler for the Objective C language'' rather than an ``Objective C
     243compiler''.  The latter is meant to be short for the former, but it does
     244not explicitly state the relationship, so it could be misinterpreted as
     245using ``Objective C'' as a label for the compiler rather than for the
     246language.
     247
    203248@node Design Advice
    204249@chapter General Program Design
     250@cindex program design
    205251
    206252This @value{CHAPTER} discusses some of the issues you should take into
    207253account when designing your program.
    208254
     255@c                         Standard or ANSI C
     256@c
     257@c In 1989 the American National Standards Institute (ANSI) standardized
     258@c C   as  standard  X3.159-1989.    In  December   of  that   year  the
     259@c International Standards Organization ISO  adopted the ANSI C standard
     260@c making  minor changes.   In 1990  ANSI then  re-adopted  ISO standard
     261@c C. This version of C is known as either ANSI C or Standard C.
     262
     263@c A major revision of the C Standard appeared in 1999.
     264
    209265@menu
     266* Source Language::             Which languges to use.
    210267* Compatibility::               Compatibility with other implementations
    211268* Using Extensions::            Using non-standard features
    212 * ANSI C::                      Using ANSI C features
    213 * Source Language::             Using languages other than C
     269* Standard C::                  Using Standard C features
     270* Conditional Compilation::     Compiling Code Only If A Conditional is True
    214271@end menu
     272
     273@node Source Language
     274@section Which Languages to Use
     275@cindex programming languges
     276
     277When you want to use a language that gets compiled and runs at high
     278speed, the best language to use is C.  Using another language is like
     279using a non-standard feature: it will cause trouble for users.  Even if
     280GCC supports the other language, users may find it inconvenient to have
     281to install the compiler for that other language in order to build your
     282program.  For example, if you write your program in C++, people will
     283have to install the GNU C++ compiler in order to compile your program.
     284
     285C has one other advantage over C++ and other compiled languages: more
     286people know C, so more people will find it easy to read and modify the
     287program if it is written in C.
     288
     289So in general it is much better to use C, rather than the
     290comparable alternatives.
     291
     292But there are two exceptions to that conclusion:
     293
     294@itemize @bullet
     295@item
     296It is no problem to use another language to write a tool specifically
     297intended for use with that language.  That is because the only people
     298who want to build the tool will be those who have installed the other
     299language anyway.
     300
     301@item
     302If an application is of interest only to a narrow part of the community,
     303then the question of which language it is written in has less effect on
     304other people, so you may as well please yourself.
     305@end itemize
     306
     307Many programs are designed to be extensible: they include an interpreter
     308for a language that is higher level than C.  Often much of the program
     309is written in that language, too.  The Emacs editor pioneered this
     310technique.
     311
     312@cindex GUILE
     313The standard extensibility interpreter for GNU software is GUILE, which
     314implements the language Scheme (an especially clean and simple dialect
     315of Lisp).  @uref{http://www.gnu.org/software/guile/}.  We don't reject
     316programs written in other ``scripting languages'' such as Perl and
     317Python, but using GUILE is very important for the overall consistency of
     318the GNU system.
    215319
    216320@node Compatibility
    217321@section Compatibility with Other Implementations
     322@cindex compatibility with C and @sc{posix} standards
     323@cindex @sc{posix} compatibility
    218324
    219325With occasional exceptions, utility programs and libraries for GNU
    220326should be upward compatible with those in Berkeley Unix, and upward
    221 compatible with @sc{ansi} C if @sc{ansi} C specifies their behavior, and
    222 upward compatible with @sc{POSIX} if @sc{POSIX} specifies their
    223 behavior.
     327compatible with Standard C if Standard C specifies their
     328behavior, and upward compatible with @sc{posix} if @sc{posix} specifies
     329their behavior.
    224330
    225331When these standards conflict, it is useful to offer compatibility
    226332modes for each of them.
    227333
    228 @sc{ansi} C and @sc{POSIX} prohibit many kinds of extensions.  Feel free
    229 to make the extensions anyway, and include a @samp{--ansi},
     334@cindex options for compatibility
     335Standard C and @sc{posix} prohibit many kinds of extensions.  Feel
     336free to make the extensions anyway, and include a @samp{--ansi},
    230337@samp{--posix}, or @samp{--compatible} option to turn them off.
    231338However, if the extension has a significant chance of breaking any real
    232 programs or scripts, then it is not really upward compatible.  Try to
    233 redesign its interface.
    234 
    235 Many GNU programs suppress extensions that conflict with POSIX if the
     339programs or scripts, then it is not really upward compatible.  So you
     340should try to redesign its interface to make it upward compatible.
     341
     342@cindex @code{POSIXLY_CORRECT}, environment variable
     343Many GNU programs suppress extensions that conflict with @sc{posix} if the
    236344environment variable @code{POSIXLY_CORRECT} is defined (even if it is
    237345defined with a null value).  Please make your program recognize this
     
    244352feature as well.  (There is a free @code{vi} clone, so we offer it.)
    245353
    246 Additional useful features not in Berkeley Unix are welcome.
     354Additional useful features are welcome regardless of whether
     355there is any precedent for them.
    247356
    248357@node Using Extensions
    249358@section Using Non-standard Features
     359@cindex non-standard extensions
    250360
    251361Many GNU facilities that already exist support a number of convenient
     
    268378
    269379An exception to this rule are the large, established programs (such as
    270 Emacs) which run on a great variety of systems.  Such programs would
    271 be broken by use of GNU extensions.
    272 
    273 Another exception is for programs that are used as part of
    274 compilation: anything that must be compiled with other compilers in
    275 order to bootstrap the GNU compilation facilities.  If these require
    276 the GNU compiler, then no one can compile them without having them
    277 installed already.  That would be no good.
    278 
    279 @node ANSI C
    280 @section @sc{ansi} C and pre-@sc{ansi} C
    281 
    282 Do not ever use the ``trigraph'' feature of @sc{ansi} C.
    283 
    284 @sc{ansi} C is widespread enough now that it is ok to write new programs
    285 that use @sc{ansi} C features (and therefore will not work in
    286 non-@sc{ansi} compilers).  And if a program is already written in
    287 @sc{ansi} C, there's no need to convert it to support non-@sc{ansi}
    288 compilers.
    289 
    290 However, it is easy to support non-@sc{ansi} compilers in most programs,
    291 so you might still consider doing so when you write a program.  Instead
    292 of writing function definitions in @sc{ansi} prototype form,
     380Emacs) which run on a great variety of systems.  Using GNU extensions in
     381such programs would make many users unhappy, so we don't do that.
     382
     383Another exception is for programs that are used as part of compilation:
     384anything that must be compiled with other compilers in order to
     385bootstrap the GNU compilation facilities.  If these require the GNU
     386compiler, then no one can compile them without having them installed
     387already.  That would be extremely troublesome in certain cases.
     388
     389@node Standard C
     390@section Standard C and Pre-Standard C
     391@cindex @sc{ansi} C standard
     392
     3931989 Standard C is widespread enough now that it is ok to use its
     394features in new programs.  There is one exception: do not ever use the
     395``trigraph'' feature of Standard C.
     396
     3971999 Standard C is not widespread yet, so please do not require its
     398features in programs.  It is ok to use its features if they are present.
     399
     400However, it is easy to support pre-standard compilers in most programs,
     401so if you know how to do that, feel free.  If a program you are
     402maintaining has such support, you should try to keep it working.
     403
     404@cindex function prototypes
     405To support pre-standard C, instead of writing function definitions in
     406standard prototype form,
    293407
    294408@example
     
    299413
    300414@noindent
    301 write the definition in pre-@sc{ansi} style like this,
     415write the definition in pre-standard style like this,
    302416
    303417@example
     
    316430
    317431You need such a declaration anyway, in a header file, to get the benefit
    318 of @sc{ansi} C prototypes in all the files where the function is called.
    319 And once you have it, you lose nothing by writing the function
    320 definition in the pre-@sc{ansi} style.
    321 
    322 If you don't know non-@sc{ansi} C, there's no need to learn it; just
    323 write in @sc{ansi} C.
    324 
    325 @node Source Language
    326 @section Using Languages Other Than C
    327 
    328 Using a language other than C is like using a non-standard feature: it
    329 will cause trouble for users.  Even if GCC supports the other language,
    330 users may find it inconvenient to have to install the compiler for that
    331 other language in order to build your program.  For example, if you
    332 write your program in C++, people will have to install the C++ compiler
    333 in order to compile your program.  Thus, it is better if you write in C.
    334 
    335 But there are three situations when there is no disadvantage in using
    336 some other language:
    337 
    338 @itemize @bullet
    339 @item
    340 It is okay to use another language if your program contains an
    341 interpreter for that language.
    342 
    343 For example, if your program links with GUILE, it is ok to write part of
    344 the program in Scheme or another language supported by GUILE.
    345 
    346 @item
    347 It is okay to use another language in a tool specifically intended for
    348 use with that language.
    349 
    350 This is okay because the only people who want to build the tool will be
    351 those who have installed the other language anyway.
    352 
    353 @item
    354 If an application is of interest to a narrow community, then perhaps
    355 it's not important if the application is inconvenient to install.
    356 @end itemize
    357 
    358 C has one other advantage over C++ and other compiled languages: more
    359 people know C, so more people will find it easy to read and modify the
    360 program if it is written in C.
     432of prototypes in all the files where the function is called.  And once
     433you have the declaration, you normally lose nothing by writing the
     434function definition in the pre-standard style.
     435
     436This technique does not work for integer types narrower than @code{int}.
     437If you think of an argument as being of a type narrower than @code{int},
     438declare it as @code{int} instead.
     439
     440There are a few special cases where this technique is hard to use.  For
     441example, if a function argument needs to hold the system type
     442@code{dev_t}, you run into trouble, because @code{dev_t} is shorter than
     443@code{int} on some machines; but you cannot use @code{int} instead,
     444because @code{dev_t} is wider than @code{int} on some machines.  There
     445is no type you can safely use on all machines in a non-standard
     446definition.  The only way to support non-standard C and pass such an
     447argument is to check the width of @code{dev_t} using Autoconf and choose
     448the argument type accordingly.  This may not be worth the trouble.
     449
     450In order to support pre-standard compilers that do not recognize
     451prototypes, you may want to use a preprocessor macro like this:
     452
     453@example
     454/* Declare the prototype for a general external function.  */
     455#if defined (__STDC__) || defined (WINDOWSNT)
     456#define P_(proto) proto
     457#else
     458#define P_(proto) ()
     459#endif
     460@end example
     461
     462@node Conditional Compilation
     463@section Conditional Compilation
     464
     465When supporting configuration options already known when building your
     466program we prefer using @code{if (... )} over conditional compilation,
     467as in the former case the compiler is able to perform more extensive
     468checking of all possible code paths.
     469
     470For example, please write
     471
     472@smallexample
     473  if (HAS_FOO)
     474    ...
     475  else
     476    ...
     477@end smallexample
     478
     479instead of:
     480
     481@smallexample
     482  #ifdef HAS_FOO
     483    ...
     484  #else
     485    ...
     486  #endif
     487@end smallexample
     488
     489A modern compiler such as GCC will generate exactly the same code in
     490both cases, and we have been using similar techniques with good success
     491in several projects.
     492
     493While this is not a silver bullet solving all portability problems,
     494following this policy would have saved the GCC project alone many person
     495hours if not days per year.
     496
     497In the case of function-like macros like @code{REVERSIBLE_CC_MODE} in
     498GCC which cannot be simply used in @code{if( ...)} statements, there is
     499an easy workaround.  Simply introduce another macro
     500@code{HAS_REVERSIBLE_CC_MODE} as in the following example:
     501
     502@smallexample
     503  #ifdef REVERSIBLE_CC_MODE
     504  #define HAS_REVERSIBLE_CC_MODE 1
     505  #else
     506  #define HAS_REVERSIBLE_CC_MODE 0
     507  #endif
     508@end smallexample
    361509
    362510@node Program Behavior
    363511@chapter Program Behavior for All Programs
    364512
    365 This @value{CHAPTER} describes how to write robust software. It also
    366 describes general standards for error messages, the command line interface,
    367 and how libraries should behave.
     513This @value{CHAPTER} describes conventions for writing robust
     514software.  It also describes general standards for error messages, the
     515command line interface, and how libraries should behave.
    368516
    369517@menu
     
    371519* Libraries::                   Library behavior
    372520* Errors::                      Formatting error messages
    373 * User Interfaces::             Standards for command line interfaces
    374 * Option Table::                Table of long options.
     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
    375525* Memory Usage::                When and how to care about memory needs
     526* File Usage::                  Which files to use, and where
    376527@end menu
    377528
     
    379530@section Writing Robust Programs
    380531
     532@cindex arbitrary limits on data
    381533Avoid arbitrary limits on the length or number of @emph{any} data
    382534structure, including file names, lines, files, and symbols, by allocating
     
    384536are silently truncated''.  This is not acceptable in a GNU utility.
    385537
     538@cindex @code{NUL} characters
    386539Utilities reading files should not drop NUL characters, or any other
    387 nonprinting characters @emph{including those with codes above 0177}.  The
    388 only sensible exceptions would be utilities specifically intended for
    389 interface to certain types of printers that can't handle those characters.
    390 
     540nonprinting characters @emph{including those with codes above 0177}.
     541The only sensible exceptions would be utilities specifically intended
     542for interface to certain types of terminals or printers
     543that can't handle those characters.
     544Whenever possible, try to make programs work properly with
     545sequences of bytes that represent multibyte characters, using encodings
     546such as UTF-8 and others.
     547
     548@cindex error messages
    391549Check every system call for an error return, unless you know you wish to
    392550ignore errors.  Include the system error text (from @code{perror} or
     
    396554sufficient.
    397555
     556@cindex @code{malloc} return value
     557@cindex memory allocation failure
    398558Check every call to @code{malloc} or @code{realloc} to see if it
    399559returned zero.  Check @code{realloc} even if you are making the block
     
    417577virtual memory, and then try the command again.
    418578
     579@cindex command-line arguments, decoding
    419580Use @code{getopt_long} to decode arguments, unless the argument syntax
    420581makes this unreasonable.
     
    429590are less likely to work compatibly.  If you need to find all the files
    430591in a directory, use @code{readdir} or some other high-level interface.
    431 These will be supported compatibly by GNU.
    432 
    433 By default, the GNU system will provide the signal handling functions of
    434 @sc{BSD} and of @sc{POSIX}.  So GNU software should be written to use
    435 these.
    436 
     592These are supported compatibly by GNU.
     593
     594@cindex signal handling
     595The preferred signal handling facilities are the BSD variant of
     596@code{signal}, and the @sc{posix} @code{sigaction} function; the
     597alternative USG @code{signal} interface is an inferior design.
     598
     599Nowadays, using the @sc{posix} signal functions may be the easiest way
     600to make a program portable.  If you use @code{signal}, then on GNU/Linux
     601systems running GNU libc version 1, you should include
     602@file{bsd/signal.h} instead of @file{signal.h}, so as to get BSD
     603behavior.  It is up to you whether to support systems where
     604@code{signal} has only the USG behavior, or give up on them.
     605
     606@cindex impossible conditions
    437607In error checks that detect ``impossible'' conditions, just abort.
    438608There is usually no point in printing any message.  These checks
     
    449619will see 0 as the status, and it will appear that the program succeeded.
    450620
     621@cindex temporary files
     622@cindex @code{TMPDIR} environment variable
    451623If you make temporary files, check the @code{TMPDIR} environment
    452624variable; if that variable is defined, use the specified directory
    453625instead of @file{/tmp}.
    454626
     627In addition, be aware that there is a possible security problem when
     628creating temporary files in world-writable directories.  In C, you can
     629avoid this problem by creating temporary files in this manner:
     630
     631@example
     632fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
     633@end example
     634
     635@noindent
     636or by using the @code{mkstemps} function from libiberty.
     637
     638In bash, use @code{set -C} to avoid this problem.
     639
    455640@node Libraries
    456641@section Library Behavior
     642@cindex libraries
    457643
    458644Try to make library functions reentrant.  If they need to do dynamic
     
    474660
    475661External symbols that are not documented entry points for the user
    476 should have names beginning with @samp{_}.  They should also contain
    477 the chosen name prefix for the library, to prevent collisions with
    478 other libraries.  These can go in the same files with user entry
    479 points if you like.
     662should have names beginning with @samp{_}.  The @samp{_} should be
     663followed by the chosen name prefix for the library, to prevent
     664collisions with other libraries.  These can go in the same files with
     665user entry points if you like.
    480666
    481667Static functions and variables can be used as you like and need not
     
    484670@node Errors
    485671@section Formatting Error Messages
     672@cindex formatting error messages
     673@cindex error messages, formatting
    486674
    487675Error messages from compilers should look like this:
     
    490678@var{source-file-name}:@var{lineno}: @var{message}
    491679@end example
     680
     681@noindent
     682If you want to mention the column number, use this format:
     683
     684@example
     685@var{source-file-name}:@var{lineno}:@var{column}: @var{message}
     686@end example
     687
     688@noindent
     689Line numbers should start from 1 at the beginning of the file, and
     690column numbers should start from 1 at the beginning of the line.  (Both
     691of these conventions are chosen for compatibility.)  Calculate column
     692numbers assuming that space and all ASCII printing characters have
     693equal width, and assuming tab stops every 8 columns.
    492694
    493695Error messages from other noninteractive programs should look like this:
     
    506708@noindent
    507709when there is no relevant source file.
     710
     711If you want to mention the column number, use this format:
     712
     713@example
     714@var{program}:@var{source-file-name}:@var{lineno}:@var{column}: @var{message}
     715@end example
    508716
    509717In an interactive program (one that is reading commands from a
     
    523731
    524732@node User Interfaces
    525 @section Standards for Command Line Interfaces
    526 
     733@section Standards for Interfaces Generally
     734
     735@cindex program name and its behavior
     736@cindex behavior, dependent on program's name
    527737Please don't make the behavior of a utility depend on the name used
    528738to invoke it.  It is useful sometimes to make a link to a utility
     
    532742to select among the alternate behaviors.
    533743
     744@cindex output device and program's behavior
    534745Likewise, please don't make the behavior of the program depend on the
    535746type of output device it is used with.  Device independence is an
    536 important principle of the system's design; do not compromise it
    537 merely to save someone from typing an option now and then.
     747important principle of the system's design; do not compromise it merely
     748to save someone from typing an option now and then.  (Variation in error
     749message syntax when using a terminal is ok, because that is a side issue
     750that people do not depend on.)
    538751
    539752If you think one behavior is most useful when the output is to a
     
    551764multi-column format.
    552765
    553 It is a good idea to follow the @sc{POSIX} guidelines for the
     766@node Graphical Interfaces
     767@section Standards for Graphical Interfaces
     768@cindex graphical user interface
     769
     770@cindex gtk
     771When you write a program that provides a graphical user interface,
     772please make it work with X Windows and the GTK toolkit unless the
     773functionality specifically requires some alternative (for example,
     774``displaying jpeg images while in console mode'').
     775
     776In addition, please provide a command-line interface to control the
     777functionality.  (In many cases, the graphical user interface can be a
     778separate program which invokes the command-line program.)  This is
     779so that the same jobs can be done from scripts.
     780
     781@cindex corba
     782@cindex gnome
     783Please also consider providing a CORBA interface (for use from GNOME), a
     784library interface (for use from C), and perhaps a keyboard-driven
     785console interface (for use by users from console mode).  Once you are
     786doing the work to provide the functionality and the graphical interface,
     787these won't be much extra work.
     788
     789@node Command-Line Interfaces
     790@section Standards for Command Line Interfaces
     791@cindex command-line interface
     792
     793@findex getopt
     794It is a good idea to follow the @sc{posix} guidelines for the
    554795command-line options of a program.  The easiest way to do this is to use
    555796@code{getopt} to parse them.  Note that the GNU version of @code{getopt}
    556797will normally permit options anywhere among the arguments unless the
    557 special argument @samp{--} is used.  This is not what @sc{POSIX}
     798special argument @samp{--} is used.  This is not what @sc{posix}
    558799specifies; it is a GNU extension.
    559800
     801@cindex long-named options
    560802Please define long-named options that are equivalent to the
    561803single-letter Unix-style options.  We hope to make GNU more user
     
    577819among GNU utilities, and fewer idiosyncracies for users to remember.
    578820
     821@cindex standard command-line options
    579822All programs should support two standard options: @samp{--version}
    580823and @samp{--help}.
    581824
    582825@table @code
     826@cindex @samp{--version} option
    583827@item --version
    584 This option should direct the program to information about its name,
     828This option should direct the program to print information about its name,
    585829version, origin and legal status, all on standard output, and then exit
    586830successfully.  Other options and arguments should be ignored once this
    587831is seen, and the program should not perform its normal function.
    588832
     833@cindex canonical name of a program
     834@cindex program's canonical name
    589835The first line is meant to be easy for a program to parse; the version
    590836number proper starts after the last space.  In addition, it contains
     
    659905line.
    660906
     907Translations of the above lines must preserve the validity of the
     908copyright notices (@pxref{Internationalization}).  If the translation's
     909character set supports it, the @samp{(C)} should be replaced with the
     910copyright symbol, as follows:
     911
     912@ifinfo
     913(the official copyright symbol, which is the letter C in a circle);
     914@end ifinfo
     915@ifnotinfo
     916@copyright{}
     917@end ifnotinfo
     918
     919Write the word ``Copyright'' exactly like that, in English.  Do not
     920translate it into another language.  International treaties recognize
     921the English word ``Copyright''; translations into other languages do not
     922have legal significance.
     923
     924
     925@cindex @samp{--help} option
    661926@item --help
    662927This option should output brief documentation for how to invoke the
     
    665930not perform its normal function.
    666931
     932@cindex address for bug reports
     933@cindex bug reports
    667934Near the end of the @samp{--help} option's output there should be a line
    668935that says where to mail bug reports.  It should have this format:
     
    675942@node Option Table
    676943@section Table of Long Options
     944@cindex long option names
     945@cindex table of long options
    677946
    678947Here is a table of long options used by GNU programs.  It is surely
    679948incomplete, but we aim to list all the options that a new program might
    680949want to be compatible with.  If you use names not already in the table,
    681 please send @email{gnu@@gnu.org} a list of them, with their
     950please send @email{bug-standards@@gnu.org} a list of them, with their
    682951meanings, so we can update the table.
    683952
     
    7391008@samp{-n} in @code{wdiff}.
    7401009
     1010@item background
     1011For server programs, run in the background.
     1012
    7411013@item backward-search
    7421014@samp{-B} in @code{ctags}.
     
    8621134@item dereference-args
    8631135@samp{-D} in @code{du}.
     1136
     1137@item device
     1138Specify an I/O device (special file name).
    8641139
    8651140@item diacritics
     
    9951270@samp{-F} in @code{shar}.
    9961271
     1272@item foreground
     1273For server programs, run in the foreground;
     1274in other words, don't do anything special to run the server
     1275in the background.
     1276
    9971277@item format
    9981278Used in @code{ls}, @code{time}, and @code{ptx}.
     
    10391319@item hide-control-chars
    10401320@samp{-q} in @code{ls}.
     1321
     1322@item html
     1323In @code{makeinfo}, output HTML.
    10411324
    10421325@item idle
     
    10991382@item info
    11001383@samp{-i}, @samp{-l}, and @samp{-m} in Finger.
     1384
     1385@item init-file
     1386In some programs, specify the name of the file to read as the user's
     1387init file.
    11011388
    11021389@item initial
     
    11181405@samp{-p} in @code{shar}.
    11191406
     1407@item iso-8601
     1408Used in @code{date}
     1409
    11201410@item jobs
    11211411@samp{-j} in Make.
     
    13531643@samp{-F} in @code{gprof}.
    13541644
     1645@item options
     1646@samp{-o} in @code{getopt}, @code{fdlist}, @code{fdmount},
     1647@code{fdmountd}, and @code{fdumount}.
     1648
    13551649@item output
    13561650In various programs, specify the output file name.
     
    14361730@item prompt
    14371731@samp{-p} in @code{ed}.
     1732
     1733@item proxy
     1734Specify an HTTP proxy.
    14381735
    14391736@item query-user
     
    15651862@samp{-s} in @code{ls}.
    15661863
     1864@item socket
     1865Specify a file descriptor for a network server to use for its socket,
     1866instead of opening and binding a new socket.  This provides a way to
     1867run, in a nonpriveledged process, a server that normally needs a
     1868reserved port number.
     1869
    15671870@item sort
    15681871Used in @code{ls}.
     
    16621965@item time
    16631966Used in @code{ls} and @code{touch}.
     1967
     1968@item timeout
     1969Specify how long to wait before giving up on some operation.
    16641970
    16651971@item to-stdout
     
    17552061@node Memory Usage
    17562062@section Memory Usage
    1757 
    1758 If it typically uses just a few meg of memory, don't bother making any
     2063@cindex memory usage
     2064
     2065If a program typically uses just a few meg of memory, don't bother making any
    17592066effort to reduce memory usage.  For example, if it is impractical for
    17602067other reasons to operate on files more than a few meg long, it is
     
    17722079core and give a fatal error if @code{malloc} returns zero.
    17732080
     2081@node File Usage
     2082@section File Usage
     2083@cindex file usage
     2084
     2085Programs should be prepared to operate when @file{/usr} and @file{/etc}
     2086are read-only file systems.  Thus, if the program manages log files,
     2087lock files, backup files, score files, or any other files which are
     2088modified for internal purposes, these files should not be stored in
     2089@file{/usr} or @file{/etc}.
     2090
     2091There are two exceptions.  @file{/etc} is used to store system
     2092configuration information; it is reasonable for a program to modify
     2093files in @file{/etc} when its job is to update the system configuration.
     2094Also, if the user explicitly asks to modify one file in a directory, it
     2095is reasonable for the program to store other files in the same
     2096directory.
     2097
    17742098@node Writing C
    17752099@chapter Making The Best Use of C
     
    17822106* Comments::                    Commenting Your Work
    17832107* Syntactic Conventions::       Clean Use of C Constructs
    1784 * Names::                       Naming Variables and Functions
     2108* Names::                       Naming Variables, Functions, and Files
    17852109* System Portability::          Portability between different operating systems
    17862110* CPU Portability::             Supporting the range of CPU types
     
    17922116@node Formatting
    17932117@section Formatting Your Source Code
    1794 
     2118@cindex formatting source code
     2119
     2120@cindex open brace
     2121@cindex braces, in C source
    17952122It is important to put the open-brace that starts the body of a C
    17962123function in column zero, and avoid putting any other open-brace or
     
    18142141
    18152142@noindent
    1816 or, if you want to use @sc{ansi} C, format the definition like this:
     2143or, if you want to use Standard C syntax, format the definition like
     2144this:
    18172145
    18182146@example
     
    18242152@end example
    18252153
    1826 In @sc{ansi} C, if the arguments don't fit nicely on one line,
     2154In Standard C, if the arguments don't fit nicely on one line,
    18272155split it like this:
    18282156
     
    18342162@end example
    18352163
    1836 For the body of the function, we prefer code formatted like this:
     2164The rest of this section gives our recommendations for other aspects of
     2165C formatting style, which is also the default style of the @code{indent}
     2166program in version 1.2 and newer.  It corresponds to the options
     2167
     2168@smallexample
     2169-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2
     2170-ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -psl -nsc -nsob
     2171@end smallexample
     2172
     2173We don't think of these recommendations as requirements, because it
     2174causes no problems for users if two different programs have different
     2175formatting styles.
     2176
     2177But whatever style you use, please use it consistently, since a mixture
     2178of styles within one program tends to look ugly.  If you are
     2179contributing changes to an existing program, please follow the style of
     2180that program.
     2181
     2182For the body of the function, our recommended style looks like this:
    18372183
    18382184@example
     
    18502196@end example
    18512197
     2198@cindex spaces before open-paren
    18522199We find it easier to read a program when it has spaces before the
    18532200open-parentheses and after the commas.  Especially after the commas.
     
    18562203before an operator, not after one.  Here is the right way:
    18572204
     2205@cindex expressions, splitting
    18582206@example
    18592207if (foo_this_is_long && bar > win (x, y, z)
     
    18802228Insert extra parentheses so that Emacs will indent the code properly.
    18812229For example, the following indentation looks nice if you do it by hand,
    1882 but Emacs would mess it up:
    18832230
    18842231@example
     
    18872234@end example
    18882235
    1889 But adding a set of parentheses solves the problem:
     2236@noindent
     2237but Emacs would alter it.  Adding a set of parentheses produces
     2238something that looks equally nice, and which Emacs will preserve:
    18902239
    18912240@example
     
    19042253@end example
    19052254
     2255@cindex formfeed
     2256@cindex control-L
    19062257Please use formfeed characters (control-L) to divide the program into
    19072258pages at logical places (but not within a function).  It does not matter
     
    19092260page.  The formfeeds should appear alone on lines by themselves.
    19102261
    1911 
    19122262@node Comments
    19132263@section Commenting Your Work
     2264@cindex commenting
    19142265
    19152266Every program should start with a comment saying briefly what it is for.
     
    19632314@end example
    19642315
     2316@cindex conditionals, comments for
     2317@cindex @code{#endif}, commenting
    19652318Every @samp{#endif} should have a comment, except in the case of short
    19662319conditionals (just a few lines) that are not nested.  The comment should
     
    20042357@node Syntactic Conventions
    20052358@section Clean Use of C Constructs
    2006 
    2007 Please explicitly declare all arguments to functions.
    2008 Don't omit them just because they are @code{int}s.
     2359@cindex syntactic conventions
     2360
     2361@cindex implicit @code{int}
     2362@cindex function argument, declaring
     2363Please explicitly declare the types of all objects.  For example, you
     2364should explicitly declare all arguments to functions, and you should
     2365declare functions to return @code{int} rather than omitting the
     2366@code{int}.
     2367
     2368@cindex compiler warnings
     2369@cindex @samp{-Wall} compiler option
     2370Some programmers like to use the GCC @samp{-Wall} option, and change the
     2371code whenever it issues a warning.  If you want to do this, then do.
     2372Other programmers prefer not to use @samp{-Wall}, because it gives
     2373warnings for valid and legitimate code which they do not want to change.
     2374If you want to do this, then do.  The compiler should be your servant,
     2375not your master.
    20092376
    20102377Declarations of external functions and functions to appear later in the
     
    20142381functions.
    20152382
     2383@cindex temporary variables
    20162384It used to be common practice to use the same local variables (with
    20172385names like @code{tem}) over and over for different values within one
     
    20252393Don't use local variables or parameters that shadow global identifiers.
    20262394
     2395@cindex multiple variables in a line
    20272396Don't declare multiple variables in one declaration that spans lines.
    20282397Start a new declaration on each line, instead.  For example, instead
     
    21252494@end example
    21262495
     2496@pindex lint
    21272497Don't make the program ugly to placate @code{lint}.  Please don't insert any
    21282498casts to @code{void}.  Zero without a cast is perfectly fine as a null
    21292499pointer constant, except when calling a varargs function.
    21302500
    2131 @node  Names
    2132 @section Naming Variables and Functions
    2133 
     2501@node Names
     2502@section Naming Variables, Functions, and Files
     2503
     2504@cindex names of variables, functions, and files
    21342505The names of global variables and functions in a program serve as
    21352506comments of a sort.  So don't choose terse names---instead, look for
     
    21412512one context, where (presumably) comments explain their purpose.
    21422513
     2514Try to limit your use of abbreviations in symbol names.  It is ok to
     2515make a few abbreviations, explain what they mean, and then use them
     2516frequently, but don't use lots of obscure abbreviations.
     2517
    21432518Please use underscores to separate words in a name, so that the Emacs
    21442519word commands can be useful within them.  Stick to lower case; reserve
     
    21652540constants.
    21662541
    2167 Use file names of 14 characters or less, to avoid creating gratuitous
    2168 problems on older System V systems.  You can use the program
    2169 @code{doschk} to test for this.  @code{doschk} also tests for potential
    2170 name conflicts if the files were loaded onto an MS-DOS file
    2171 system---something you may or may not care about.
     2542@cindex file-name limitations
     2543@pindex doschk
     2544You might want to make sure that none of the file names would conflict
     2545the files were loaded onto an MS-DOS file system which shortens the
     2546names.  You can use the program @code{doschk} to test for this.
     2547
     2548Some GNU programs were designed to limit themselves to file names of 14
     2549characters or less, to avoid file name conflicts if they are read into
     2550older System V systems.  Please preserve this feature in the existing
     2551GNU programs that have it, but there is no need to do this in new GNU
     2552programs.  @code{doschk} also reports file names longer than 14
     2553characters.
    21722554
    21732555@node System Portability
    21742556@section Portability between System Types
     2557@cindex portability, between system types
    21752558
    21762559In the Unix world, ``portability'' refers to porting to different Unix
     
    21792562
    21802563The primary purpose of GNU software is to run on top of the GNU kernel,
    2181 compiled with the GNU C compiler, on various types of @sc{cpu}.  The
    2182 amount and kinds of variation among GNU systems on different @sc{cpu}s
    2183 will be comparable to the variation among Linux-based GNU systems or
    2184 among BSD systems today.  So the kinds of portability that are absolutely
    2185 necessary are quite limited.
    2186 
    2187 But many users do run GNU software on non-GNU Unix or Unix-like systems.
    2188 So supporting a variety of Unix-like systems is desirable, although not
    2189 paramount.
    2190 
     2564compiled with the GNU C compiler, on various types of @sc{cpu}.  So the
     2565kinds of portability that are absolutely necessary are quite limited.
     2566But it is important to support Linux-based GNU systems, since they
     2567are the form of GNU that is popular.
     2568
     2569Beyond that, it is good to support the other free operating systems
     2570(*BSD), and it is nice to support other Unix-like systems if you want
     2571to.  Supporting a variety of Unix-like systems is desirable, although
     2572not paramount.  It is usually not too hard, so you may as well do it.
     2573But you don't have to consider it an obligation, if it does turn out to
     2574be hard.
     2575
     2576@pindex autoconf
    21912577The easiest way to achieve portability to most Unix-like systems is to
    21922578use Autoconf.  It's unlikely that your program needs to know more
     
    21982584when there is a higher-level alternative (@code{readdir}).
    21992585
     2586@cindex non-@sc{posix} systems, and portability
    22002587As for systems that are not like Unix, such as MSDOS, Windows, the
    2201 Macintosh, VMS, and MVS, supporting them is usually so much work that it
    2202 is better if you don't.
    2203 
    2204 The planned GNU kernel is not finished yet, but you can tell which
    2205 facilities it will provide by looking at the GNU C Library Manual.  The
    2206 GNU kernel is based on Mach, so the features of Mach will also be
    2207 available.  However, if you use Mach features, you'll probably have
    2208 trouble debugging your program today.
     2588Macintosh, VMS, and MVS, supporting them is often a lot of work.  When
     2589that is the case, it is better to spend your time adding features that
     2590will be useful on GNU and GNU/Linux, rather than on supporting other
     2591incompatible systems.
     2592
     2593It is a good idea to define the ``feature test macro''
     2594@code{_GNU_SOURCE} when compiling your C files.  When you compile on GNU
     2595or GNU/Linux, this will enable the declarations of GNU library extension
     2596functions, and that will usually give you a compiler error message if
     2597you define the same function names in some other way in your program.
     2598(You don't have to actually @emph{use} these functions, if you prefer
     2599to make the program more portable to other systems.)
     2600
     2601But whether or not you use these GNU extensions, you should avoid
     2602using their names for any other meanings.  Doing so would make it hard
     2603to move your code into other GNU programs.
    22092604
    22102605@node CPU Portability
    22112606@section Portability between @sc{cpu}s
    22122607
     2608@cindex data types, and portability
     2609@cindex portability, and data types
    22132610Even GNU systems will differ because of differences among @sc{cpu}
    22142611types---for example, difference in byte ordering and alignment
     
    22182615in GNU.
    22192616
     2617Similarly, don't make any effort to cater to the possibility that
     2618@code{long} will be smaller than predefined types like @code{size_t}.
     2619For example, the following code is ok:
     2620
     2621@example
     2622printf ("size = %lu\n", (unsigned long) sizeof array);
     2623printf ("diff = %ld\n", (long) (pointer2 - pointer1));
     2624@end example
     2625
     26261989 Standard C requires this to work, and we know of only one
     2627counterexample: 64-bit programs on Microsoft Windows IA-64.  We will
     2628leave it to those who want to port GNU programs to that environment
     2629to figure out how to do it.
     2630
     2631Predefined file-size types like @code{off_t} are an exception: they are
     2632longer than @code{long} on many platforms, so code like the above won't
     2633work with them.  One way to print an @code{off_t} value portably is to
     2634print its digits yourself, one by one.
     2635
    22202636Don't assume that the address of an @code{int} object is also the
    22212637address of its least-significant byte.  This is false on big-endian
     
    22322648pointers of various types, or between pointers and integers.  On most
    22332649machines, there's no difference anyway.  As for the few machines where
    2234 there is a difference, all of them support @sc{ansi} C, so you can use
    2235 prototypes (conditionalized to be active only in @sc{ansi} C) to make
    2236 the code work on those systems.
     2650there is a difference, all of them support Standard C prototypes, so you can
     2651use prototypes (perhaps conditionalized to be active only in Standard C)
     2652to make the code work on those systems.
    22372653
    22382654In certain cases, it is ok to pass integer and pointer arguments
     
    22442660error (s, a1, a2, a3)
    22452661     char *s;
    2246      int a1, a2, a3;
     2662     char *a1, *a2, *a3;
    22472663@{
    22482664  fprintf (stderr, "error: ");
     
    22522668
    22532669@noindent
    2254 In practice, this works on all machines, and it is much simpler than any
    2255 ``correct'' alternative.  Be sure @emph{not} to use a prototype
    2256 for such functions.
    2257 
    2258 However, avoid casting pointers to integers unless you really need to.
    2259 These assumptions really reduce portability, and in most programs they
    2260 are easy to avoid.  In the cases where casting pointers to integers is
    2261 essential---such as, a Lisp interpreter which stores type information as
    2262 well as an address in one word---it is ok to do so, but you'll have to
    2263 make explicit provisions to handle different word sizes.
     2670In practice, this works on all machines, since a pointer is generally
     2671the widest possible kind of argument; it is much simpler than any
     2672``correct'' alternative.  Be sure @emph{not} to use a prototype for such
     2673functions.
     2674
     2675If you have decided to use Standard C, then you can instead define
     2676@code{error} using @file{stdarg.h}, and pass the arguments along to
     2677@code{vfprintf}.
     2678
     2679@cindex casting pointers to integers
     2680Avoid casting pointers to integers if you can.  Such casts greatly
     2681reduce portability, and in most programs they are easy to avoid.  In the
     2682cases where casting pointers to integers is essential---such as, a Lisp
     2683interpreter which stores type information as well as an address in one
     2684word---you'll have to make explicit provisions to handle different word
     2685sizes.  You will also need to make provision for systems in which the
     2686normal range of addresses you can get from @code{malloc} starts far away
     2687from zero.
    22642688
    22652689@node System Functions
    22662690@section Calling System Functions
    2267 
    2268 C implementations differ substantially.  @sc{ansi} C reduces but does not
    2269 eliminate the incompatibilities; meanwhile, many users wish to compile
    2270 GNU software with pre-@sc{ansi} compilers.  This chapter gives
    2271 recommendations for how to use the more or less standard C library
    2272 functions to avoid unnecessary loss of portability.
     2691@cindex library functions, and portability
     2692@cindex portability, and library functions
     2693
     2694C implementations differ substantially.  Standard C reduces but does
     2695not eliminate the incompatibilities; meanwhile, many GNU packages still
     2696support pre-standard compilers because this is not hard to do.  This
     2697chapter gives recommendations for how to use the more-or-less standard C
     2698library functions to avoid unnecessary loss of portability.
    22732699
    22742700@itemize @bullet
    22752701@item
    2276 Don't use the value of @code{sprintf}.  It returns the number of
     2702Don't use the return value of @code{sprintf}.  It returns the number of
    22772703characters written on some systems, but not on all systems.
     2704
     2705@item
     2706Be aware that @code{vfprintf} is not always available.
    22782707
    22792708@item
     
    22822711status code; make sure it cannot ever return an undefined value.
    22832712
     2713@cindex declaration for system functions
    22842714@item
    22852715Don't declare system functions explicitly.
     
    22982728@item
    22992729If you must declare a system function, don't specify the argument types.
    2300 Use an old-style declaration, not an @sc{ansi} prototype.  The more you
     2730Use an old-style declaration, not a Standard C prototype.  The more you
    23012731specify about the function, the more likely a conflict.
    23022732
     
    23202750specific to those systems.
    23212751
     2752@cindex string library functions
    23222753@item
    23232754The string functions require special treatment.  Some Unix systems have
     
    23302761the string functions from the header file in the usual way.
    23312762
    2332 That causes less of a problem than you might think.  The newer @sc{ansi}
     2763That causes less of a problem than you might think.  The newer standard
    23332764string functions should be avoided anyway because many systems still
    23342765don't support them.  The string functions you can use are these:
     
    23602791You should pick a single pair of names and use it throughout your
    23612792program.  (Nowadays, it is better to choose @code{strchr} and
    2362 @code{strrchr} for new programs, since those are the standard @sc{ansi}
     2793@code{strrchr} for new programs, since those are the standard
    23632794names.)  Declare both of those names as functions returning @code{char
    23642795*}.  On systems which don't support those names, define them as macros
     
    23862817@node Internationalization
    23872818@section Internationalization
    2388 
     2819@cindex internationalization
     2820
     2821@pindex gettext
    23892822GNU has a library called GNU gettext that makes it easy to translate the
    23902823messages in a program into various languages.  You should use this
     
    24132846package---for example, @samp{fileutils} for the GNU file utilities.
    24142847
     2848@cindex message text, and internationalization
    24152849To enable gettext to work well, avoid writing code that makes
    24162850assumptions about the structure of words or sentences.  When you want
     
    24842918@node Mmap
    24852919@section Mmap
     2920@findex mmap
    24862921
    24872922Don't assume that @code{mmap} either works on all files or fails
     
    25002935@node Documentation
    25012936@chapter Documenting Programs
     2937@cindex documentation
     2938
     2939A GNU program should ideally come with full free documentation, adequate
     2940for both reference and tutorial purposes.  If the package can be
     2941programmed or extended, the documentation should cover programming or
     2942extending it, as well as just using it.
    25022943
    25032944@menu
    25042945* GNU Manuals::                 Writing proper manuals.
     2946* Doc Strings and Manuals::     Compiling doc strings doesn't make a manual.
    25052947* Manual Structure Details::    Specific structure conventions.
     2948* License for Manuals::         Writing the distribution terms for a manual.
     2949* Manual Credits::              Giving credit to documentation contributors.
     2950* Printed Manuals::             Mentioning the printed manual.
    25062951* NEWS File::                   NEWS files supplement manuals.
    25072952* Change Logs::                 Recording Changes
     
    25142959@section GNU Manuals
    25152960
    2516 The preferred way to document part of the GNU system is to write a
    2517 manual in the Texinfo formatting language.  See the Texinfo manual,
    2518 either the hardcopy, or the on-line version available through
    2519 @code{info} or the Emacs Info subsystem (@kbd{C-h i}).
     2961The preferred document format for the GNU system is the Texinfo
     2962formatting language.  Every GNU package should (ideally) have
     2963documentation in Texinfo both for reference and for learners.  Texinfo
     2964makes it possible to produce a good quality formatted book, using
     2965@TeX{}, and to generate an Info file.  It is also possible to generate
     2966HTML output from Texinfo source.  See the Texinfo manual, either the
     2967hardcopy, or the on-line version available through @code{info} or the
     2968Emacs Info subsystem (@kbd{C-h i}).
     2969
     2970Nowadays some other formats such as Docbook and Sgmltexi can be
     2971converted automatically into Texinfo.  It is ok to produce the Texinfo
     2972documentation by conversion this way, as long as it gives good results.
    25202973
    25212974Programmers often find it most natural to structure the documentation
     
    25462999together, we can make the whole subject clearer.
    25473000
    2548 The manual which discusses a program should document all of the
    2549 program's command-line options and all of its commands.  It should give
    2550 examples of their use.  But don't organize the manual as a list of
     3001The manual which discusses a program should certainly document all of
     3002the program's command-line options and all of its commands.  It should
     3003give examples of their use.  But don't organize the manual as a list of
    25513004features.  Instead, organize it logically, by subtopics.  Address the
    25523005questions that a user will ask when thinking about the job that the
     
    25583011should give a good introduction to a beginner reading through from the
    25593012start, and should also provide all the details that hackers want.
     3013The Bison manual is a good example of this---please take a look at it
     3014to see what we mean.
    25603015
    25613016That is not as hard as it first sounds.  Arrange each chapter as a
     
    25713026Bison manual provides a good example of how to do this.
    25723027
     3028To serve as a reference, a manual should have an Index that list all the
     3029functions, variables, options, and important concepts that are part of
     3030the program.  One combined Index should do for a short manual, but
     3031sometimes for a complex package it is better to use multiple indices.
     3032The Texinfo manual includes advice on preparing good index entries, see
     3033@ref{Index Entries, , Making Index Entries, texinfo, The GNU Texinfo
     3034Manual}, and see @ref{Indexing Commands, , Defining the Entries of an
     3035Index, texinfo, The GNU Texinfo manual}.
     3036
    25733037Don't use Unix man pages as a model for how to write GNU documentation;
    25743038most of them are terse, badly structured, and give inadequate
    2575 explanation of the underlying concepts.  (There are, of course
    2576 exceptions.)  Also Unix man pages use a particular format which is
     3039explanation of the underlying concepts.  (There are, of course, some
     3040exceptions.)  Also, Unix man pages use a particular format which is
    25773041different from what we use in GNU manuals.
     3042
     3043Please include an email address in the manual for where to report
     3044bugs @emph{in the manual}.
    25783045
    25793046Please do not use the term ``pathname'' that is used in Unix
    25803047documentation; use ``file name'' (two words) instead.  We use the term
    2581 ``path'' only for search paths, which are lists of file names.
     3048``path'' only for search paths, which are lists of directory names.
    25823049
    25833050Please do not use the term ``illegal'' to refer to erroneous input to a
    25843051computer program.  Please use ``invalid'' for this, and reserve the term
    2585 ``illegal'' for violations of law.
     3052``illegal'' for activities punishable by law.
     3053
     3054@node Doc Strings and Manuals
     3055@section Doc Strings and Manuals
     3056
     3057Some programming systems, such as Emacs, provide a documentation string
     3058for each function, command or variable.  You may be tempted to write a
     3059reference manual by compiling the documentation strings and writing a
     3060little additional text to go around them---but you must not do it.  That
     3061approach is a fundamental mistake.  The text of well-written
     3062documentation strings will be entirely wrong for a manual.
     3063
     3064A documentation string needs to stand alone---when it appears on the
     3065screen, there will be no other text to introduce or explain it.
     3066Meanwhile, it can be rather informal in style.
     3067
     3068The text describing a function or variable in a manual must not stand
     3069alone; it appears in the context of a section or subsection.  Other text
     3070at the beginning of the section should explain some of the concepts, and
     3071should often make some general points that apply to several functions or
     3072variables.  The previous descriptions of functions and variables in the
     3073section will also have given information about the topic.  A description
     3074written to stand alone would repeat some of that information; this
     3075redundance looks bad.  Meanwhile, the informality that is acceptable in
     3076a documentation string is totally unacceptable in a manual.
     3077
     3078The only good way to use documentation strings in writing a good manual
     3079is to use them as a source of information for writing good text.
    25863080
    25873081@node Manual Structure Details
    25883082@section Manual Structure Details
     3083@cindex manual structure
    25893084
    25903085The title page of the manual should state the version of the programs or
     
    25943089number for the manual in both of these places.
    25953090
    2596 Each program documented in the manual should should have a node named
     3091Each program documented in the manual should have a node named
    25973092@samp{@var{program} Invocation} or @samp{Invoking @var{program}}.  This
    25983093node (together with its subnodes, if any) should describe the program's
     
    26063101as the node for this purpose, regardless of the node's actual name.
    26073102
    2608 There will be automatic features for specifying a program name and
    2609 quickly reading just this part of its manual.
     3103The @samp{--usage} feature of the Info reader looks for such a node
     3104or menu item in order to find the relevant text, so it is essential
     3105for every Texinfo file to have one.
    26103106
    26113107If one manual describes several programs, it should have such a node for
    2612 each program described.
     3108each program described in the manual.
     3109
     3110@node License for Manuals
     3111@section License for Manuals
     3112@cindex license for manuals
     3113
     3114Please use the GNU Free Documentation License for all GNU manuals that
     3115are more than a few pages long.  Likewise for a collection of short
     3116documents---you only need one copy of the GNU FDL for the whole
     3117collection.  For a single short document, you can use a very permissive
     3118non-copyleft license, to avoid taking up space with a long license.
     3119
     3120See @uref{http://www.gnu.org/copyleft/fdl-howto.html} for more explanation
     3121of how to employ the GFDL.
     3122
     3123Note that it is not obligatory to include a copy of the GNU GPL or GNU
     3124LGPL in a manual whose license is neither the GPL nor the LGPL.  It can
     3125be a good idea to include the program's license in a large manual; in a
     3126short manual, whose size would be increased considerably by including
     3127the program's license, it is probably better not to include it.
     3128
     3129@node Manual Credits
     3130@section Manual Credits
     3131@cindex credits for manuals
     3132
     3133Please credit the principal human writers of the manual as the authors,
     3134on the title page of the manual.  If a company sponsored the work, thank
     3135the company in a suitable place in the manual, but do not cite the
     3136company as an author.
     3137
     3138@node Printed Manuals
     3139@section Printed Manuals
     3140
     3141The FSF publishes some GNU manuals in printed form.  To encourage sales
     3142of these manuals, the on-line versions of the manual should mention at
     3143the very start that the printed manual is available and should point at
     3144information for getting it---for instance, with a link to the page
     3145@url{http://www.gnu.org/order/order.html}.  This should not be included
     3146in the printed manual, though, because there it is redundant.
     3147
     3148It is also useful to explain in the on-line forms of the manual how the
     3149user can print out the manual from the sources.
    26133150
    26143151@node NEWS File
    26153152@section The NEWS File
     3153@cindex @file{NEWS} file
    26163154
    26173155In addition to its manual, the package should have a file named
     
    26283166@node Change Logs
    26293167@section Change Logs
     3168@cindex change logs
    26303169
    26313170Keep a change log to describe all the changes made to program source
     
    26423181* Simple Changes::             
    26433182* Conditional Changes::         
     3183* Indicating the Part Changed::
    26443184@end menu
    26453185
     
    26603200Another alternative is to record change log information with a version
    26613201control system such as RCS or CVS.  This can be converted automatically
    2662 to a @file{ChangeLog} file.
     3202to a @file{ChangeLog} file using @code{rcs2log}; in Emacs, the command
     3203@kbd{C-x v a} (@code{vc-update-change-log}) does the job.
    26633204
    26643205There's no need to describe the full purpose of the changes or how they
     
    26813222@node Style of Change Logs
    26823223@subsection Style of Change Logs
    2683 
    2684 Here are some examples of change log entries:
    2685 
    2686 @example
     3224@cindex change logs, style
     3225
     3226Here are some simple examples of change log entries, starting with the
     3227header line that says who made the change and when, followed by
     3228descriptions of specific changes.  (These examples are drawn from Emacs
     3229and GCC.)
     3230
     3231@example
     32321998-08-17  Richard Stallman  <rms@@gnu.org>
     3233
    26873234* register.el (insert-register): Return nil.
    26883235(jump-to-register): Likewise.
     
    27153262name and the asterisk when successive entries are in the same file.
    27163263
     3264Break long lists of function names by closing continued lines with
     3265@samp{)}, rather than @samp{,}, and opening the continuation with
     3266@samp{(} as in this example:
     3267
     3268@example
     3269* keyboard.c (menu_bar_items, tool_bar_items)
     3270(Fexecute_extended_command): Deal with `keymap' property.
     3271@end example
     3272
    27173273@node Simple Changes
    27183274@subsection Simple Changes
     
    27223278
    27233279When you change the calling sequence of a function in a simple fashion,
    2724 and you change all the callers of the function, there is no need to make
    2725 individual entries for all the callers that you changed.  Just write in
    2726 the entry for the function being called, ``All callers changed.''
     3280and you change all the callers of the function to use the new calling
     3281sequence, there is no need to make individual entries for all the
     3282callers that you changed.  Just write in the entry for the function
     3283being called, ``All callers changed''---like this:
    27273284
    27283285@example
     
    27443301@node Conditional Changes
    27453302@subsection Conditional Changes
     3303@cindex conditional changes, and change logs
     3304@cindex change logs, conditional changes
    27463305
    27473306C programs often contain compile-time @code{#if} conditionals.  Many
     
    27833342@end example
    27843343
     3344@node Indicating the Part Changed
     3345@subsection Indicating the Part Changed
     3346
     3347Indicate the part of a function which changed by using angle brackets
     3348enclosing an indication of what the changed part does.  Here is an entry
     3349for a change in the part of the function @code{sh-while-getopts} that
     3350deals with @code{sh} commands:
     3351
     3352@example
     3353* progmodes/sh-script.el (sh-while-getopts) <sh>: Handle case that
     3354user-specified option string is empty.
     3355@end example
     3356
     3357
    27853358@node Man Pages
    27863359@section Man Pages
     3360@cindex man pages
    27873361
    27883362In the GNU project, man pages are secondary.  It is not necessary or
     
    28313405@node Managing Releases
    28323406@chapter The Release Process
     3407@cindex releasing
    28333408
    28343409Making a release is more than just bundling up your source files in a
     
    28423417@menu
    28433418* Configuration::               How Configuration Should Work
    2844 * Makefile Conventions::        Makefile Conventions
     3419* Makefile Conventions::        Makefile Conventions
    28453420* Releases::                    Making Releases
    28463421@end menu
     
    28483423@node Configuration
    28493424@section How Configuration Should Work
    2850 
     3425@cindex program configuration
     3426
     3427@pindex configure
    28513428Each GNU distribution should come with a shell script named
    28523429@code{configure}.  This script is given arguments which describe the
     
    29163493would be a valid alias.  For many programs, @samp{vax-dec-ultrix} would
    29173494be an alias for @samp{vax-dec-bsd}, simply because the differences
    2918 between Ultrix and @sc{BSD} are rarely noticeable, but a few programs
     3495between Ultrix and @sc{bsd} are rarely noticeable, but a few programs
    29193496might need to distinguish them.
    29203497@c Real 4.4BSD now runs on some Suns.
     
    29233500as a subroutine to validate system types and canonicalize aliases.
    29243501
     3502@cindex optional features, configure-time
    29253503Other options are permitted to specify in more detail the software
    29263504or hardware present on the machine, and include or exclude optional
     
    29483526@c @samp{no} should omit @var{package}, if it is used by default.
    29493527
    2950 Possible values of @var{package} include 
     3528Possible values of @var{package} include
    29513529@samp{gnu-as} (or @samp{gas}), @samp{gnu-ld}, @samp{gnu-libc},
    29523530@samp{gdb},
    2953 @samp{x}, 
     3531@samp{x},
    29543532and
    29553533@samp{x-toolkit}.
     
    29583536find certain files.  That is outside the scope of what @samp{--with}
    29593537options are for.
    2960 
    2961 @item --nfp
    2962 The target machine has no floating point processor.
    2963 
    2964 @item --gas
    2965 The target machine assembler is GAS, the GNU assembler.
    2966 This is obsolete; users should use @samp{--with-gnu-as} instead.
    2967 
    2968 @item --x
    2969 The target machine has the X Window System installed.
    2970 This is obsolete; users should use @samp{--with-x} instead.
    29713538@end table
    29723539
     
    29843551have idiosyncratic configuration options.
    29853552
    2986 Packages that perform part of the compilation process may support cross-compilation.
    2987 In such a case, the host and target machines for the program may be
    2988 different.  The @code{configure} script should normally treat the
    2989 specified type of system as both the host and the target, thus producing
    2990 a program which works for the same type of machine that it runs on.
    2991 
    2992 The way to build a cross-compiler, cross-assembler, or what have you, is
    2993 to specify the option @samp{--host=@var{hosttype}} when running
    2994 @code{configure}.  This specifies the host system without changing the
    2995 type of target system.  The syntax for @var{hosttype} is the same as
    2996 described above.
     3553Packages that perform part of the compilation process may support
     3554cross-compilation.  In such a case, the host and target machines for the
     3555program may be different.
     3556
     3557The @code{configure} script should normally treat the specified type of
     3558system as both the host and the target, thus producing a program which
     3559works for the same type of machine that it runs on.
     3560
     3561To configure a cross-compiler, cross-assembler, or what have you, you
     3562should specify a target different from the host, using the configure
     3563option @samp{--target=@var{targettype}}.  The syntax for
     3564@var{targettype} is the same as for the host type.  So the command would
     3565look like this:
     3566
     3567@example
     3568./configure @var{hosttype} --target=@var{targettype}
     3569@end example
     3570
     3571Programs for which cross-operation is not meaningful need not accept the
     3572@samp{--target} option, because configuring an entire operating system for
     3573cross-operation is not a meaningful operation.
    29973574
    29983575Bootstrapping a cross-compiler requires compiling it on a machine other
    29993576than the host it will run on.  Compilation packages accept a
    3000 configuration option @samp{--build=@var{hosttype}} for specifying the
    3001 configuration on which you will compile them, in case that is different
    3002 from the host.
    3003 
    3004 Programs for which cross-operation is not meaningful need not accept the
    3005 @samp{--host} option, because configuring an entire operating system for
    3006 cross-operation is not a meaningful thing.
     3577configuration option @samp{--build=@var{buildtype}} for specifying the
     3578configuration on which you will compile them, but the configure script
     3579should normally guess the build machine type (using
     3580@file{config.guess}), so this option is probably not necessary.  The
     3581host and target types normally default from the build type, so in
     3582bootstrapping a cross-compiler you must specify them both explicitly.
    30073583
    30083584Some programs have ways of configuring themselves automatically.  If
     
    30193595@node Releases
    30203596@section Making Releases
     3597@cindex packaging
    30213598
    30223599Package the distribution of @code{Foo version 69.96} up in a gzipped tar
     
    30303607and never changed automatically; non-source files are produced from
    30313608source files by programs under the control of the Makefile.
     3609
     3610@cindex @file{README} file
     3611The distribution should contain a file named @file{README} which gives
     3612the name of the package, and a general description of what it does.  It
     3613is also good to explain the purpose of each of the first-level
     3614subdirectories in the package, if there are any.  The @file{README} file
     3615should either state the version number of the package, or refer to where
     3616in the package it can be found.
     3617
     3618The @file{README} file should refer to the file @file{INSTALL}, which
     3619should contain an explanation of the installation procedure.
     3620
     3621The @file{README} file should also refer to the file which contains the
     3622copying conditions.  The GNU GPL, if used, should be in a file called
     3623@file{COPYING}.  If the GNU LGPL is used, it should be in a file called
     3624@file{COPYING.LIB}.
    30323625
    30333626Naturally, all the source files must be in the distribution.  It is okay
     
    30553648characters long.  Likewise, no file created by building the program
    30563649should have a name longer than 14 characters.  The reason for this is
    3057 that some systems adhere to a foolish interpretation of the POSIX
     3650that some systems adhere to a foolish interpretation of the @sc{posix}
    30583651standard, and refuse to open a longer name, rather than truncating as
    30593652they did in the past.
     
    30743667distinct.
    30753668
     3669@cindex @file{texinfo.tex}, in a distribution
    30763670Include in your distribution a copy of the @file{texinfo.tex} you used
    30773671to test print any @file{*.texinfo} or @file{*.texi} files.
     
    30833677other files to get.
    30843678
     3679@node References
     3680@chapter References to Non-Free Software and Documentation
     3681@cindex references to non-free material
     3682
     3683A GNU program should not recommend use of any non-free program.  We
     3684can't stop some people from writing proprietary programs, or stop
     3685other people from using them, but we can and should avoid helping to
     3686advertise them to new potential customers.  Proprietary software is a
     3687social and ethical problem, and the point of GNU is to solve that
     3688problem.
     3689
     3690When a non-free program or system is well known, you can mention it in
     3691passing---that is harmless, since users who might want to use it
     3692probably already know about it.  For instance, it is fine to explain
     3693how to build your package on top of some non-free operating system, or
     3694how to use it together with some widely used non-free program.
     3695
     3696However, you should give only the necessary information to help those
     3697who already use the non-free program to use your program with
     3698it---don't give, or refer to, any further information about the
     3699proprietary program, and don't imply that the proprietary program
     3700enhances your program, or that its existence is in any way a good
     3701thing.  The goal should be that people already using the proprietary
     3702program will get the advice they need about how to use your free
     3703program, while people who don't already use the proprietary program
     3704will not see anything to lead them to take an interest in it.
     3705
     3706If a non-free program or system is obscure in your program's domain,
     3707your program should not mention or support it at all, since doing so
     3708would tend to popularize the non-free program more than it popularizes
     3709your program.  (You cannot hope to find many additional users among
     3710the users of Foobar if the users of Foobar are few.)
     3711
     3712A GNU package should not refer the user to any non-free documentation
     3713for free software.  Free documentation that can be included in free
     3714operating systems is essential for completing the GNU system, so it is
     3715a major focus of the GNU Project; to recommend use of documentation
     3716that we are not allowed to use in GNU would undermine the efforts to
     3717get documentation that we can include.  So GNU packages should never
     3718recommend non-free documentation.
     3719
     3720@node Copying This Manual
     3721@appendix Copying This Manual
     3722
     3723@menu
     3724* GNU Free Documentation License::  License for copying this manual
     3725@end menu
     3726
     3727@include fdl.texi
     3728
     3729@node Index
     3730@unnumbered Index
     3731@printindex cp
     3732
    30853733@contents
    30863734
    30873735@bye
    3088 Local variables:
    3089 update-date-leading-regexp: "@c This date is automagically updated when you save this file:\n@set lastupdate "
    3090 update-date-trailing-regexp: ""
    3091 eval: (load "/gd/gnuorg/update-date.el")
    3092 eval: (add-hook 'write-file-hooks 'update-date)
    3093 End:
     3736@c Local variables:
     3737@c eval: (add-hook 'write-file-hooks 'time-stamp)
     3738@c time-stamp-start: "@set lastupdate "
     3739@c time-stamp-end: "$"
     3740@c time-stamp-format: "%:b %:d, %:y"
     3741@c compile-command: "make just-standards"
     3742@c End:
Note: See TracChangeset for help on using the changeset viewer.