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 //===----------------------------------------------------------------------===//
13 size_t strlen(const char *Str) {
15 while (*Str) { ++Count; ++Str; }
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);
32 char *strndup(const char *str, size_t n) {
33 size_t Len = strlen(str);
35 char *Result = (char*)malloc((Len+1)*sizeof(char));
36 memcpy(Result, str, Len);
44 char *strcpy(char *s1, const char *s2) {
46 while ((*s1++ = *s2++));
53 char *strncpy(char *s1, const char *s2, size_t n) {
55 while (n-- && (*s1++ = *s2++));
62 char *strcat(char *s1, const char *s2) {
63 strcpy(s1+strlen(s1), s2);
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;
81 c1 = (unsigned char) *s1++;
82 c2 = (unsigned char) *s2++;
91 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
93 typedef unsigned int op_t;
96 void *memset (void *dstpp, int c, size_t len) {
97 long long int dstp = (long long int) dstpp;
104 cccc = (unsigned char) c;
108 /* Do the shift in two steps to avoid warning if long has 32 bits. */
109 cccc |= (cccc << 16) << 16;
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)
115 ((unsigned char *) dstp)[0] = c;
120 /* Write 8 `op_t' per iteration until less than 8 `op_t' remain. */
121 xlen = len / (OPSIZ * 8);
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;
137 /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */
141 ((op_t *) dstp)[0] = cccc;
148 /* Write the last few bytes. */
151 ((unsigned char *) dstp)[0] = c;
163 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
164 char *dstp = (char*)dstpp;
165 char *srcp = (char*) srcpp;
168 for (i = 0; i < len; ++i)