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 *restrict dest, const char *src);
12       char *strncpy(char *restrict dest, const char *restrict src, size_t n);
13

DESCRIPTION

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

RETURN VALUE

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

ATTRIBUTES

48       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
49       tributes(7).
50
51       ┌────────────────────────────────────────────┬───────────────┬─────────┐
52Interface                                   Attribute     Value   
53       ├────────────────────────────────────────────┼───────────────┼─────────┤
54strcpy(), strncpy()                         │ Thread safety │ MT-Safe │
55       └────────────────────────────────────────────┴───────────────┴─────────┘
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 fa‐
106       vorite 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.13 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                               2021-03-22                         STRCPY(3)
Impressum