linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "J." <mailing-lists@xs4all.nl>
To: linux-c-programming@vger.kernel.org
Subject: Re: getopt() library function options
Date: Sat, 5 Mar 2005 13:18:26 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.21.0503051244020.903-100000@hestia> (raw)
In-Reply-To: <d9def9db05030501573360eeab@mail.gmail.com>

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;
> > }


  reply	other threads:[~2005-03-05 12:18 UTC|newest]

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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0503051244020.903-100000@hestia \
    --to=mailing-lists@xs4all.nl \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).