youjustreadthis
youjustreadthis

Reputation: 652

Command line arguments not working. Logic error of some sort

I'm having problems getting my command-line arguments to work properly. The arguments appear to fail when the argc is higher than 3 and after that you can only get them to work if you write -fwhatever and not -f whatever as it should be. -t also copies the value of -o.

int main(int argc,char **argv)
{
    int c;

    /* Are we in root? */

    if(geteuid() !=0)
        {
            printf("Root access is required to run this program. \n");

            exit(0);        
        }   

    /* How do we use the program */

    if ((argc < 6) || (argc > 8))
        {
            usage(argv[0]);

            exit (0);
        } 

    while (1)
        {
            static struct option long_options[] =
                {
                    /* Options */
                {"send",       no_argument,       0, 's'},
                {"recieve",    no_argument,       0, 'r'},
                {"file",       required_argument, 0, 'f'},
                {"data",       required_argument, 0, 'd'},
                {"destip",     required_argument, 0, 'i'},
                {"destport",   required_argument, 0, 'p'},
                {"sourceip",   required_argument, 0, 'o'},
                {"sourceport", required_argument, 0, 't'},
                {0, 0, 0, 0}
                };

               int option_index = 0;

               c = getopt_long (argc, argv, "srf:d:i:p:o:t:",
                            long_options, &option_index);

                          /* Detect the end of the options. */
               if (c == -1)
                 break;

               switch (c)
                 {
                 case 0:
                   /* If this option set a flag, do nothing else now. */
                   if (long_options[option_index].flag != 0)
                 break;
                   printf ("option %s", long_options[option_index].name);
                   if (optarg)
                 printf (" with arg %s", optarg);
                   printf ("\n");
                   break;

                 case 's':
                   puts ("option -s\n");
                   break;

                 case 'r':
                   puts ("option -r\n");
                   break;

                 case 'f':
                   printf ("option -f with value `%s'\n", optarg);
                   break;

                 case 'd':
                   printf ("option -d with value `%s'\n", optarg);
                   break;

                 case 'i':
                   printf ("option -i with value `%s'\n", optarg);
                   break;

                 case 'p':
                   printf ("option -p with value `%s'\n", optarg);
                   break;

                 case 'o': 
                   printf ("option -o with value `%s'\n", optarg);

                 case 't': 
                   printf ("option -t with value `%s'\n", optarg);


                 case '?':
                   /* Error message printed */
                   break;

                 default:
                   abort ();
                 }
             }

               /* Print any remaining command line arguments (not options). */
            if (optind < argc)
        {
            printf ("non-option ARGV-elements: ");
            while (optind < argc)
            printf ("%s ", argv[optind++]);
            putchar ('\n');
        }

        getchar ();
        exit (0);

}

There's obviously something horribly wrong here, but it can't seem to find it.

Upvotes: 0

Views: 321

Answers (1)

eyalm
eyalm

Reputation: 3366

Regarding the -o copied to -t, you forgot to put a break; at the end of the case.

Also, I would have removed the argc checking. Let getopt do its magic. You can check at the end of parsing if all mandatory options were set. You can also check for unknown arguments.

Upvotes: 2

Related Questions