Squelch warning
[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 void *malloc(size_t);
10 void free(void *);
11
12 size_t strlen(const char *Str) {
13   size_t Count = 0;
14   while (*Str) { ++Count; ++Str; }
15   return Count;
16 }
17
18 char *strdup(const char *str) {
19   long Len = strlen(str);
20   char *Result = (char*)malloc((Len+1)*sizeof(char));
21   memcpy(Result, str, Len+1);
22   return Result;
23 }
24
25 char *strcpy(char *s1, const char *s2) {
26   while ((*s1++ = *s2++));
27   return s1;
28 }
29
30 char *strcat(char *s1, const char *s2) {
31   strcpy(s1+strlen(s1), s2);
32   return s1;
33 }
34
35
36 /* Compare S1 and S2, returning less than, equal to or
37    greater than zero if S1 is lexicographically less than,
38    equal to or greater than S2.  */
39 int strcmp (const char *p1, const char *p2) {
40   register const unsigned char *s1 = (const unsigned char *) p1;
41   register const unsigned char *s2 = (const unsigned char *) p2;
42   unsigned char c1, c2;
43
44   do
45     {
46       c1 = (unsigned char) *s1++;
47       c2 = (unsigned char) *s2++;
48       if (c1 == '\0')
49         return c1 - c2;
50     }
51   while (c1 == c2);
52
53   return c1 - c2;
54 }
55
56 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
57 #if 0
58 typedef unsigned int op_t;
59 #define OPSIZ 4
60
61 void *memset (void *dstpp, int c, size_t len) {
62   long long int dstp = (long long int) dstpp;
63
64   if (len >= 8)
65     {
66       size_t xlen;
67       op_t cccc;
68
69       cccc = (unsigned char) c;
70       cccc |= cccc << 8;
71       cccc |= cccc << 16;
72       if (OPSIZ > 4)
73         /* Do the shift in two steps to avoid warning if long has 32 bits.  */
74         cccc |= (cccc << 16) << 16;
75
76       /* There are at least some bytes to set.
77          No need to test for LEN == 0 in this alignment loop.  */
78       while (dstp % OPSIZ != 0)
79         {
80           ((unsigned char *) dstp)[0] = c;
81           dstp += 1;
82           len -= 1;
83         }
84
85       /* Write 8 `op_t' per iteration until less than 8 `op_t' remain.  */
86       xlen = len / (OPSIZ * 8);
87       while (xlen > 0)
88         {
89           ((op_t *) dstp)[0] = cccc;
90           ((op_t *) dstp)[1] = cccc;
91           ((op_t *) dstp)[2] = cccc;
92           ((op_t *) dstp)[3] = cccc;
93           ((op_t *) dstp)[4] = cccc;
94           ((op_t *) dstp)[5] = cccc;
95           ((op_t *) dstp)[6] = cccc;
96           ((op_t *) dstp)[7] = cccc;
97           dstp += 8 * OPSIZ;
98           xlen -= 1;
99         }
100       len %= OPSIZ * 8;
101
102       /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain.  */
103       xlen = len / OPSIZ;
104       while (xlen > 0)
105         {
106           ((op_t *) dstp)[0] = cccc;
107           dstp += OPSIZ;
108           xlen -= 1;
109         }
110       len %= OPSIZ;
111     }
112
113   /* Write the last few bytes.  */
114   while (len > 0)
115     {
116       ((unsigned char *) dstp)[0] = c;
117       dstp += 1;
118       len -= 1;
119     }
120
121   return dstpp;
122 }
123 #endif
124
125 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
126   char *dstp = (char*)dstpp;
127   char *srcp = (char*) srcpp;
128   unsigned i;
129
130   for (i = 0; i < len; ++i)
131     dstp[i] = srcp[i];
132
133   return dstpp;
134 }