Skip to content

Commit

Permalink
test_server.c: some cleanup
Browse files Browse the repository at this point in the history
* find_server(void): add a line break
* parse_command_line(int, char **): wrap the number parsing code in a new
  function called parse_number
  • Loading branch information
neuschaefer committed Mar 22, 2010
1 parent 4211c0f commit 2561c87
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions test_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void find_server(void)
struct hostent *h;

h=gethostbyname(name);
if (!h){fprintf(stderr,"Error: Can't resolve server address.");exit(1);}
if (!h){fprintf(stderr,"Error: Can't resolve server address.\n");exit(1);}

server.sin_family=AF_INET;
server.sin_port=htons(port);
Expand Down Expand Up @@ -96,10 +96,26 @@ static void print_help(void)
}


static int parse_number (const char * arg)
{
char *c; /* used by strtoul */
int n;

n=strtoul(arg,&c,10);
if (*c){fprintf(stderr,"Error: Not a number.\n");exit(1);}
if (errno==ERANGE)
{
if (!port){fprintf(stderr,"Error: Number underflow.\n");exit(1);}
else {fprintf(stderr,"Error: Number overflow.\n");exit(1);}
}

return n;
}


static void parse_command_line(int argc,char **argv)
{
int a;
char *c;

while((a=getopt(argc,argv,"hp:a:t:")) != -1)
{
Expand All @@ -110,23 +126,11 @@ static void parse_command_line(int argc,char **argv)
break;

case 'p':
port=strtoul(optarg,&c,10);
if (*c){fprintf(stderr,"Error: Not a number.\n");exit(1);}
if (errno==ERANGE)
{
if (!port){fprintf(stderr,"Error: Number underflow.\n");exit(1);}
else {fprintf(stderr,"Error: Number overflow.\n");exit(1);}
}
port=parse_number(optarg);
break;

case 't':
ttime=strtoul(optarg,&c,10);
if (*c){fprintf(stderr,"Error: Not a number.\n");exit(1);}
if (errno==ERANGE)
{
if (!ttime){fprintf(stderr,"Error: Number underflow.\n");exit(1);}
else {fprintf(stderr,"Error: Number overflow.\n");exit(1);}
}
ttime=parse_number(optarg);
if(ttime<1){fprintf(stderr,"Error: Timeout too low.\n");exit(1);}
break;

Expand Down

0 comments on commit 2561c87

Please sign in to comment.