T
Thomas Matthews
Think about:Roman said:Hello, All!
I wrote function parsing the fully-quilified domain name and extracting
'host' and 'domain' parts seperately. The important thing for me is to keep
original string (pointed by 'fqdn' in function). Is my way of coding
correct, is there a way to optimize function and make it not so clumsy (I
like the code produced by K&R in their famous book: brief, clear,
comprehensible, nothing superfluous ).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 1024
static int
parse_fqdn(char *fqdn, char *host, char *domain)
{
char p[BUFLEN], *s;
1. 1024 chars is a lot to place on the local variable area.
2. One variable declaration per line.
3. Is the variable 'p' necessary?
4. If a parameter points to a constant string or
the function will not modify the string, consider
declaring the parameter as a pointer to a const char.
Think about:strcpy(p, fqdn);
1. What happens when the length of fqdn is greater than BUFLEN?
2. What happens when fqdn is NULL?
Think about:if ( (s = strstr(p, ".")) ) {
1. To prevent typeos and logic errors, compare the result
of a function to a variable or constant.
Although "return 0;" is valid, consider using*s = '\0';
strcpy(host, p);
strcpy(domain, ++s);
return 0;
}
else
return -1;
}
int main(void)
{
char dom[] = "www.my.example.dom.ain";
char h[BUFLEN], d[BUFLEN];
if ( parse_fqdn(dom, h, d) == 0 )
printf("fqdn='%s'\nhostname='%s', domain name='%s'\n", dom, h, d);
return 0;
EXIT_SUCCESS or EXIT_FAILURE. These two named constants
make the code more readable.
}
Thanks in advance
With best regards, Roman Mashak. E-mail: (e-mail address removed)
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library