bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnulib] Re: getaddrinfo


From: Paul Eggert
Subject: Re: [Bug-gnulib] Re: getaddrinfo
Date: Tue, 09 Nov 2004 10:49:38 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Simon Josefsson <address@hidden> writes:

> Btw, this construct:
>
>   if (hints
> #if HAVE_IPV4
>       && hints->ai_family != PF_INET
> #endif
> #if HAVE_IPV6
>       && hints->ai_family != PF_INET6
> #endif
>       && hints->ai_family != PF_UNSPEC)
>     /* FIXME: Support more families. */
>     return EAI_FAMILY;
>
> is it legal C?

Yes, but it's really ugly.  Please see below.

> I have a vague recollection that using CPP #if's inside a C statement
> in this way was not valid.

It's not valid to use #if's like this inside a macro call.  For example:

   #include <stdlib.h>
   ...
   char *x = getenv (
#if X
                     "X"
#else
                     "Y"
#endif
                     );

isn't valid, since "getenv" might be a macro.

Even though your code valid, it's really ugly and makes the code hard
to read.  I usually find it better to do something like this:

#ifndef PF_INET
# define PF_INET 0.5
#endif
#ifndef PF_INET6
# define PF_INET6 0.5
#endif

at the start, so that the mainline code can look ordinary:

  if (hints
      && hints->ai_family != PF_INET
      && hints->ai_family != PF_INET6
      && hints->ai_family != PF_UNSPEC)

The basic idea is to define the macro to an impossible value if it
isn't already defined.  You can use (-1) instead of 0.5 if you know -1
is impossible.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]