0xEDD1E
0xEDD1E

Reputation: 1162

What do these codes do?

I saw this source code in a book:

#include <stdio.h>
#include <unistd.h>


int main(int argc, char *argv[])
{
    char *delivery = "";
    int thick = 0;
    int count = 0;
    char ch;

    while ((ch = getopt(argc, argv, "d: t")) != EOF)
        switch(ch)
        {
            case 'd':
                delivery = optarg;
                break;

            case 't':
                thick = 1;
                break;

            default:
                fprintf(stderr, "Unknown option: '%s'\n", optarg);
                return 1;
        }

        argc -= optind;
        argv += optind;

        if (thick)
            puts("Thick Crust.");

        if (delivery[0])
            printf("To be deliverd %s\n", delivery);

        puts("Ingredients: ");

        for (count = 0; count < argc; count++)
            puts(argv[count]);

    return 0;
}

I can understand the entire source except:

argc -= optind;
argv += optind;

I know what is argc and argv, but what happen them in these two lines, what is "optind" Explain me.

Thank you.

Upvotes: 5

Views: 6020

Answers (4)

Prashasthi
Prashasthi

Reputation: 99

while running this code you might execute it as ./executable_file:name -d some_argument

When getopt is called it starts scanning what is written in command line. argc = 3, argv[0]=./executable_file:name argv[1]=-d argv[2]=some_argument . getopt will get the option d that time optind = 2 that is the index of the next i.e argv[2]. Hence optind+argv will point to argv[2].

In general optind will have the index of the next.

Upvotes: 0

Matteo Italia
Matteo Italia

Reputation: 126777

The getopt library provides several function to help parsing the command line arguments.

When you call getopt it "eats" a variable number of arguments (depending on the type of command line option); the number of arguments "eaten" is indicated in the optind global variable.

Your code adjusts argv and argc using optind to jump the arguments just consumed.

Upvotes: 4

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382092

Regarding the how :

optind is the number of elements of argv which will be ignored after this code :

argc -= optind; // reduces the argument number by optind 
argv += optind; // changes the pointer to go optind items after the first one

argc (the count) is reduced by optind

and the pointer to the first element of argv is upgraded accordingly.

Regarding the why :

See Karoly's answer.

Upvotes: 2

Karoly Horvath
Karoly Horvath

Reputation: 96258

it's a global variable used by getopt.

From the manual:

The getopt() function parses the command-line arguments. Its arguments argc and argv are the argument count and array as passed to the main() function on program invocation.

The variable optind is the index of the next element to be processed in argv. The system initializes this value to 1.

That code just updates argc and argv to point to the rest of the arguments (- options skipped).

Upvotes: 3

Related Questions