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 //===----------------------------------------------------------------------===//
11 size_t strlen(const char *Str) {
13 while (*Str) { ++Count; ++Str; }
17 char *strdup(const char *str) {
18 long 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++));
29 char *strcat(char *s1, const char *s2) {
30 strcpy(s1+strlen(s1), s2);
35 /* Compare S1 and S2, returning less than, equal to or
36 greater than zero if S1 is lexicographically less than,
37 equal to or greater than S2. */
38 int strcmp (const char *p1, const char *p2) {
39 register const unsigned char *s1 = (const unsigned char *) p1;
40 register const unsigned char *s2 = (const unsigned char *) p2;
45 c1 = (unsigned char) *s1++;
46 c2 = (unsigned char) *s2++;
55 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
57 typedef unsigned int op_t;
60 void *memset (void *dstpp, int c, size_t len) {
61 long long int dstp = (long long int) dstpp;
68 cccc = (unsigned char) c;
72 /* Do the shift in two steps to avoid warning if long has 32 bits. */
73 cccc |= (cccc << 16) << 16;
75 /* There are at least some bytes to set.
76 No need to test for LEN == 0 in this alignment loop. */
77 while (dstp % OPSIZ != 0)
79 ((unsigned char *) dstp)[0] = c;
84 /* Write 8 `op_t' per iteration until less than 8 `op_t' remain. */
85 xlen = len / (OPSIZ * 8);
88 ((op_t *) dstp)[0] = cccc;
89 ((op_t *) dstp)[1] = cccc;
90 ((op_t *) dstp)[2] = cccc;
91 ((op_t *) dstp)[3] = cccc;
92 ((op_t *) dstp)[4] = cccc;
93 ((op_t *) dstp)[5] = cccc;
94 ((op_t *) dstp)[6] = cccc;
95 ((op_t *) dstp)[7] = cccc;
101 /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */
105 ((op_t *) dstp)[0] = cccc;
112 /* Write the last few bytes. */
115 ((unsigned char *) dstp)[0] = c;
123 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
124 char *dstp = (char*)dstpp;
125 char *srcp = (char*) srcpp;
128 for (i = 0; i < len; ++i)