2008年3月20日星期四

C++ Linux 中mencpy(),get(),strcpy(),strcmp()....的函数原型

1 /*
2 * linux/lib/string.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 /*
8 * stupid library routines.. The optimized versions should generally be found
9 * as inline code in
10 *
11 * These are buggy as well..
12 */
13
14 #include
15
16 char * ___strtok = NULL;
17
18 char * strcpy(char * dest,const char *src)
19 {
20 char *tmp = dest;
21
22 while ((*dest++ = *src++) != '\0')
23 /* nothing */;
24 return tmp;
25 }
26
27 char * strncpy(char * dest,const char *src,size_t count)
28 {
29 char *tmp = dest;
30
31 while (count-- && (*dest++ = *src++) != '\0')
32 /* nothing */;
33
34 return tmp;
35 }
36
37 char * strcat(char * dest, const char * src)
38 {
39 char *tmp = dest;
40
41 while (*dest)
42 dest++;
43 while ((*dest++ = *src++) != '\0')
44 ;
45
46 return tmp;
47 }
48
49 char * strncat(char *dest, const char *src, size_t count)
50 {
51 char *tmp = dest;
52
53 if (count) {
54 while (*dest)
55 dest++;
56 while ((*dest++ = *src++)) {
57 if (--count == 0)
58 break;
59 }
60 }
61
62 return tmp;
63 }
64
65 int strcmp(const char * cs,const char * ct)
66 {
67 register signed char __res;
68
69 while (1) {
70 if ((__res = *cs - *ct++) != 0 || !*cs++)
71 break;
72 }
73
74 return __res;
75 }
76
77 int strncmp(const char * cs,const char * ct,size_t count)
78 {
79 register signed char __res = 0;
80
81 while (count) {
82 if ((__res = *cs - *ct++) != 0 || !*cs++)
83 break;
84 count--;
85 }
86
87 return __res;
88 }
89
90 char * strchr(const char * s,char c)
91 {
92 for(; *s != c; ++s)
93 if (*s == '\0')
94 return NULL;
95 return (char *) s;
96 }
97
98 size_t strlen(const char * s)
99 {
100 const char *sc;
101
102 for (sc = s; *sc != '\0'; ++sc)
103 /* nothing */;
104 return sc - s;
105 }
106
107 size_t strnlen(const char * s, size_t count)
108 {
109 const char *sc;
110
111 for (sc = s; *sc != '\0' && count--; ++sc)
112 /* nothing */;
113 return sc - s;
114 }
115
116 size_t strspn(const char *s, const char *accept)
117 {
118 const char *p;
119 const char *a;
120 size_t count = 0;
121
122 for (p = s; *p != '\0'; ++p) {
123 for (a = accept; *a != '\0'; ++a) {
124 if (*p == *a)
125 break;
126 }
127 if (*a == '\0')
128 return count;
129 ++count;
130 }
131
132 return count;
133 }
134
135 char * strpbrk(const char * cs,const char * ct)
136 {
137 const char *sc1,*sc2;
138
139 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
140 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
141 if (*sc1 == *sc2)
142 return (char *) sc1;
143 }
144 }
145 return NULL;
146 }
147
148 char * strtok(char * s,const char * ct)
149 {
150 char *sbegin, *send;
151
152 sbegin = s ? s : ___strtok;
153 if (!sbegin) {
154 return NULL;
155 }
156 sbegin += strspn(sbegin,ct);
157 if (*sbegin == '\0') {
158 ___strtok = NULL;
159 return( NULL );
160 }
161 send = strpbrk( sbegin, ct);
162 if (send && *send != '\0')
163 *send++ = '\0';
164 ___strtok = send;
165 return (sbegin);
166 }
167
168 void * memset(void * s,char c,size_t count)
169 {
170 char *xs = (char *) s;
171
172 while (count--)
173 *xs++ = c;
174
175 return s;
176 }
177
178 char * bcopy(const char * src, char * dest, int count)
179 {
180 char *tmp = dest;
181
182 while (count--)
183 *tmp++ = *src++;
184
185 return dest;
186 }
187
188 void * memcpy(void * dest,const void *src,size_t count)
189 {
190 char *tmp = (char *) dest, *s = (char *) src;
191
192 while (count--)
193 *tmp++ = *s++;
194
195 return dest;
196 }
197
198 void * memmove(void * dest,const void *src,size_t count)
199 {
200 char *tmp, *s;
201
202 if (dest <= src) {
203 tmp = (char *) dest;
204 s = (char *) src;
205 while (count--)
206 *tmp++ = *s++;
207 }
208 else {
209 tmp = (char *) dest + count;
210 s = (char *) src + count;
211 while (count--)
212 *--tmp = *--s;
213 }
214
215 return dest;
216 }
217
218 int memcmp(const void * cs,const void * ct,size_t count)
219 {
220 const unsigned char *su1, *su2;
221 signed char res = 0;
222
223 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
224 if ((res = *su1 - *su2) != 0)
225 break;
226 return res;
227 }
228
229 /*
230 * find the first occurrence of byte 'c', or 1 past the area if none
231 */
232 void * memscan(void * addr, unsigned char c, size_t size)
233 {
234 unsigned char * p = (unsigned char *) addr;
235
236 while (size) {
237 if (*p == c)
238 return (void *) p;
239 p++;
240 size--;
241 }
242 return (void *) p;
243 }

1 条评论:

annidy 说...

面试时经常考这个,多谢了