| 1 | /* | 
|---|
| 2 |  * string.c - User level <string> library implementation. | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Author     Alain Greiner (2016,2017,2018) | 
|---|
| 5 |  * | 
|---|
| 6 |  * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 7 |  * | 
|---|
| 8 |  * This file is part of ALMOS-MKH. | 
|---|
| 9 |  * | 
|---|
| 10 |  * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| 11 |  * under the terms of the GNU General Public License as published by | 
|---|
| 12 |  * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 |  * | 
|---|
| 14 |  * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| 15 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 |  * General Public License for more details. | 
|---|
| 18 |  * | 
|---|
| 19 |  * You should have received a copy of the GNU General Public License | 
|---|
| 20 |  * along with ALMOS-MKH; if not, write to the Free Software Foundation, | 
|---|
| 21 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 |  */ | 
|---|
| 23 |  | 
|---|
| 24 | #include <string.h> | 
|---|
| 25 | #include <stdio.h> | 
|---|
| 26 |  | 
|---|
| 27 | /////////////////////////////////////// | 
|---|
| 28 | unsigned int strlen( const char * str ) | 
|---|
| 29 | { | 
|---|
| 30 |         register const char * ptr = str; | 
|---|
| 31 |  | 
|---|
| 32 |         while (* ptr) ptr++; | 
|---|
| 33 |  | 
|---|
| 34 |         return (ptr - str); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | /////////////////////////////////////// | 
|---|
| 38 | unsigned int strnlen( const char * str, | 
|---|
| 39 |                       unsigned int     maxlen ) | 
|---|
| 40 | { | 
|---|
| 41 |         register const char *ptr = str; | 
|---|
| 42 |  | 
|---|
| 43 |         while (*ptr && (maxlen-- > 0)) ptr++; | 
|---|
| 44 |  | 
|---|
| 45 |         return (ptr - str); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | //////////////////////////// | 
|---|
| 49 | int strcmp( const char * s1, | 
|---|
| 50 |             const char * s2 ) | 
|---|
| 51 | { | 
|---|
| 52 |  | 
|---|
| 53 |         while (*s1 && *s1 == *s2) | 
|---|
| 54 |         { | 
|---|
| 55 |                 s1 ++; | 
|---|
| 56 |                 s2 ++; | 
|---|
| 57 |         } | 
|---|
| 58 |  | 
|---|
| 59 |         return (*s1 - *s2); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | ////////////////////////////// | 
|---|
| 63 | int strncmp( const char   * s1, | 
|---|
| 64 |              const char   * s2, | 
|---|
| 65 |              unsigned int   n ) | 
|---|
| 66 | {  | 
|---|
| 67 |         if (n == 0) | 
|---|
| 68 |                 return s1 - s2;    // pseudo random result... | 
|---|
| 69 |  | 
|---|
| 70 |         while (*s1 && (*s1 == *s2) && (n > 1)) | 
|---|
| 71 |         { | 
|---|
| 72 |                 s1 ++; | 
|---|
| 73 |                 s2 ++; | 
|---|
| 74 |                 n--; | 
|---|
| 75 |         } | 
|---|
| 76 |  | 
|---|
| 77 |         return (*s1 - *s2); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /////////////////////////// | 
|---|
| 83 | char * strcpy (char * dest, | 
|---|
| 84 |                char * src ) | 
|---|
| 85 | { | 
|---|
| 86 |         char *src_ptr = src; | 
|---|
| 87 |         char *dst_ptr = dest; | 
|---|
| 88 |  | 
|---|
| 89 |         while(*src_ptr) *(dst_ptr++) = *(src_ptr++); | 
|---|
| 90 |  | 
|---|
| 91 |         *dst_ptr = 0; | 
|---|
| 92 |         return dest; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | //////////////////////////////////// | 
|---|
| 96 | char * strncpy( char         * dest, | 
|---|
| 97 |                 char         * src, | 
|---|
| 98 |                 unsigned int   n ) | 
|---|
| 99 | { | 
|---|
| 100 |         unsigned int i; | 
|---|
| 101 |  | 
|---|
| 102 |         for (i = 0; (i < n) && (src[i] != '\0') ; i++) dest[i] = src[i]; | 
|---|
| 103 |  | 
|---|
| 104 |         for (; i < n; i++) dest[i] = '\0'; | 
|---|
| 105 |  | 
|---|
| 106 |         return dest; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | ////////////////////////////// | 
|---|
| 110 | char * strstr( char       * s, | 
|---|
| 111 |                const char * find ) | 
|---|
| 112 | { | 
|---|
| 113 |     char     sc; | 
|---|
| 114 |     char     c; | 
|---|
| 115 |     unsigned int len; | 
|---|
| 116 |  | 
|---|
| 117 |     if ((c = *find++) != 0)  | 
|---|
| 118 |     { | 
|---|
| 119 |         len = strlen( find ); | 
|---|
| 120 |         do  | 
|---|
| 121 |         { | 
|---|
| 122 |             do  | 
|---|
| 123 |             { | 
|---|
| 124 |                 if ((sc = *s++) == 0) return NULL; | 
|---|
| 125 |             }  | 
|---|
| 126 |             while (sc != c); | 
|---|
| 127 |         }  | 
|---|
| 128 |         while (strncmp(s, find, len) != 0); | 
|---|
| 129 |         s--; | 
|---|
| 130 |     } | 
|---|
| 131 |     return s; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | ////////////////////////////// | 
|---|
| 135 | char * strchr( const char * s, | 
|---|
| 136 |                int          c ) | 
|---|
| 137 | { | 
|---|
| 138 |         while(*s && *s != (char) c) s++; | 
|---|
| 139 |  | 
|---|
| 140 |         if(*s == (char) c) return (char*)s; | 
|---|
| 141 |  | 
|---|
| 142 |         return NULL; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | /////////////////////////////// | 
|---|
| 146 | char * strrchr( const char * t, | 
|---|
| 147 |                 int          c ) | 
|---|
| 148 | { | 
|---|
| 149 |         register char         ch; | 
|---|
| 150 |         register const char * l = 0; | 
|---|
| 151 |  | 
|---|
| 152 |         ch = c; | 
|---|
| 153 |         for (;;)  | 
|---|
| 154 |     { | 
|---|
| 155 |                 if (*t == ch) l=t; | 
|---|
| 156 |                 if (!*t) return (char*)l; | 
|---|
| 157 |                 ++t; | 
|---|
| 158 |         } | 
|---|
| 159 |         return (char*)l; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | /////////////////////////////////////////////////////////////// | 
|---|
| 163 | void * memcpy(void *_dst, const void * _src, unsigned int size)  | 
|---|
| 164 | { | 
|---|
| 165 |     unsigned int * dst = _dst; | 
|---|
| 166 |     const unsigned int * src = _src; | 
|---|
| 167 |     if (!((unsigned int) dst & 3) && !((unsigned int) src & 3) ) | 
|---|
| 168 |     { | 
|---|
| 169 |         while (size > 3)  | 
|---|
| 170 |         { | 
|---|
| 171 |             *dst++ = *src++; | 
|---|
| 172 |             size -= 4; | 
|---|
| 173 |         } | 
|---|
| 174 |     } | 
|---|
| 175 |  | 
|---|
| 176 |     unsigned char *cdst = (unsigned char*)dst; | 
|---|
| 177 |     unsigned char *csrc = (unsigned char*)src; | 
|---|
| 178 |  | 
|---|
| 179 |     while (size--)  | 
|---|
| 180 |     { | 
|---|
| 181 |         *cdst++ = *csrc++; | 
|---|
| 182 |     } | 
|---|
| 183 |     return _dst; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | ////////////////////////////////////////////////////////// | 
|---|
| 187 | inline void * memset(void * dst, int s, unsigned int size)  | 
|---|
| 188 | { | 
|---|
| 189 |     char * a = (char *) dst; | 
|---|
| 190 |     while (size--) | 
|---|
| 191 |     { | 
|---|
| 192 |         *a++ = (char)s; | 
|---|
| 193 |     } | 
|---|
| 194 |     return dst; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 |  | 
|---|