Mac Developer Library Developer
Search

 

This manual page is part of Xcode Tools version 5.0

To obtain these tools:

If you are running a version of Xcode Tools other than 5.0, view the documentation locally:

  • In Xcode

  • In Terminal, using the man(1) command

Reading manual pages

Manual pages are intended as a quick reference for people who already understand a technology.

  • To learn how the manual is organized or to learn about command syntax, read the manual page for manpages(5).

  • For more information about this technology, look for other documentation in the Apple Developer Library.

  • For general information about writing shell scripts, read Shell Scripting Primer.




STRCAT(3)                BSD Library Functions Manual                STRCAT(3)

NAME
     strcat, strncat -- concatenate strings

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <string.h>

     char *
     strcat(char *restrict s1, const char *restrict s2);

     char *
     strncat(char *restrict s1, const char *restrict s2, size_t n);

DESCRIPTION
     The strcat() and strncat() functions append a copy of the null-terminated string s2 to the end of the
     null-terminated string s1, then add a terminating `\0'.  The string s1 must have sufficient space to
     hold the result.

     The strncat() function appends not more than n characters from s2, and then adds a terminating `\0'.

     The source and destination strings should not overlap, as the behavior is undefined.

RETURN VALUES
     The strcat() and strncat() functions return the pointer s1.

SECURITY CONSIDERATIONS
     The strcat() function is easily misused in a manner which enables malicious users to arbitrarily change
     a running program's functionality through a buffer overflow attack.  (See the FSA.)

     Avoid using strcat().  Instead, use strncat() or strlcat() and ensure that no more characters are
     copied to the destination buffer than it can hold.

     Note that strncat() can also be problematic.  It may be a security concern for a string to be truncated
     at all.  Since the truncated string will not be as long as the original, it may refer to a completely
     different resource and usage of the truncated resource could result in very incorrect behavior.  Exam-ple: Example:
     ple:

     void
     foo(const char *arbitrary_string)
     {
             char onstack[8] = "";

     #if defined(BAD)
             /*
              * This first strcat is bad behavior.  Do not use strcat!
              */
             (void)strcat(onstack, arbitrary_string);        /* BAD! */
     #elif defined(BETTER)
             /*
              * The following two lines demonstrate better use of
              * strncat().
              */
             (void)strncat(onstack, arbitrary_string,
                 sizeof(onstack) - strlen(onstack) - 1);
     #elif defined(BEST)
             /*
              * These lines are even more robust due to testing for
              * truncation.
              */
             if (strlen(arbitrary_string) + 1 >
                 sizeof(onstack) - strlen(onstack))
                     err(1, "onstack would be truncated");
             (void)strncat(onstack, arbitrary_string,
                 sizeof(onstack) - strlen(onstack) - 1);
     #endif
     }

SEE ALSO
     bcopy(3), memccpy(3), memcpy(3), memmove(3), strcpy(3), strlcat(3), strlcpy(3), wcscat(3)

STANDARDS
     The strcat() and strncat() functions conform to ISO/IEC 9899:1990 (``ISO C90'').

BSD                            December 1, 2009                            BSD

Reporting Problems

The way to report a problem with this manual page depends on the type of problem:

Content errors
Report errors in the content of this documentation with the feedback links below.
Bug reports
Report bugs in the functionality of the described tool or API through Bug Reporter.
Formatting problems
Report formatting mistakes in the online version of these pages with the feedback links below.

Feedback