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 destination buffer are  zeroed  out
68       (perhaps 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           if (buflen > 0) {
77               strncpy(buf, str, buflen - 1);
78               buf[buflen - 1]= '\0';
79           }
80
81       (Of  course, the above technique ignores the fact that, if src contains
82       more than buflen - 1 bytes, information  is  lost  in  the  copying  to
83       dest.)
84
85   strlcpy()
86       Some  systems  (the  BSDs,  Solaris,  and others) provide the following
87       function:
88
89           size_t strlcpy(char *dest, const char *src, size_t size);
90
91       This function is similar to strncpy(), but it  copies  at  most  size-1
92       bytes  to  dest,  always adds a terminating null byte, and does not pad
93       the destination with (further) null bytes.  This function fixes some of
94       the  problems of strcpy() and strncpy(), but the caller must still han‐
95       dle the possibility of data loss if size  is  too  small.   The  return
96       value  of the function is the length of src, which allows truncation to
97       be easily detected: if the return value is greater  than  or  equal  to
98       size,  truncation  occurred.   If loss of data matters, the caller must
99       either check the arguments before the call, or test the function return
100       value.   strlcpy()  is  not present in glibc and is not standardized by
101       POSIX, but is available on Linux via the libbsd library.
102

BUGS

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

SEE ALSO

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

COLOPHON

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