0

I am writing a program that is a custom version of ls with some options. The options can be inputted as "-i" or as something like "-ilR". This portion of my code iterates through the command line arguments and sets a struct accordingly. Except if I run the program with "./myProgram -i Directory" it doesn't go into the test print I have set up.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/fcntl.h>
#include <stdbool.h>
#include <string.h>

struct optionArguments{
    bool argumenti;
    bool argumentl;
    bool argumentR; 
};

int main(int argc, char* argv[]){

    struct optionArguments options;
        options.argumenti = false;
        options.argumentl = false;
        options.argumentR = false;

   
    for(int i = 1; i<argc-2; i++){
        for(int j = 0; j<strlen(argv[i]); j++){
            char chr = argv[i][j];
            if(chr == 'i'){
                options.argumenti = true;
            }
            if(chr == "l"){
                options.argumentl = true;
            }
             if(chr == "R"){
                options.argumentR = true;
            }
        }
    }

    if(options.argumenti){
        printf("OPTION I\n");
    }
}

any help/advice is greatly appreciated

edit: I just put a test print inside of the second loop and it doesn't print anything at all so the second loop isn't even running.

Steve
  • 31
  • 7
  • 1
    why `i < argc-2`? – William Pursell Aug 05 '20 at 23:28
  • 1
    Have you tried debugging (or even using prints) to see what values you are looking at and which bits of the code are being executed? – John3136 Aug 05 '20 at 23:35
  • @WilliamPursell because argv[argc] will be a null pointer and argv[argc-1] is the directory argument – Steve Aug 05 '20 at 23:36
  • 1
    You can't use `==` to compare strings in C, or a char to a string. – Shawn Aug 05 '20 at 23:51
  • @John3136 I just put a test print inside of the second loop and it doesn't print anything at all so the second loop isn't even running. Do you have any idea why? – Steve Aug 05 '20 at 23:52
  • 2
    @Shawn You can compare characters to characters with `==`, though, and it looks like that's what the OP was trying to do. The double-quotes instead of single-quotes look like a typo, since they use single quotes the first time around. – Charles Srstka Aug 05 '20 at 23:58
  • @DayneHack Well, what arguments are you invoking your program with on the command line? Since you're going from `1` to ` – Charles Srstka Aug 06 '20 at 00:03
  • @DayneHack try changing the `argc-2` to `argc-1` for `./myProgram -i Directory` arguments. – Sourabh Choure Aug 06 '20 at 00:08
  • 1
    Try printing argc - maybe that isn't what you expect (3 args, argc -2, 1<1)... – John3136 Aug 06 '20 at 00:26

1 Answers1

1

It will be much simpler if you use getopt() function designed to handle command-line argument parsing.

Note: Since you have already included unistd library so using getopt which will be much wiser, otherwise, there are other implementations available as well like argp()

Here is a program which uses getopt from the POSIX C Library unistd

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/fcntl.h>
#include <stdbool.h>
#include <string.h>

struct optionArguments{
    bool argumenti;
    bool argumentl;
    bool argumentR; 
};

int main(int argc, char* argv[]){

    struct optionArguments options;
        options.argumenti = false;
        options.argumentl = false;
        options.argumentR = false;

    int opt;
    while ((opt = getopt(argc, argv, "ilR")) != -1) {
        switch (opt) {
        case 'i': options.argumenti = true; break;
        case 'l': options.argumentl = true; break;
        case 'R': options.argumentR = true; break;
        default:
            fprintf(stderr, "Usage: %s [-ilR] \n", argv[0]);
            exit(EXIT_FAILURE);
        }
    }


    if(options.argumenti){
        printf("OPTION I\n");
    }
    if(options.argumentl){
        printf("OPTION l\n");
    }
    if(options.argumentR){
        printf("OPTION R\n");
    }
}

For other ways of implementation visit: Parsing command-line arguments in C?

Sourabh Choure
  • 723
  • 4
  • 15