OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [ColdFire_MCF52259_CodeWarrior/] [stdlib.c] - Blame information for rev 609

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 jeremybenn
/* FILENAME: stdlib.c
2
 *
3
 * Functions normally found in a standard C lib.
4
 *
5
 * 12/28/2005 - added memcmp and memmove
6
 *
7
 * Notes: These functions support ASCII only!!!
8
 */
9
 
10
#include "common.h"
11
#include "stdlib.h"
12
 
13
/****************************************************************/
14
int
15
isspace (int ch)
16
{
17
        if ((ch == ' ') || (ch == '\t'))        /* \n ??? */
18
                return TRUE;
19
        else
20
                return FALSE;
21
}
22
 
23
/****************************************************************/
24
int
25
isalnum (int ch)
26
{
27
        /* ASCII only */
28
        if (((ch >= '0') && (ch <= '9')) ||
29
                ((ch >= 'A') && (ch <= 'Z')) ||
30
                ((ch >= 'a') && (ch <= 'z')))
31
                return TRUE;
32
        else
33
                return FALSE;
34
}
35
 
36
/****************************************************************/
37
int
38
isdigit (int ch)
39
{
40
        /* ASCII only */
41
        if ((ch >= '0') && (ch <= '9'))
42
                return TRUE;
43
        else
44
                return FALSE;
45
}
46
 
47
/****************************************************************/
48
int
49
isupper (int ch)
50
{
51
        /* ASCII only */
52
        if ((ch >= 'A') && (ch <= 'Z'))
53
                return TRUE;
54
        else
55
                return FALSE;
56
}
57
 
58
/****************************************************************/
59
int
60
strcasecmp (const char *s1, const char *s2)
61
{
62
        char    c1, c2;
63
        int             result = 0;
64
 
65
        while (result == 0)
66
        {
67
                c1 = *s1++;
68
                c2 = *s2++;
69
                if ((c1 >= 'a') && (c1 <= 'z'))
70
                        c1 = (char)(c1 - ' ');
71
                if ((c2 >= 'a') && (c2 <= 'z'))
72
                        c2 = (char)(c2 - ' ');
73
                if ((result = (c1 - c2)) != 0)
74
                        break;
75
                if ((c1 == 0) || (c2 == 0))
76
                        break;
77
        }
78
        return result;
79
}
80
 
81
 
82
/****************************************************************/
83
int
84
stricmp (const char *s1, const char *s2)
85
{
86
   return (strcasecmp(s1, s2));
87
}
88
 
89
/****************************************************************/
90
int
91
strncasecmp (const char *s1, const char *s2, int n)
92
{
93
        char    c1, c2;
94
        int             k = 0;
95
        int             result = 0;
96
 
97
        while ( k++ < n )
98
        {
99
                c1 = *s1++;
100
                c2 = *s2++;
101
                if ((c1 >= 'a') && (c1 <= 'z'))
102
                        c1 = (char)(c1 - ' ');
103
                if ((c2 >= 'a') && (c2 <= 'z'))
104
                        c2 = (char)(c2 - ' ');
105
                if ((result = (c1 - c2)) != 0)
106
                        break;
107
                if ((c1 == 0) || (c2 == 0))
108
                        break;
109
        }
110
        return result;
111
}
112
 
113
/****************************************************************/
114
int
115
strnicmp (const char *s1, const char *s2, int n)
116
{
117
   return (strncasecmp(s1, s2, n));
118
}
119
 
120
/****************************************************************/
121
uint32
122
strtoul (char *str, char **ptr, int base)
123
{
124
   unsigned long rvalue = 0;
125
   int neg = 0;
126
   int c;
127
 
128
   /* Validate parameters */
129
   if ((str != NULL) && (base >= 0) && (base <= 36))
130
   {
131
      /* Skip leading white spaces */
132
      while (isspace(*str))
133
      {
134
         ++str;
135
        }
136
 
137
        /* Check for notations */
138
       switch (str[0])
139
        {
140
                case '0':
141
          if (base == 0)
142
          {
143
             if ((str[1] == 'x') || (str[1] == 'X'))
144
                                {
145
                                        base = 16;
146
                str += 2;
147
             }
148
             else
149
             {
150
                base = 8;
151
                str++;
152
                                }
153
                        }
154
                        break;
155
 
156
                case '-':
157
                        neg = 1;
158
          str++;
159
          break;
160
 
161
       case '+':
162
          str++;
163
                        break;
164
 
165
                default:
166
                        break;
167
        }
168
 
169
        if (base == 0)
170
                base = 10;
171
 
172
      /* Valid "digits" are 0..9, A..Z, a..z */
173
      while (isalnum(c = *str))
174
      {
175
                /* Convert char to num in 0..36 */
176
         if ((c -= ('a' - 10)) < 10)         /* 'a'..'z' */
177
         {
178
            if ((c += ('a' - 'A')) < 10)     /* 'A'..'Z' */
179
            {
180
               c += ('A' - '0' - 10);        /* '0'..'9' */
181
                        }
182
                }
183
 
184
                /* check c against base */
185
                if (c >= base)
186
                {
187
                        break;
188
                }
189
 
190
                if (neg)
191
                {
192
                        rvalue = (rvalue * base) - c;
193
                }
194
                else
195
                {
196
                        rvalue = (rvalue * base) + c;
197
         }
198
 
199
         ++str;
200
                }
201
        }
202
 
203
   /* Upon exit, 'str' points to the character at which valid info */
204
   /* STOPS.  No chars including and beyond 'str' are used.        */
205
 
206
        if (ptr != NULL)
207
                        *ptr = str;
208
 
209
                return rvalue;
210
        }
211
 
212
/****************************************************************/
213
int
214
atoi (const char *str)
215
{
216
   char *s = (char *)str;
217
 
218
   return ((int)strtoul(s, NULL, 10));
219
}
220
 
221
/****************************************************************/
222
int
223
strlen (const char *str)
224
{
225
        char *s = (char *)str;
226
        int len = 0;
227
 
228
        if (s == NULL)
229
                return 0;
230
 
231
        while (*s++ != '\0')
232
                ++len;
233
 
234
        return len;
235
}
236
 
237
/****************************************************************/
238
char *
239
strcat (char *dest, const char *src)
240
{
241
        char *dp;
242
        char *sp = (char *)src;
243
 
244
        if ((dest != NULL) && (src != NULL))
245
        {
246
                dp = &dest[strlen(dest)];
247
 
248
                while (*sp != '\0')
249
                {
250
                        *dp++ = *sp++;
251
                }
252
                *dp = '\0';
253
        }
254
        return dest;
255
}
256
 
257
/****************************************************************/
258
char *
259
strncat (char *dest, const char *src, int n)
260
{
261
        char *dp;
262
        char *sp = (char *)src;
263
 
264
        if ((dest != NULL) && (src != NULL) && (n > 0))
265
        {
266
                dp = &dest[strlen(dest)];
267
 
268
                while ((*sp != '\0') && (n-- > 0))
269
                {
270
                        *dp++ = *sp++;
271
                }
272
                *dp = '\0';
273
        }
274
        return dest;
275
}
276
 
277
/****************************************************************/
278
char *
279
strcpy (char *dest, const char *src)
280
{
281
        char *dp = (char *)dest;
282
        char *sp = (char *)src;
283
 
284
        if ((dest != NULL) && (src != NULL))
285
        {
286
                while (*sp != '\0')
287
                {
288
                        *dp++ = *sp++;
289
                }
290
                *dp = '\0';
291
        }
292
        return dest;
293
}
294
 
295
/****************************************************************/
296
char *
297
strncpy (char *dest, const char *src, int n)
298
{
299
        char *dp = (char *)dest;
300
        char *sp = (char *)src;
301
 
302
        if ((dest != NULL) && (src != NULL) && (n > 0))
303
        {
304
                while ((*sp != '\0') && (n-- > 0))
305
                {
306
                        *dp++ = *sp++;
307
                }
308
                *dp = '\0';
309
        }
310
        return dest;
311
}
312
 
313
/****************************************************************/
314
int
315
strcmp (const char *s1, const char *s2)
316
{
317
        /* No checks for NULL */
318
        char *s1p = (char *)s1;
319
        char *s2p = (char *)s2;
320
 
321
        while (*s2p != '\0')
322
        {
323
                if (*s1p != *s2p)
324
                        break;
325
 
326
                ++s1p;
327
                ++s2p;
328
        }
329
        return (*s1p - *s2p);
330
}
331
 
332
/****************************************************************/
333
int
334
strncmp (const char *s1, const char *s2, int n)
335
{
336
        /* No checks for NULL */
337
        char *s1p = (char *)s1;
338
        char *s2p = (char *)s2;
339
 
340
        if (n <= 0)
341
                return 0;
342
 
343
        while (*s2p != '\0')
344
        {
345
                if (*s1p != *s2p)
346
                        break;
347
 
348
                if (--n == 0)
349
                        break;
350
 
351
                ++s1p;
352
                ++s2p;
353
        }
354
        return (*s1p - *s2p);
355
}
356
 
357
/****************************************************************/
358
char *
359
strstr(const char *s1, const char *s2)
360
{
361
   char *sp = (char *)s1;
362
   int  len1 = strlen(s1);
363
   int  len2 = strlen(s2);
364
 
365
   while (len1 >= len2)
366
   {
367
      if (strncmp(sp, s2, len2) == 0)
368
      {
369
         return (sp);
370
      }
371
      ++sp;
372
      --len1;
373
   }
374
 
375
   return (NULL);
376
}
377
 
378
/****************************************************************/
379
char *
380
strchr(const char *str, int c)
381
{
382
   char *sp = (char *)str;
383
   char  ch = (char)(c & 0xff);
384
 
385
   while (*sp != '\0')
386
   {
387
      if (*sp == ch)
388
      {
389
         return (sp);
390
      }
391
      ++sp;
392
   }
393
 
394
   return (NULL);
395
}
396
 
397
/****************************************************************/
398
void *
399
memcpy (void *dest, const void *src, unsigned n)
400
{
401
        unsigned char *dbp = (unsigned char *)dest;
402
        unsigned char *sbp = (unsigned char *)src;
403
 
404
        if ((dest != NULL) && (src != NULL) && (n > 0))
405
        {
406
      while (n--)
407
                        *dbp++ = *sbp++;
408
        }
409
        return dest;
410
}
411
 
412
/****************************************************************/
413
void *
414
memset (void *s, int c, unsigned n)
415
{
416
        /* Not optimized, but very portable */
417
        unsigned char *sp = (unsigned char *)s;
418
 
419
        if ((s != NULL) && (n > 0))
420
        {
421
                while (n--)
422
                {
423
                        *sp++ = (unsigned char)c;
424
                }
425
        }
426
        return s;
427
}
428
 
429
/****************************************************************/
430
int
431
memcmp (const void *s1, const void *s2, unsigned n)
432
{
433
   unsigned char *s1p, *s2p;
434
 
435
   if (s1 && s2 && (n > 0))
436
   {
437
      s1p = (unsigned char *)s1;
438
      s2p = (unsigned char *)s2;
439
 
440
      while ((--n >= 0) && (*s1p == *s2p))
441
      {
442
         if (*s1p != *s2p)
443
            return (*s1p - *s2p);
444
         ++s1p;
445
         ++s2p;
446
      }
447
   }
448
 
449
   return (0);
450
}
451
 
452
/****************************************************************/
453
void *
454
memmove (void *dest, const void *src, unsigned n)
455
{
456
   unsigned char *dbp = (unsigned char *)dest;
457
   unsigned char *sbp = (unsigned char *)src;
458
   unsigned char *dend = dbp + n;
459
   unsigned char *send = sbp + n;
460
 
461
   if ((dest != NULL) && (src != NULL) && (n > 0))
462
   {
463
      /* see if a memcpy would overwrite source buffer */
464
      if ((sbp < dbp) && (dbp < send))
465
      {
466
         while (n--)
467
            *(--dend) = *(--send);
468
      }
469
      else
470
      {
471
         while (n--)
472
            *dbp++ = *sbp++;
473
      }
474
   }
475
 
476
   return dest;
477
}
478
 
479
/****************************************************************/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.