Style (Was: Re: format for declaring variables?)
At about the time of 2/9/2007 2:20 AM, Chris Dollin stated the following:
I've seen code like this that compiles just fine under C with all
warnings enabled (or at least the ones that I have turned on):
int function(int i)
{
switch(i)
{
case 0:
{
int a;
/* do something */
return(0);
}
case 1:
{
char *p;
/* do something */
return(0);
}
case 2:
{
unsigned long lax;
/* do something */
return(0);
}
default:
return(1);
}
return(0);
}
And here's the actual code:
char * inet_ntop_host( const struct sockaddr *sa, socklen_t salen)
{
char portstr[8];
static char str[128];
switch(sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
if (inet_ntop(AF_I NET, &sin->sin_addr, str, sizeof(str)) ==
NULL) return(NULL);
if(ntohs(sin->sin_port) != 0)
{
snprintf(portst r, sizeof(portstr) , ":%d",
ntohs(sin->sin_port));
strcat(str, portstr);
}
return(str);
}
#ifdef IPV6
case AF_INET6:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
if (inet_ntop(AF_I NET6, &sin6->sin6_addr, str, sizeof(str))
== NULL) return(NULL);
if(ntohs(sin6->sin6_port) != 0)
{
snprintf(portst r, sizeof(portstr) , ":%d",
ntohs(sini6->sin6_port));
strcat(str, portstr);
}
return(str);
}
#endif
#ifdef AF_UNIX
case AF_UNIX:
{
struct sockaddr_un *unp = (struct sockaddr_un *) sa;
if (unp->sun_path[0] == 0) strcpy(str, "(no pathname bound)");
else snprintf(str, sizeof(str), "%s", unp->sun_path);
return(str);
}
#endif
default:
snprintf(str, sizeof(str), "sock_ntop_host : unknown AF_xxx:
%d, len %d", sa->sa_family, salen);
return(NULL);
break;
}
return(NULL);
}
--
Daniel Rudy
Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
At about the time of 2/9/2007 2:20 AM, Chris Dollin stated the following:
Daniel Rudy wrote:
>
>
But doesn't work as part of an else-if without putting up
extra scaffolding.
>
(Myself, I want to know why I can't put /declarations/ in
the conditions of an if [1,2].)
>
[1] Because then I don't have to introduce variables before
I know their values.
>
[2] Yes, it can be done cleanly, although I haven't thought
hard about it /for C/.
>
>
>OT: Speaking of style, forms like this bug me:
>>
>if ((fc = open(filename)) < 0)
> errx("file error %d\n", errno);
>>
>How hard is it to sit there and do this instead?
>>
>fc = open(filename);
>if (fc < 0) errx("file error %d\n", errno);
>>
>It's just so much easier to read, less error prone (since there are
>fewer nested () ), and it probably generates the exact same code at the
>machine level.
>>
>if ((fc = open(filename)) < 0)
> errx("file error %d\n", errno);
>>
>How hard is it to sit there and do this instead?
>>
>fc = open(filename);
>if (fc < 0) errx("file error %d\n", errno);
>>
>It's just so much easier to read, less error prone (since there are
>fewer nested () ), and it probably generates the exact same code at the
>machine level.
But doesn't work as part of an else-if without putting up
extra scaffolding.
>
(Myself, I want to know why I can't put /declarations/ in
the conditions of an if [1,2].)
>
[1] Because then I don't have to introduce variables before
I know their values.
>
[2] Yes, it can be done cleanly, although I haven't thought
hard about it /for C/.
>
warnings enabled (or at least the ones that I have turned on):
int function(int i)
{
switch(i)
{
case 0:
{
int a;
/* do something */
return(0);
}
case 1:
{
char *p;
/* do something */
return(0);
}
case 2:
{
unsigned long lax;
/* do something */
return(0);
}
default:
return(1);
}
return(0);
}
And here's the actual code:
char * inet_ntop_host( const struct sockaddr *sa, socklen_t salen)
{
char portstr[8];
static char str[128];
switch(sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
if (inet_ntop(AF_I NET, &sin->sin_addr, str, sizeof(str)) ==
NULL) return(NULL);
if(ntohs(sin->sin_port) != 0)
{
snprintf(portst r, sizeof(portstr) , ":%d",
ntohs(sin->sin_port));
strcat(str, portstr);
}
return(str);
}
#ifdef IPV6
case AF_INET6:
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
if (inet_ntop(AF_I NET6, &sin6->sin6_addr, str, sizeof(str))
== NULL) return(NULL);
if(ntohs(sin6->sin6_port) != 0)
{
snprintf(portst r, sizeof(portstr) , ":%d",
ntohs(sini6->sin6_port));
strcat(str, portstr);
}
return(str);
}
#endif
#ifdef AF_UNIX
case AF_UNIX:
{
struct sockaddr_un *unp = (struct sockaddr_un *) sa;
if (unp->sun_path[0] == 0) strcpy(str, "(no pathname bound)");
else snprintf(str, sizeof(str), "%s", unp->sun_path);
return(str);
}
#endif
default:
snprintf(str, sizeof(str), "sock_ntop_host : unknown AF_xxx:
%d, len %d", sa->sa_family, salen);
return(NULL);
break;
}
return(NULL);
}
--
Daniel Rudy
Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Comment