Hi, I'm trying to use the getopt_long function to handle parameters to my C program, in Linux. Here is the relevant code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#define ECB_MODE 1000
int main(int argc, char* argv[])
{
int mode;
struct option long_opts[2] = {
{"ecb", no_argument, &mode, ECB_MODE},
{0, 0, 0, 0}
};
while (1)
{
ch = getopt_long(argc, argv, "dek:i:o:", long_opts, NULL);
if (ch == -1)
{
break;
}
switch(ch)
{
case 'd':
{
encrypt = 0;
break;
}
case 'e':
{
encrypt = 1;
break;
}
case 'i':
{
infilename = optarg;
break;
}
case 'o':
{
outfilename = optarg;
break;
}
case 'k':
{
keyfilename = optarg;
break;
}
case 0:
{
printf("ECB option\n");
break;
}
default:
{
printf("Invalid arguments\n");
return 0;
}
}
}
return 0;
}
All of the short options work, but if I call the program with "--ecb" getopt_long returns '?' citing "ambiguous option". In my understanding, it should return 0 and store the value of the macro in the int variable I thoughtfully provided for it in the long_opts struct. My question is, why/how is "--ecb" ambiguous, and how can I make it unambiguous?