1STRCPY(3)                  Linux Programmer's Manual                 STRCPY(3)
2
3
4

NAME

6       strcpy, strncpy - copy a string
7

SYNOPSIS

9       #include <string.h>
10
11       char *strcpy(char *dest, const char *src);
12
13       char *strncpy(char *dest, const char *src, size_t n);
14

DESCRIPTION

16       The  strcpy()  function  copies the string pointed to by src, including
17       the terminating null byte ('\0'), to the buffer  pointed  to  by  dest.
18       The  strings  may  not overlap, and the destination string dest must be
19       large enough to receive the copy.  Beware  of  buffer  overruns!   (See
20       BUGS.)
21
22       The  strncpy()  function is similar, except that at most n bytes of src
23       are copied.  Warning: If there is no null byte among the first n  bytes
24       of src, the string placed in dest will not be null-terminated.
25
26       If  the  length of src is less than n, strncpy() writes additional null
27       bytes to dest to ensure that a total of n bytes are written.
28
29       A simple implementation of strncpy() might be:
30
31           char *
32           strncpy(char *dest, const char *src, size_t n)
33           {
34               size_t i;
35
36               for (i = 0; i < n && src[i] != '\0'; i++)
37                   dest[i] = src[i];
38               for ( ; i < n; i++)
39                   dest[i] = '\0';
40
41               return dest;
42           }
43

RETURN VALUE

45       The strcpy() and strncpy() functions return a pointer to  the  destina‐
46       tion string dest.
47

ATTRIBUTES

49       For   an   explanation   of   the  terms  used  in  this  section,  see
50       attributes(7).
51
52       ┌────────────────────┬───────────────┬─────────┐
53Interface           Attribute     Value   
54       ├────────────────────┼───────────────┼─────────┤
55strcpy(), strncpy() │ Thread safety │ MT-Safe │
56       └────────────────────┴───────────────┴─────────┘

CONFORMING TO

58       POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
59

NOTES

61       Some programmers consider strncpy() to be inefficient and error  prone.
62       If  the  programmer knows (i.e., includes code to test!)  that the size
63       of dest is greater than the length of src, then strcpy() can be used.
64
65       One valid (and intended) use of strncpy() is to copy a C  string  to  a
66       fixed-length  buffer  while  ensuring both that the buffer is not over‐
67       flowed and that unused bytes in the target buffer are zeroed out  (per‐
68       haps  to  prevent  information  leaks if the buffer is to be written to
69       media or transmitted to another process via an interprocess  communica‐
70       tion technique).
71
72       If  there  is  no  terminating  null  byte in the first n bytes of src,
73       strncpy() produces an unterminated string in dest.  If buf  has  length
74       buflen, you can force termination using something like the following:
75
76           strncpy(buf, str, buflen - 1);
77           if (buflen > 0)
78               buf[buflen - 1]= '\0';
79
80       (Of  course, the above technique ignores the fact that, if src contains
81       more than buflen - 1 bytes, information  is  lost  in  the  copying  to
82       dest.)
83
84   strlcpy()
85       Some  systems  (the  BSDs,  Solaris,  and others) provide the following
86       function:
87
88           size_t strlcpy(char *dest, const char *src, size_t size);
89
90       This function is similar to strncpy(), but it  copies  at  most  size-1
91       bytes  to  dest,  always adds a terminating null byte, and does not pad
92       the target with (further) null bytes.  This function fixes some of  the
93       problems  of  strcpy()  and strncpy(), but the caller must still handle
94       the possibility of data loss if size is too small.  The return value of
95       the function is the length of src, which allows truncation to be easily
96       detected: if the return value is greater than or equal to size, trunca‐
97       tion  occurred.   If loss of data matters, the caller must either check
98       the arguments before the call,  or  test  the  function  return  value.
99       strlcpy() is not present in glibc and is not standardized by POSIX, but
100       is available on Linux via the libbsd library.
101

BUGS

103       If the destination string of a strcpy() is not large enough, then  any‐
104       thing  might  happen.   Overflowing  fixed-length  string  buffers is a
105       favorite cracker technique for taking complete control of the  machine.
106       Any  time  a  program  reads  or copies data into a buffer, the program
107       first needs to check that there's enough space.  This may  be  unneces‐
108       sary  if you can show that overflow is impossible, but be careful: pro‐
109       grams can get changed over time, in ways that may make  the  impossible
110       possible.
111

SEE ALSO

113       bcopy(3),  memccpy(3),  memcpy(3),  memmove(3),  stpcpy(3), stpncpy(3),
114       strdup(3), string(3), wcscpy(3), wcsncpy(3)
115

COLOPHON

117       This page is part of release 4.15 of the Linux  man-pages  project.   A
118       description  of  the project, information about reporting bugs, and the
119       latest    version    of    this    page,    can     be     found     at
120       https://www.kernel.org/doc/man-pages/.
121
122
123
124GNU                               2017-09-15                         STRCPY(3)
Impressum