Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: How can I validate and store IP address that user enter from command line?

Reply
Thread Tools

Re: How can I validate and store IP address that user enter from command line?

 
 
sumit.sharma@wipro.com
Guest
Posts: n/a
 
      07-09-2003
Hi,

In order to get the inputs from command line, you can us gets () API.

After you get the number, use strchr () to see if the input value contains
"-". if it is, then use strstr to get the substring (range value) starting
from "-".

In this way, you will get two diff strings that will be two IP
address...

In the strings you've got, again use strchr to see that there's atleast
one dot (.). If neither strings contains dot, print invalid string and exit..

otherwise. determine which is the last byte in both the string..
Calculate the difference and allocate array and start STORINg.


(Abby) wrote in message news:<. com>...
> My program will need to get ip address from command line. User can
> enter a single IP or multiple IPs(enter in range).
>
> For examples,
>
> c:\>programname 192.168.0.1 --> user enter a single ip
>
> c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of
> ip
>
> My question is:
>
> 1. How to get a range IP addresses and store them into an array? I'm
> not sure if my example is the easiest way to get it or there're other
> ways easier than this one. I need the program to automatically fill in
> an array from ip 192.168.0.1 until 192.168.0.100. How can I do that?
>
> 2. How can I validate if user has entered valid IP address? Any
> function or easy way to do it?
>
> Thank you so much.
>
> Abb.

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      07-09-2003
wrote:

[ Do not top-post. Learn to snip. ]

> (Abby) wrote in message news:<. com>...
> > My program will need to get ip address from command line. User can
> > enter a single IP or multiple IPs(enter in range).
> >
> > For examples,
> >
> > c:\>programname 192.168.0.1 --> user enter a single ip
> >
> > c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of
> > ip

>
> In order to get the inputs from command line, you can us gets () API.


NO. Not ever. gets() is the single most dangerous function in C, because
it cannot be used safely except with the use of outside appliances, such
as chains and straitjackets. There is no way to prevent it from reading
too much input into its buffer, because there is no way for it to know
how large that buffer is. NEVER use gets(). Use fgets() instead.

Besides, neither gets() nor fgets() reads from the command line. They
read from standard input, which is a different matter entirely. To read
from the command line, use the arguments to main():

int main(int argc, char **argv)

> After you get the number, use strchr () to see if the input value contains
> "-". if it is, then use strstr to get the substring (range value) starting
> from "-".
>
> In this way, you will get two diff strings that will be two IP
> address...


You hope. Make sure that they actually _are_ IP addresses

> In the strings you've got, again use strchr to see that there's atleast
> one dot (.). If neither strings contains dot, print invalid string and exit..


That's not enough. It's a start, but it's not enough.

> > 1. How to get a range IP addresses and store them into an array? I'm
> > not sure if my example is the easiest way to get it or there're other
> > ways easier than this one. I need the program to automatically fill in
> > an array from ip 192.168.0.1 until 192.168.0.100. How can I do that?


Question: how do you want to handle 192.168.1.0 - 192.168.100.0? Subnet
masks of 255.255.0.128 are valid, AFAICT. Unwise, of course, but valid.

> > 2. How can I validate if user has entered valid IP address? Any
> > function or easy way to do it?


Do it by hand, as demonstrated by earlier replies. Note: use strtoul(),
not atoi(), unless you do some pre-checking. You do not want to invoke
undefined behaviour if some joker types "yourprogram 34278459780489034".

Richard
 
Reply With Quote
 
 
 
 
Zoran Cutura
Guest
Posts: n/a
 
      07-09-2003
wrote:
> Hi,
>
> In order to get the inputs from command line, you can us gets () API.


You are kidding?! Never, never use gets(), unless you are Dan Pop and
know which platform you are using.

Also the OP obviously meant to get arguments passed to his program
instead of reading from the standard input stream. There we want to use
the parameters from one of mains legal defintions/declarations:
int main(int argc, char **argv)
where argv will hold pointers to the strings representing the arguments
given at invocation of the program.

>
> After you get the number, use strchr () to see if the input value contains
> "-". if it is, then use strstr to get the substring (range value) starting
> from "-".


If the arguments are read from argv there will be no need to do this.

>
> In this way, you will get two diff strings that will be two IP
> address...


argv[1] and argv[3] should be the two strings.

>
> In the strings you've got, again use strchr to see that there's atleast
> one dot (.). If neither strings contains dot, print invalid string and exit..


Verifying that a string is a ip-address will probably need mor than
this.

>
> otherwise. determine which is the last byte in both the string..
> Calculate the difference and allocate array and start STORINg.
>
>
> (Abby) wrote in message news:<. com>...
>> My program will need to get ip address from command line. User can
>> enter a single IP or multiple IPs(enter in range).
>>
>> For examples,
>>
>> c:\>programname 192.168.0.1 --> user enter a single ip
>>
>> c:\>programname 192.168.0.1 - 192.168.0.100 --> user enter a range of
>> ip
>>
>> My question is:
>>
>> 1. How to get a range IP addresses and store them into an array? I'm
>> not sure if my example is the easiest way to get it or there're other
>> ways easier than this one. I need the program to automatically fill in
>> an array from ip 192.168.0.1 until 192.168.0.100. How can I do that?
>>
>> 2. How can I validate if user has entered valid IP address? Any
>> function or easy way to do it?
>>
>> Thank you so much.
>>
>> Abb.


--
Z ()
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      07-10-2003
(Richard Bos) writes:

> wrote:
>
> > In order to get the inputs from command line, you can us gets () API.

>
> NO. Not ever. gets() is the single most dangerous function in C, because
> it cannot be used safely except with the use of outside appliances, such
> as chains and straitjackets.


Think chains and straitjackets would be enough to make this usage safe
on the DS9000?

....straightjacket for the programmer, maybe

-Micah
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to store the user enter the data's into file muthukkumaran C++ 0 02-19-2010 11:15 AM
User Prompt as a pop up - not IRB/command line? Mmcolli00 Mom Ruby 6 12-12-2008 04:06 PM
Parameters (command line, preferences, user input) Kenneth P. Turvey Java 1 02-25-2006 09:10 AM
How to change user passwords on the command line? Doug Cisco 2 07-14-2004 08:02 PM
Can i check user press the <enter> key in multi-line textbox? Chad Z. Hower aka Kudzu ASP .Net 1 01-23-2004 10:10 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57