# string_copying - copying strings and character sequences - man(7) - [phpMan]

[_string_copying_(7)](https://www.chedong.com/phpMan.php/man/stringcopying/7/markdown)                 Miscellaneous Information Manual                 [_string_copying_(7)](https://www.chedong.com/phpMan.php/man/stringcopying/7/markdown)

## NAME
       stpcpy, strcpy, strcat, stpecpy, strtcpy, strlcpy, strlcat, stpncpy, strncpy, strncat - copy‐
       ing strings and character sequences

## SYNOPSIS
### Strings
       // Chain-copy a string.
       **char *stpcpy(char *restrict _**dst_**, const char *restrict _**src_**);**

       // Copy/catenate a string.
       **char *strcpy(char *restrict _**dst_**, const char *restrict _**src_**);**
       **char *strcat(char *restrict _**dst_**, const char *restrict _**src_**);**

       // Chain-copy a string with truncation.
       **char *stpecpy(char *_**dst_**, char _**end_**[0], const char *restrict _**src_**);**

       // Copy/catenate a string with truncation.
       **ssize_t strtcpy(char _**dst_**[restrict ._**dsize_**], const char *restrict _**src_**,**
                      **size_t _**dsize_**);**
       **size_t strlcpy(char _**dst_**[restrict ._**dsize_**], const char *restrict _**src_**,**
                      **size_t _**dsize_**);**
       **size_t strlcat(char _**dst_**[restrict ._**dsize_**], const char *restrict _**src_**,**
                      **size_t _**dsize_**);**

### Null-padded character sequences
       // Fill a fixed-size buffer with characters from a string
       // and pad with null bytes.
       **char *strncpy(char _**dst_**[restrict ._**dsize_**], const char *restrict _**src_**,**
                      **size_t _**dsize_**);**
       **char *stpncpy(char _**dst_**[restrict ._**dsize_**], const char *restrict _**src_**,**
                      **size_t _**dsize_**);**

       // Chain-copy a null-padded character sequence into a character sequence.
       _mempcpy(dst,_ _src,_ _strnlen(src,_ _NITEMS(src)));_

       // Chain-copy a null-padded character sequence into a string.
       _stpcpy(mempcpy(dst,_ _src,_ _strnlen(src,_ _NITEMS(src))),_ _"");_

       // Catenate a null-padded character sequence into a string.
       **char *strncat(char *restrict _**dst_**, const char _**src_**[restrict ._**ssize_**],**
                      **size_t _**ssize_**);**

### Known-length character sequences
       // Chain-copy a known-length character sequence.
       **void *mempcpy(void _**dst_**[restrict ._**len_**], const void _**src_**[restrict ._**len_**],**
                      **size_t _**len_**);**

       // Chain-copy a known-length character sequence into a string.
       _stpcpy(mempcpy(dst,_ _src,_ _len),_ _"");_

## DESCRIPTION
### Terms (and abbreviations)
       _string_ (_str_)
              is a sequence of zero or more non-null characters followed by a null character.

       _character_ _sequence_
              is a sequence of zero or more non-null characters.  A program should never use a char‐
              acter  sequence  where a string is required.  However, with appropriate care, a string
              can be used in the place of a character sequence.

              _null-padded_ _character_ _sequence_
                     Character sequences can be  contained  in  fixed-size  buffers,  which  contain
                     padding null bytes after the character sequence, to fill the rest of the buffer
                     without affecting the character sequence; however, those padding null bytes are
                     not part of the character sequence.  Don't confuse null-padded with null-termi‐
                     nated:  null-padded  means  0 or more padding null bytes, while null-terminated
                     means exactly 1 terminating null character.

              _known-length_ _character_ _sequence_
                     Character sequence delimited by its length.  It may be  a  slice  of  a  larger
                     character sequence, or even of a string.

       _length_ (_len_)
              is the number of non-null characters in a string or character sequence.  It is the re‐
              turn value of _strlen(str)_ and of _strnlen(buf,_ _size)_.

       _size_   refers to the entire buffer where the string or character sequence is contained.

       _end_    is  the  name of a pointer to one past the last element of a buffer.  It is equivalent
              to _&str[size]_.  It is used as a sentinel value, to be  able  to  truncate  strings  or
              character sequences instead of overrunning the containing buffer.

       _copy_   This term is used when the writing starts at the first element pointed to by _dst_.

       _catenate_
              This  term  is used when a function first finds the terminating null character in _dst_,
              and then starts writing at that position.

       _chain_  This term is used when it's the programmer who provides a pointer to  the  terminating
              null  character  in the string _dst_ (or one after the last character in a character se‐
              quence), and the function starts writing at that location.   The  function  returns  a
              pointer  to  the new location of the terminating null character (or one after the last
              character in a character sequence) after the call, so that the programmer can  use  it
              to chain such calls.

### Copy, catenate, and chain-copy
       Originally,  there  was  a  distinction  between functions that copy and those that catenate.
       However, newer functions that copy while allowing chaining cover both use cases with a single
       API.  They are also algorithmically faster, since they don't need to search for the terminat‐
       ing null character of the existing string.  However, functions that catenate have a much sim‐
       pler use, so if performance is not important, it can make sense to  use  them  for  improving
       readability.

       The  pointer  returned by functions that allow chaining is a byproduct of the copy operation,
       so it has no performance costs.  Functions that return  such  a  pointer,  and  thus  can  be
       chained, have names of the form ***stp***(), since it's common to name the pointer just _p_.

       Chain-copying  functions  that truncate should accept a pointer to the end of the destination
       buffer, and have names of the form ***stpe***().  This allows not having to recalculate  the  re‐
       maining size after each call.

### Truncate or not?
       The  first  thing  to note is that programmers should be careful with buffers, so they always
       have the correct size, and truncation is not necessary.

       In most cases, truncation is not desired, and it is simpler to just  do  the  copy.   Simpler
       code  is  safer code.  Programming against programming mistakes by adding more code just adds
       more points where mistakes can be made.

       Nowadays, compilers can detect most programmer errors with features like  compiler  warnings,
       static  analyzers,  and  **_FORTIFY_SOURCE  **(see  [**ftm**(7)](https://www.chedong.com/phpMan.php/man/ftm/7/markdown)).  Keeping the code simple helps these
       overflow-detection features be more precise.

       When validating user input, code should normally not truncate, but instead fail  and  prevent
       the copy at all.

       In some cases, however, it makes sense to truncate.

       Functions that truncate:

       •  **stpecpy**()

       •  **strtcpy**()

       •  [**strlcpy**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcpy/3bsd/markdown)  and [**strlcat**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcat/3bsd/markdown) are similar, but have important performance problems; see
          BUGS.

       •  [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown) and [**strncpy**(3)](https://www.chedong.com/phpMan.php/man/strncpy/3/markdown) also truncate, but they don't write strings,  but  rather  null-
          padded character sequences.

### Null-padded character sequences
       For  historic  reasons, some standard APIs and file formats, such as [**utmpx**(5)](https://www.chedong.com/phpMan.php/man/utmpx/5/markdown) and [**tar**(1)](https://www.chedong.com/phpMan.php/man/tar/1/markdown), use
       null-padded character sequences in fixed-size buffers.  To interface with  them,  specialized
       functions need to be used.

       To copy bytes from strings into these buffers, use [**strncpy**(3)](https://www.chedong.com/phpMan.php/man/strncpy/3/markdown) or [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown).

       To  read  a  null-padded  character sequence, use _strnlen(src,_ _NITEMS(src))_, and then you can
       treat it as a known-length character sequence; or use [**strncat**(3)](https://www.chedong.com/phpMan.php/man/strncat/3/markdown) directly.

### Known-length character sequences
       The simplest character sequence copying function is [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown).  It requires  always  knowing
       the  length of your character sequences, for which structures can be used.  It makes the code
       much faster, since you always know the length of your character sequences,  and  can  do  the
       minimal  copies  and length measurements.  [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown) copies character sequences, so you need
       to explicitly set the terminating null character if you need a string.

       In programs that make considerable use of strings or character sequences, and need  the  best
       performance,  using  overlapping  character  sequences  can make a big difference.  It allows
       holding subsequences of a larger character sequence, while not duplicating memory  nor  using
       time to do a copy.

       However,  this  is delicate, since it requires using character sequences.  C library APIs use
       strings, so programs that use character sequences will have to take care  of  differentiating
       strings from character sequences.

       To copy a known-length character sequence, use [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown).

       To     copy     a     known-length     character     sequence     into    a    string,    use
       _stpcpy(mempcpy(dst,_ _src,_ _len),_ _"")_.

       A string is also accepted as input, because [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown) asks for the length, and a  string  is
       composed of a character sequence of the same length plus a terminating null character.

### String vs character sequence
       Some  functions  only  operate on strings.  Those require that the input _src_ is a string, and
       guarantee an output string (even when truncation occurs).  Functions that catenate  also  re‐
       quire that _dst_ holds a string before the call.  List of functions:

       •  [**stpcpy**(3)](https://www.chedong.com/phpMan.php/man/stpcpy/3/markdown)
       •  [**strcpy**(3)](https://www.chedong.com/phpMan.php/man/strcpy/3/markdown), [**strcat**(3)](https://www.chedong.com/phpMan.php/man/strcat/3/markdown)
       •  **stpecpy**()
       •  **strtcpy**()
       •  [**strlcpy**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcpy/3bsd/markdown), [**strlcat**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcat/3bsd/markdown)

       Other  functions  require  an input string, but create a character sequence as output.  These
       functions have confusing names, and have a long history of misuse.  List of functions:

       •  [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown)
       •  [**strncpy**(3)](https://www.chedong.com/phpMan.php/man/strncpy/3/markdown)

       Other functions operate on an input character sequence, and create an output  string.   Func‐
       tions  that catenate also require that _dst_ holds a string before the call.  [**strncat**(3)](https://www.chedong.com/phpMan.php/man/strncat/3/markdown) has an
       even more misleading name than the functions above.  List of functions:

       •  [**strncat**(3)](https://www.chedong.com/phpMan.php/man/strncat/3/markdown)

       Other functions operate on an input character sequence to  create  an  output  character  se‐
       quence.  List of functions:

       •  [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown)

### Functions
       [**stpcpy**(3)](https://www.chedong.com/phpMan.php/man/stpcpy/3/markdown)
              Copy  the  input  string into a destination string.  The programmer is responsible for
              allocating a buffer large enough.  It returns a pointer suitable for chaining.

       [**strcpy**(3)](https://www.chedong.com/phpMan.php/man/strcpy/3/markdown)
       [**strcat**(3)](https://www.chedong.com/phpMan.php/man/strcat/3/markdown)
              Copy and catenate the input string into a destination string.  The programmer  is  re‐
              sponsible for allocating a buffer large enough.  The return value is useless.

              [**stpcpy**(3)](https://www.chedong.com/phpMan.php/man/stpcpy/3/markdown) is a faster alternative to these functions.

       **stpecpy**()
              Chain-copy  the  input  string  into a destination string.  If the destination buffer,
              limited by a pointer to its end, isn't large enough to hold the  copy,  the  resulting
              string  is  truncated  (but  it  is  guaranteed  to be null-terminated).  It returns a
              pointer suitable for chaining.  Truncation needs to be detected only  once  after  the
              last chained call.

              This function is not provided by any library; see EXAMPLES for a reference implementa‐
              tion.

       **strtcpy**()
              Copy  the  input  string  into  a destination string.  If the destination buffer isn't
              large enough to hold the copy, the resulting string is truncated (but it is guaranteed
              to be null-terminated).  It returns the length of the string, or -1 if it truncated.

              This function is not provided by any library; see EXAMPLES for a reference implementa‐
              tion.

       [**strlcpy**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcpy/3bsd/markdown)
       [**strlcat**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcat/3bsd/markdown)
              Copy and catenate the input string into a  destination  string.   If  the  destination
              buffer, limited by its size, isn't large enough to hold the copy, the resulting string
              is  truncated (but it is guaranteed to be null-terminated).  They return the length of
              the total string they tried to create.

              Check BUGS before using these functions.

              **strtcpy**() and **stpecpy**() are better alternatives to these functions.

       [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown)
              Copy the input string into a destination null-padded character sequence  in  a  fixed-
              size  buffer.   If  the destination buffer, limited by its size, isn't large enough to
              hold the copy, the resulting character sequence is  truncated.   Since  it  creates  a
              character  sequence,  it doesn't need to write a terminating null character.  It's im‐
              possible to distinguish truncation by the result of the call,  from  a  character  se‐
              quence that just fits the destination buffer; truncation should be detected by compar‐
              ing the length of the input string with the size of the destination buffer.

       [**strncpy**(3)](https://www.chedong.com/phpMan.php/man/strncpy/3/markdown)
              This function is identical to [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown) except for the useless return value.

              [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown) is a more useful alternative to this function.

       [**strncat**(3)](https://www.chedong.com/phpMan.php/man/strncat/3/markdown)
              Catenate  the  input character sequence, contained in a null-padded fixed-size buffer,
              into a destination string.  The programmer is  responsible  for  allocating  a  buffer
              large enough.  The return value is useless.

              Do not confuse this function with [**strncpy**(3)](https://www.chedong.com/phpMan.php/man/strncpy/3/markdown); they are not related at all.

              _stpcpy(mempcpy(dst,_ _src,_ _strnlen(src,_ _NITEMS(src))),_ _"")_  is  a  faster alternative to
              this function.

       [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown)
              Copy the input character sequence, limited by its length, into a destination character
              sequence.  The programmer is responsible for allocating a buffer large enough.  It re‐
              turns a pointer suitable for chaining.

## RETURN VALUE
       [**stpcpy**(3)](https://www.chedong.com/phpMan.php/man/stpcpy/3/markdown)
              A pointer to the terminating null character in the destination string.

       **stpecpy**()
              A pointer to the terminating null character in the destination string, on success.  On
              error, NULL is returned, and _errno_ is set to indicate the error.

       [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown)
       [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown)
              A pointer to one after the last character in the destination character sequence.

       **strtcpy**()
              The length of the string, on success.  On error, -1 is returned, and _errno_ is  set  to
              indicate the error.

       [**strlcpy**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcpy/3bsd/markdown)
       [**strlcat**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcat/3bsd/markdown)
              The  length of the total string that they tried to create (as if truncation didn't oc‐
              cur).

       [**strcpy**(3)](https://www.chedong.com/phpMan.php/man/strcpy/3/markdown)
       [**strcat**(3)](https://www.chedong.com/phpMan.php/man/strcat/3/markdown)
       [**strncpy**(3)](https://www.chedong.com/phpMan.php/man/strncpy/3/markdown)
       [**strncat**(3)](https://www.chedong.com/phpMan.php/man/strncat/3/markdown)
              The _dst_ pointer, which is useless.

## ERRORS
       Most of these functions don't set _errno_.

       **stpecpy**()
       **strtcpy**()

              **ENOBUFS**
                     _dsize_ was **0**.

              **E2BIG  **The string has been truncated.

## NOTES
       The Linux kernel has an internal function for copying strings, [**strscpy**(9)](https://www.chedong.com/phpMan.php/man/strscpy/9/markdown), which is identical
       to **strtcpy**(), except that it returns **-E2BIG **instead of -1 and it doesn't set _errno_.

## CAVEATS
       Don't mix chain calls to truncating and non-truncating functions.  It is  conceptually  wrong
       unless  you know that the first part of a copy will always fit.  Anyway, the performance dif‐
       ference will probably be negligible, so it will probably be more clear if you use  consistent
       semantics:  either  truncating  or non-truncating.  Calling a non-truncating function after a
       truncating one is necessarily wrong.

## BUGS
       All catenation functions share the same performance problem: [Shlemiel the painter](<https://www.joelonsoftware.com/2001/12/11/back-to-basics/>).  As a mit‐
       igation, compilers are able to transform some calls to catenation functions into normal  copy
       functions, since _strlen(dst)_ is usually a byproduct of the previous copy.

       [**strlcpy**(3)](https://www.chedong.com/phpMan.php/man/strlcpy/3/markdown)  and [**strlcat**(3)](https://www.chedong.com/phpMan.php/man/strlcat/3/markdown) need to read the entire _src_ string, even if the destination buffer
       is small.  This makes them vulnerable to Denial of Service (DoS) attacks if an  attacker  can
       control the length of the _src_ string.  And if not, they're still unnecessarily slow.

## EXAMPLES
       The following are examples of correct use of each of these functions.

       [**stpcpy**(3)](https://www.chedong.com/phpMan.php/man/stpcpy/3/markdown)
              p = buf;
              p = stpcpy(p, "Hello ");
              p = stpcpy(p, "world");
              p = stpcpy(p, "!");
              len = p - buf;
              puts(buf);

       [**strcpy**(3)](https://www.chedong.com/phpMan.php/man/strcpy/3/markdown)
       [**strcat**(3)](https://www.chedong.com/phpMan.php/man/strcat/3/markdown)
              strcpy(buf, "Hello ");
              strcat(buf, "world");
              strcat(buf, "!");
              len = strlen(buf);
              puts(buf);

       **stpecpy**()
              end = buf + NITEMS(buf);
              p = buf;
              p = stpecpy(p, end, "Hello ");
              p = stpecpy(p, end, "world");
              p = stpecpy(p, end, "!");
              if (p == NULL) {
                  len = NITEMS(buf) - 1;
                  goto toolong;
              }
              len = p - buf;
              puts(buf);

       **strtcpy**()
              len = strtcpy(buf, "Hello world!", NITEMS(buf));
              if (len == -1)
                  goto toolong;
              puts(buf);

       [**strlcpy**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcpy/3bsd/markdown)
       [**strlcat**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcat/3bsd/markdown)
              if (strlcpy(buf, "Hello ", NITEMS(buf)) >= NITEMS(buf))
                  goto toolong;
              if (strlcat(buf, "world", NITEMS(buf)) >= NITEMS(buf))
                  goto toolong;
              len = strlcat(buf, "!", NITEMS(buf));
              if (len >= NITEMS(buf))
                  goto toolong;
              puts(buf);

       [**stpncpy**(3)](https://www.chedong.com/phpMan.php/man/stpncpy/3/markdown)
              p = stpncpy(u->ut_user, "alx", NITEMS(u->ut_user));
              if (NITEMS(u->ut_user) < strlen("alx"))
                  goto toolong;
              len = p - u->ut_user;
              fwrite(u->ut_user, 1, len, stdout);

       [**strncpy**(3)](https://www.chedong.com/phpMan.php/man/strncpy/3/markdown)
              strncpy(u->ut_user, "alx", NITEMS(u->ut_user));
              if (NITEMS(u->ut_user) < strlen("alx"))
                  goto toolong;
              len = strnlen(u->ut_user, NITEMS(u->ut_user));
              fwrite(u->ut_user, 1, len, stdout);

       _mempcpy(dst,_ _src,_ _strnlen(src,_ _NITEMS(src)))_
              char  buf[NITEMS(u->ut_user)];
              p = buf;
              p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user)));
              len = p - buf;
              fwrite(buf, 1, len, stdout);

       _stpcpy(mempcpy(dst,_ _src,_ _strnlen(src,_ _NITEMS(src))),_ _"")_
              char  buf[NITEMS(u->ut_user) + 1];
              p = buf;
              p = mempcpy(p, u->ut_user, strnlen(u->ut_user, NITEMS(u->ut_user)));
              p = stpcpy(p, "");
              len = p - buf;
              puts(buf);

       [**strncat**(3)](https://www.chedong.com/phpMan.php/man/strncat/3/markdown)
              char  buf[NITEMS(u->ut_user) + 1];
              strcpy(buf, "");
              strncat(buf, u->ut_user, NITEMS(u->ut_user));
              len = strlen(buf);
              puts(buf);

       [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown)
              p = buf;
              p = mempcpy(p, "Hello ", 6);
              p = mempcpy(p, "world", 5);
              p = mempcpy(p, "!", 1);
              len = p - buf;
              fwrite(buf, 1, len, stdout);

       _stpcpy(mempcpy(dst,_ _src,_ _len),_ _"")_
              p = buf;
              p = mempcpy(p, "Hello ", 6);
              p = mempcpy(p, "world", 5);
              p = mempcpy(p, "!", 1);
              p = stpcpy(p, "");
              len = p - buf;
              puts(buf);

### Implementations
       Here are reference implementations for functions not provided by libc.

           /* This code is in the public domain. */

           char *
           _stpecpy_(char *dst, char end[0], const char *restrict src)
           {
               size_t  dlen;

               if (dst == NULL)
                   return NULL;

               dlen = strtcpy(dst, src, end - dst);
               return (dlen == -1) ? NULL : dst + dlen;
           }

           ssize_t
           _strtcpy_(char *restrict dst, const char *restrict src, size_t dsize)
           {
               bool    trunc;
               size_t  dlen, slen;

               if (dsize == 0) {
                   errno = ENOBUFS;
                   return -1;
               }

               slen = strnlen(src, dsize);
               trunc = (slen == dsize);
               dlen = slen - trunc;

               stpcpy(mempcpy(dst, src, dlen), "");
               if (trunc)
                   errno = E2BIG;
               return trunc ? -1 : slen;
           }

## SEE ALSO
       [**bzero**(3)](https://www.chedong.com/phpMan.php/man/bzero/3/markdown),  [**memcpy**(3)](https://www.chedong.com/phpMan.php/man/memcpy/3/markdown),  [**memccpy**(3)](https://www.chedong.com/phpMan.php/man/memccpy/3/markdown),  [**mempcpy**(3)](https://www.chedong.com/phpMan.php/man/mempcpy/3/markdown),  [**stpcpy**(3)](https://www.chedong.com/phpMan.php/man/stpcpy/3/markdown),  [**strlcpy**(3bsd)](https://www.chedong.com/phpMan.php/man/strlcpy/3bsd/markdown),  [**strncat**(3)](https://www.chedong.com/phpMan.php/man/strncat/3/markdown), **stp‐**
       [**ncpy**(3)](https://www.chedong.com/phpMan.php/man/ncpy/3/markdown), [**string**(3)](https://www.chedong.com/phpMan.php/man/string/3/markdown)

Linux man-pages 6.7                          2023-12-17                            [_string_copying_(7)](https://www.chedong.com/phpMan.php/man/stringcopying/7/markdown)
