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 //===----------------------------------------------------------------------===//
12 size_t strlen(const char *Str) {
14 while (*Str) { ++Count; ++Str; }
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);
25 char *strcpy(char *s1, const char *s2) {
26 while ((*s1++ = *s2++));
30 char *strcat(char *s1, const char *s2) {
31 strcpy(s1+strlen(s1), s2);
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;
46 c1 = (unsigned char) *s1++;
47 c2 = (unsigned char) *s2++;
56 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
58 typedef unsigned int op_t;
61 void *memset (void *dstpp, int c, size_t len) {
62 long long int dstp = (long long int) dstpp;
69 cccc = (unsigned char) c;
73 /* Do the shift in two steps to avoid warning if long has 32 bits. */
74 cccc |= (cccc << 16) << 16;
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)
80 ((unsigned char *) dstp)[0] = c;
85 /* Write 8 `op_t' per iteration until less than 8 `op_t' remain. */
86 xlen = len / (OPSIZ * 8);
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;
102 /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */
106 ((op_t *) dstp)[0] = cccc;
113 /* Write the last few bytes. */
116 ((unsigned char *) dstp)[0] = c;
125 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
126 char *dstp = (char*)dstpp;
127 char *srcp = (char*) srcpp;
130 for (i = 0; i < len; ++i)