linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* getopt() library function options
@ 2005-03-04  4:35 Fabio
  2005-03-04  8:42 ` Steve Graegert
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Fabio @ 2005-03-04  4:35 UTC (permalink / raw)
  To: linux-c-programming

Hello,

I am coding a small utility for system administrator. The following command line options will be accepted:

$apstat 
$apstat -t 1
$apstat -n 1
$apstat -t 2 -n 2
$apstat -v
$apstat -t 1 -v
$apstat -v -t 1 -n 2

unaccepted command line options:

$apstat -t
$apstat -n 
$apstat -t <<non integer value>>
$apstat -n <<non integer value>>


I would like that know what would be the while() command that I have to call getopt() inside the case(), for example, I need all this:


     while ((c = getopt(argc, argv, ":abf:")) != -1) {
         switch(c) {
         case 'a':
              printf("a is set\n");
              break;
         case 'b':
              printf("b is set\n");
              break;
         case 'f':
              filename = optarg;
              printf("filename is %s\n", filename);
              break;
         case ':':
              printf("-%c without filename\n", optopt);
              break;
         case '?':
              printf("unknown arg %c\n", optopt);
              break;
         }
     }


This was I got on a getopt() man page, I understand some basic concept, but I cant put the unaccpted arguments to work. Thanks alot if someone can build this from scratch.

Thanks in advance,

fabio.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getopt() library function options
  2005-03-04  4:35 getopt() library function options Fabio
@ 2005-03-04  8:42 ` Steve Graegert
  2005-03-04  8:49 ` Rechberger Markus
  2005-03-04 20:37 ` J.
  2 siblings, 0 replies; 9+ messages in thread
From: Steve Graegert @ 2005-03-04  8:42 UTC (permalink / raw)
  To: linux-c-programming; +Cc: Fabio

On Thu, 03 Mar 2005 22:35:02 -0600, Fabio <fabio@crearium.com> wrote:
> Hello,
> 
> I am coding a small utility for system administrator. The following command line options will be accepted:
> 
> $apstat
> $apstat -t 1
> $apstat -n 1
> $apstat -t 2 -n 2
> $apstat -v
> $apstat -t 1 -v
> $apstat -v -t 1 -n 2
> 
> unaccepted command line options:
> 
> $apstat -t
> $apstat -n
> $apstat -t <<non integer value>>
> $apstat -n <<non integer value>>
> 
> I would like that know what would be the while() command that I have to call getopt() inside the case(), for example, I need all this:
> 
>     while ((c = getopt(argc, argv, ":abf:")) != -1) {
>         switch(c) {
>         case 'a':
>              printf("a is set\n");
>              break;
>         case 'b':
>              printf("b is set\n");
>              break;
>         case 'f':
>              filename = optarg;
>              printf("filename is %s\n", filename);
>              break;
>         case ':':
>              printf("-%c without filename\n", optopt);
>              break;
>         case '?':
>              printf("unknown arg %c\n", optopt);
>              break;
>         }
>     }
> 

Hi Fabio,

this is what I would try:

while ((c = getopt(argc, argv, "n:t:v")) != -1) {
	switch (c)
	{
	case 'n':
		n_flag = (int)strtoul(optarg, NULL, 0);
		break;
	case 't':
		t_flag = (int)strtoul(optarg, NULL, 0);
		break;
	case 'v':
		v_flag = true;
		break;
	default:
		return -1;      
	}
}

It's not tested though, but hopefully points you to the right direction.

-- 
Kind Regards

    \Steve

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getopt() library function options
  2005-03-04  4:35 getopt() library function options Fabio
  2005-03-04  8:42 ` Steve Graegert
@ 2005-03-04  8:49 ` Rechberger Markus
  2005-03-04 20:37 ` J.
  2 siblings, 0 replies; 9+ messages in thread
From: Rechberger Markus @ 2005-03-04  8:49 UTC (permalink / raw)
  To: Fabio; +Cc: linux-c-programming

Hey Fabio,

if(argc==1){
  printf("Syntax: ....\n");
  exit(1);
}
while ((c = getopt(argc, argv, "t:n:v")) != -1) {
     switch(c) {
        case 't':
            tvar = atoi(optarg); //check if optarg is numeric if not ->exit(1);
            break;
       case 'n':
            nvar = atoi(optarg); //same here..
            break;
....

Markus



On Thu, 03 Mar 2005 22:35:02 -0600, Fabio <fabio@crearium.com> wrote:
> Hello,
> 
> I am coding a small utility for system administrator. The following command line options will be accepted:
> 
> $apstat
> $apstat -t 1
> $apstat -n 1
> $apstat -t 2 -n 2
> $apstat -v
> $apstat -t 1 -v
> $apstat -v -t 1 -n 2
> 
> unaccepted command line options:
> 
> $apstat -t
> $apstat -n
> $apstat -t <<non integer value>>
> $apstat -n <<non integer value>>
> 
> I would like that know what would be the while() command that I have to call getopt() inside the case(), for example, I need all this:
> 
>      while ((c = getopt(argc, argv, ":abf:")) != -1) {
>          switch(c) {
>          case 'a':
>               printf("a is set\n");
>               break;
>          case 'b':
>               printf("b is set\n");
>               break;
>          case 'f':
>               filename = optarg;
>               printf("filename is %s\n", filename);
>               break;
>          case ':':
>               printf("-%c without filename\n", optopt);
>               break;
>          case '?':
>               printf("unknown arg %c\n", optopt);
>               break;
>          }
>      }
> 
> This was I got on a getopt() man page, I understand some basic concept, but I cant put the unaccpted arguments to work. Thanks alot if someone can build this from scratch.
> 
> Thanks in advance,
> 
> fabio.
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: getopt() library function options
@ 2005-03-04 15:42 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 9+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2005-03-04 15:42 UTC (permalink / raw)
  To: linux-c-programming

Fabio wrote:

>I am coding a small utility for system administrator. The following command line options will be accepted:
>
>$apstat 
>$apstat -t 1
>$apstat -n 1
>$apstat -t 2 -n 2
>$apstat -v
>$apstat -t 1 -v
>$apstat -v -t 1 -n 2

>unaccepted command line options:

>$apstat -t
>$apstat -n 
>$apstat -t <<non integer value>>
>$apstat -n <<non integer value>>

with the getopt function, a colon after a flag will designate that an argument is required.  For example,

while(-1 != (choice = getopt(argc, argv, "t:n:vh")))
{
   ... stuff
}

would require that t and n require and argument while v and h do not.  This means that the following 
command lines would be accepted:

apstat
apstat -t 1
apstat -t a

while the line apstat -t would not be accepted.  If getopt encounters an option not in the list, or if 
a required argument is missing then the character `?' is returned.  The argument is returned in the 
buffer `optarg'.  So in your case I would do this:

while(-1 != (choice = getopt(argc, argv, "t:n:vh")))
{
    switch(choice)
    {
        case 't':
            if(IsInteger(optarg))
                  _t = atoi(optarg);
            else
	         printf("Argument of t flag must be an integer");
     
            break;

        case 'n':
            if(IsInteger(optarg))
                  _n = atoi(optarg);
            else
	         printf("Argument of n flag must be an integer");
     
            break 

        case 'v':
            // Print version information??
            break;

        case '?':
        case 'h':
            // display help information
            break;
     }
}

In the above, I assume the existance of an IsInteger function with the signature, 
bool IsInteger(const char*);
Because I do most of my programming using C++ my implementation of IsInteger is:

bool IsInteger(const char* cp)
{
    const char* ep = cp + strlen(cp);
    return(std::find_if(cp, ep, std::not1(std::ptr_fun(isdigit))) == ep);
}

but the idea is the same in C - check to see if each element on optarg is an integer
and return true if its is.

George

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getopt() library function options
  2005-03-04  4:35 getopt() library function options Fabio
  2005-03-04  8:42 ` Steve Graegert
  2005-03-04  8:49 ` Rechberger Markus
@ 2005-03-04 20:37 ` J.
  2 siblings, 0 replies; 9+ messages in thread
From: J. @ 2005-03-04 20:37 UTC (permalink / raw)
  To: linux-c-programming

On Thu, 3 Mar 2005, Fabio wrote:

> Hello,
> 
> I am coding a small utility for system administrator. The following command line options will be accepted:
> 
> $apstat 
> $apstat -t 1
> $apstat -n 1
> $apstat -t 2 -n 2
> $apstat -v
> $apstat -t 1 -v
> $apstat -v -t 1 -n 2
> 
> unaccepted command line options:
> 
> $apstat -t
> $apstat -n 
> $apstat -t <<non integer value>>
> $apstat -n <<non integer value>>
> 
> I would like that know what would be the while() command that I have to call getopt() 
> inside the case(), for example, I need all this:
> 
> 
>      while ((c = getopt(argc, argv, ":abf:")) != -1) {
>          switch(c) {
>          case 'a':
>               printf("a is set\n");
>               break;
>          case 'b':
>               printf("b is set\n");
>               break;
>          case 'f':
>               filename = optarg;
>               printf("filename is %s\n", filename);
>               break;
>          case ':':
>               printf("-%c without filename\n", optopt);
>               break;
>          case '?':
>               printf("unknown arg %c\n", optopt);
>               break;
>          }
>      }
> 
> 
> This was I got on a getopt() man page, I understand some basic concept, 
> but I cant put the unaccpted arguments to work. Thanks alot if someone
> can build this from scratch.
> 
> Thanks in advance,
> 
> fabio.

Friday, March 03

You could use an regex to determine if your dealing with an INT or
not.. Or check the string with isdigit() <ctype.h>. 

If atoi is used on non-digit conversion the result is a segfault.
Use strtol family instead because atoi can't detect errors. Also 
casting return value's is a bad idea since they can hide errors..

your code with might looklike something like this....

#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>

#include <regex.h>
#include <locale.h>

#define PACKAGE    "getoptex"
#define VERSION    "0.0.1"

void print_help(int exval);
int isthisanint(char *str);

int main(int argc, char *argv[]) {
 int opt;

 /* 
 // no arguments given
 */
 if(argc == 1) {
  fprintf(stderr, "This program needs arguments....\n\n");
  print_help(1);
 }

 while((opt = getopt(argc, argv, "hVvd:f:o:")) != -1) {
  switch(opt) {
   case 'h':
    print_help(0);
   case 'V':
    printf("%s %s\n\n", PACKAGE, VERSION); 
    exit(0);
   case 'v':
    printf("%s: Verbose option is set `%c'\n", PACKAGE, optopt);
    break;
   case 'd':
    printf("%s: isthisanint(%s), says: %d\n", PACKAGE, 
      optarg, isthisanint(optarg));
    break;
   case 'f':
    printf("%s: Filename %s\n", PACKAGE, optarg);
    break;
   case 'o':
    printf("Output: %s\n", optarg);
    break;
   case ':':
    fprintf(stderr, "%s: Error - Option `%c' needs a value\n\n", PACKAGE, optopt);
    exit(1);
   case '?':
    fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
    exit(1);
   }
 }

 /* 
 // print all remaining options
 */
 for(; optind < argc; optind++)
  printf("argument: %s\n", argv[optind]);

 return 0;
}

void print_help(int exval) {
 printf("%s,%s show working getopt example\n", PACKAGE, VERSION); 
 printf("%s [-h] [-V] [-v] [-d INT] [-f FILE] [-o FILE]\n\n", PACKAGE);

 printf("  -h              print this help and exit\n");
 printf("  -V              print version and exit\n\n");

 printf("  -v              set verbose flag\n");
 printf("  -d INT          set `-d' to `INT'\n");
 printf("  -f FILE         set intput file\n");
 printf("  -o FILE         set output file\n\n");

 exit(exval);
}

int isthisanint(char *str) {
 regex_t re;
 int retval = -1;

 setlocale(LC_ALL, "");
 if(regcomp(&re, "^([0-9]*)$", REG_EXTENDED) != 0) {
  fprintf(stderr, "%s: Error - Unable to compile regex", PACKAGE);
  return -1;
 }

 if(regexec(&re, str, 0, NULL, 0) != 0)
  retval = -1;
 else
  retval = atoi(str);

 /* or.. Like listed in the manual page */
 /*  strtol(nptr, (char **)NULL, 10); */

 regfree(&re);
 return retval;
}


Hope that gave you some usable idea's... 

GoodLuck..

J.

--
http://www.rdrs.net/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getopt() library function options
@ 2005-03-04 21:16 J.
  2005-03-05  9:57 ` Rechberger Markus
  0 siblings, 1 reply; 9+ messages in thread
From: J. @ 2005-03-04 21:16 UTC (permalink / raw)
  To: linux-c-programming

On Thu, 3 Mar 2005, Fabio wrote:

> Hello,
> 
> I am coding a small utility for system administrator. 
> The following command line options will be accepted:

........
 
> This was I got on a getopt() man page, I understand some basic concept, 
> but I cant put the unaccpted arguments to work. Thanks alot if someone
> can build this from scratch.
> 
> Thanks in advance,
> 
> fabio.

Friday, March 03

You could use an regex to determine if your dealing with an INT or
not.. Or check the option string with isdigit() <ctype.h>. 

If atoi is used on non-digit conversion the result is a segfault.
Use strtol family instead because atoi can't detect errors. Also 
casting return value's is a bad idea since they can hide errors..

your code with might looklike something like this....

#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>

#include <regex.h>
#include <locale.h>

#define PACKAGE    "getoptex"
#define VERSION    "0.0.1"

void print_help(int exval);
int isthisanint(char *str);

int main(int argc, char *argv[]) {
 int opt;

 /* 
 // no arguments given
 */
 if(argc == 1) {
  fprintf(stderr, "This program needs arguments....\n\n");
  print_help(1);
 }

 while((opt = getopt(argc, argv, "hVvd:f:o:")) != -1) {
  switch(opt) {
   case 'h':
    print_help(0);
   case 'V':
    printf("%s %s\n\n", PACKAGE, VERSION); 
    exit(0);
   case 'v':
    printf("%s: Verbose option is set `%c'\n", PACKAGE, optopt);
    break;
   case 'd':
    printf("%s: isthisanint(%s), says: %d\n", PACKAGE, 
      optarg, isthisanint(optarg));
    break;
   case 'f':
    printf("%s: Filename %s\n", PACKAGE, optarg);
    break;
   case 'o':
    printf("Output: %s\n", optarg);
    break;
   case ':':
    fprintf(stderr, "%s: Error - Option `%c' needs a value\n\n", PACKAGE, optopt);
    exit(1);
   case '?':
    fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
    exit(1);
   }
 }

 /* 
 // print all remaining options
 */
 for(; optind < argc; optind++)
  printf("argument: %s\n", argv[optind]);

 return 0;
}

void print_help(int exval) {
 printf("%s,%s show working getopt example\n", PACKAGE, VERSION); 
 printf("%s [-h] [-V] [-v] [-d INT] [-f FILE] [-o FILE]\n\n", PACKAGE);

 printf("  -h              print this help and exit\n");
 printf("  -V              print version and exit\n\n");

 printf("  -v              set verbose flag\n");
 printf("  -d INT          set `-d' to `INT'\n");
 printf("  -f FILE         set intput file\n");
 printf("  -o FILE         set output file\n\n");

 exit(exval);
}

int isthisanint(char *str) {
 regex_t re;
 int retval = -1;

 setlocale(LC_ALL, "");
 if(regcomp(&re, "^([0-9]*)$", REG_EXTENDED) != 0) {
  fprintf(stderr, "%s: Error - Unable to compile regex", PACKAGE);
  return -1;
 }

 if(regexec(&re, str, 0, NULL, 0) != 0)
  retval = -1;
 else
  retval = atoi(str);

 /* or.. Like listed in the manual page */
 /*  strtol(nptr, (char **)NULL, 10); */

 regfree(&re);
 return retval;
}


Hope that left you with some usable idea's... 

GoodLuck..

J.

--
http://www.rdrs.net/



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getopt() library function options
  2005-03-04 21:16 J.
@ 2005-03-05  9:57 ` Rechberger Markus
  2005-03-05 12:18   ` J.
  0 siblings, 1 reply; 9+ messages in thread
From: Rechberger Markus @ 2005-03-05  9:57 UTC (permalink / raw)
  To: linux-c-programming

Hey,

regarding atoi:

#include <stdio.h>

int main(){
        printf("value: %d\n",atoi("nosegv123"));
        return(0);
}
output:
value: 0

this doesn't segfault


SYNOPSIS
       #include <ctype.h>

       int isalnum(int c);
       int isalpha(int c);
       int isascii(int c);
       int isblank(int c);
       int iscntrl(int c);
       int isdigit(int c);
       int isgraph(int c);
       int islower(int c);
       int isprint(int c);
       int ispunct(int c);
       int isspace(int c);
       int isupper(int c);
       int isxdigit(int c);

ctype only takes one variable as argument _not_ a string, he would
have to check every element of the array with isdigit.

Markus
On Fri, 4 Mar 2005 22:16:54 +0100 (CET), J. <mailing-lists@xs4all.nl> wrote:
> On Thu, 3 Mar 2005, Fabio wrote:
> 
> > Hello,
> >
> > I am coding a small utility for system administrator.
> > The following command line options will be accepted:
> 
> ........
> 
> > This was I got on a getopt() man page, I understand some basic concept,
> > but I cant put the unaccpted arguments to work. Thanks alot if someone
> > can build this from scratch.
> >
> > Thanks in advance,
> >
> > fabio.
> 
> Friday, March 03
> 
> You could use an regex to determine if your dealing with an INT or
> not.. Or check the option string with isdigit() <ctype.h>.
> 
> If atoi is used on non-digit conversion the result is a segfault.
> Use strtol family instead because atoi can't detect errors. Also
> casting return value's is a bad idea since they can hide errors..
> 
> your code with might looklike something like this....
> 
> #include <stdio.h>
> #include <getopt.h>
> #include <stdlib.h>
> 
> #include <regex.h>
> #include <locale.h>
> 
> #define PACKAGE    "getoptex"
> #define VERSION    "0.0.1"
> 
> void print_help(int exval);
> int isthisanint(char *str);
> 
> int main(int argc, char *argv[]) {
>  int opt;
> 
>  /*
>  // no arguments given
>  */
>  if(argc == 1) {
>   fprintf(stderr, "This program needs arguments....\n\n");
>   print_help(1);
>  }
> 
>  while((opt = getopt(argc, argv, "hVvd:f:o:")) != -1) {
>   switch(opt) {
>    case 'h':
>     print_help(0);
>    case 'V':
>     printf("%s %s\n\n", PACKAGE, VERSION);
>     exit(0);
>    case 'v':
>     printf("%s: Verbose option is set `%c'\n", PACKAGE, optopt);
>     break;
>    case 'd':
>     printf("%s: isthisanint(%s), says: %d\n", PACKAGE,
>       optarg, isthisanint(optarg));
>     break;
>    case 'f':
>     printf("%s: Filename %s\n", PACKAGE, optarg);
>     break;
>    case 'o':
>     printf("Output: %s\n", optarg);
>     break;
>    case ':':
>     fprintf(stderr, "%s: Error - Option `%c' needs a value\n\n", PACKAGE, optopt);
>     exit(1);
>    case '?':
>     fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
>     exit(1);
>    }
>  }
> 
>  /*
>  // print all remaining options
>  */
>  for(; optind < argc; optind++)
>   printf("argument: %s\n", argv[optind]);
> 
>  return 0;
> }
> 
> void print_help(int exval) {
>  printf("%s,%s show working getopt example\n", PACKAGE, VERSION);
>  printf("%s [-h] [-V] [-v] [-d INT] [-f FILE] [-o FILE]\n\n", PACKAGE);
> 
>  printf("  -h              print this help and exit\n");
>  printf("  -V              print version and exit\n\n");
> 
>  printf("  -v              set verbose flag\n");
>  printf("  -d INT          set `-d' to `INT'\n");
>  printf("  -f FILE         set intput file\n");
>  printf("  -o FILE         set output file\n\n");
> 
>  exit(exval);
> }
> 
> int isthisanint(char *str) {
>  regex_t re;
>  int retval = -1;
> 
>  setlocale(LC_ALL, "");
>  if(regcomp(&re, "^([0-9]*)$", REG_EXTENDED) != 0) {
>   fprintf(stderr, "%s: Error - Unable to compile regex", PACKAGE);
>   return -1;
>  }
> 
>  if(regexec(&re, str, 0, NULL, 0) != 0)
>   retval = -1;
>  else
>   retval = atoi(str);
> 
>  /* or.. Like listed in the manual page */
>  /*  strtol(nptr, (char **)NULL, 10); */
> 
>  regfree(&re);
>  return retval;
> }
> 
> Hope that left you with some usable idea's...
> 
> GoodLuck..
> 
> J.
> 
> --
> http://www.rdrs.net/
> 
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getopt() library function options
  2005-03-05  9:57 ` Rechberger Markus
@ 2005-03-05 12:18   ` J.
  2005-03-05 13:37     ` Rechberger Markus
  0 siblings, 1 reply; 9+ messages in thread
From: J. @ 2005-03-05 12:18 UTC (permalink / raw)
  To: linux-c-programming

On Sat, 5 Mar 2005, Rechberger Markus wrote:

> Hey,

Hey.. ;-)
 
> regarding atoi:
> 
> #include <stdio.h>

#include <stdlib.h>

> int main(){
>         printf("value: %d\n",atoi("nosegv123"));
>         return(0);
> }
> output:
> value: 0
> 
> this doesn't segfault

Yes, - atoi in stdlib.h is actually strtol...

But, the answer was about using atoi in combination with 
getopt(), maybe it was my lack of explanation sorry...
Anyway, atoi(optarg) while there is no optarg will segfault...
Then, you will always want to check for digits, since atoi 
can't determine the difference inbetween the argument `0' 
or "nosegv123" . atoi returns the same value for both, thus `0' .

If your program checks something INT times and atoi returns `0'
even on errorlike user input, the program will check `0' times..

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
 char *str1 = "nosegv123";
 char *str2 = "0.012";
 char *str3 = "0";
 char *str4 = "-1";

 printf("str: %s - value: %d\n",str1, atoi(str1));
 printf("str: %s - value: %d\n",str2, atoi(str2));
 printf("str: %s - value: %d\n",str3, atoi(str3));
 printf("str: %s - value: %d\n",argv[1], atoi(argv[1]));
 printf("str: %s - value: %d\n",str4, atoi(str4));
 return(0);
}

> ctype only takes one variable as argument _not_ a string, he would
> have to check every element of the array with isdigit.

Yep.. But *pointers are quite cheap these days I hear.. ;-)

Thnkx..  J.

> > int isthisanint(char *str) {
> >  regex_t re;
> >  int retval = -1;
> > 
> >  setlocale(LC_ALL, "");
> >  if(regcomp(&re, "^([0-9]*)$", REG_EXTENDED) != 0) {
> >   fprintf(stderr, "%s: Error - Unable to compile regex", PACKAGE);
> >   return -1;
> >  }
> > 
> >  if(regexec(&re, str, 0, NULL, 0) != 0)
> >   retval = -1;
> >  else
> >   retval = atoi(str);
> > 
> >  /* or.. Like listed in the manual page */
> >  /*  strtol(nptr, (char **)NULL, 10); */
> > 
> >  regfree(&re);
> >  return retval;
> > }


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: getopt() library function options
  2005-03-05 12:18   ` J.
@ 2005-03-05 13:37     ` Rechberger Markus
  0 siblings, 0 replies; 9+ messages in thread
From: Rechberger Markus @ 2005-03-05 13:37 UTC (permalink / raw)
  To: linux-c-programming

On Sat, 5 Mar 2005 13:18:26 +0100 (CET), J. <mailing-lists@xs4all.nl> wrote:
> On Sat, 5 Mar 2005, Rechberger Markus wrote:
> 
> > Hey,
> 
> Hey.. ;-)
> 
> > regarding atoi:
> >
> > #include <stdio.h>
> 
> #include <stdlib.h>
> 
> > int main(){
> >         printf("value: %d\n",atoi("nosegv123"));
> >         return(0);
> > }
> > output:
> > value: 0
> >
> > this doesn't segfault
> 
> Yes, - atoi in stdlib.h is actually strtol...
> 
> But, the answer was about using atoi in combination with
> getopt(), maybe it was my lack of explanation sorry...
> Anyway, atoi(optarg) while there is no optarg will segfault...

for that case we have the doublepoint in optarg which says that it
needs an argument otherwise it would return for example:
./main: option requires an argument -- n

value=atoi(optarg)
if(value==0&&*optarg!='0'){
  printf("invalid argument\n");
  exit(1);
}

this doesn't check overflow/underflow strtol would probably be better
in that case.. the manpage explains this quite accurate..

Markus

> Then, you will always want to check for digits, since atoi
> can't determine the difference inbetween the argument `0'
> or "nosegv123" . atoi returns the same value for both, thus `0' .
> 
> If your program checks something INT times and atoi returns `0'
> even on errorlike user input, the program will check `0' times..
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> int main(int argc, char *argv[]){
>  char *str1 = "nosegv123";
>  char *str2 = "0.012";
>  char *str3 = "0";
>  char *str4 = "-1";
> 
>  printf("str: %s - value: %d\n",str1, atoi(str1));
>  printf("str: %s - value: %d\n",str2, atoi(str2));
>  printf("str: %s - value: %d\n",str3, atoi(str3));
>  printf("str: %s - value: %d\n",argv[1], atoi(argv[1]));
>  printf("str: %s - value: %d\n",str4, atoi(str4));
>  return(0);
> }
> 
> > ctype only takes one variable as argument _not_ a string, he would
> > have to check every element of the array with isdigit.
> 
> Yep.. But *pointers are quite cheap these days I hear.. ;-)
> 
> Thnkx..  J.
> 
> > > int isthisanint(char *str) {
> > >  regex_t re;
> > >  int retval = -1;
> > >
> > >  setlocale(LC_ALL, "");
> > >  if(regcomp(&re, "^([0-9]*)$", REG_EXTENDED) != 0) {
> > >   fprintf(stderr, "%s: Error - Unable to compile regex", PACKAGE);
> > >   return -1;
> > >  }
> > >
> > >  if(regexec(&re, str, 0, NULL, 0) != 0)
> > >   retval = -1;
> > >  else
> > >   retval = atoi(str);
> > >
> > >  /* or.. Like listed in the manual page */
> > >  /*  strtol(nptr, (char **)NULL, 10); */
> > >
> > >  regfree(&re);
> > >  return retval;
> > > }
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-03-05 13:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-04  4:35 getopt() library function options Fabio
2005-03-04  8:42 ` Steve Graegert
2005-03-04  8:49 ` Rechberger Markus
2005-03-04 20:37 ` J.
  -- strict thread matches above, loose matches on Subject: below --
2005-03-04 15:42 Huber, George K RDECOM CERDEC STCD SRI
2005-03-04 21:16 J.
2005-03-05  9:57 ` Rechberger Markus
2005-03-05 12:18   ` J.
2005-03-05 13:37     ` Rechberger Markus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).