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