2. The main program

The main program is contained in a function called get_main, taking in argc and argv parameters.

<<function_declarations>>=
int p_get(int argc, char *argv[]);
<<functions>>=
int p_get(int argc, char *argv[])
{
    int len;
    if(argc < 2) {
        print_help();
        return 1;
    }
    len = strlen(argv[1]);
    if(0) {
        /* starts the chain */
    }
<<command_parsing>>
    else {
        fprintf(stderr,
                "Could not find subcommand '%s'\n",
                argv[1]);
        return 1;
    }
    return 0;
}

2.1. Print Help

Prints all available commands if there are not enough arguments provided.

<<function_declarations>>=
static void print_help(void);
<<functions>>=
static void print_help(void)
{
    fprintf(stderr, "Available Commands\n\n");
<<available_commands>>
}

2.2. Matching

It's not enough to use strncmp with this program, because then file and filelist will be treated as the same. String length of the input string must be taken into account.

The static function match is an abstraction around this.

<<function_declarations>>=
static int match(const char *s1,
                 int sz1,
                 const char *s2,
                 int sz2);
<<functions>>=
static int match(const char *s1,
                 int sz1,
                 const char *s2,
                 int sz2)
{
    return sz1 == sz2 && !strncmp(s1, s2, sz2);
}



prev | home | next