Search code examples
ccommand-linegetopt

I need to get the directory as a command-line argument using getopt in c


I am very new to getopt and I need to get the directory name as an argument by using getopt. It does not work. The program needs to figure out which argv is the directory so that I can pass the path to a function. I pass either the last command-line argument as a path, if there is a dirname argument, or pass the current working directory to that function.
Please help me on that by providing the correct code fragment:

dt [-h] [-I n] [-L -d -g -i -p -s -t -u | -l] [dirname]

I have tried using optopt but it did not work.

int c;
while( (c = getopt(argc, argv, "hI:Ldgipstul")) != -1){

        switch(c){
                case 'h':
                        printf("This is the help message, please read README file for further information");
                        exit(1);
                        printf("In the help page\n");
                        break;
                case 'I':
                        printf("Setting indentation\n");
                        indentation = atoi(optarg);
                        printf("Indentation is: %d\n", indentation);
                        break;
                case 'L':
                        printf("Following symbolic links\n");
                        break;
                case 'd':
                        //printf("Time of last modification\n");
                        break;
                case 'g':
                        //printf("Print group id\n");
                        groupid = groupId(path);
                        printf("Group Id is: %d\n",groupid);
                        break;
                case 'i':
                        printf("Print number of links in inode table\n");
                        int numberlink = numberLinks(path);
                        printf("number of links: %d\n",numberlink);
                        break;
                case 'p':
                        printf("Permissions\n");
                        break;
                case 's':
                        printf("Sizes\n");
                        break;
                case 't':
                        printf("Information of file\n");
                        break;
                case 'u':
                        //printf("Print user id\n");
                        userid = userId(path);
                        printf("User Id is: %d\n",userid);
                        break;
                case 'l':
                        printf("Optional one\n");
                        break;
                default:
                        perror("Not a valid command-line argument");
                        break;
        }
}

Solution

  • When the getopt() loop finishes, the variable optind contains the index of the first non-option argument. That will be the dirname argument. So you can write:

    char *directory;
    if (optind < argc) {
        directory = argv[optind];
    } else {
        directory = "."; // default to current directory
    }