If the end of the directory stream is reached, NULL is returned and errno is not changed. If an error occurs, NULL is returned and errno is set appropriately. To distinguish end of stream from an error, set errno to zero before calling readdir() and then check the value of errno if NULL is returned.
写在前面的话：笔者写这篇文章并非为了教会读者Git的详细用法，而是为了告诉读者们Git能满足什么需求．笔者期望阅读本文的读者在某一天出现有关版本控制的需求时，能想起曾在某个不知名的博客中看到过Git有个功能可以解决当前遇到的问题．与其说本文在介绍Git的使用方式，不如说本文在介绍Git已有的部分常用功能．至于有意详细的学习Git的用法的读者，本文无法满足你的需求，笔者建议阅读Git Pro Book来满足你的需求．
optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so getopt() places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg
if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, −oarg), then it is returned in optarg, otherwise optarg is set to zero. This is a GNU extension.
If optstring contains W followed by a semicolon, then −W foo is treated as the long option −−foo. (The −W option is reserved by POSIX.2 for implementation extensions.) This behavior is a GNU extension, not available with libraries before glibc 2.
If the first character of optstring is + or the environment variable POSIXLY_CORRECT is set, then option processing stops as soon as a nonoption argument is encountered.
If the first character of optstring is −, then each nonoption argv-element is handled as if it were the argument of an option with character code 1. (This is used by programs that were written to expect options and other argv-elements in any order and that care about the ordering of the two.)
The special argument −− forces an end of option-scanning regardless of the scanning mode.
While processing the option list, getopt() can detect two kinds of errors:
an option character that was not specified in optstring
a missing option argument (i.e., an option at the end of the command line without an expected argument).
Such errors are handled and reported as follows:
By default, getopt() prints an error message on standard error, places the erroneous option character in optopt, and returns ? as the function result.
If the caller has set the global variable opterr to zero, then getopt() does not print an error message. The caller can determine that there was an error by testing whether the function return value is ?. (By default, opterr has a nonzero value.)
If the first character (following any optional + or − described above) of optstring is a colon (:), then getopt() likewise does not print an error message. In addition, it returns : instead of ? to indicate a missing option argument. This allows the caller to distinguish the two different types of errors.
has_arg is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argument; or optional_argument (or 2) if the option takes an optional argument.
flag specifies how results are returned for a long option.
If flag is NULL , then getopt_long() returns val . (For example, the calling program may set val to the equivalent short option character.)
Otherwise, getopt_long() returns 0, and flag points to a variable which is set to val if the option is found, but left unchanged if the option is not found.
val is the value to return, or to load into the variable pointed to by flag .>The last element of the array has to be filled with zeros.