Profilo di 凤鸣单身公寓FotoBlogElenchi Strumenti Guida

Blog


20 maggio

getopt() and getopt_long()

getopt and getopt_long

1.How to define ?
-- getopt()
#include <unistd.h>
int getopt(int argc, char *const argv[], const char * optstring);
extern chart *optarg;
extern int optind, opterr, optopt;
 
-- getopt_long()
#include <unistd.h>
int getopt_long(int argc, char *const argv[],
                const char *optstring,
                const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char *const argv[],
                const char *optstring,
                const struct option *longopts, int *longindex);
struct option{
        const char *name; /* full name of option */
        int has_arg;          /* Whether the long option has an argument*/
        int *flag;              /* If NULL , getopt_long() returns value of val,
                                   * If not NULL, points to is filled in withe value
                                   * in val and getopt_long() return 0
                                   */

        int val;                 /* First, it corresponds to a short one character
                                   * that will be returned by function
                                   * (flag = NULL)
                                   * Second, flag is not NULL,val is a 1/0 value
                                   */

}

And the values for has_arg
no_argument           0  The option does not take an argument
required_argument     1  The option requires an argument.
optional_argument     2  The option's argument is optional.
 
getopt_long_only() works identically, but it is used for programs where all options
are long and options begin with a single '-' character.
 
2.What're meanings of these variables ?
argc and argv: These are passed straight from those of main().
optstring: It is a string of letters that will be matched in command-line option.
optarg: If there will be a argument followed option,it will point to that argument.
optind: The current index in argv.
optopt: When an invalid option character is found, getopt() returns either a "?" or a ":",
        and optopt contains the invalid character that was found.
 
3.Details in optstring
if not starting by a colon,it will be a normal behavior,we will be told a invalid option when
we type a option that donot contains in optstring.In another way, starting by a colon, we need
add my error messages.

One colon: It must be had an argument following option
Two colon: An argument follows a option is optional.
Absent: No argument is followed by option
 
14 maggio

Begin linux programming

Yesterday ,I bought a book named Linux Programming by Example.
For reviewing C ? For learning Linux? Or for what? I donot know.But learning must be continued.Creat a module for Linux programming in my space as Ruby.
Ok,it is a beginning~!
Read preface and all will be continue naturely.