return value of strlcat

  • Thread starter Aleksandar Milivojevic
  • Start date
A

Aleksandar Milivojevic

I had to write implementation of strlcat() (some source from Microsoft
I got was using it, and strlcat/strlcpy doesn't exist in my libc).

Two quick questions.

If NULL is not encountered in first _size_ bytes of _dst_, should
strlcat() return _size_ + strlen(_src_), or should it return _size_?
Some manual pages I found were not exactly clear about this.

If it should return just the _size_ as passed as third parameter to
strlcat(), what it should return in special case where _size_ is zero?
strlen(src)? Or zero (since terminating NULL was obviously never
traversed)?
 
K

Keith Thompson

I had to write implementation of strlcat() (some source from Microsoft
I got was using it, and strlcat/strlcpy doesn't exist in my libc).

Two quick questions.

If NULL is not encountered in first _size_ bytes of _dst_, should
strlcat() return _size_ + strlen(_src_), or should it return _size_?
Some manual pages I found were not exactly clear about this.

If it should return just the _size_ as passed as third parameter to
strlcat(), what it should return in special case where _size_ is zero?
strlen(src)? Or zero (since terminating NULL was obviously never
traversed)?

First, strlcat() is not a standard C function, so we can't really
advise you about what it should do. <OT>A little Googling indicates
that it's provided by OpenBSD. You can probably grab an
implementation from the OpenBSD source tree, or you can ask about it
in comp.unix.bsd.openbsd.misc (I suggest reading their FAQ
first).</OT>

Second, NULL is a macro that expands to a null pointer constant.
Referring to a null charater as NULL is confusing and/or incorrect. A
null character should be referred to as a null character, as '\0', or
as NUL (the latter is a bit charset-specific, but the term is used by
both ASCII and EBCDIC so it's ok in informal use).
 
C

CBFalconer

Aleksandar said:
I had to write implementation of strlcat() (some source from
Microsoft I got was using it, and strlcat/strlcpy doesn't exist
in my libc).

Two quick questions.

If NULL is not encountered in first _size_ bytes of _dst_,
should strlcat() return _size_ + strlen(_src_), or should it
return _size_? Some manual pages I found were not exactly
clear about this.

If it should return just the _size_ as passed as third parameter
to strlcat(), what it should return in special case where _size_
is zero? strlen(src)? Or zero (since terminating NULL was
obviously never traversed)?

strlcat (and strlcpy) are not standard. However you can find
portable source, and a reference to the original OPENBSD article,
at:

<http://cbfalconer.home.att.net/download/strlcpy.zip>

Those will answer your questions.
 
A

Aleksandar Milivojevic

Keith said:
First, strlcat() is not a standard C function, so we can't really
advise you about what it should do. <OT>A little Googling indicates
that it's provided by OpenBSD. You can probably grab an
implementation from the OpenBSD source tree, or you can ask about it
in comp.unix.bsd.openbsd.misc (I suggest reading their FAQ
first).</OT>

Second, NULL is a macro that expands to a null pointer constant.
Referring to a null charater as NULL is confusing and/or incorrect. A
null character should be referred to as a null character, as '\0', or
as NUL (the latter is a bit charset-specific, but the term is used by
both ASCII and EBCDIC so it's ok in informal use).

Thanks to both you and CBF for rather quick and helpfull responses. I
saw the mistake about NULL (as soon as I sent my question). What I ment
was ASCII NUL character ('\0'), not the null pointer constant. Ah,
shame on me...
 
R

Richard Bos

Keith Thompson said:
First, strlcat() is not a standard C function, so we can't really
advise you about what it should do. <OT>A little Googling indicates
that it's provided by OpenBSD.

But note that _because_ it is not ISO C, you do not know a priori that
the Microsoft strlcat() did exactly what the Open BSD one does. It might
be wise to check that it does.

Richard
 
A

Aleksandar Milivojevic

But note that _because_ it is not ISO C, you do not know a priori that
the Microsoft strlcat() did exactly what the Open BSD one does. It might
be wise to check that it does.

The strlcat() function is called from part of source that would be
(normally) compilied only on AIX. However because of the changes I'm
making, it will be included for other systems also. They also used
strlcpy() in parts of source that compiles on AIX, Linux, Solaris and
HP/UX, so there's Microsoft's implementation of strlcpy() provided
that does (by looking at source) exactly as manual page from BSD-ish
system says it should do (but no strlcat() implementation, since AIX
has that function). So I guess it is safe to assume that Microsoft
strlcat() does the same thing as BSD strlcat().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top