0

I don't have an OS X development environment. I don't have the OS X header files. I'm having to guess some of my library calls. At this point, one of the things I would like to use is strlcpy().

  1. Is strlcpy() part of libc on OS X?

  2. If so, what is the declaration, or where on the web is a copy of the header file?

  3. If not, is it a part of a system library on OS X?

  4. If not, is there another "safe" strcpy() that is available on OS X?

unwind
  • 391,730
  • 64
  • 469
  • 606
david
  • 2,435
  • 1
  • 21
  • 33
  • [Why are strlcpy and strlcat considered insecure?](https://stackoverflow.com/questions/2114896/why-are-strlcpy-and-strlcat-considered-insecure), in particular this answer: https://stackoverflow.com/a/2115015/584518 – Lundin Feb 28 '18 at 07:36
  • 3
    And of course `strcpy` is perfectly safe. People claiming otherwise have been brainwashed. Like with every other C function, library or user-defined, you have to know what the function does in order to use it. – Lundin Feb 28 '18 at 07:49

1 Answers1

2

An extract from man strlcpy on a Mac running macOS High Sierra 10.13.3.

STRLCPY(3) — BSD Library Functions Manual — STRLCPY(3)

NAME

strlcpy, strlcat -- size-bounded string copying and concatenation

LIBRARY

Standard C Library (libc, -lc)

SYNOPSIS

#include <string.h>

size_t strlcpy(char * restrict dst, const char * restrict src, size_t dstsize);

size_t strlcat(char * restrict dst, const char * restrict src, size_t dstsize);

DESCRIPTION

The strlcpy() and strlcat() functions copy and concatenate strings with the same input parameters and output result as snprintf(3). They are designed to be safer, more consistent, and less error prone replacements for the easily misused functions strncpy(3) and strncat(3).

strlcpy() and strlcat() take the full size of the destination buffer and guarantee NUL-termination if there is room. Note that room for the NUL should be included in dstsize.

strlcpy() copies up to dstsize - 1 characters from the string src to dst, NUL-terminating the result if dstsize is not 0.

strlcat() appends string src to the end of dst. It will append at most dstsize - strlen(dst) - 1 characters. It will then NUL-terminate, unless dstsize is 0 or the original dst string was longer than dstsize (in practice this should not happen as it means that either dstsize is incorrect or that dst is not a proper string).

If the src and dst strings overlap, the behavior is undefined.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I understand that OS X is based on BSD under the covers, so I'm happy to accept the answer. But I note that OSX is /NOT BSD/, and absent a header file with the macro declarations for SUSv3 or DARWIN, there is no assurance that any particular BSD function is supported by any particular version of any Mac OS. – david Feb 28 '18 at 07:51
  • That's what I got from `man strlcpy` on a Mac running macOS High Sierra 10.13.3. I haven't gone back in time to check, but I'm tolerably certain that `strlcpy()` will be available on an macOS or on any Mac OS X that people are still running. – Jonathan Leffler Feb 28 '18 at 07:53
  • Sorry, got it now. – david Feb 28 '18 at 09:14