Rev 6934 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6934 | Rev 6936 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * linux/lib/string.c |
2 | * linux/lib/string.c |
3 | * |
3 | * |
4 | * Copyright (C) 1991, 1992 Linus Torvalds |
4 | * Copyright (C) 1991, 1992 Linus Torvalds |
5 | */ |
5 | */ |
6 | 6 | ||
7 | /* |
7 | /* |
8 | * stupid library routines.. The optimized versions should generally be found |
8 | * stupid library routines.. The optimized versions should generally be found |
9 | * as inline code in |
9 | * as inline code in |
10 | * |
10 | * |
11 | * These are buggy as well.. |
11 | * These are buggy as well.. |
12 | * |
12 | * |
13 | * * Fri Jun 25 1999, Ingo Oeser |
13 | * * Fri Jun 25 1999, Ingo Oeser |
14 | * - Added strsep() which will replace strtok() soon (because strsep() is |
14 | * - Added strsep() which will replace strtok() soon (because strsep() is |
15 | * reentrant and should be faster). Use only strsep() in new code, please. |
15 | * reentrant and should be faster). Use only strsep() in new code, please. |
16 | * |
16 | * |
17 | * * Sat Feb 09 2002, Jason Thomas |
17 | * * Sat Feb 09 2002, Jason Thomas |
18 | * Matthew Hawkins |
18 | * Matthew Hawkins |
19 | * - Kissed strtok() goodbye |
19 | * - Kissed strtok() goodbye |
20 | */ |
20 | */ |
21 | 21 | ||
22 | #include |
22 | #include |
23 | #include |
23 | #include |
24 | #include |
24 | #include |
25 | #include |
25 | #include |
26 | #include |
26 | #include |
27 | #include |
27 | #include |
28 | #include |
28 | #include |
29 | 29 | ||
- | 30 | ||
- | 31 | #ifndef __HAVE_ARCH_STRNCPY |
|
- | 32 | /** |
|
- | 33 | * strncpy - Copy a length-limited, C-string |
|
- | 34 | * @dest: Where to copy the string to |
|
- | 35 | * @src: Where to copy the string from |
|
- | 36 | * @count: The maximum number of bytes to copy |
|
- | 37 | * |
|
- | 38 | * The result is not %NUL-terminated if the source exceeds |
|
- | 39 | * @count bytes. |
|
- | 40 | * |
|
- | 41 | * In the case where the length of @src is less than that of |
|
- | 42 | * count, the remainder of @dest will be padded with %NUL. |
|
- | 43 | * |
|
- | 44 | */ |
|
- | 45 | char *strncpy(char *dest, const char *src, size_t count) |
|
- | 46 | { |
|
- | 47 | char *tmp = dest; |
|
- | 48 | ||
- | 49 | while (count) { |
|
- | 50 | if ((*tmp = *src) != 0) |
|
- | 51 | src++; |
|
- | 52 | tmp++; |
|
- | 53 | count--; |
|
- | 54 | } |
|
- | 55 | return dest; |
|
- | 56 | } |
|
- | 57 | EXPORT_SYMBOL(strncpy); |
|
30 | 58 | #endif |
|
31 | 59 | ||
32 | #ifndef __HAVE_ARCH_STRLCPY |
60 | #ifndef __HAVE_ARCH_STRLCPY |
33 | /** |
61 | /** |
34 | * strlcpy - Copy a C-string into a sized buffer |
62 | * strlcpy - Copy a C-string into a sized buffer |
35 | * @dest: Where to copy the string to |
63 | * @dest: Where to copy the string to |
36 | * @src: Where to copy the string from |
64 | * @src: Where to copy the string from |
37 | * @size: size of destination buffer |
65 | * @size: size of destination buffer |
38 | * |
66 | * |
39 | * Compatible with *BSD: the result is always a valid |
67 | * Compatible with *BSD: the result is always a valid |
40 | * NUL-terminated string that fits in the buffer (unless, |
68 | * NUL-terminated string that fits in the buffer (unless, |
41 | * of course, the buffer size is zero). It does not pad |
69 | * of course, the buffer size is zero). It does not pad |
42 | * out the result like strncpy() does. |
70 | * out the result like strncpy() does. |
43 | */ |
71 | */ |
44 | size_t strlcpy(char *dest, const char *src, size_t size) |
72 | size_t strlcpy(char *dest, const char *src, size_t size) |
45 | { |
73 | { |
46 | size_t ret = strlen(src); |
74 | size_t ret = strlen(src); |
47 | 75 | ||
48 | if (size) { |
76 | if (size) { |
49 | size_t len = (ret >= size) ? size - 1 : ret; |
77 | size_t len = (ret >= size) ? size - 1 : ret; |
50 | memcpy(dest, src, len); |
78 | memcpy(dest, src, len); |
51 | dest[len] = '\0'; |
79 | dest[len] = '\0'; |
52 | } |
80 | } |
53 | return ret; |
81 | return ret; |
54 | } |
82 | } |
55 | 83 | ||
56 | #ifndef __HAVE_ARCH_STRLCAT |
84 | #ifndef __HAVE_ARCH_STRLCAT |
57 | /** |
85 | /** |
58 | * strlcat - Append a length-limited, C-string to another |
86 | * strlcat - Append a length-limited, C-string to another |
59 | * @dest: The string to be appended to |
87 | * @dest: The string to be appended to |
60 | * @src: The string to append to it |
88 | * @src: The string to append to it |
61 | * @count: The size of the destination buffer. |
89 | * @count: The size of the destination buffer. |
62 | */ |
90 | */ |
63 | size_t strlcat(char *dest, const char *src, size_t count) |
91 | size_t strlcat(char *dest, const char *src, size_t count) |
64 | { |
92 | { |
65 | size_t dsize = strlen(dest); |
93 | size_t dsize = strlen(dest); |
66 | size_t len = strlen(src); |
94 | size_t len = strlen(src); |
67 | size_t res = dsize + len; |
95 | size_t res = dsize + len; |
68 | 96 | ||
69 | /* This would be a bug */ |
97 | /* This would be a bug */ |
70 | BUG_ON(dsize >= count); |
98 | BUG_ON(dsize >= count); |
71 | 99 | ||
72 | dest += dsize; |
100 | dest += dsize; |
73 | count -= dsize; |
101 | count -= dsize; |
74 | if (len >= count) |
102 | if (len >= count) |
75 | len = count-1; |
103 | len = count-1; |
76 | memcpy(dest, src, len); |
104 | memcpy(dest, src, len); |
77 | dest[len] = 0; |
105 | dest[len] = 0; |
78 | return res; |
106 | return res; |
79 | } |
107 | } |
80 | #endif |
108 | #endif |
81 | 109 | ||
82 | 110 | ||
83 | #endif |
111 | #endif |