Subject: Re: __UNCONST(a)
To: Christos Zoulas <christos@zoulas.com>
From: Pavel Cahyna <pavel.cahyna@st.cuni.cz>
List: tech-userlevel
Date: 07/05/2004 14:20:43
Hello,

On Wed, 30 Jun 2004 12:21:42 +0000, Christos Zoulas wrote:

> On Jun 30,  4:44pm, lukem@NetBSD.org (Luke Mewburn) wrote:
> | Is this to work around gcc and/or lint being overly aggressive
> | in enforcing "const"ness of a type?  Or bugs in those tools?
> 
> No, the tools do what they were asked for. Write strchr(),
> and make it work without warnings setting WARNS=3... The problem
> is const castaways are parts of the apis of functions such as
> strchr(), strtol() etc:

Why not implement strchr as a macro which returns const char * if the
input is const char * and a non-const * if the input is non-const *?

static inline long __strindex(const char *p, int ch)
{
        long i = 0;
        for (;; ++i) {
                if (p[i] == ch) {
                        return i;
                }
                if (!p[i])
                        return -1L;
        }
}

#define strchr(p, ch) ((__strindex(p,ch)== -1 ? NULL : (p)+__strindex(p,ch)))

Bye	Pavel