LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2004, 05:24 PM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
Getopt() newbie...


I have the following statement in my app to get getopt() arguments but I need to figure out a way to tell if an argument is passed or not.

I have static char stropts[] = "u:h"; defined but if there is no argument after the -u then I need the program to exit... This is the case statement:

Code:
while((optch = getopt(argc, argv, stropts)) != EOF)
    switch(optch) {
      case 'h':
        usage(argv[0]);
        return 0;
      case 'u':
          sprintf(name, "%s", optarg);
          break;
      default:
        printf("Unknown option -- %s.\n", optarg);
        printf("For help on usage please use: %s -h\n", argv[0]);
        return 1;
Thanks
 
Old 02-10-2004, 07:18 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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... */
}

Last edited by Hko; 02-10-2004 at 07:23 PM.
 
Old 02-10-2004, 08:15 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
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.
 
Old 02-11-2004, 12:02 AM   #4
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
K my bad - i left the other variable declarations at the top of my code - works perfectly now

Thanks for the help Hko
 
Old 02-11-2004, 12:05 AM   #5
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Ummm k i tried bob as a user name, that worked... but anything else (i.e. root, bo, foo etc) doesn't work
 
Old 02-11-2004, 11:06 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
I copied / pasted the program from the forum from scratch, and it does work for me. For all names.
 
Old 02-11-2004, 01:02 PM   #7
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Yeah urs does but when i copied it into my program it doesn't seem to...

K just going through my code, my fault - I had a deugging test in their from when I was trying out getopt() before, it works now
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getopt availability Bostonian Programming 2 12-26-2004 10:37 AM
getopt in unix hari_s_82 Linux - Newbie 2 10-01-2004 04:19 AM
trouble finding getopt.h (c++) Feminista Programming 2 09-19-2004 09:39 PM
getopt(3) Berhanie Programming 8 06-15-2004 09:50 PM
Using getopt in cplus UltimaGuy Programming 3 08-28-2003 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration