Changeset 609 for branches/GNU/src/binutils/etc/standards.texi
- Timestamp:
- Aug 16, 2003, 6:59:22 PM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/binutils/etc/standards.texi
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.1.1.2
r608 r609 4 4 @settitle GNU Coding Standards 5 5 @c This date is automagically updated when you save this file: 6 @set lastupdate March 13, 19986 @set lastupdate February 14, 2002 7 7 @c %**end of header 8 8 … … 17 17 @c @setchapternewpage odd 18 18 @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 19 25 20 26 @c This is used by a cross ref in make-stds.texi … … 29 35 @ifinfo 30 36 GNU 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. 37 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. 38 39 Permission is granted to copy, distribute and/or modify this document 40 under the terms of the GNU Free Documentation License, Version 1.1 41 or any later version published by the Free Software Foundation; 42 with no Invariant Sections, with no 43 Front-Cover Texts, and with no Back-Cover Texts. 44 A copy of the license is included in the section entitled ``GNU 45 Free Documentation License''. 53 46 @end ifinfo 54 47 55 48 @titlepage 56 49 @title GNU Coding Standards 57 @author Richard Stallman 50 @author Richard Stallman, et al. 58 51 @author last updated @value{lastupdate} 59 52 @page 60 53 61 54 @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. 55 Copyright @copyright{} 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. 56 57 Permission is granted to copy, distribute and/or modify this document 58 under the terms of the GNU Free Documentation License, Version 1.1 59 or any later version published by the Free Software Foundation; 60 with no Invariant Sections, with no 61 Front-Cover Texts, and with no Back-Cover Texts. 62 A copy of the license is included in the section entitled ``GNU 63 Free Documentation License''. 77 64 @end titlepage 78 65 … … 86 73 @menu 87 74 * Preface:: About the GNU Coding Standards 88 * Intellectual Property::Keeping Free Software Free75 * Legal Issues:: Keeping Free Software Free 89 76 * Design Advice:: General Program Design 90 77 * Program Behavior:: Program Behavior for All Programs … … 92 79 * Documentation:: Documenting Programs 93 80 * 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 94 85 @end menu 95 86 … … 105 96 state reasons for writing in a certain way. 106 97 98 This 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 103 If you did not obtain this file directly from the GNU project and 104 recently, please check for a newer version. You can ftp the GNU 105 Coding Standards from any GNU FTP host in the directory 106 @file{/pub/gnu/standards/}. The GNU Coding Standards are available 107 there in several different formats: @file{standards.text}, 108 @file{standards.info}, and @file{standards.dvi}, as well as the 109 Texinfo ``source'' which is divided in two files: 110 @file{standards.texi} and @file{make-stds.texi}. The GNU Coding 111 Standards are also available on the GNU World Wide Web server: 112 @uref{http://www.gnu.org/prep/standards_toc.html}. 113 107 114 Corrections or suggestions for this document should be sent to 108 @email{ gnu@@gnu.org}. If you make a suggestion, please include a115 @email{bug-standards@@gnu.org}. If you make a suggestion, please include a 109 116 suggested new wording for it; our time is limited. We prefer a context 110 117 diff to the @file{standards.texi} or @file{make-stds.texi} files, but if 111 118 you don't have those files, please mail your suggestion anyway. 112 119 113 This release of the GNU Coding Standards was last updated 114 @value{lastupdate}. 115 116 @node Intellectual Property 120 These standards cover the minimum of what is important when writing a 121 GNU package. Likely, the needs for additional standards will come up. 122 Sometimes, you might suggest that such standards be added to this 123 document. If you think your standards would be generally useful, please 124 do suggest them. 125 126 You should also set standards for your package on many questions not 127 addressed or not firmly specified here. The most important point is to 128 be self-consistent---try to stick to the conventions you pick, and try 129 to document them as much as possible. That way, your program will be 130 more maintainable by others. 131 132 @node Legal Issues 117 133 @chapter Keeping Free Software Free 134 @cindex legal aspects 118 135 119 136 This @value{CHAPTER} discusses how you can make sure that GNU software 120 remains unencumbered.137 avoids legal difficulties, and other related issues. 121 138 122 139 @menu 123 140 * Reading Non-Free Code:: Referring to Proprietary Programs 124 141 * Contributions:: Accepting Contributions 142 * Trademarks:: How We Deal with Trademark Issues 125 143 @end menu 126 144 127 145 @node Reading Non-Free Code 128 146 @section Referring to Proprietary Programs 147 @cindex proprietary programs 148 @cindex avoiding proprietary code 129 149 130 150 Don't in any circumstances refer to Unix source code for or during … … 158 178 to free memory, or use a new GNU facility such as obstacks. 159 179 160 161 180 @node Contributions 162 181 @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 185 If the program you are working on is copyrighted by the Free Software 186 Foundation, then when someone else sends you a piece of code to add to 187 the program, we need legal papers to use it---just as we asked you to 188 sign papers initially. @emph{Each} person who makes a nontrivial 189 contribution to a program must sign some sort of legal papers in order 190 for us to have clear title to the program; the main author alone is not 169 191 enough. 170 192 … … 182 204 text, so we need legal papers for all kinds. 183 205 206 We know it is frustrating to ask for legal papers; it's frustrating for 207 us as well. But if you don't wait, you are going out on a limb---for 208 example, what if the contributor's employer won't sign a disclaimer? 209 You might have to take that code out again! 210 184 211 You don't need papers for changes of a few lines here or there, since 185 212 they are not significant for copyright purposes. Also, you don't need 186 213 papers 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! 214 which you use. For example, if someone send you one implementation, but 215 you write a different implementation of the same idea, you don't need to 216 get papers. 194 217 195 218 The very worst thing is if you forget to tell us about the other … … 201 224 released or not), please ask us for a copy. 202 225 226 @node Trademarks 227 @section Trademarks 228 @cindex trademarks 229 230 Please do not include any trademark acknowledgements in GNU software 231 packages or documentation. 232 233 Trademark acknowledgements are the statements that such-and-such is a 234 trademark of so-and-so. The GNU Project has no objection to the basic 235 idea of trademarks, but these acknowledgements feel like kowtowing, so 236 we don't use them. There is no legal requirement for them. 237 238 What is legally required, as regards other people's trademarks, is to 239 avoid using them in ways which a reader might read as naming or labeling 240 our 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 243 compiler''. The latter is meant to be short for the former, but it does 244 not explicitly state the relationship, so it could be misinterpreted as 245 using ``Objective C'' as a label for the compiler rather than for the 246 language. 247 203 248 @node Design Advice 204 249 @chapter General Program Design 250 @cindex program design 205 251 206 252 This @value{CHAPTER} discusses some of the issues you should take into 207 253 account when designing your program. 208 254 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 209 265 @menu 266 * Source Language:: Which languges to use. 210 267 * Compatibility:: Compatibility with other implementations 211 268 * Using Extensions:: Using non-standard features 212 * ANSI C:: Using ANSIC features213 * Source Language:: Using languages other than C269 * Standard C:: Using Standard C features 270 * Conditional Compilation:: Compiling Code Only If A Conditional is True 214 271 @end menu 272 273 @node Source Language 274 @section Which Languages to Use 275 @cindex programming languges 276 277 When you want to use a language that gets compiled and runs at high 278 speed, the best language to use is C. Using another language is like 279 using a non-standard feature: it will cause trouble for users. Even if 280 GCC supports the other language, users may find it inconvenient to have 281 to install the compiler for that other language in order to build your 282 program. For example, if you write your program in C++, people will 283 have to install the GNU C++ compiler in order to compile your program. 284 285 C has one other advantage over C++ and other compiled languages: more 286 people know C, so more people will find it easy to read and modify the 287 program if it is written in C. 288 289 So in general it is much better to use C, rather than the 290 comparable alternatives. 291 292 But there are two exceptions to that conclusion: 293 294 @itemize @bullet 295 @item 296 It is no problem to use another language to write a tool specifically 297 intended for use with that language. That is because the only people 298 who want to build the tool will be those who have installed the other 299 language anyway. 300 301 @item 302 If an application is of interest only to a narrow part of the community, 303 then the question of which language it is written in has less effect on 304 other people, so you may as well please yourself. 305 @end itemize 306 307 Many programs are designed to be extensible: they include an interpreter 308 for a language that is higher level than C. Often much of the program 309 is written in that language, too. The Emacs editor pioneered this 310 technique. 311 312 @cindex GUILE 313 The standard extensibility interpreter for GNU software is GUILE, which 314 implements the language Scheme (an especially clean and simple dialect 315 of Lisp). @uref{http://www.gnu.org/software/guile/}. We don't reject 316 programs written in other ``scripting languages'' such as Perl and 317 Python, but using GUILE is very important for the overall consistency of 318 the GNU system. 215 319 216 320 @node Compatibility 217 321 @section Compatibility with Other Implementations 322 @cindex compatibility with C and @sc{posix} standards 323 @cindex @sc{posix} compatibility 218 324 219 325 With occasional exceptions, utility programs and libraries for GNU 220 326 should be upward compatible with those in Berkeley Unix, and upward 221 compatible with @sc{ansi} C if @sc{ansi} C specifies their behavior, and222 upward compatible with @sc{POSIX} if @sc{POSIX} specifies their 223 behavior.327 compatible with Standard C if Standard C specifies their 328 behavior, and upward compatible with @sc{posix} if @sc{posix} specifies 329 their behavior. 224 330 225 331 When these standards conflict, it is useful to offer compatibility 226 332 modes for each of them. 227 333 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 335 Standard C and @sc{posix} prohibit many kinds of extensions. Feel 336 free to make the extensions anyway, and include a @samp{--ansi}, 230 337 @samp{--posix}, or @samp{--compatible} option to turn them off. 231 338 However, 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 339 programs or scripts, then it is not really upward compatible. So you 340 should try to redesign its interface to make it upward compatible. 341 342 @cindex @code{POSIXLY_CORRECT}, environment variable 343 Many GNU programs suppress extensions that conflict with @sc{posix} if the 236 344 environment variable @code{POSIXLY_CORRECT} is defined (even if it is 237 345 defined with a null value). Please make your program recognize this … … 244 352 feature as well. (There is a free @code{vi} clone, so we offer it.) 245 353 246 Additional useful features not in Berkeley Unix are welcome. 354 Additional useful features are welcome regardless of whether 355 there is any precedent for them. 247 356 248 357 @node Using Extensions 249 358 @section Using Non-standard Features 359 @cindex non-standard extensions 250 360 251 361 Many GNU facilities that already exist support a number of convenient … … 268 378 269 379 An 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, 380 Emacs) which run on a great variety of systems. Using GNU extensions in 381 such programs would make many users unhappy, so we don't do that. 382 383 Another exception is for programs that are used as part of compilation: 384 anything that must be compiled with other compilers in order to 385 bootstrap the GNU compilation facilities. If these require the GNU 386 compiler, then no one can compile them without having them installed 387 already. 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 393 1989 Standard C is widespread enough now that it is ok to use its 394 features in new programs. There is one exception: do not ever use the 395 ``trigraph'' feature of Standard C. 396 397 1999 Standard C is not widespread yet, so please do not require its 398 features in programs. It is ok to use its features if they are present. 399 400 However, it is easy to support pre-standard compilers in most programs, 401 so if you know how to do that, feel free. If a program you are 402 maintaining has such support, you should try to keep it working. 403 404 @cindex function prototypes 405 To support pre-standard C, instead of writing function definitions in 406 standard prototype form, 293 407 294 408 @example … … 299 413 300 414 @noindent 301 write the definition in pre- @sc{ansi}style like this,415 write the definition in pre-standard style like this, 302 416 303 417 @example … … 316 430 317 431 You 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. 432 of prototypes in all the files where the function is called. And once 433 you have the declaration, you normally lose nothing by writing the 434 function definition in the pre-standard style. 435 436 This technique does not work for integer types narrower than @code{int}. 437 If you think of an argument as being of a type narrower than @code{int}, 438 declare it as @code{int} instead. 439 440 There are a few special cases where this technique is hard to use. For 441 example, 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, 444 because @code{dev_t} is wider than @code{int} on some machines. There 445 is no type you can safely use on all machines in a non-standard 446 definition. The only way to support non-standard C and pass such an 447 argument is to check the width of @code{dev_t} using Autoconf and choose 448 the argument type accordingly. This may not be worth the trouble. 449 450 In order to support pre-standard compilers that do not recognize 451 prototypes, 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 465 When supporting configuration options already known when building your 466 program we prefer using @code{if (... )} over conditional compilation, 467 as in the former case the compiler is able to perform more extensive 468 checking of all possible code paths. 469 470 For example, please write 471 472 @smallexample 473 if (HAS_FOO) 474 ... 475 else 476 ... 477 @end smallexample 478 479 instead of: 480 481 @smallexample 482 #ifdef HAS_FOO 483 ... 484 #else 485 ... 486 #endif 487 @end smallexample 488 489 A modern compiler such as GCC will generate exactly the same code in 490 both cases, and we have been using similar techniques with good success 491 in several projects. 492 493 While this is not a silver bullet solving all portability problems, 494 following this policy would have saved the GCC project alone many person 495 hours if not days per year. 496 497 In the case of function-like macros like @code{REVERSIBLE_CC_MODE} in 498 GCC which cannot be simply used in @code{if( ...)} statements, there is 499 an 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 361 509 362 510 @node Program Behavior 363 511 @chapter Program Behavior for All Programs 364 512 365 This @value{CHAPTER} describes how to write robust software. It also366 describes general standards for error messages, the command line interface, 367 and how libraries should behave.513 This @value{CHAPTER} describes conventions for writing robust 514 software. It also describes general standards for error messages, the 515 command line interface, and how libraries should behave. 368 516 369 517 @menu … … 371 519 * Libraries:: Library behavior 372 520 * 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 375 525 * Memory Usage:: When and how to care about memory needs 526 * File Usage:: Which files to use, and where 376 527 @end menu 377 528 … … 379 530 @section Writing Robust Programs 380 531 532 @cindex arbitrary limits on data 381 533 Avoid arbitrary limits on the length or number of @emph{any} data 382 534 structure, including file names, lines, files, and symbols, by allocating … … 384 536 are silently truncated''. This is not acceptable in a GNU utility. 385 537 538 @cindex @code{NUL} characters 386 539 Utilities 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 540 nonprinting characters @emph{including those with codes above 0177}. 541 The only sensible exceptions would be utilities specifically intended 542 for interface to certain types of terminals or printers 543 that can't handle those characters. 544 Whenever possible, try to make programs work properly with 545 sequences of bytes that represent multibyte characters, using encodings 546 such as UTF-8 and others. 547 548 @cindex error messages 391 549 Check every system call for an error return, unless you know you wish to 392 550 ignore errors. Include the system error text (from @code{perror} or … … 396 554 sufficient. 397 555 556 @cindex @code{malloc} return value 557 @cindex memory allocation failure 398 558 Check every call to @code{malloc} or @code{realloc} to see if it 399 559 returned zero. Check @code{realloc} even if you are making the block … … 417 577 virtual memory, and then try the command again. 418 578 579 @cindex command-line arguments, decoding 419 580 Use @code{getopt_long} to decode arguments, unless the argument syntax 420 581 makes this unreasonable. … … 429 590 are less likely to work compatibly. If you need to find all the files 430 591 in 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 592 These are supported compatibly by GNU. 593 594 @cindex signal handling 595 The preferred signal handling facilities are the BSD variant of 596 @code{signal}, and the @sc{posix} @code{sigaction} function; the 597 alternative USG @code{signal} interface is an inferior design. 598 599 Nowadays, using the @sc{posix} signal functions may be the easiest way 600 to make a program portable. If you use @code{signal}, then on GNU/Linux 601 systems running GNU libc version 1, you should include 602 @file{bsd/signal.h} instead of @file{signal.h}, so as to get BSD 603 behavior. 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 437 607 In error checks that detect ``impossible'' conditions, just abort. 438 608 There is usually no point in printing any message. These checks … … 449 619 will see 0 as the status, and it will appear that the program succeeded. 450 620 621 @cindex temporary files 622 @cindex @code{TMPDIR} environment variable 451 623 If you make temporary files, check the @code{TMPDIR} environment 452 624 variable; if that variable is defined, use the specified directory 453 625 instead of @file{/tmp}. 454 626 627 In addition, be aware that there is a possible security problem when 628 creating temporary files in world-writable directories. In C, you can 629 avoid this problem by creating temporary files in this manner: 630 631 @example 632 fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600); 633 @end example 634 635 @noindent 636 or by using the @code{mkstemps} function from libiberty. 637 638 In bash, use @code{set -C} to avoid this problem. 639 455 640 @node Libraries 456 641 @section Library Behavior 642 @cindex libraries 457 643 458 644 Try to make library functions reentrant. If they need to do dynamic … … 474 660 475 661 External symbols that are not documented entry points for the user 476 should have names beginning with @samp{_}. The y should also contain477 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.662 should have names beginning with @samp{_}. The @samp{_} should be 663 followed by the chosen name prefix for the library, to prevent 664 collisions with other libraries. These can go in the same files with 665 user entry points if you like. 480 666 481 667 Static functions and variables can be used as you like and need not … … 484 670 @node Errors 485 671 @section Formatting Error Messages 672 @cindex formatting error messages 673 @cindex error messages, formatting 486 674 487 675 Error messages from compilers should look like this: … … 490 678 @var{source-file-name}:@var{lineno}: @var{message} 491 679 @end example 680 681 @noindent 682 If 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 689 Line numbers should start from 1 at the beginning of the file, and 690 column numbers should start from 1 at the beginning of the line. (Both 691 of these conventions are chosen for compatibility.) Calculate column 692 numbers assuming that space and all ASCII printing characters have 693 equal width, and assuming tab stops every 8 columns. 492 694 493 695 Error messages from other noninteractive programs should look like this: … … 506 708 @noindent 507 709 when there is no relevant source file. 710 711 If 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 508 716 509 717 In an interactive program (one that is reading commands from a … … 523 731 524 732 @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 527 737 Please don't make the behavior of a utility depend on the name used 528 738 to invoke it. It is useful sometimes to make a link to a utility … … 532 742 to select among the alternate behaviors. 533 743 744 @cindex output device and program's behavior 534 745 Likewise, please don't make the behavior of the program depend on the 535 746 type 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. 747 important principle of the system's design; do not compromise it merely 748 to save someone from typing an option now and then. (Variation in error 749 message syntax when using a terminal is ok, because that is a side issue 750 that people do not depend on.) 538 751 539 752 If you think one behavior is most useful when the output is to a … … 551 764 multi-column format. 552 765 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 771 When you write a program that provides a graphical user interface, 772 please make it work with X Windows and the GTK toolkit unless the 773 functionality specifically requires some alternative (for example, 774 ``displaying jpeg images while in console mode''). 775 776 In addition, please provide a command-line interface to control the 777 functionality. (In many cases, the graphical user interface can be a 778 separate program which invokes the command-line program.) This is 779 so that the same jobs can be done from scripts. 780 781 @cindex corba 782 @cindex gnome 783 Please also consider providing a CORBA interface (for use from GNOME), a 784 library interface (for use from C), and perhaps a keyboard-driven 785 console interface (for use by users from console mode). Once you are 786 doing the work to provide the functionality and the graphical interface, 787 these 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 794 It is a good idea to follow the @sc{posix} guidelines for the 554 795 command-line options of a program. The easiest way to do this is to use 555 796 @code{getopt} to parse them. Note that the GNU version of @code{getopt} 556 797 will normally permit options anywhere among the arguments unless the 557 special argument @samp{--} is used. This is not what @sc{ POSIX}798 special argument @samp{--} is used. This is not what @sc{posix} 558 799 specifies; it is a GNU extension. 559 800 801 @cindex long-named options 560 802 Please define long-named options that are equivalent to the 561 803 single-letter Unix-style options. We hope to make GNU more user … … 577 819 among GNU utilities, and fewer idiosyncracies for users to remember. 578 820 821 @cindex standard command-line options 579 822 All programs should support two standard options: @samp{--version} 580 823 and @samp{--help}. 581 824 582 825 @table @code 826 @cindex @samp{--version} option 583 827 @item --version 584 This option should direct the program to information about its name,828 This option should direct the program to print information about its name, 585 829 version, origin and legal status, all on standard output, and then exit 586 830 successfully. Other options and arguments should be ignored once this 587 831 is seen, and the program should not perform its normal function. 588 832 833 @cindex canonical name of a program 834 @cindex program's canonical name 589 835 The first line is meant to be easy for a program to parse; the version 590 836 number proper starts after the last space. In addition, it contains … … 659 905 line. 660 906 907 Translations of the above lines must preserve the validity of the 908 copyright notices (@pxref{Internationalization}). If the translation's 909 character set supports it, the @samp{(C)} should be replaced with the 910 copyright 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 919 Write the word ``Copyright'' exactly like that, in English. Do not 920 translate it into another language. International treaties recognize 921 the English word ``Copyright''; translations into other languages do not 922 have legal significance. 923 924 925 @cindex @samp{--help} option 661 926 @item --help 662 927 This option should output brief documentation for how to invoke the … … 665 930 not perform its normal function. 666 931 932 @cindex address for bug reports 933 @cindex bug reports 667 934 Near the end of the @samp{--help} option's output there should be a line 668 935 that says where to mail bug reports. It should have this format: … … 675 942 @node Option Table 676 943 @section Table of Long Options 944 @cindex long option names 945 @cindex table of long options 677 946 678 947 Here is a table of long options used by GNU programs. It is surely 679 948 incomplete, but we aim to list all the options that a new program might 680 949 want 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 their950 please send @email{bug-standards@@gnu.org} a list of them, with their 682 951 meanings, so we can update the table. 683 952 … … 739 1008 @samp{-n} in @code{wdiff}. 740 1009 1010 @item background 1011 For server programs, run in the background. 1012 741 1013 @item backward-search 742 1014 @samp{-B} in @code{ctags}. … … 862 1134 @item dereference-args 863 1135 @samp{-D} in @code{du}. 1136 1137 @item device 1138 Specify an I/O device (special file name). 864 1139 865 1140 @item diacritics … … 995 1270 @samp{-F} in @code{shar}. 996 1271 1272 @item foreground 1273 For server programs, run in the foreground; 1274 in other words, don't do anything special to run the server 1275 in the background. 1276 997 1277 @item format 998 1278 Used in @code{ls}, @code{time}, and @code{ptx}. … … 1039 1319 @item hide-control-chars 1040 1320 @samp{-q} in @code{ls}. 1321 1322 @item html 1323 In @code{makeinfo}, output HTML. 1041 1324 1042 1325 @item idle … … 1099 1382 @item info 1100 1383 @samp{-i}, @samp{-l}, and @samp{-m} in Finger. 1384 1385 @item init-file 1386 In some programs, specify the name of the file to read as the user's 1387 init file. 1101 1388 1102 1389 @item initial … … 1118 1405 @samp{-p} in @code{shar}. 1119 1406 1407 @item iso-8601 1408 Used in @code{date} 1409 1120 1410 @item jobs 1121 1411 @samp{-j} in Make. … … 1353 1643 @samp{-F} in @code{gprof}. 1354 1644 1645 @item options 1646 @samp{-o} in @code{getopt}, @code{fdlist}, @code{fdmount}, 1647 @code{fdmountd}, and @code{fdumount}. 1648 1355 1649 @item output 1356 1650 In various programs, specify the output file name. … … 1436 1730 @item prompt 1437 1731 @samp{-p} in @code{ed}. 1732 1733 @item proxy 1734 Specify an HTTP proxy. 1438 1735 1439 1736 @item query-user … … 1565 1862 @samp{-s} in @code{ls}. 1566 1863 1864 @item socket 1865 Specify a file descriptor for a network server to use for its socket, 1866 instead of opening and binding a new socket. This provides a way to 1867 run, in a nonpriveledged process, a server that normally needs a 1868 reserved port number. 1869 1567 1870 @item sort 1568 1871 Used in @code{ls}. … … 1662 1965 @item time 1663 1966 Used in @code{ls} and @code{touch}. 1967 1968 @item timeout 1969 Specify how long to wait before giving up on some operation. 1664 1970 1665 1971 @item to-stdout … … 1755 2061 @node Memory Usage 1756 2062 @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 2065 If a program typically uses just a few meg of memory, don't bother making any 1759 2066 effort to reduce memory usage. For example, if it is impractical for 1760 2067 other reasons to operate on files more than a few meg long, it is … … 1772 2079 core and give a fatal error if @code{malloc} returns zero. 1773 2080 2081 @node File Usage 2082 @section File Usage 2083 @cindex file usage 2084 2085 Programs should be prepared to operate when @file{/usr} and @file{/etc} 2086 are read-only file systems. Thus, if the program manages log files, 2087 lock files, backup files, score files, or any other files which are 2088 modified for internal purposes, these files should not be stored in 2089 @file{/usr} or @file{/etc}. 2090 2091 There are two exceptions. @file{/etc} is used to store system 2092 configuration information; it is reasonable for a program to modify 2093 files in @file{/etc} when its job is to update the system configuration. 2094 Also, if the user explicitly asks to modify one file in a directory, it 2095 is reasonable for the program to store other files in the same 2096 directory. 2097 1774 2098 @node Writing C 1775 2099 @chapter Making The Best Use of C … … 1782 2106 * Comments:: Commenting Your Work 1783 2107 * Syntactic Conventions:: Clean Use of C Constructs 1784 * Names:: Naming Variables and Functions2108 * Names:: Naming Variables, Functions, and Files 1785 2109 * System Portability:: Portability between different operating systems 1786 2110 * CPU Portability:: Supporting the range of CPU types … … 1792 2116 @node Formatting 1793 2117 @section Formatting Your Source Code 1794 2118 @cindex formatting source code 2119 2120 @cindex open brace 2121 @cindex braces, in C source 1795 2122 It is important to put the open-brace that starts the body of a C 1796 2123 function in column zero, and avoid putting any other open-brace or … … 1814 2141 1815 2142 @noindent 1816 or, if you want to use @sc{ansi} C, format the definition like this: 2143 or, if you want to use Standard C syntax, format the definition like 2144 this: 1817 2145 1818 2146 @example … … 1824 2152 @end example 1825 2153 1826 In @sc{ansi}C, if the arguments don't fit nicely on one line,2154 In Standard C, if the arguments don't fit nicely on one line, 1827 2155 split it like this: 1828 2156 … … 1834 2162 @end example 1835 2163 1836 For the body of the function, we prefer code formatted like this: 2164 The rest of this section gives our recommendations for other aspects of 2165 C formatting style, which is also the default style of the @code{indent} 2166 program 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 2173 We don't think of these recommendations as requirements, because it 2174 causes no problems for users if two different programs have different 2175 formatting styles. 2176 2177 But whatever style you use, please use it consistently, since a mixture 2178 of styles within one program tends to look ugly. If you are 2179 contributing changes to an existing program, please follow the style of 2180 that program. 2181 2182 For the body of the function, our recommended style looks like this: 1837 2183 1838 2184 @example … … 1850 2196 @end example 1851 2197 2198 @cindex spaces before open-paren 1852 2199 We find it easier to read a program when it has spaces before the 1853 2200 open-parentheses and after the commas. Especially after the commas. … … 1856 2203 before an operator, not after one. Here is the right way: 1857 2204 2205 @cindex expressions, splitting 1858 2206 @example 1859 2207 if (foo_this_is_long && bar > win (x, y, z) … … 1880 2228 Insert extra parentheses so that Emacs will indent the code properly. 1881 2229 For example, the following indentation looks nice if you do it by hand, 1882 but Emacs would mess it up:1883 2230 1884 2231 @example … … 1887 2234 @end example 1888 2235 1889 But adding a set of parentheses solves the problem: 2236 @noindent 2237 but Emacs would alter it. Adding a set of parentheses produces 2238 something that looks equally nice, and which Emacs will preserve: 1890 2239 1891 2240 @example … … 1904 2253 @end example 1905 2254 2255 @cindex formfeed 2256 @cindex control-L 1906 2257 Please use formfeed characters (control-L) to divide the program into 1907 2258 pages at logical places (but not within a function). It does not matter … … 1909 2260 page. The formfeeds should appear alone on lines by themselves. 1910 2261 1911 1912 2262 @node Comments 1913 2263 @section Commenting Your Work 2264 @cindex commenting 1914 2265 1915 2266 Every program should start with a comment saying briefly what it is for. … … 1963 2314 @end example 1964 2315 2316 @cindex conditionals, comments for 2317 @cindex @code{#endif}, commenting 1965 2318 Every @samp{#endif} should have a comment, except in the case of short 1966 2319 conditionals (just a few lines) that are not nested. The comment should … … 2004 2357 @node Syntactic Conventions 2005 2358 @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 2363 Please explicitly declare the types of all objects. For example, you 2364 should explicitly declare all arguments to functions, and you should 2365 declare functions to return @code{int} rather than omitting the 2366 @code{int}. 2367 2368 @cindex compiler warnings 2369 @cindex @samp{-Wall} compiler option 2370 Some programmers like to use the GCC @samp{-Wall} option, and change the 2371 code whenever it issues a warning. If you want to do this, then do. 2372 Other programmers prefer not to use @samp{-Wall}, because it gives 2373 warnings for valid and legitimate code which they do not want to change. 2374 If you want to do this, then do. The compiler should be your servant, 2375 not your master. 2009 2376 2010 2377 Declarations of external functions and functions to appear later in the … … 2014 2381 functions. 2015 2382 2383 @cindex temporary variables 2016 2384 It used to be common practice to use the same local variables (with 2017 2385 names like @code{tem}) over and over for different values within one … … 2025 2393 Don't use local variables or parameters that shadow global identifiers. 2026 2394 2395 @cindex multiple variables in a line 2027 2396 Don't declare multiple variables in one declaration that spans lines. 2028 2397 Start a new declaration on each line, instead. For example, instead … … 2125 2494 @end example 2126 2495 2496 @pindex lint 2127 2497 Don't make the program ugly to placate @code{lint}. Please don't insert any 2128 2498 casts to @code{void}. Zero without a cast is perfectly fine as a null 2129 2499 pointer constant, except when calling a varargs function. 2130 2500 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 2134 2505 The names of global variables and functions in a program serve as 2135 2506 comments of a sort. So don't choose terse names---instead, look for … … 2141 2512 one context, where (presumably) comments explain their purpose. 2142 2513 2514 Try to limit your use of abbreviations in symbol names. It is ok to 2515 make a few abbreviations, explain what they mean, and then use them 2516 frequently, but don't use lots of obscure abbreviations. 2517 2143 2518 Please use underscores to separate words in a name, so that the Emacs 2144 2519 word commands can be useful within them. Stick to lower case; reserve … … 2165 2540 constants. 2166 2541 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 2544 You might want to make sure that none of the file names would conflict 2545 the files were loaded onto an MS-DOS file system which shortens the 2546 names. You can use the program @code{doschk} to test for this. 2547 2548 Some GNU programs were designed to limit themselves to file names of 14 2549 characters or less, to avoid file name conflicts if they are read into 2550 older System V systems. Please preserve this feature in the existing 2551 GNU programs that have it, but there is no need to do this in new GNU 2552 programs. @code{doschk} also reports file names longer than 14 2553 characters. 2172 2554 2173 2555 @node System Portability 2174 2556 @section Portability between System Types 2557 @cindex portability, between system types 2175 2558 2176 2559 In the Unix world, ``portability'' refers to porting to different Unix … … 2179 2562 2180 2563 The 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 2564 compiled with the GNU C compiler, on various types of @sc{cpu}. So the 2565 kinds of portability that are absolutely necessary are quite limited. 2566 But it is important to support Linux-based GNU systems, since they 2567 are the form of GNU that is popular. 2568 2569 Beyond 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 2571 to. Supporting a variety of Unix-like systems is desirable, although 2572 not paramount. It is usually not too hard, so you may as well do it. 2573 But you don't have to consider it an obligation, if it does turn out to 2574 be hard. 2575 2576 @pindex autoconf 2191 2577 The easiest way to achieve portability to most Unix-like systems is to 2192 2578 use Autoconf. It's unlikely that your program needs to know more … … 2198 2584 when there is a higher-level alternative (@code{readdir}). 2199 2585 2586 @cindex non-@sc{posix} systems, and portability 2200 2587 As 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. 2588 Macintosh, VMS, and MVS, supporting them is often a lot of work. When 2589 that is the case, it is better to spend your time adding features that 2590 will be useful on GNU and GNU/Linux, rather than on supporting other 2591 incompatible systems. 2592 2593 It is a good idea to define the ``feature test macro'' 2594 @code{_GNU_SOURCE} when compiling your C files. When you compile on GNU 2595 or GNU/Linux, this will enable the declarations of GNU library extension 2596 functions, and that will usually give you a compiler error message if 2597 you 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 2599 to make the program more portable to other systems.) 2600 2601 But whether or not you use these GNU extensions, you should avoid 2602 using their names for any other meanings. Doing so would make it hard 2603 to move your code into other GNU programs. 2209 2604 2210 2605 @node CPU Portability 2211 2606 @section Portability between @sc{cpu}s 2212 2607 2608 @cindex data types, and portability 2609 @cindex portability, and data types 2213 2610 Even GNU systems will differ because of differences among @sc{cpu} 2214 2611 types---for example, difference in byte ordering and alignment … … 2218 2615 in GNU. 2219 2616 2617 Similarly, don't make any effort to cater to the possibility that 2618 @code{long} will be smaller than predefined types like @code{size_t}. 2619 For example, the following code is ok: 2620 2621 @example 2622 printf ("size = %lu\n", (unsigned long) sizeof array); 2623 printf ("diff = %ld\n", (long) (pointer2 - pointer1)); 2624 @end example 2625 2626 1989 Standard C requires this to work, and we know of only one 2627 counterexample: 64-bit programs on Microsoft Windows IA-64. We will 2628 leave it to those who want to port GNU programs to that environment 2629 to figure out how to do it. 2630 2631 Predefined file-size types like @code{off_t} are an exception: they are 2632 longer than @code{long} on many platforms, so code like the above won't 2633 work with them. One way to print an @code{off_t} value portably is to 2634 print its digits yourself, one by one. 2635 2220 2636 Don't assume that the address of an @code{int} object is also the 2221 2637 address of its least-significant byte. This is false on big-endian … … 2232 2648 pointers of various types, or between pointers and integers. On most 2233 2649 machines, 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 use2235 prototypes (conditionalized to be active only in @sc{ansi} C) to make 2236 t he code work on those systems.2650 there is a difference, all of them support Standard C prototypes, so you can 2651 use prototypes (perhaps conditionalized to be active only in Standard C) 2652 to make the code work on those systems. 2237 2653 2238 2654 In certain cases, it is ok to pass integer and pointer arguments … … 2244 2660 error (s, a1, a2, a3) 2245 2661 char *s; 2246 int a1, a2,a3;2662 char *a1, *a2, *a3; 2247 2663 @{ 2248 2664 fprintf (stderr, "error: "); … … 2252 2668 2253 2669 @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. 2670 In practice, this works on all machines, since a pointer is generally 2671 the widest possible kind of argument; it is much simpler than any 2672 ``correct'' alternative. Be sure @emph{not} to use a prototype for such 2673 functions. 2674 2675 If 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 2680 Avoid casting pointers to integers if you can. Such casts greatly 2681 reduce portability, and in most programs they are easy to avoid. In the 2682 cases where casting pointers to integers is essential---such as, a Lisp 2683 interpreter which stores type information as well as an address in one 2684 word---you'll have to make explicit provisions to handle different word 2685 sizes. You will also need to make provision for systems in which the 2686 normal range of addresses you can get from @code{malloc} starts far away 2687 from zero. 2264 2688 2265 2689 @node System Functions 2266 2690 @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 2694 C implementations differ substantially. Standard C reduces but does 2695 not eliminate the incompatibilities; meanwhile, many GNU packages still 2696 support pre-standard compilers because this is not hard to do. This 2697 chapter gives recommendations for how to use the more-or-less standard C 2698 library functions to avoid unnecessary loss of portability. 2273 2699 2274 2700 @itemize @bullet 2275 2701 @item 2276 Don't use the value of @code{sprintf}. It returns the number of2702 Don't use the return value of @code{sprintf}. It returns the number of 2277 2703 characters written on some systems, but not on all systems. 2704 2705 @item 2706 Be aware that @code{vfprintf} is not always available. 2278 2707 2279 2708 @item … … 2282 2711 status code; make sure it cannot ever return an undefined value. 2283 2712 2713 @cindex declaration for system functions 2284 2714 @item 2285 2715 Don't declare system functions explicitly. … … 2298 2728 @item 2299 2729 If you must declare a system function, don't specify the argument types. 2300 Use an old-style declaration, not a n @sc{ansi}prototype. The more you2730 Use an old-style declaration, not a Standard C prototype. The more you 2301 2731 specify about the function, the more likely a conflict. 2302 2732 … … 2320 2750 specific to those systems. 2321 2751 2752 @cindex string library functions 2322 2753 @item 2323 2754 The string functions require special treatment. Some Unix systems have … … 2330 2761 the string functions from the header file in the usual way. 2331 2762 2332 That causes less of a problem than you might think. The newer @sc{ansi}2763 That causes less of a problem than you might think. The newer standard 2333 2764 string functions should be avoided anyway because many systems still 2334 2765 don't support them. The string functions you can use are these: … … 2360 2791 You should pick a single pair of names and use it throughout your 2361 2792 program. (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 2363 2794 names.) Declare both of those names as functions returning @code{char 2364 2795 *}. On systems which don't support those names, define them as macros … … 2386 2817 @node Internationalization 2387 2818 @section Internationalization 2388 2819 @cindex internationalization 2820 2821 @pindex gettext 2389 2822 GNU has a library called GNU gettext that makes it easy to translate the 2390 2823 messages in a program into various languages. You should use this … … 2413 2846 package---for example, @samp{fileutils} for the GNU file utilities. 2414 2847 2848 @cindex message text, and internationalization 2415 2849 To enable gettext to work well, avoid writing code that makes 2416 2850 assumptions about the structure of words or sentences. When you want … … 2484 2918 @node Mmap 2485 2919 @section Mmap 2920 @findex mmap 2486 2921 2487 2922 Don't assume that @code{mmap} either works on all files or fails … … 2500 2935 @node Documentation 2501 2936 @chapter Documenting Programs 2937 @cindex documentation 2938 2939 A GNU program should ideally come with full free documentation, adequate 2940 for both reference and tutorial purposes. If the package can be 2941 programmed or extended, the documentation should cover programming or 2942 extending it, as well as just using it. 2502 2943 2503 2944 @menu 2504 2945 * GNU Manuals:: Writing proper manuals. 2946 * Doc Strings and Manuals:: Compiling doc strings doesn't make a manual. 2505 2947 * 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. 2506 2951 * NEWS File:: NEWS files supplement manuals. 2507 2952 * Change Logs:: Recording Changes … … 2514 2959 @section GNU Manuals 2515 2960 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}). 2961 The preferred document format for the GNU system is the Texinfo 2962 formatting language. Every GNU package should (ideally) have 2963 documentation in Texinfo both for reference and for learners. Texinfo 2964 makes it possible to produce a good quality formatted book, using 2965 @TeX{}, and to generate an Info file. It is also possible to generate 2966 HTML output from Texinfo source. See the Texinfo manual, either the 2967 hardcopy, or the on-line version available through @code{info} or the 2968 Emacs Info subsystem (@kbd{C-h i}). 2969 2970 Nowadays some other formats such as Docbook and Sgmltexi can be 2971 converted automatically into Texinfo. It is ok to produce the Texinfo 2972 documentation by conversion this way, as long as it gives good results. 2520 2973 2521 2974 Programmers often find it most natural to structure the documentation … … 2546 2999 together, we can make the whole subject clearer. 2547 3000 2548 The manual which discusses a program should document all of the2549 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 of3001 The manual which discusses a program should certainly document all of 3002 the program's command-line options and all of its commands. It should 3003 give examples of their use. But don't organize the manual as a list of 2551 3004 features. Instead, organize it logically, by subtopics. Address the 2552 3005 questions that a user will ask when thinking about the job that the … … 2558 3011 should give a good introduction to a beginner reading through from the 2559 3012 start, and should also provide all the details that hackers want. 3013 The Bison manual is a good example of this---please take a look at it 3014 to see what we mean. 2560 3015 2561 3016 That is not as hard as it first sounds. Arrange each chapter as a … … 2571 3026 Bison manual provides a good example of how to do this. 2572 3027 3028 To serve as a reference, a manual should have an Index that list all the 3029 functions, variables, options, and important concepts that are part of 3030 the program. One combined Index should do for a short manual, but 3031 sometimes for a complex package it is better to use multiple indices. 3032 The Texinfo manual includes advice on preparing good index entries, see 3033 @ref{Index Entries, , Making Index Entries, texinfo, The GNU Texinfo 3034 Manual}, and see @ref{Indexing Commands, , Defining the Entries of an 3035 Index, texinfo, The GNU Texinfo manual}. 3036 2573 3037 Don't use Unix man pages as a model for how to write GNU documentation; 2574 3038 most 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 is3039 explanation of the underlying concepts. (There are, of course, some 3040 exceptions.) Also, Unix man pages use a particular format which is 2577 3041 different from what we use in GNU manuals. 3042 3043 Please include an email address in the manual for where to report 3044 bugs @emph{in the manual}. 2578 3045 2579 3046 Please do not use the term ``pathname'' that is used in Unix 2580 3047 documentation; use ``file name'' (two words) instead. We use the term 2581 ``path'' only for search paths, which are lists of filenames.3048 ``path'' only for search paths, which are lists of directory names. 2582 3049 2583 3050 Please do not use the term ``illegal'' to refer to erroneous input to a 2584 3051 computer 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 3057 Some programming systems, such as Emacs, provide a documentation string 3058 for each function, command or variable. You may be tempted to write a 3059 reference manual by compiling the documentation strings and writing a 3060 little additional text to go around them---but you must not do it. That 3061 approach is a fundamental mistake. The text of well-written 3062 documentation strings will be entirely wrong for a manual. 3063 3064 A documentation string needs to stand alone---when it appears on the 3065 screen, there will be no other text to introduce or explain it. 3066 Meanwhile, it can be rather informal in style. 3067 3068 The text describing a function or variable in a manual must not stand 3069 alone; it appears in the context of a section or subsection. Other text 3070 at the beginning of the section should explain some of the concepts, and 3071 should often make some general points that apply to several functions or 3072 variables. The previous descriptions of functions and variables in the 3073 section will also have given information about the topic. A description 3074 written to stand alone would repeat some of that information; this 3075 redundance looks bad. Meanwhile, the informality that is acceptable in 3076 a documentation string is totally unacceptable in a manual. 3077 3078 The only good way to use documentation strings in writing a good manual 3079 is to use them as a source of information for writing good text. 2586 3080 2587 3081 @node Manual Structure Details 2588 3082 @section Manual Structure Details 3083 @cindex manual structure 2589 3084 2590 3085 The title page of the manual should state the version of the programs or … … 2594 3089 number for the manual in both of these places. 2595 3090 2596 Each program documented in the manual should shouldhave a node named3091 Each program documented in the manual should have a node named 2597 3092 @samp{@var{program} Invocation} or @samp{Invoking @var{program}}. This 2598 3093 node (together with its subnodes, if any) should describe the program's … … 2606 3101 as the node for this purpose, regardless of the node's actual name. 2607 3102 2608 There will be automatic features for specifying a program name and 2609 quickly reading just this part of its manual. 3103 The @samp{--usage} feature of the Info reader looks for such a node 3104 or menu item in order to find the relevant text, so it is essential 3105 for every Texinfo file to have one. 2610 3106 2611 3107 If one manual describes several programs, it should have such a node for 2612 each program described. 3108 each program described in the manual. 3109 3110 @node License for Manuals 3111 @section License for Manuals 3112 @cindex license for manuals 3113 3114 Please use the GNU Free Documentation License for all GNU manuals that 3115 are more than a few pages long. Likewise for a collection of short 3116 documents---you only need one copy of the GNU FDL for the whole 3117 collection. For a single short document, you can use a very permissive 3118 non-copyleft license, to avoid taking up space with a long license. 3119 3120 See @uref{http://www.gnu.org/copyleft/fdl-howto.html} for more explanation 3121 of how to employ the GFDL. 3122 3123 Note that it is not obligatory to include a copy of the GNU GPL or GNU 3124 LGPL in a manual whose license is neither the GPL nor the LGPL. It can 3125 be a good idea to include the program's license in a large manual; in a 3126 short manual, whose size would be increased considerably by including 3127 the 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 3133 Please credit the principal human writers of the manual as the authors, 3134 on the title page of the manual. If a company sponsored the work, thank 3135 the company in a suitable place in the manual, but do not cite the 3136 company as an author. 3137 3138 @node Printed Manuals 3139 @section Printed Manuals 3140 3141 The FSF publishes some GNU manuals in printed form. To encourage sales 3142 of these manuals, the on-line versions of the manual should mention at 3143 the very start that the printed manual is available and should point at 3144 information 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 3146 in the printed manual, though, because there it is redundant. 3147 3148 It is also useful to explain in the on-line forms of the manual how the 3149 user can print out the manual from the sources. 2613 3150 2614 3151 @node NEWS File 2615 3152 @section The NEWS File 3153 @cindex @file{NEWS} file 2616 3154 2617 3155 In addition to its manual, the package should have a file named … … 2628 3166 @node Change Logs 2629 3167 @section Change Logs 3168 @cindex change logs 2630 3169 2631 3170 Keep a change log to describe all the changes made to program source … … 2642 3181 * Simple Changes:: 2643 3182 * Conditional Changes:: 3183 * Indicating the Part Changed:: 2644 3184 @end menu 2645 3185 … … 2660 3200 Another alternative is to record change log information with a version 2661 3201 control system such as RCS or CVS. This can be converted automatically 2662 to a @file{ChangeLog} file. 3202 to 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. 2663 3204 2664 3205 There's no need to describe the full purpose of the changes or how they … … 2681 3222 @node Style of Change Logs 2682 3223 @subsection Style of Change Logs 2683 2684 Here are some examples of change log entries: 2685 2686 @example 3224 @cindex change logs, style 3225 3226 Here are some simple examples of change log entries, starting with the 3227 header line that says who made the change and when, followed by 3228 descriptions of specific changes. (These examples are drawn from Emacs 3229 and GCC.) 3230 3231 @example 3232 1998-08-17 Richard Stallman <rms@@gnu.org> 3233 2687 3234 * register.el (insert-register): Return nil. 2688 3235 (jump-to-register): Likewise. … … 2715 3262 name and the asterisk when successive entries are in the same file. 2716 3263 3264 Break 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 2717 3273 @node Simple Changes 2718 3274 @subsection Simple Changes … … 2722 3278 2723 3279 When 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.'' 3280 and you change all the callers of the function to use the new calling 3281 sequence, there is no need to make individual entries for all the 3282 callers that you changed. Just write in the entry for the function 3283 being called, ``All callers changed''---like this: 2727 3284 2728 3285 @example … … 2744 3301 @node Conditional Changes 2745 3302 @subsection Conditional Changes 3303 @cindex conditional changes, and change logs 3304 @cindex change logs, conditional changes 2746 3305 2747 3306 C programs often contain compile-time @code{#if} conditionals. Many … … 2783 3342 @end example 2784 3343 3344 @node Indicating the Part Changed 3345 @subsection Indicating the Part Changed 3346 3347 Indicate the part of a function which changed by using angle brackets 3348 enclosing an indication of what the changed part does. Here is an entry 3349 for a change in the part of the function @code{sh-while-getopts} that 3350 deals with @code{sh} commands: 3351 3352 @example 3353 * progmodes/sh-script.el (sh-while-getopts) <sh>: Handle case that 3354 user-specified option string is empty. 3355 @end example 3356 3357 2785 3358 @node Man Pages 2786 3359 @section Man Pages 3360 @cindex man pages 2787 3361 2788 3362 In the GNU project, man pages are secondary. It is not necessary or … … 2831 3405 @node Managing Releases 2832 3406 @chapter The Release Process 3407 @cindex releasing 2833 3408 2834 3409 Making a release is more than just bundling up your source files in a … … 2842 3417 @menu 2843 3418 * Configuration:: How Configuration Should Work 2844 * Makefile Conventions:: 3419 * Makefile Conventions:: Makefile Conventions 2845 3420 * Releases:: Making Releases 2846 3421 @end menu … … 2848 3423 @node Configuration 2849 3424 @section How Configuration Should Work 2850 3425 @cindex program configuration 3426 3427 @pindex configure 2851 3428 Each GNU distribution should come with a shell script named 2852 3429 @code{configure}. This script is given arguments which describe the … … 2916 3493 would be a valid alias. For many programs, @samp{vax-dec-ultrix} would 2917 3494 be an alias for @samp{vax-dec-bsd}, simply because the differences 2918 between Ultrix and @sc{ BSD} are rarely noticeable, but a few programs3495 between Ultrix and @sc{bsd} are rarely noticeable, but a few programs 2919 3496 might need to distinguish them. 2920 3497 @c Real 4.4BSD now runs on some Suns. … … 2923 3500 as a subroutine to validate system types and canonicalize aliases. 2924 3501 3502 @cindex optional features, configure-time 2925 3503 Other options are permitted to specify in more detail the software 2926 3504 or hardware present on the machine, and include or exclude optional … … 2948 3526 @c @samp{no} should omit @var{package}, if it is used by default. 2949 3527 2950 Possible values of @var{package} include 3528 Possible values of @var{package} include 2951 3529 @samp{gnu-as} (or @samp{gas}), @samp{gnu-ld}, @samp{gnu-libc}, 2952 3530 @samp{gdb}, 2953 @samp{x}, 3531 @samp{x}, 2954 3532 and 2955 3533 @samp{x-toolkit}. … … 2958 3536 find certain files. That is outside the scope of what @samp{--with} 2959 3537 options are for. 2960 2961 @item --nfp2962 The target machine has no floating point processor.2963 2964 @item --gas2965 The target machine assembler is GAS, the GNU assembler.2966 This is obsolete; users should use @samp{--with-gnu-as} instead.2967 2968 @item --x2969 The target machine has the X Window System installed.2970 This is obsolete; users should use @samp{--with-x} instead.2971 3538 @end table 2972 3539 … … 2984 3551 have idiosyncratic configuration options. 2985 3552 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. 3553 Packages that perform part of the compilation process may support 3554 cross-compilation. In such a case, the host and target machines for the 3555 program may be different. 3556 3557 The @code{configure} script should normally treat the specified type of 3558 system as both the host and the target, thus producing a program which 3559 works for the same type of machine that it runs on. 3560 3561 To configure a cross-compiler, cross-assembler, or what have you, you 3562 should specify a target different from the host, using the configure 3563 option @samp{--target=@var{targettype}}. The syntax for 3564 @var{targettype} is the same as for the host type. So the command would 3565 look like this: 3566 3567 @example 3568 ./configure @var{hosttype} --target=@var{targettype} 3569 @end example 3570 3571 Programs for which cross-operation is not meaningful need not accept the 3572 @samp{--target} option, because configuring an entire operating system for 3573 cross-operation is not a meaningful operation. 2997 3574 2998 3575 Bootstrapping a cross-compiler requires compiling it on a machine other 2999 3576 than 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. 3577 configuration option @samp{--build=@var{buildtype}} for specifying the 3578 configuration on which you will compile them, but the configure script 3579 should normally guess the build machine type (using 3580 @file{config.guess}), so this option is probably not necessary. The 3581 host and target types normally default from the build type, so in 3582 bootstrapping a cross-compiler you must specify them both explicitly. 3007 3583 3008 3584 Some programs have ways of configuring themselves automatically. If … … 3019 3595 @node Releases 3020 3596 @section Making Releases 3597 @cindex packaging 3021 3598 3022 3599 Package the distribution of @code{Foo version 69.96} up in a gzipped tar … … 3030 3607 and never changed automatically; non-source files are produced from 3031 3608 source files by programs under the control of the Makefile. 3609 3610 @cindex @file{README} file 3611 The distribution should contain a file named @file{README} which gives 3612 the name of the package, and a general description of what it does. It 3613 is also good to explain the purpose of each of the first-level 3614 subdirectories in the package, if there are any. The @file{README} file 3615 should either state the version number of the package, or refer to where 3616 in the package it can be found. 3617 3618 The @file{README} file should refer to the file @file{INSTALL}, which 3619 should contain an explanation of the installation procedure. 3620 3621 The @file{README} file should also refer to the file which contains the 3622 copying 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}. 3032 3625 3033 3626 Naturally, all the source files must be in the distribution. It is okay … … 3055 3648 characters long. Likewise, no file created by building the program 3056 3649 should have a name longer than 14 characters. The reason for this is 3057 that some systems adhere to a foolish interpretation of the POSIX3650 that some systems adhere to a foolish interpretation of the @sc{posix} 3058 3651 standard, and refuse to open a longer name, rather than truncating as 3059 3652 they did in the past. … … 3074 3667 distinct. 3075 3668 3669 @cindex @file{texinfo.tex}, in a distribution 3076 3670 Include in your distribution a copy of the @file{texinfo.tex} you used 3077 3671 to test print any @file{*.texinfo} or @file{*.texi} files. … … 3083 3677 other files to get. 3084 3678 3679 @node References 3680 @chapter References to Non-Free Software and Documentation 3681 @cindex references to non-free material 3682 3683 A GNU program should not recommend use of any non-free program. We 3684 can't stop some people from writing proprietary programs, or stop 3685 other people from using them, but we can and should avoid helping to 3686 advertise them to new potential customers. Proprietary software is a 3687 social and ethical problem, and the point of GNU is to solve that 3688 problem. 3689 3690 When a non-free program or system is well known, you can mention it in 3691 passing---that is harmless, since users who might want to use it 3692 probably already know about it. For instance, it is fine to explain 3693 how to build your package on top of some non-free operating system, or 3694 how to use it together with some widely used non-free program. 3695 3696 However, you should give only the necessary information to help those 3697 who already use the non-free program to use your program with 3698 it---don't give, or refer to, any further information about the 3699 proprietary program, and don't imply that the proprietary program 3700 enhances your program, or that its existence is in any way a good 3701 thing. The goal should be that people already using the proprietary 3702 program will get the advice they need about how to use your free 3703 program, while people who don't already use the proprietary program 3704 will not see anything to lead them to take an interest in it. 3705 3706 If a non-free program or system is obscure in your program's domain, 3707 your program should not mention or support it at all, since doing so 3708 would tend to popularize the non-free program more than it popularizes 3709 your program. (You cannot hope to find many additional users among 3710 the users of Foobar if the users of Foobar are few.) 3711 3712 A GNU package should not refer the user to any non-free documentation 3713 for free software. Free documentation that can be included in free 3714 operating systems is essential for completing the GNU system, so it is 3715 a major focus of the GNU Project; to recommend use of documentation 3716 that we are not allowed to use in GNU would undermine the efforts to 3717 get documentation that we can include. So GNU packages should never 3718 recommend 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 3085 3733 @contents 3086 3734 3087 3735 @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: -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.