ia64/xen-unstable

view extras/mini-os/lib/string.c @ 19664:878e4c128991

minios: implement ffs, ffsl and ffsll.

The first function is compiled only in case minios is compiled without
newlib, since newlib already provides an implementation for ffs.
On the other hand ffsl and ffsll are always compiled because newlib
misses those functions.
This patch also provides an implementation for __ffsti2 and __ffsdi2
because they are needed by gcc in order to successfully link ffsll.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 28 09:41:59 2009 +0100 (2009-05-28)
parents 74bd94747ca3
children
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 ****************************************************************************
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 ****************************************************************************
5 *
6 * File: string.c
7 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
8 * Changes:
9 *
10 * Date: Aug 2003
11 *
12 * Environment: Xen Minimal OS
13 * Description: Library function for string and memory manipulation
14 * Origin unknown
15 *
16 ****************************************************************************
17 * $Id: c-insert.c,v 1.7 2002/11/08 16:04:34 rn Exp $
18 ****************************************************************************
19 */
21 #include <strings.h>
23 /* newlib defines ffs but not ffsll or ffsl */
24 int __ffsti2 (long long int lli)
25 {
26 int i, num, t, tmpint, len;
28 num = sizeof(long long int) / sizeof(int);
29 if (num == 1) return (ffs((int) lli));
30 len = sizeof(int) * 8;
32 for (i = 0; i < num; i++) {
33 tmpint = (int) (((lli >> len) << len) ^ lli);
35 t = ffs(tmpint);
36 if (t)
37 return (t + i * len);
38 lli = lli >> len;
39 }
40 return 0;
41 }
43 int __ffsdi2 (long int li)
44 {
45 return __ffsti2 ((long long int) li);
46 }
48 int ffsl (long int li)
49 {
50 return __ffsti2 ((long long int) li);
51 }
53 int ffsll (long long int lli)
54 {
55 return __ffsti2 (lli);
56 }
58 #if !defined HAVE_LIBC
60 #include <os.h>
61 #include <types.h>
62 #include <lib.h>
63 #include <xmalloc.h>
65 int memcmp(const void * cs,const void * ct,size_t count)
66 {
67 const unsigned char *su1, *su2;
68 signed char res = 0;
70 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
71 if ((res = *su1 - *su2) != 0)
72 break;
73 return res;
74 }
76 void * memcpy(void * dest,const void *src,size_t count)
77 {
78 char *tmp = (char *) dest;
79 const char *s = src;
81 while (count--)
82 *tmp++ = *s++;
84 return dest;
85 }
87 int strncmp(const char * cs,const char * ct,size_t count)
88 {
89 register signed char __res = 0;
91 while (count) {
92 if ((__res = *cs - *ct++) != 0 || !*cs++)
93 break;
94 count--;
95 }
97 return __res;
98 }
100 int strcmp(const char * cs,const char * ct)
101 {
102 register signed char __res;
104 while (1) {
105 if ((__res = *cs - *ct++) != 0 || !*cs++)
106 break;
107 }
109 return __res;
110 }
112 char * strcpy(char * dest,const char *src)
113 {
114 char *tmp = dest;
116 while ((*dest++ = *src++) != '\0')
117 /* nothing */;
118 return tmp;
119 }
121 char * strncpy(char * dest,const char *src,size_t count)
122 {
123 char *tmp = dest;
125 while (count-- && (*dest++ = *src++) != '\0')
126 /* nothing */;
128 return tmp;
129 }
131 void * memset(void * s,int c,size_t count)
132 {
133 char *xs = (char *) s;
135 while (count--)
136 *xs++ = c;
138 return s;
139 }
141 size_t strnlen(const char * s, size_t count)
142 {
143 const char *sc;
145 for (sc = s; count-- && *sc != '\0'; ++sc)
146 /* nothing */;
147 return sc - s;
148 }
151 char * strcat(char * dest, const char * src)
152 {
153 char *tmp = dest;
155 while (*dest)
156 dest++;
158 while ((*dest++ = *src++) != '\0');
160 return tmp;
161 }
163 size_t strlen(const char * s)
164 {
165 const char *sc;
167 for (sc = s; *sc != '\0'; ++sc)
168 /* nothing */;
169 return sc - s;
170 }
172 char * strchr(const char * s, int c)
173 {
174 for(; *s != (char) c; ++s)
175 if (*s == '\0')
176 return NULL;
177 return (char *)s;
178 }
180 char * strrchr(const char * s, int c)
181 {
182 const char *res = NULL;
183 for(; *s != '\0'; ++s)
184 if (*s == (char) c)
185 res = s;
186 return (char *)res;
187 }
189 char * strstr(const char * s1,const char * s2)
190 {
191 int l1, l2;
193 l2 = strlen(s2);
194 if (!l2)
195 return (char *) s1;
196 l1 = strlen(s1);
197 while (l1 >= l2) {
198 l1--;
199 if (!memcmp(s1,s2,l2))
200 return (char *) s1;
201 s1++;
202 }
203 return NULL;
204 }
206 char *strdup(const char *x)
207 {
208 int l = strlen(x);
209 char *res = malloc(l + 1);
210 if (!res) return NULL;
211 memcpy(res, x, l + 1);
212 return res;
213 }
215 int ffs(int i)
216 {
217 int c = 1;
219 do {
220 if (i & 1)
221 return (c);
222 i = i >> 1;
223 c++;
224 } while (i);
225 return 0;
226 }
228 #endif