ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When there is an argument missing for -u, getopt() itself also print a error message and exit the program. However, you can handle this case yourself if you have ':' as the very first character of your option string ("stropts" in your program). getopt() will then return ':' if an argument is missing for any option.
From "man 3 getopt":
Quote:
If getopt() finds an option character in argv that was not included in
optstring, or if it detects a missing option argument, it returns `?'
and sets the external variable optopt to the actual option character.
If the first character of optstring is a colon (`:'), then getopt()
returns `:' instead of `?' to indicate a missing option argument. If
an error was detected, and the first character of optstring is not a
colon, and the external variable opterr is nonzero (which is the
default), getopt() prints an error message.
You also have a few mistakes in your program. The biggest being that you try to use "optarg" to print the unknown options character. This should be "optopt". Note that your program prints "Unknown option -- (null).". If printf() wasn't so kind to check for the pointer to be NULL, it would segfault...
Another is that it's better to test for getopt() to return >= 0 in the while-statement instead of test for EOF.
Here's an improved version that also checks itself for a missing argument for -u.
Code:
#include <stdio.h>
#include <unistd.h>
#define MAXNAME 20
void usage(char *cmd)
{
printf("Usage: %s [-h] [-u <name>]\n", cmd);
}
int main(int argc, char *argv[])
{
int optch;
char stropts[] = ":u:h";
char name[MAXNAME + 1];
while((optch = getopt(argc, argv, stropts)) >= 0) {
switch (optch) {
case 'h':
usage(argv[0]);
return 0;
case 'u':
snprintf(name, MAXNAME, "%s", optarg);
break;
case ':':
fprintf(stderr, "No argument specified for -%c option\n", optopt);
return 1;
case '?':
printf("Unknown option -%c.\n", optopt);
printf("For help on usage please use: %s -h\n", argv[0]);
return 1;
}
}
printf("User name = %s\n", name);
/* Rest of program follows... */
}
Ummm that didn't work, it compiled fine but when I run it like ./pwdDiary -u it comes up with Unknown option -u (the ? argument in the switch)... any ideas?
Ummm another thing, now that I have errors setup I tried to get rid of the standard ones... In the man page it says to set opterr to 0 but when I do that I still get the errors.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.