| 1 | ////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // File     : stdlib.c | 
|---|
| 3 | // Date     : 05/12/2013 | 
|---|
| 4 | // Author   : Clément DEVIGNE | 
|---|
| 5 | // Copyright (c) UPMC-LIP6 | 
|---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 7 |  | 
|---|
| 8 | #include <stdlib.h> | 
|---|
| 9 |  | 
|---|
| 10 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 11 | // int atoi ( char * str ) | 
|---|
| 12 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 13 |  | 
|---|
| 14 | int atoi(char *str) | 
|---|
| 15 | { | 
|---|
| 16 | int res  = 0; // Initialize result | 
|---|
| 17 | int sign = 1; // Initialize sign as positive | 
|---|
| 18 | int i    = 0; // Initialize index of first digit | 
|---|
| 19 |  | 
|---|
| 20 | if (str[0] == '-') //If number is negative, then update sign | 
|---|
| 21 | { | 
|---|
| 22 | sign = -1; | 
|---|
| 23 | i++;           // Also update index of first digit | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | for (; str[i] != '\0'; ++i) // Iterate through all digits and update the result | 
|---|
| 27 | { | 
|---|
| 28 | res = res*10 + str[i] - '0'; | 
|---|
| 29 | } | 
|---|
| 30 |  | 
|---|
| 31 | // Return result with sign | 
|---|
| 32 | return sign*res; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | //////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 36 | //  mempcy() | 
|---|
| 37 | // GCC requires this function. Taken from MutekH. | 
|---|
| 38 | //////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 39 | void * memcpy(void *_dst, const void * _src, unsigned int size) { | 
|---|
| 40 | unsigned int * dst = _dst; | 
|---|
| 41 | const unsigned int * src = _src; | 
|---|
| 42 | if (!((unsigned int) dst & 3) && !((unsigned int) src & 3) ) | 
|---|
| 43 | while (size > 3) { | 
|---|
| 44 | *dst++ = *src++; | 
|---|
| 45 | size -= 4; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | unsigned char *cdst = (unsigned char*)dst; | 
|---|
| 49 | unsigned char *csrc = (unsigned char*)src; | 
|---|
| 50 |  | 
|---|
| 51 | while (size--) { | 
|---|
| 52 | *cdst++ = *csrc++; | 
|---|
| 53 | } | 
|---|
| 54 | return _dst; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | //////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 59 | //  mempcy() | 
|---|
| 60 | // GCC requires this function. Taken from MutekH. | 
|---|
| 61 | //////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 62 | inline void * memset(void * dst, int s, unsigned int count) { | 
|---|
| 63 | char * a = (char *) dst; | 
|---|
| 64 | while (count--){ | 
|---|
| 65 | *a++ = (char)s; | 
|---|
| 66 | } | 
|---|
| 67 | return dst; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | // Local Variables: | 
|---|
| 71 | // tab-width: 4 | 
|---|
| 72 | // c-basic-offset: 4 | 
|---|
| 73 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) | 
|---|
| 74 | // indent-tabs-mode: nil | 
|---|
| 75 | // End: | 
|---|
| 76 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 | 
|---|