📄 standards.texi
字号:
@samp{-p} in @code{diff}.@item show-ends@samp{-E} in @code{cat}.@item show-function-line@samp{-F} in @code{diff}.@item show-tabs@samp{-T} in @code{cat}.@item silentUsed in many programs to inhibit the usual output.@strong{Note:} every program accepting@samp{--silent} should accept @samp{--quiet} as a synonym.@item size@samp{-s} in @code{ls}.@item socketSpecify a file descriptor for a network server to use for its socket,instead of opening and binding a new socket. This provides a way torun, in a nonpriveledged process, a server that normally needs areserved port number.@item sortUsed in @code{ls}.@item source@samp{-W source} in @code{gawk}.@item sparse@samp{-S} in @code{tar}.@item speed-large-files@samp{-H} in @code{diff}.@item split-at@samp{-E} in @code{unshar}.@item split-size-limit@samp{-L} in @code{shar}.@item squeeze-blank@samp{-s} in @code{cat}.@item start-delete@samp{-w} in @code{wdiff}.@item start-insert@samp{-y} in @code{wdiff}.@item starting-fileUsed in @code{tar} and @code{diff} to specify which file withina directory to start processing with.@item statistics@samp{-s} in @code{wdiff}.@item stdin-file-list@samp{-S} in @code{shar}.@item stop@samp{-S} in Make.@item strict@samp{-s} in @code{recode}.@item strip@samp{-s} in @code{install}.@item strip-all@samp{-s} in @code{strip}.@item strip-debug@samp{-S} in @code{strip}.@item submitter@samp{-s} in @code{shar}.@item suffix@samp{-S} in @code{cp}, @code{ln}, @code{mv}.@item suffix-format@samp{-b} in @code{csplit}.@item sum@samp{-s} in @code{gprof}.@item summarize@samp{-s} in @code{du}.@item symbolic@samp{-s} in @code{ln}.@item symbolsUsed in GDB and @code{objdump}.@item synclines@samp{-s} in @code{m4}.@item sysname@samp{-s} in @code{uname}.@item tabs@samp{-t} in @code{expand} and @code{unexpand}.@item tabsize@samp{-T} in @code{ls}.@item terminal@samp{-T} in @code{tput} and @code{ul}.@samp{-t} in @code{wdiff}.@item text@samp{-a} in @code{diff}.@item text-files@samp{-T} in @code{shar}.@item timeUsed in @code{ls} and @code{touch}.@item timeoutSpecify how long to wait before giving up on some operation.@item to-stdout@samp{-O} in @code{tar}.@item total@samp{-c} in @code{du}.@item touch@samp{-t} in Make, @code{ranlib}, and @code{recode}.@item trace@samp{-t} in @code{m4}.@item traditional@samp{-t} in @code{hello};@samp{-W traditional} in @code{gawk};@samp{-G} in @code{ed}, @code{m4}, and @code{ptx}.@item ttyUsed in GDB.@item typedefs@samp{-t} in @code{ctags}.@item typedefs-and-c++@samp{-T} in @code{ctags}.@item typeset-mode@samp{-t} in @code{ptx}.@item uncompress@samp{-z} in @code{tar}.@item unconditional@samp{-u} in @code{cpio}.@item undefine@samp{-U} in @code{m4}.@item undefined-only@samp{-u} in @code{nm}.@item update@samp{-u} in @code{cp}, @code{ctags}, @code{mv}, @code{tar}.@item usageUsed in @code{gawk}; same as @samp{--help}.@item uuencode@samp{-B} in @code{shar}.@item vanilla-operation@samp{-V} in @code{shar}.@item verbosePrint more information about progress. Many programs support this.@item verify@samp{-W} in @code{tar}.@item versionPrint the version number.@item version-control@samp{-V} in @code{cp}, @code{ln}, @code{mv}.@item vgrind@samp{-v} in @code{ctags}.@item volume@samp{-V} in @code{tar}.@item what-if@samp{-W} in Make.@item whole-size-limit@samp{-l} in @code{shar}.@item width@samp{-w} in @code{ls} and @code{ptx}.@item word-regexp@samp{-W} in @code{ptx}.@item writable@samp{-T} in @code{who}.@item zeros@samp{-z} in @code{gprof}.@end table@node Memory Usage@section Memory Usage@cindex memory usageIf a program typically uses just a few meg of memory, don't bother making anyeffort to reduce memory usage. For example, if it is impractical forother reasons to operate on files more than a few meg long, it isreasonable to read entire input files into core to operate on them.However, for programs such as @code{cat} or @code{tail}, that canusefully operate on very large files, it is important to avoid using atechnique that would artificially limit the size of files it can handle.If a program works by lines and could be applied to arbitraryuser-supplied input files, it should keep only a line in memory, becausethis is not very hard and users will want to be able to operate on inputfiles that are bigger than will fit in core all at once.If your program creates complicated data structures, just make them incore and give a fatal error if @code{malloc} returns zero.@node File Usage@section File Usage@cindex file usagePrograms should be prepared to operate when @file{/usr} and @file{/etc}are read-only file systems. Thus, if the program manages log files,lock files, backup files, score files, or any other files which aremodified for internal purposes, these files should not be stored in@file{/usr} or @file{/etc}.There are two exceptions. @file{/etc} is used to store systemconfiguration information; it is reasonable for a program to modifyfiles in @file{/etc} when its job is to update the system configuration.Also, if the user explicitly asks to modify one file in a directory, itis reasonable for the program to store other files in the samedirectory.@node Writing C@chapter Making The Best Use of CThis @value{CHAPTER} provides advice on how best to use the C languagewhen writing GNU software.@menu* Formatting:: Formatting Your Source Code* Comments:: Commenting Your Work* Syntactic Conventions:: Clean Use of C Constructs* Names:: Naming Variables, Functions, and Files* System Portability:: Portability between different operating systems* CPU Portability:: Supporting the range of CPU types* System Functions:: Portability and ``standard'' library functions* Internationalization:: Techniques for internationalization* Mmap:: How you can safely use @code{mmap}.@end menu@node Formatting@section Formatting Your Source Code@cindex formatting source code@cindex open brace@cindex braces, in C sourceIt is important to put the open-brace that starts the body of a Cfunction in column zero, and avoid putting any other open-brace oropen-parenthesis or open-bracket in column zero. Several tools lookfor open-braces in column zero to find the beginnings of C functions.These tools will not work on code not formatted that way.It is also important for function definitions to start the name of thefunction in column zero. This helps people to search for functiondefinitions, and may also help certain tools recognize them. Thus,the proper format is this:@examplestatic char *concat (s1, s2) /* Name starts in column zero here */ char *s1, *s2;@{ /* Open brace in column zero here */ @dots{}@}@end example@noindentor, if you want to use Standard C syntax, format the definition likethis:@examplestatic char *concat (char *s1, char *s2)@{ @dots{}@}@end exampleIn Standard C, if the arguments don't fit nicely on one line,split it like this:@exampleintlots_of_args (int an_integer, long a_long, short a_short, double a_double, float a_float)@dots{}@end exampleThe rest of this section gives our recommendations for other aspects ofC formatting style, which is also the default style of the @code{indent}program in version 1.2 and newer. It corresponds to the options@smallexample-nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2-ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -psl -nsc -nsob@end smallexampleWe don't think of these recommendations as requirements, because itcauses no problems for users if two different programs have differentformatting styles.But whatever style you use, please use it consistently, since a mixtureof styles within one program tends to look ugly. If you arecontributing changes to an existing program, please follow the style ofthat program.For the body of the function, our recommended style looks like this:@exampleif (x < foo (y, z)) haha = bar[4] + 5;else @{ while (z) @{ haha += foo (z, z); z--; @} return ++x + bar (); @}@end example@cindex spaces before open-parenWe find it easier to read a program when it has spaces before theopen-parentheses and after the commas. Especially after the commas.When you split an expression into multiple lines, split itbefore an operator, not after one. Here is the right way:@cindex expressions, splitting@exampleif (foo_this_is_long && bar > win (x, y, z) && remaining_condition)@end exampleTry to avoid having two operators of different precedence at the samelevel of indentation. For example, don't write this:@examplemode = (inmode[j] == VOIDmode || GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j]) ? outmode[j] : inmode[j]);@end exampleInstead, use extra parentheses so that the indentation shows the nesting:@examplemode = ((inmode[j] == VOIDmode || (GET_MODE_SIZE (outmode[j]) > GET_MODE_SIZE (inmode[j]))) ? outmode[j] : inmode[j]);@end exampleInsert extra parentheses so that Emacs will indent the code properly.For example, the following indentation looks nice if you do it by hand,@examplev = rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000;@end example@noindentbut Emacs would alter it. Adding a set of parentheses producessomething that looks equally nice, and which Emacs will preserve:@examplev = (rup->ru_utime.tv_sec*1000 + rup->ru_utime.tv_usec/1000 + rup->ru_stime.tv_sec*1000 + rup->ru_stime.tv_usec/1000);@end exampleFormat do-while statements like this:@exampledo @{ a = foo (a); @}while (a > 0);@end example@cindex formfeed@cindex control-LPlease use formfeed characters (control-L) to divide the program intopages at logical places (but not within a function). It does not matterjust how long the pages are, since they do not have to fit on a printedpage. The formfeeds should appear alone on lines by themselves.@node Comments@section Commenting Your Work@cindex commentingEvery program should start with a comment saying briefly what it is for.Example: @samp{fmt - filter for simple filling of text}.Please write the comments in a GNU program in English, because Englishis the one language that nearly all programmers in all countries canread. If you do not write English well, please write comments inEnglish as well as you can, then ask other people to help rewrite them.If you can't write comments in English, please find someone to work withyou and translate your comments into English.Please put a comment on each function saying what the function does,what sorts of arguments it gets, and what the possible values ofarguments mean and are used for. It is not necessary to duplicate inwords the meaning of the C argument declarations, if a C type is beingused in its customary fashion. If there is anything nonstandard aboutits use (such as an argument of type @code{char *} which is really theaddress of the second character of a string, not the first), or anypossible values that would not work the way one would expect (such as,that strings containing newlines are not guaranteed to work), be sureto say so.Also explain the significance of the return value, if there is one.Please put two spaces after the end of a sentence in your comments, sothat the Emacs sentence commands will work. Also, please writecomplete sentences and capitalize the first word. If a lower-caseidentifier comes at the beginning of a sentence, don't capitalize it!Changing the spelling makes it a different identifier. If you don'tlike starting a sentence with a lower case letter, write the sentencedifferently (e.g., ``The identifier lower-case is @dots{}'').The comment on a function is much clearer if you use the argumentnames to speak about the argument values. The variable name itselfshould be lower case, but write it in upper case when you are speakingabout the value rather than the variable itself. Thus, ``the inodenumber NODE_NUM'' rather than ``an inode''.There is usually no purpose in restating the name of the function inthe comment before it, because the reader can see that for himself.There might be an exception when the comment is so long that the functionitself would be off the bottom
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -