| 1 | This is Info file standards.info, produced by Makeinfo version 1.67
|
|---|
| 2 | from the input file /home/bje/autoconf-2.13/standards.texi.
|
|---|
| 3 |
|
|---|
| 4 | START-INFO-DIR-ENTRY
|
|---|
| 5 | * Standards: (standards). GNU coding standards.
|
|---|
| 6 | END-INFO-DIR-ENTRY
|
|---|
| 7 |
|
|---|
| 8 | GNU Coding Standards Copyright (C) 1992, 1993, 1994, 1995, 1996,
|
|---|
| 9 | 1997, 1998 Free Software Foundation, Inc.
|
|---|
| 10 |
|
|---|
| 11 | Permission is granted to make and distribute verbatim copies of this
|
|---|
| 12 | manual provided the copyright notice and this permission notice are
|
|---|
| 13 | preserved on all copies.
|
|---|
| 14 |
|
|---|
| 15 | Permission is granted to copy and distribute modified versions of
|
|---|
| 16 | this manual under the conditions for verbatim copying, provided that
|
|---|
| 17 | the entire resulting derived work is distributed under the terms of a
|
|---|
| 18 | permission notice identical to this one.
|
|---|
| 19 |
|
|---|
| 20 | Permission is granted to copy and distribute translations of this
|
|---|
| 21 | manual into another language, under the above conditions for modified
|
|---|
| 22 | versions, except that this permission notice may be stated in a
|
|---|
| 23 | translation approved by the Free Software Foundation.
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | File: standards.info, Node: Top, Next: Preface, Prev: (dir), Up: (dir)
|
|---|
| 27 |
|
|---|
| 28 | Version
|
|---|
| 29 | *******
|
|---|
| 30 |
|
|---|
| 31 | Last updated August 26, 1998.
|
|---|
| 32 |
|
|---|
| 33 | * Menu:
|
|---|
| 34 |
|
|---|
| 35 | * Preface:: About the GNU Coding Standards
|
|---|
| 36 | * Intellectual Property:: Keeping Free Software Free
|
|---|
| 37 | * Design Advice:: General Program Design
|
|---|
| 38 | * Program Behavior:: Program Behavior for All Programs
|
|---|
| 39 | * Writing C:: Making The Best Use of C
|
|---|
| 40 | * Documentation:: Documenting Programs
|
|---|
| 41 | * Managing Releases:: The Release Process
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | File: standards.info, Node: Preface, Next: Intellectual Property, Prev: Top, Up: Top
|
|---|
| 45 |
|
|---|
| 46 | About the GNU Coding Standards
|
|---|
| 47 | ******************************
|
|---|
| 48 |
|
|---|
| 49 | The GNU Coding Standards were written by Richard Stallman and other
|
|---|
| 50 | GNU Project volunteers. Their purpose is to make the GNU system clean,
|
|---|
| 51 | consistent, and easy to install. This document can also be read as a
|
|---|
| 52 | guide to writing portable, robust and reliable programs. It focuses on
|
|---|
| 53 | programs written in C, but many of the rules and principles are useful
|
|---|
| 54 | even if you write in another programming language. The rules often
|
|---|
| 55 | state reasons for writing in a certain way.
|
|---|
| 56 |
|
|---|
| 57 | Corrections or suggestions for this document should be sent to
|
|---|
| 58 | <[email protected]>. If you make a suggestion, please include a suggested
|
|---|
| 59 | new wording for it; our time is limited. We prefer a context diff to
|
|---|
| 60 | the `standards.texi' or `make-stds.texi' files, but if you don't have
|
|---|
| 61 | those files, please mail your suggestion anyway.
|
|---|
| 62 |
|
|---|
| 63 | This release of the GNU Coding Standards was last updated August 26,
|
|---|
| 64 | 1998.
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | File: standards.info, Node: Intellectual Property, Next: Design Advice, Prev: Preface, Up: Top
|
|---|
| 68 |
|
|---|
| 69 | Keeping Free Software Free
|
|---|
| 70 | **************************
|
|---|
| 71 |
|
|---|
| 72 | This node discusses how you can make sure that GNU software remains
|
|---|
| 73 | unencumbered.
|
|---|
| 74 |
|
|---|
| 75 | * Menu:
|
|---|
| 76 |
|
|---|
| 77 | * Reading Non-Free Code:: Referring to Proprietary Programs
|
|---|
| 78 | * Contributions:: Accepting Contributions
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | File: standards.info, Node: Reading Non-Free Code, Next: Contributions, Up: Intellectual Property
|
|---|
| 82 |
|
|---|
| 83 | Referring to Proprietary Programs
|
|---|
| 84 | =================================
|
|---|
| 85 |
|
|---|
| 86 | Don't in any circumstances refer to Unix source code for or during
|
|---|
| 87 | your work on GNU! (Or to any other proprietary programs.)
|
|---|
| 88 |
|
|---|
| 89 | If you have a vague recollection of the internals of a Unix program,
|
|---|
| 90 | this does not absolutely mean you can't write an imitation of it, but
|
|---|
| 91 | do try to organize the imitation internally along different lines,
|
|---|
| 92 | because this is likely to make the details of the Unix version
|
|---|
| 93 | irrelevant and dissimilar to your results.
|
|---|
| 94 |
|
|---|
| 95 | For example, Unix utilities were generally optimized to minimize
|
|---|
| 96 | memory use; if you go for speed instead, your program will be very
|
|---|
| 97 | different. You could keep the entire input file in core and scan it
|
|---|
| 98 | there instead of using stdio. Use a smarter algorithm discovered more
|
|---|
| 99 | recently than the Unix program. Eliminate use of temporary files. Do
|
|---|
| 100 | it in one pass instead of two (we did this in the assembler).
|
|---|
| 101 |
|
|---|
| 102 | Or, on the contrary, emphasize simplicity instead of speed. For some
|
|---|
| 103 | applications, the speed of today's computers makes simpler algorithms
|
|---|
| 104 | adequate.
|
|---|
| 105 |
|
|---|
| 106 | Or go for generality. For example, Unix programs often have static
|
|---|
| 107 | tables or fixed-size strings, which make for arbitrary limits; use
|
|---|
| 108 | dynamic allocation instead. Make sure your program handles NULs and
|
|---|
| 109 | other funny characters in the input files. Add a programming language
|
|---|
| 110 | for extensibility and write part of the program in that language.
|
|---|
| 111 |
|
|---|
| 112 | Or turn some parts of the program into independently usable
|
|---|
| 113 | libraries. Or use a simple garbage collector instead of tracking
|
|---|
| 114 | precisely when to free memory, or use a new GNU facility such as
|
|---|
| 115 | obstacks.
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 | File: standards.info, Node: Contributions, Prev: Reading Non-Free Code, Up: Intellectual Property
|
|---|
| 119 |
|
|---|
| 120 | Accepting Contributions
|
|---|
| 121 | =======================
|
|---|
| 122 |
|
|---|
| 123 | If someone else sends you a piece of code to add to the program you
|
|---|
| 124 | are working on, we need legal papers to use it--the same sort of legal
|
|---|
| 125 | papers we will need to get from you. *Each* significant contributor to
|
|---|
| 126 | a program must sign some sort of legal papers in order for us to have
|
|---|
| 127 | clear title to the program. The main author alone is not enough.
|
|---|
| 128 |
|
|---|
| 129 | So, before adding in any contributions from other people, please tell
|
|---|
| 130 | us, so we can arrange to get the papers. Then wait until we tell you
|
|---|
| 131 | that we have received the signed papers, before you actually use the
|
|---|
| 132 | contribution.
|
|---|
| 133 |
|
|---|
| 134 | This applies both before you release the program and afterward. If
|
|---|
| 135 | you receive diffs to fix a bug, and they make significant changes, we
|
|---|
| 136 | need legal papers for that change.
|
|---|
| 137 |
|
|---|
| 138 | This also applies to comments and documentation files. For copyright
|
|---|
| 139 | law, comments and code are just text. Copyright applies to all kinds of
|
|---|
| 140 | text, so we need legal papers for all kinds.
|
|---|
| 141 |
|
|---|
| 142 | You don't need papers for changes of a few lines here or there, since
|
|---|
| 143 | they are not significant for copyright purposes. Also, you don't need
|
|---|
| 144 | papers if all you get from the suggestion is some ideas, not actual code
|
|---|
| 145 | which you use. For example, if you write a different solution to the
|
|---|
| 146 | problem, you don't need to get papers.
|
|---|
| 147 |
|
|---|
| 148 | We know this is frustrating; it's frustrating for us as well. But if
|
|---|
| 149 | you don't wait, you are going out on a limb--for example, what if the
|
|---|
| 150 | contributor's employer won't sign a disclaimer? You might have to take
|
|---|
| 151 | that code out again!
|
|---|
| 152 |
|
|---|
| 153 | The very worst thing is if you forget to tell us about the other
|
|---|
| 154 | contributor. We could be very embarrassed in court some day as a
|
|---|
| 155 | result.
|
|---|
| 156 |
|
|---|
| 157 | We have more detailed advice for maintainers of programs; if you have
|
|---|
| 158 | reached the stage of actually maintaining a program for GNU (whether
|
|---|
| 159 | released or not), please ask us for a copy.
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | File: standards.info, Node: Design Advice, Next: Program Behavior, Prev: Intellectual Property, Up: Top
|
|---|
| 163 |
|
|---|
| 164 | General Program Design
|
|---|
| 165 | **********************
|
|---|
| 166 |
|
|---|
| 167 | This node discusses some of the issues you should take into account
|
|---|
| 168 | when designing your program.
|
|---|
| 169 |
|
|---|
| 170 | * Menu:
|
|---|
| 171 |
|
|---|
| 172 | * Compatibility:: Compatibility with other implementations
|
|---|
| 173 | * Using Extensions:: Using non-standard features
|
|---|
| 174 | * ANSI C:: Using ANSI C features
|
|---|
| 175 | * Source Language:: Using languages other than C
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 | File: standards.info, Node: Compatibility, Next: Using Extensions, Up: Design Advice
|
|---|
| 179 |
|
|---|
| 180 | Compatibility with Other Implementations
|
|---|
| 181 | ========================================
|
|---|
| 182 |
|
|---|
| 183 | With occasional exceptions, utility programs and libraries for GNU
|
|---|
| 184 | should be upward compatible with those in Berkeley Unix, and upward
|
|---|
| 185 | compatible with ANSI C if ANSI C specifies their behavior, and upward
|
|---|
| 186 | compatible with POSIX if POSIX specifies their behavior.
|
|---|
| 187 |
|
|---|
| 188 | When these standards conflict, it is useful to offer compatibility
|
|---|
| 189 | modes for each of them.
|
|---|
| 190 |
|
|---|
| 191 | ANSI C and POSIX prohibit many kinds of extensions. Feel free to
|
|---|
| 192 | make the extensions anyway, and include a `--ansi', `--posix', or
|
|---|
| 193 | `--compatible' option to turn them off. However, if the extension has
|
|---|
| 194 | a significant chance of breaking any real programs or scripts, then it
|
|---|
| 195 | is not really upward compatible. Try to redesign its interface.
|
|---|
| 196 |
|
|---|
| 197 | Many GNU programs suppress extensions that conflict with POSIX if the
|
|---|
| 198 | environment variable `POSIXLY_CORRECT' is defined (even if it is
|
|---|
| 199 | defined with a null value). Please make your program recognize this
|
|---|
| 200 | variable if appropriate.
|
|---|
| 201 |
|
|---|
| 202 | When a feature is used only by users (not by programs or command
|
|---|
| 203 | files), and it is done poorly in Unix, feel free to replace it
|
|---|
| 204 | completely with something totally different and better. (For example,
|
|---|
| 205 | `vi' is replaced with Emacs.) But it is nice to offer a compatible
|
|---|
| 206 | feature as well. (There is a free `vi' clone, so we offer it.)
|
|---|
| 207 |
|
|---|
| 208 | Additional useful features not in Berkeley Unix are welcome.
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | File: standards.info, Node: Using Extensions, Next: ANSI C, Prev: Compatibility, Up: Design Advice
|
|---|
| 212 |
|
|---|
| 213 | Using Non-standard Features
|
|---|
| 214 | ===========================
|
|---|
| 215 |
|
|---|
| 216 | Many GNU facilities that already exist support a number of convenient
|
|---|
| 217 | extensions over the comparable Unix facilities. Whether to use these
|
|---|
| 218 | extensions in implementing your program is a difficult question.
|
|---|
| 219 |
|
|---|
| 220 | On the one hand, using the extensions can make a cleaner program.
|
|---|
| 221 | On the other hand, people will not be able to build the program unless
|
|---|
| 222 | the other GNU tools are available. This might cause the program to
|
|---|
| 223 | work on fewer kinds of machines.
|
|---|
| 224 |
|
|---|
| 225 | With some extensions, it might be easy to provide both alternatives.
|
|---|
| 226 | For example, you can define functions with a "keyword" `INLINE' and
|
|---|
| 227 | define that as a macro to expand into either `inline' or nothing,
|
|---|
| 228 | depending on the compiler.
|
|---|
| 229 |
|
|---|
| 230 | In general, perhaps it is best not to use the extensions if you can
|
|---|
| 231 | straightforwardly do without them, but to use the extensions if they
|
|---|
| 232 | are a big improvement.
|
|---|
| 233 |
|
|---|
| 234 | An exception to this rule are the large, established programs (such
|
|---|
| 235 | as Emacs) which run on a great variety of systems. Such programs would
|
|---|
| 236 | be broken by use of GNU extensions.
|
|---|
| 237 |
|
|---|
| 238 | Another exception is for programs that are used as part of
|
|---|
| 239 | compilation: anything that must be compiled with other compilers in
|
|---|
| 240 | order to bootstrap the GNU compilation facilities. If these require
|
|---|
| 241 | the GNU compiler, then no one can compile them without having them
|
|---|
| 242 | installed already. That would be no good.
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 | File: standards.info, Node: ANSI C, Next: Source Language, Prev: Using Extensions, Up: Design Advice
|
|---|
| 246 |
|
|---|
| 247 | ANSI C and pre-ANSI C
|
|---|
| 248 | =====================
|
|---|
| 249 |
|
|---|
| 250 | Do not ever use the "trigraph" feature of ANSI C.
|
|---|
| 251 |
|
|---|
| 252 | ANSI C is widespread enough now that it is ok to write new programs
|
|---|
| 253 | that use ANSI C features (and therefore will not work in non-ANSI
|
|---|
| 254 | compilers). And if a program is already written in ANSI C, there's no
|
|---|
| 255 | need to convert it to support non-ANSI compilers.
|
|---|
| 256 |
|
|---|
| 257 | However, it is easy to support non-ANSI compilers in most programs,
|
|---|
| 258 | so you might still consider doing so when you write a program. Instead
|
|---|
| 259 | of writing function definitions in ANSI prototype form,
|
|---|
| 260 |
|
|---|
| 261 | int
|
|---|
| 262 | foo (int x, int y)
|
|---|
| 263 | ...
|
|---|
| 264 |
|
|---|
| 265 | write the definition in pre-ANSI style like this,
|
|---|
| 266 |
|
|---|
| 267 | int
|
|---|
| 268 | foo (x, y)
|
|---|
| 269 | int x, y;
|
|---|
| 270 | ...
|
|---|
| 271 |
|
|---|
| 272 | and use a separate declaration to specify the argument prototype:
|
|---|
| 273 |
|
|---|
| 274 | int foo (int, int);
|
|---|
| 275 |
|
|---|
| 276 | You need such a declaration anyway, in a header file, to get the
|
|---|
| 277 | benefit of ANSI C prototypes in all the files where the function is
|
|---|
| 278 | called. And once you have it, you lose nothing by writing the function
|
|---|
| 279 | definition in the pre-ANSI style.
|
|---|
| 280 |
|
|---|
| 281 | If you don't know non-ANSI C, there's no need to learn it; just
|
|---|
| 282 | write in ANSI C.
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 | File: standards.info, Node: Source Language, Prev: ANSI C, Up: Design Advice
|
|---|
| 286 |
|
|---|
| 287 | Using Languages Other Than C
|
|---|
| 288 | ============================
|
|---|
| 289 |
|
|---|
| 290 | Using a language other than C is like using a non-standard feature:
|
|---|
| 291 | it will cause trouble for users. Even if GCC supports the other
|
|---|
| 292 | language, users may find it inconvenient to have to install the
|
|---|
| 293 | compiler for that other language in order to build your program. For
|
|---|
| 294 | example, if you write your program in C++, people will have to install
|
|---|
| 295 | the C++ compiler in order to compile your program. Thus, it is better
|
|---|
| 296 | if you write in C.
|
|---|
| 297 |
|
|---|
| 298 | But there are three situations when there is no disadvantage in using
|
|---|
| 299 | some other language:
|
|---|
| 300 |
|
|---|
| 301 | * It is okay to use another language if your program contains an
|
|---|
| 302 | interpreter for that language.
|
|---|
| 303 |
|
|---|
| 304 | For example, if your program links with GUILE, it is ok to write
|
|---|
| 305 | part of the program in Scheme or another language supported by
|
|---|
| 306 | GUILE.
|
|---|
| 307 |
|
|---|
| 308 | * It is okay to use another language in a tool specifically intended
|
|---|
| 309 | for use with that language.
|
|---|
| 310 |
|
|---|
| 311 | This is okay because the only people who want to build the tool
|
|---|
| 312 | will be those who have installed the other language anyway.
|
|---|
| 313 |
|
|---|
| 314 | * If an application is of interest to a narrow community, then
|
|---|
| 315 | perhaps it's not important if the application is inconvenient to
|
|---|
| 316 | install.
|
|---|
| 317 |
|
|---|
| 318 | C has one other advantage over C++ and other compiled languages: more
|
|---|
| 319 | people know C, so more people will find it easy to read and modify the
|
|---|
| 320 | program if it is written in C.
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 | File: standards.info, Node: Program Behavior, Next: Writing C, Prev: Design Advice, Up: Top
|
|---|
| 324 |
|
|---|
| 325 | Program Behavior for All Programs
|
|---|
| 326 | *********************************
|
|---|
| 327 |
|
|---|
| 328 | This node describes how to write robust software. It also describes
|
|---|
| 329 | general standards for error messages, the command line interface, and
|
|---|
| 330 | how libraries should behave.
|
|---|
| 331 |
|
|---|
| 332 | * Menu:
|
|---|
| 333 |
|
|---|
| 334 | * Semantics:: Writing robust programs
|
|---|
| 335 | * Libraries:: Library behavior
|
|---|
| 336 | * Errors:: Formatting error messages
|
|---|
| 337 | * User Interfaces:: Standards for command line interfaces
|
|---|
| 338 | * Option Table:: Table of long options.
|
|---|
| 339 | * Memory Usage:: When and how to care about memory needs
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 | File: standards.info, Node: Semantics, Next: Libraries, Up: Program Behavior
|
|---|
| 343 |
|
|---|
| 344 | Writing Robust Programs
|
|---|
| 345 | =======================
|
|---|
| 346 |
|
|---|
| 347 | Avoid arbitrary limits on the length or number of *any* data
|
|---|
| 348 | structure, including file names, lines, files, and symbols, by
|
|---|
| 349 | allocating all data structures dynamically. In most Unix utilities,
|
|---|
| 350 | "long lines are silently truncated". This is not acceptable in a GNU
|
|---|
| 351 | utility.
|
|---|
| 352 |
|
|---|
| 353 | Utilities reading files should not drop NUL characters, or any other
|
|---|
| 354 | nonprinting characters *including those with codes above 0177*. The
|
|---|
| 355 | only sensible exceptions would be utilities specifically intended for
|
|---|
| 356 | interface to certain types of printers that can't handle those
|
|---|
| 357 | characters.
|
|---|
| 358 |
|
|---|
| 359 | Check every system call for an error return, unless you know you
|
|---|
| 360 | wish to ignore errors. Include the system error text (from `perror' or
|
|---|
| 361 | equivalent) in *every* error message resulting from a failing system
|
|---|
| 362 | call, as well as the name of the file if any and the name of the
|
|---|
| 363 | utility. Just "cannot open foo.c" or "stat failed" is not sufficient.
|
|---|
| 364 |
|
|---|
| 365 | Check every call to `malloc' or `realloc' to see if it returned
|
|---|
| 366 | zero. Check `realloc' even if you are making the block smaller; in a
|
|---|
| 367 | system that rounds block sizes to a power of 2, `realloc' may get a
|
|---|
| 368 | different block if you ask for less space.
|
|---|
| 369 |
|
|---|
| 370 | In Unix, `realloc' can destroy the storage block if it returns zero.
|
|---|
| 371 | GNU `realloc' does not have this bug: if it fails, the original block
|
|---|
| 372 | is unchanged. Feel free to assume the bug is fixed. If you wish to
|
|---|
| 373 | run your program on Unix, and wish to avoid lossage in this case, you
|
|---|
| 374 | can use the GNU `malloc'.
|
|---|
| 375 |
|
|---|
| 376 | You must expect `free' to alter the contents of the block that was
|
|---|
| 377 | freed. Anything you want to fetch from the block, you must fetch before
|
|---|
| 378 | calling `free'.
|
|---|
| 379 |
|
|---|
| 380 | If `malloc' fails in a noninteractive program, make that a fatal
|
|---|
| 381 | error. In an interactive program (one that reads commands from the
|
|---|
| 382 | user), it is better to abort the command and return to the command
|
|---|
| 383 | reader loop. This allows the user to kill other processes to free up
|
|---|
| 384 | virtual memory, and then try the command again.
|
|---|
| 385 |
|
|---|
| 386 | Use `getopt_long' to decode arguments, unless the argument syntax
|
|---|
| 387 | makes this unreasonable.
|
|---|
| 388 |
|
|---|
| 389 | When static storage is to be written in during program execution, use
|
|---|
| 390 | explicit C code to initialize it. Reserve C initialized declarations
|
|---|
| 391 | for data that will not be changed.
|
|---|
| 392 |
|
|---|
| 393 | Try to avoid low-level interfaces to obscure Unix data structures
|
|---|
| 394 | (such as file directories, utmp, or the layout of kernel memory), since
|
|---|
| 395 | these are less likely to work compatibly. If you need to find all the
|
|---|
| 396 | files in a directory, use `readdir' or some other high-level interface.
|
|---|
| 397 | These will be supported compatibly by GNU.
|
|---|
| 398 |
|
|---|
| 399 | The preferred signal handling facilities are the BSD variant of
|
|---|
| 400 | `signal', and the POSIX `sigaction' function; the alternative USG
|
|---|
| 401 | `signal' interface is an inferior design.
|
|---|
| 402 |
|
|---|
| 403 | Nowadays, using the POSIX signal functions may be the easiest way to
|
|---|
| 404 | make a program portable. If you use `signal', then on GNU/Linux
|
|---|
| 405 | systems running GNU libc version 1, you should include `bsd/signal.h'
|
|---|
| 406 | instead of `signal.h', so as to get BSD behavior. It is up to you
|
|---|
| 407 | whether to support systems where `signal' has only the USG behavior, or
|
|---|
| 408 | give up on them.
|
|---|
| 409 |
|
|---|
| 410 | In error checks that detect "impossible" conditions, just abort.
|
|---|
| 411 | There is usually no point in printing any message. These checks
|
|---|
| 412 | indicate the existence of bugs. Whoever wants to fix the bugs will have
|
|---|
| 413 | to read the source code and run a debugger. So explain the problem with
|
|---|
| 414 | comments in the source. The relevant data will be in variables, which
|
|---|
| 415 | are easy to examine with the debugger, so there is no point moving them
|
|---|
| 416 | elsewhere.
|
|---|
|
|---|