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.
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() pads  the  remainder  of
26       dest with null bytes.
27
28       A simple implementation of strncpy() might be:
29
30           char*
31           strncpy(char *dest, const char *src, size_t n){
32               size_t i;
33
34               for (i = 0 ; i < n && src[i] != '\0' ; i++)
35                   dest[i] = src[i];
36               for ( ; i < n ; i++)
37                   dest[i] = '\0';
38
39               return dest;
40           }
41

RETURN VALUE

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

CONFORMING TO

47       SVr4, 4.3BSD, C89, C99.
48

NOTES

50       Some programmers consider strncpy() to be inefficient and error  prone.
51       If  the  programmer knows (i.e., includes code to test!)  that the size
52       of dest is greater than the length of src, then strcpy() can be used.
53
54       If there is no terminating null byte in the first n characters of  src,
55       strncpy()  produces  an unterminated string in dest.  Programmers often
56       prevent this mistake by forcing termination as follows:
57
58           strncpy(buf, str, n);
59           if (n > 0)
60               buf[n - 1]= '\0';
61

BUGS

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

SEE ALSO

73       bcopy(3),  memccpy(3),  memcpy(3),  memmove(3),  strdup(3),  stpcpy(3),
74       wcscpy(3), wcsncpy(3)
75

COLOPHON

77       This page is part of release 3.22 of the Linux  man-pages  project.   A
78       description  of  the project, and information about reporting bugs, can
79       be found at http://www.kernel.org/doc/man-pages/.
80
81
82
83GNU                               2009-06-01                         STRCPY(3)
Impressum