OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [ColdFire_MCF52221_CodeWarrior/] [sources/] [stdlib.c] - Blame information for rev 606

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

powered by: WebSVN 2.1.0

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