]>
Commit | Line | Data |
---|---|---|
a4560bdf CP |
1 | /* |
2 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | |
3 | * | |
4 | * Permission to use, copy, modify, and distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
15 | */ | |
16 | ||
17 | #include <sys/types.h> | |
18 | #include <string.h> | |
19 | ||
20 | /*\r | |
21 | * Appends src to string dst of size siz (unlike strncat, siz is the\r | |
22 | * full size of dst, not space left). At most siz-1 characters\r | |
23 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)).\r | |
24 | * Returns strlen(src) + MIN(siz, strlen(initial dst)).\r | |
25 | * If retval >= siz, truncation occurred.\r | |
26 | */\r | |
27 | size_t strlcat(char *dst, const char *src, size_t siz) {\r | |
28 | register char *d = dst;\r | |
29 | register const char *s = src;\r | |
30 | register size_t n = siz;\r | |
31 | size_t dlen;\r | |
32 | \r | |
33 | /* Find the end of dst and adjust bytes left but don't go past end */\r | |
34 | while((n-- != 0) && (*d != '\0'))\r | |
35 | d++;\r | |
36 | \r | |
37 | dlen = d - dst;\r | |
38 | n = siz - dlen;\r | |
39 | \r | |
40 | if(!n)\r | |
41 | return dlen + strlen(s);\r | |
42 | \r | |
43 | while(*s != '\0') {\r | |
44 | if(n != 1) {\r | |
45 | *d++ = *s;\r | |
46 | n--;\r | |
47 | }\r | |
48 | \r | |
49 | s++;\r | |
50 | }\r | |
51 | \r | |
52 | *d = '\0';\r | |
53 | \r | |
54 | return dlen + (s - src); /* count does not include NUL */\r | |
55 | }\r | |
56 | ||
57 | /*\r | |
58 | * Copy src to string dst of size siz. At most siz-1 characters\r | |
59 | * will be copied. Always NUL terminates (unless siz == 0).\r | |
60 | * Returns strlen(src); if retval >= siz, truncation occurred.\r | |
61 | */\r | |
62 | size_t strlcpy(char *dst, const char *src, size_t siz) {\r | |
63 | register char *d = dst;\r | |
64 | register const char *s = src;\r | |
65 | register size_t n = siz;\r | |
66 | \r | |
67 | /* Copy as many bytes as will fit */\r | |
68 | if((n != 0) && (--n != 0))\r | |
69 | do {\r | |
70 | if((*d++ = *s++) == 0)\r | |
71 | break;\r | |
72 | } while(--n);\r | |
73 | \r | |
74 | /* Not enough room in dst, add NUL and traverse rest of src */\r | |
75 | if (n == 0) {\r | |
76 | if (siz != 0)\r | |
77 | *d = '\0'; /* NUL-terminate dst */\r | |
78 | while(*s++) {\r | |
79 | /* Empty while */\r | |
80 | };\r | |
81 | }\r | |
82 | \r | |
83 | return s - src - 1; /* count does not include NUL */\r | |
84 | }\r |