g3k0
March 4th, 2007, 07:41 PM
Greetings, I am using the getopt function and I had a question. Is there anyway to easily, not require an argument after the r - example
./webserver -p 5000 -r
rather than
./webserver -p 5000 -r /asd
because it does this to me
[bwhit132@csunix webserver]$ ./webserver -p 5000 -r
./webserver: option requires an argument -- r
I want to allow the option but not have it as a necessity.
this is what im doing with it
void processargs(int argc, char **argv) {
int dirSize = 100;
int next_option;
const char* short_options = "p:r:";
do {
next_option = getopt(argc, argv, short_options);
if (next_option != -1) {
switch (next_option)
{
case 'p': /* -p -- port option */
port = optarg;
break;
case 'r':
myPath = malloc(dirSize); //allocate because we are copying into a null ptr
while (getcwd(myPath,dirSize) == NULL){ //if the directory has a long name, reallocate size
free(myPath);
dirSize *= 2;
myPath = malloc(dirSize);
}
downPath = optarg;
if (downPath == NULL){
printf("\nHAHAAAAA NULL BOY\n");
} else { printf("\n%s\n",downPath); }
break;
default:
/* Unknown option detected. Print it to standard
output, and exit with exit code zero (normal termination). */
fprintf(stderr, USAGE_STRING);
exit(1);
}
}
} while (next_option != -1);
if (port == NULL) {
fprintf(stderr, USAGE_STRING);
exit(1);
}
}
Edit: In case you were wondering I was just testing with the whole hahaha null boy thing..
./webserver -p 5000 -r
rather than
./webserver -p 5000 -r /asd
because it does this to me
[bwhit132@csunix webserver]$ ./webserver -p 5000 -r
./webserver: option requires an argument -- r
I want to allow the option but not have it as a necessity.
this is what im doing with it
void processargs(int argc, char **argv) {
int dirSize = 100;
int next_option;
const char* short_options = "p:r:";
do {
next_option = getopt(argc, argv, short_options);
if (next_option != -1) {
switch (next_option)
{
case 'p': /* -p -- port option */
port = optarg;
break;
case 'r':
myPath = malloc(dirSize); //allocate because we are copying into a null ptr
while (getcwd(myPath,dirSize) == NULL){ //if the directory has a long name, reallocate size
free(myPath);
dirSize *= 2;
myPath = malloc(dirSize);
}
downPath = optarg;
if (downPath == NULL){
printf("\nHAHAAAAA NULL BOY\n");
} else { printf("\n%s\n",downPath); }
break;
default:
/* Unknown option detected. Print it to standard
output, and exit with exit code zero (normal termination). */
fprintf(stderr, USAGE_STRING);
exit(1);
}
}
} while (next_option != -1);
if (port == NULL) {
fprintf(stderr, USAGE_STRING);
exit(1);
}
}
Edit: In case you were wondering I was just testing with the whole hahaha null boy thing..