Rev | Author | # | Line |
---|---|---|---|
5 | PerryLorier | 1 | !!!STRCPY |
1 | perry | 2 | ---- |
3 | !!NAME | ||
4 | |||
5 | |||
6 | strcpy, strncpy - copy a string | ||
7 | !!SYNOPSIS | ||
8 | |||
9 | |||
4 | PerryLorier | 10 | #include <string.h> |
3 | PerryLorier | 11 | |
4 | PerryLorier | 12 | __char *strcpy(__''dest''__, const char *__''src''__);__ |
13 | |||
14 | __char *strncpy(char *__''dest''__, const char *__''src''__, size_t__ ''n''__);__ | ||
3 | PerryLorier | 15 | |
1 | perry | 16 | !!DESCRIPTION |
17 | |||
18 | The __strcpy()__ function copies the string pointed to by | ||
19 | ''src'' (including the terminating `0' character) to the | ||
20 | array pointed to by ''dest''. The strings may not | ||
21 | overlap, and the destination string ''dest'' must be | ||
22 | large enough to receive the copy. | ||
23 | |||
24 | The __strncpy()__ function is similar, except that not | ||
25 | more than ''n'' bytes of ''src'' are copied. Thus, if | ||
26 | there is no null byte among the first ''n'' bytes of | ||
27 | ''src'', the result wil not be | ||
28 | null-terminated. | ||
29 | |||
30 | In the case where the length of ''src'' is less than that | ||
31 | of ''n'', the remainder of ''dest'' will be padded | ||
32 | with nulls. | ||
2 | PerryLorier | 33 | |
1 | perry | 34 | !!RETURN VALUE |
35 | The __strcpy()__ and __strncpy()__ functions return a | ||
36 | pointer to the destination string ''dest''. | ||
2 | PerryLorier | 37 | |
1 | perry | 38 | !!BUGS |
39 | If the destination string of a __strcpy()__ is not large | ||
40 | enough (that is, if the programmer was stupid/lazy, and | ||
41 | failed to check the size before copying) then anything might | ||
42 | happen. Overflowing fixed length strings is a favourite | ||
43 | cracker technique. | ||
2 | PerryLorier | 44 | |
1 | perry | 45 | !!CONFORMING TO |
2 | PerryLorier | 46 | SVID 3, POSIX, BSD 4.3, ISO 9899 |
1 | perry | 47 | |
2 | PerryLorier | 48 | !!HISTORY |
6 | HarunVos | 49 | Rumour has it that the reason that strncpy() has such bizarre semantics (copying up to the entire length of the string and not leaving space for the terminating '0', and padding to the end of the buffer with '0's if it's shorter) come from the days when you could read/write a directory. If you wanted to create a hardlink you opened the directory for writing and wrote the filename and the inode number into the directory. The filename had to be exactly ''n'' bytes long and must be '0' padded (instead of just terminated), hence the strncpy() semantics. It's far too late now to change the semantics to something more sane, so the [OpenBSD] people are advocating the use of strlcpy(3) instead. Note, in high performance applications, strlcpy(3) is much faster than strncpy(3) as it doesn't waste time padding the entire string. |
1 | perry | 50 | |
51 | !!SEE ALSO | ||
2 | PerryLorier | 52 | bcopy(3), memccpy(3), memcpy(3), memmove(3) |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 9 times)