Author | Message | Time |
---|---|---|
mynameistmp | [quote] bash-2.05b$ uname -osr ; gcc --version Linux 2.4.22 GNU/Linux gcc (GCC) 3.2.3 bash-2.05b$ make gcc -Wall -ggdb -c main.c gcc main.o libs/isock.o libs/cmdline.c -o ircclient bash-2.05b$ ./ircclient Using default configuration file: bot.cfg... $connect -s test test $connect -s test2 $connect test Segmentation fault bash-2.05b$ [/quote] I'm trying to use getopt() to parse a commandline I created within the program. It works fine the first time through, but has problems the second time through. I'm not much of a programmer, so I've probably missed something fundamental. Any input is appreciated. If invocation code is necessary let me know, I tried to limit the length of the message. (For some reason phpbb screws up the indentation on the case in the switch statement.) [code] void com (char *buf, int buflen) { int b = 0; int option = 0; int argn = 0; char *args[16]; char *p = strtok(buf, " "); for (; p; p = strtok(NULL, " "), argn++) { if (argn <= (sizeof(args)/4)) { args[argn] = p; } else { puts("Maximum of 17 parameters."); b = 1; } } if (!b && !strncmp(buf, "connect", 7)) { while ((option = getopt(argn, args, "s:")) != EOF) { switch (option) { case 's' : strcpy(server, optarg); puts(server); break; default: break; } } } } [/code] | May 15, 2004, 8:02 AM |
Adron | I just read the getopt man page, and I don't see a way of telling it to restart the scan? Maybe that's the problem? You could also try [ code ] tags, and see if those work any better than [ quote ]. | May 15, 2004, 10:41 AM |
mynameistmp | I fixed it by setting the external integer optind to 0 per iteration of the function, or 'restarting the scan'. Thanks for the insight =) man 3 getopt: [quote] If there are no more option characters, getopt() returns -1. Then optind is the index in argv of the first argv-element that is not an option. [/quote] | May 15, 2004, 7:11 PM |