Rename bunch of variables.
[oota-llvm.git] / runtime / GCCLibraries / libc / string.c
1 //===-- string.c - String functions for the LLVM libc Library -----*- C -*-===//
2 // 
3 // A lot of this code is ripped gratuitously from glibc and libiberty.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include <stdlib.h>
8 #include <string.h>
9
10 #ifdef strlen
11 #undef strlen
12 #endif
13 size_t strlen(const char *Str) {
14   size_t Count = 0;
15   while (*Str) { ++Count; ++Str; }
16   return Count;
17 }
18
19 #ifdef strdup
20 #undef strdup
21 #endif
22 char *strdup(const char *str) {
23   size_t Len = strlen(str);
24   char *Result = (char*)malloc((Len+1)*sizeof(char));
25   memcpy(Result, str, Len+1);
26   return Result;
27 }
28
29 #ifdef strndup
30 #undef strndup
31 #endif
32 char *strndup(const char *str, size_t n) {
33   size_t Len = strlen(str);
34   if (Len > n) Len = n;
35   char *Result = (char*)malloc((Len+1)*sizeof(char));
36   memcpy(Result, str, Len);
37   Result[Len] = 0;
38   return Result;
39 }
40
41 #ifdef strcpy
42 #undef strcpy
43 #endif
44 char *strcpy(char *s1, const char *s2) {
45   char *dest = s1;
46   while ((*s1++ = *s2++));
47   return dest;
48 }
49
50 #ifdef strncpy
51 #undef strncpy
52 #endif
53 char *strncpy(char *s1, const char *s2, size_t n) {
54   char *dest = s1;
55   while (n-- && (*s1++ = *s2++));
56   return dest;
57 }
58
59 #ifdef strcat
60 #undef strcat
61 #endif
62 char *strcat(char *s1, const char *s2) {
63   strcpy(s1+strlen(s1), s2);
64   return s1;
65 }
66
67
68 #ifdef strcmp
69 #undef strcmp
70 #endif
71 /* Compare S1 and S2, returning less than, equal to or
72    greater than zero if S1 is lexicographically less than,
73    equal to or greater than S2.  */
74 int strcmp (const char *p1, const char *p2) {
75   register const unsigned char *s1 = (const unsigned char *) p1;
76   register const unsigned char *s2 = (const unsigned char *) p2;
77   unsigned char c1, c2;
78
79   do
80     {
81       c1 = (unsigned char) *s1++;
82       c2 = (unsigned char) *s2++;
83       if (c1 == '\0')
84         return c1 - c2;
85     }
86   while (c1 == c2);
87
88   return c1 - c2;
89 }
90
91 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
92 #if 0
93 typedef unsigned int op_t;
94 #define OPSIZ 4
95
96 void *memset (void *dstpp, int c, size_t len) {
97   long long int dstp = (long long int) dstpp;
98
99   if (len >= 8)
100     {
101       size_t xlen;
102       op_t cccc;
103
104       cccc = (unsigned char) c;
105       cccc |= cccc << 8;
106       cccc |= cccc << 16;
107       if (OPSIZ > 4)
108         /* Do the shift in two steps to avoid warning if long has 32 bits.  */
109         cccc |= (cccc << 16) << 16;
110
111       /* There are at least some bytes to set.
112          No need to test for LEN == 0 in this alignment loop.  */
113       while (dstp % OPSIZ != 0)
114         {
115           ((unsigned char *) dstp)[0] = c;
116           dstp += 1;
117           len -= 1;
118         }
119
120       /* Write 8 `op_t' per iteration until less than 8 `op_t' remain.  */
121       xlen = len / (OPSIZ * 8);
122       while (xlen > 0)
123         {
124           ((op_t *) dstp)[0] = cccc;
125           ((op_t *) dstp)[1] = cccc;
126           ((op_t *) dstp)[2] = cccc;
127           ((op_t *) dstp)[3] = cccc;
128           ((op_t *) dstp)[4] = cccc;
129           ((op_t *) dstp)[5] = cccc;
130           ((op_t *) dstp)[6] = cccc;
131           ((op_t *) dstp)[7] = cccc;
132           dstp += 8 * OPSIZ;
133           xlen -= 1;
134         }
135       len %= OPSIZ * 8;
136
137       /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain.  */
138       xlen = len / OPSIZ;
139       while (xlen > 0)
140         {
141           ((op_t *) dstp)[0] = cccc;
142           dstp += OPSIZ;
143           xlen -= 1;
144         }
145       len %= OPSIZ;
146     }
147
148   /* Write the last few bytes.  */
149   while (len > 0)
150     {
151       ((unsigned char *) dstp)[0] = c;
152       dstp += 1;
153       len -= 1;
154     }
155
156   return dstpp;
157 }
158 #endif
159
160 #ifdef memcpy
161 #undef memcpy
162 #endif
163 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
164   char *dstp = (char*)dstpp;
165   char *srcp = (char*) srcpp;
166   unsigned i;
167
168   for (i = 0; i < len; ++i)
169     dstp[i] = srcp[i];
170
171   return dstpp;
172 }