⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 startup.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 3 页
字号:
Non-option argument arg1% testopt -a -- -baflag = 1, bflag = 0, cvalue = (null)Non-option argument -b% testopt -a -aflag = 1, bflag = 0, cvalue = (null)Non-option argument -@end smallexample@node Long Options@subsection Parsing Long OptionsTo accept GNU-style long options as well as single-character options,use @code{getopt_long} instead of @code{getopt}.  You should make everyprogram accept long options if it uses any options, for this takeslittle extra work and helps beginners remember how to use the program.@comment getopt.h@comment GNU@deftp {Data Type} {struct option}This structure describes a single long option name for the sake of@code{getopt_long}.  The argument @var{longopts} must be an array ofthese structures, one for each long option.  Terminate the array with anelement containing all zeros.The @code{struct option} structure has these fields:@table @code@item const char *nameThis field is the name of the option.  It is a string.@item int has_argThis field says whether the option takes an argument.  It is an integer,and there are three legitimate values: @code{no_argument},@code{required_argument} and @code{optional_argument}.@item int *flag@itemx int valThese fields control how to report or act on the option when it occurs.If @code{flag} is a null pointer, then the @code{val} is a value whichidentifies this option.  Often these values are chosen to uniquelyidentify particular long options.If @code{flag} is not a null pointer, it should be the address of an@code{int} variable which is the flag for this option.  The value in@code{val} is the value to store in the flag to indicate that the optionwas seen.@end table@end deftp@comment getopt.h@comment GNU@deftypefun int getopt_long (int @var{argc}, char **@var{argv}, const char *@var{shortopts}, struct option *@var{longopts}, int *@var{indexptr})Decode options from the vector @var{argv} (whose length is @var{argc}).The argument @var{shortopts} describes the short options to accept, just asit does in @code{getopt}.  The argument @var{longopts} describes the longoptions to accept (see above).When @code{getopt_long} encounters a short option, it does the samething that @code{getopt} would do: it returns the character code for theoption, and stores the options argument (if it has one) in @code{optarg}.When @code{getopt_long} encounters a long option, it takes actions basedon the @code{flag} and @code{val} fields of the definition of thatoption.If @code{flag} is a null pointer, then @code{getopt_long} returns thecontents of @code{val} to indicate which option it found.  You shouldarrange distinct values in the @code{val} field for options withdifferent meanings, so you can decode these values after@code{getopt_long} returns.  If the long option is equivalent to a shortoption, you can use the short option's character code in @code{val}.If @code{flag} is not a null pointer, that means this option should justset a flag in the program.  The flag is a variable of type @code{int}that you define.  Put the address of the flag in the @code{flag} field.Put in the @code{val} field the value you would like this option tostore in the flag.  In this case, @code{getopt_long} returns @code{0}.For any long option, @code{getopt_long} tells you the index in the array@var{longopts} of the options definition, by storing it into@code{*@var{indexptr}}.  You can get the name of the option with@code{@var{longopts}[*@var{indexptr}].name}.  So you can distinguish amonglong options either by the values in their @code{val} fields or by theirindices.  You can also distinguish in this way among long options thatset flags.When a long option has an argument, @code{getopt_long} puts the argumentvalue in the variable @code{optarg} before returning.  When the optionhas no argument, the value in @code{optarg} is a null pointer.  This ishow you can tell whether an optional argument was supplied.When @code{getopt_long} has no more options to handle, it returns@code{-1}, and leaves in the variable @code{optind} the index in@var{argv} of the next remaining argument.@end deftypefun@node Long Option Example@subsection Example of Parsing Long Options@smallexample@include longopt.c.texi@end smallexample@node Environment Variables@section Environment Variables@cindex environment variableWhen a program is executed, it receives information about the context inwhich it was invoked in two ways.  The first mechanism uses the@var{argv} and @var{argc} arguments to its @code{main} function, and isdiscussed in @ref{Program Arguments}.  The second mechanism uses@dfn{environment variables} and is discussed in this section.The @var{argv} mechanism is typically used to pass command-linearguments specific to the particular program being invoked.  Theenvironment, on the other hand, keeps track of information that isshared by many programs, changes infrequently, and that is lessfrequently used.The environment variables discussed in this section are the sameenvironment variables that you set using assignments and the@code{export} command in the shell.  Programs executed from the shellinherit all of the environment variables from the shell.@c !!! xref to right part of bash manual when it exists@cindex environmentStandard environment variables are used for information about the user'shome directory, terminal type, current locale, and so on; you can defineadditional variables for other purposes.  The set of all environmentvariables that have values is collectively known as the@dfn{environment}.Names of environment variables are case-sensitive and must not containthe character @samp{=}.  System-defined environment variables areinvariably uppercase.The values of environment variables can be anything that can berepresented as a string.  A value must not contain an embedded nullcharacter, since this is assumed to terminate the string.@menu* Environment Access::    How to get and set the values of			   environment variables.* Standard Environment::  These environment variables have			   standard interpretations.@end menu@node Environment Access@subsection Environment Access@cindex environment access@cindex environment representationThe value of an environment variable can be accessed with the@code{getenv} function.  This is declared in the header file@file{stdlib.h}.@pindex stdlib.h@comment stdlib.h@comment ANSI@deftypefun {char *} getenv (const char *@var{name})This function returns a string that is the value of the environmentvariable @var{name}.  You must not modify this string.  In some non-Unixsystems not using the GNU library, it might be overwritten by subsequentcalls to @code{getenv} (but not by any other library function).  If theenvironment variable @var{name} is not defined, the value is a nullpointer.@end deftypefun@comment stdlib.h@comment SVID@deftypefun int putenv (const char *@var{string})The @code{putenv} function adds or removes definitions from the environment.If the @var{string} is of the form @samp{@var{name}=@var{value}}, thedefinition is added to the environment.  Otherwise, the @var{string} isinterpreted as the name of an environment variable, and any definitionfor this variable in the environment is removed.The GNU library provides this function for compatibility with SVID; itmay not be available in other systems.@end deftypefun@c !!! BSD function setenvYou can deal directly with the underlying representation of environmentobjects to add more variables to the environment (for example, tocommunicate with another program you are about to execute; see@ref{Executing a File}).  @comment unistd.h@comment POSIX.1@deftypevar {char **} environThe environment is represented as an array of strings.  Each string isof the format @samp{@var{name}=@var{value}}.  The order in whichstrings appear in the environment is not significant, but the same@var{name} must not appear more than once.  The last element of thearray is a null pointer.This variable is declared in the header file @file{unistd.h}.If you just want to get the value of an environment variable, use@code{getenv}.@end deftypevarUnix systems, and the GNU system, pass the initial value of@code{environ} as the third argument to @code{main}.@xref{Program Arguments}.@node Standard Environment@subsection Standard Environment Variables@cindex standard environment variablesThese environment variables have standard meanings.  This doesn't meanthat they are always present in the environment; but if these variables@emph{are} present, they have these meanings.  You shouldn't try to usethese environment variable names for some other purpose.@comment Extra blank lines make it look better.@table @code@item HOME@cindex HOME environment variable@cindex home directoryThis is a string representing the user's @dfn{home directory}, orinitial default working directory.The user can set @code{HOME} to any value.If you need to make sure to obtain the proper home directoryfor a particular user, you should not use @code{HOME}; instead,look up the user's name in the user database (@pxref{User Database}).For most purposes, it is better to use @code{HOME}, precisely becausethis lets the user specify the value.@c !!! also USER@item LOGNAME@cindex LOGNAME environment variableThis is the name that the user used to log in.  Since the value in theenvironment can be tweaked arbitrarily, this is not a reliable way toidentify the user who is running a process; a function like@code{getlogin} (@pxref{Who Logged In}) is better for that purpose.For most purposes, it is better to use @code{LOGNAME}, precisely becausethis lets the user specify the value.@item PATH@cindex PATH environment variableA @dfn{path} is a sequence of directory names which is used forsearching for a file.  The variable @code{PATH} holds a path usedfor searching for programs to be run.The @code{execlp} and @code{execvp} functions (@pxref{Executing a File})use this environment variable, as do many shells and other utilitieswhich are implemented in terms of those functions.The syntax of a path is a sequence of directory names separated bycolons.  An empty string instead of a directory name stands for the current directory (@pxref{Working Directory}).A typical value for this environment variable might be a string like:@smallexample:/bin:/etc:/usr/bin:/usr/new/X11:/usr/new:/usr/local/bin@end smallexampleThis means that if the user tries to execute a program named @code{foo},the system will look for files named @file{foo}, @file{/bin/foo},@file{/etc/foo}, and so on.  The first of these files that exists isthe one that is executed.@c !!! also TERMCAP@item TERM@cindex TERM environment variableThis specifies the kind of terminal that is receiving program output.Some programs can make use of this information to take advantage ofspecial escape sequences or terminal modes supported by particular kindsof terminals.  Many programs which use the termcap library(@pxref{Finding a Terminal Description,Find,,termcap,The Termcap LibraryManual}) use the @code{TERM} environment variable, for example.@item TZ@cindex TZ environment variableThis specifies the time zone.  @xref{TZ Variable}, for information aboutthe format of this string and how it is used.@item LANG@cindex LANG environment variableThis specifies the default locale to use for attribute categories whereneither @code{LC_ALL} nor the specific environment variable for thatcategory is set.  @xref{Locales}, for more information aboutlocales.@ignore

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -