1 //===-- string.c - String functions for the LLVM libc Library -----*- C -*-===//
3 // A lot of this code is ripped gratuitously from glibc and libiberty.
5 //===----------------------------------------------------------------------===//
8 void *malloc(unsigned);
11 unsigned strlen(const char *Str) {
13 while (*Str) { ++Count; ++Str; }
17 char *strdup(const char *str) {
18 int Len = strlen(str);
19 char *Result = (char*)malloc((Len+1)*sizeof(char));
20 memcpy(Result, str, Len+1);
24 char *strcpy(char *s1, const char *s2) {
25 while ((*s1++ = *s2++));
30 /* Compare S1 and S2, returning less than, equal to or
31 greater than zero if S1 is lexicographically less than,
32 equal to or greater than S2. */
33 int strcmp (const char *p1, const char *p2) {
34 register const unsigned char *s1 = (const unsigned char *) p1;
35 register const unsigned char *s2 = (const unsigned char *) p2;
40 c1 = (unsigned char) *s1++;
41 c2 = (unsigned char) *s2++;
50 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
52 typedef unsigned int op_t;
55 void *memset (void *dstpp, int c, size_t len) {
56 long long int dstp = (long long int) dstpp;
63 cccc = (unsigned char) c;
67 /* Do the shift in two steps to avoid warning if long has 32 bits. */
68 cccc |= (cccc << 16) << 16;
70 /* There are at least some bytes to set.
71 No need to test for LEN == 0 in this alignment loop. */
72 while (dstp % OPSIZ != 0)
74 ((unsigned char *) dstp)[0] = c;
79 /* Write 8 `op_t' per iteration until less than 8 `op_t' remain. */
80 xlen = len / (OPSIZ * 8);
83 ((op_t *) dstp)[0] = cccc;
84 ((op_t *) dstp)[1] = cccc;
85 ((op_t *) dstp)[2] = cccc;
86 ((op_t *) dstp)[3] = cccc;
87 ((op_t *) dstp)[4] = cccc;
88 ((op_t *) dstp)[5] = cccc;
89 ((op_t *) dstp)[6] = cccc;
90 ((op_t *) dstp)[7] = cccc;
96 /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */
100 ((op_t *) dstp)[0] = cccc;
107 /* Write the last few bytes. */
110 ((unsigned char *) dstp)[0] = c;
118 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
119 char *dstp = (char*)dstpp;
120 char *srcp = (char*) srcpp;
123 for (i = 0; i < len; ++i)