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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [ColdFire_MCF51CN128_CodeWarrior/] [Sources/] [stdlib.c] - Blame information for rev 578

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

powered by: WebSVN 2.1.0

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