From: Rechberger Markus <mrechberger@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: getopt() library function options
Date: Sat, 5 Mar 2005 14:37:19 +0100 [thread overview]
Message-ID: <d9def9db05030505372bc1a63c@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.21.0503051244020.903-100000@hestia>
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
>
next prev parent reply other threads:[~2005-03-05 13:37 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.
2005-03-05 13:37 ` Rechberger Markus [this message]
-- 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=d9def9db05030505372bc1a63c@mail.gmail.com \
--to=mrechberger@gmail.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.