SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /sos/klibc.c (Article 6) and /sos/klibc.c (Article 9)


001 /* Copyright (C) 2004  David Decotigny (with I    001 /* Copyright (C) 2004  David Decotigny (with INSA Rennes for vsnprintf)
002    Copyright (C) 2003  The KOS Team               002    Copyright (C) 2003  The KOS Team
003    Copyright (C) 1999  Free Software Foundatio    003    Copyright (C) 1999  Free Software Foundation
004                                                   004 
005    This program is free software; you can redi    005    This program is free software; you can redistribute it and/or
006    modify it under the terms of the GNU Genera    006    modify it under the terms of the GNU General Public License
007    as published by the Free Software Foundatio    007    as published by the Free Software Foundation; either version 2
008    of the License, or (at your option) any lat    008    of the License, or (at your option) any later version.
009                                                   009    
010    This program is distributed in the hope tha    010    This program is distributed in the hope that it will be useful,
011    but WITHOUT ANY WARRANTY; without even the     011    but WITHOUT ANY WARRANTY; without even the implied warranty of
012    MERCHANTABILITY or FITNESS FOR A PARTICULAR    012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013    GNU General Public License for more details    013    GNU General Public License for more details.
014                                                   014    
015    You should have received a copy of the GNU     015    You should have received a copy of the GNU General Public License
016    along with this program; if not, write to t    016    along with this program; if not, write to the Free Software
017    Foundation, Inc., 59 Temple Place - Suite 3    017    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
018    USA.                                           018    USA. 
019 */                                                019 */
020 #include "klibc.h"                                020 #include "klibc.h"
021                                                   021 
022 /* For an optimized version, see BSD sources ;    022 /* For an optimized version, see BSD sources ;) */
023 void *memcpy(void *dst0, const void *src0, reg    023 void *memcpy(void *dst0, const void *src0, register unsigned int size)
024 {                                                 024 {
025   char *dst;                                      025   char *dst;
026   const char *src;                                026   const char *src;
027   for (dst = (char*)dst0, src = (const char*)s    027   for (dst = (char*)dst0, src = (const char*)src0 ;
028        size > 0 ;                                 028        size > 0 ;
029        dst++, src++, size--)                      029        dst++, src++, size--)
030     *dst = *src;                                  030     *dst = *src;
031   return dst0;                                    031   return dst0;
032 }                                                 032 }
033                                                   033 
034 /* ditto */                                       034 /* ditto */
035 void *memset(void *dst0, register int c, regis    035 void *memset(void *dst0, register int c, register unsigned int length)
036 {                                                 036 {
037   char *dst;                                      037   char *dst;
038   for (dst = (char*) dst0 ;                       038   for (dst = (char*) dst0 ;
039        length > 0 ;                               039        length > 0 ;
040        dst++, length --)                          040        dst++, length --)
041     *dst = (char)c;                               041     *dst = (char)c;
042   return dst0;                                    042   return dst0;
043 }                                                 043 }
044                                                   044 
045 int memcmp(const void *s1, const void *s2, sos    045 int memcmp(const void *s1, const void *s2, sos_size_t len)
046 {                                                 046 {
047   const unsigned char *c1, *c2;                   047   const unsigned char *c1, *c2;
048   unsigned int i;                                 048   unsigned int i;
049                                                   049  
050   for (i = 0, c1 = s1, c2 = s2; i < len; i++,     050   for (i = 0, c1 = s1, c2 = s2; i < len; i++, c1++, c2++)
051     {                                             051     {
052       if(*c1 != *c2)                              052       if(*c1 != *c2)
053         return *c1 - *c2;                         053         return *c1 - *c2;
054     }                                             054     }
055                                                   055  
056   return 0;                                       056   return 0;
057 }                                                 057 }
058                                                   058 
059                                                   059 
060 unsigned int strlen(register const char *str)     060 unsigned int strlen(register const char *str)
061 {                                                 061 {
062   unsigned int retval = 0;                        062   unsigned int retval = 0;
063                                                   063   
064   while (*str++)                                  064   while (*str++)
065     retval++;                                     065     retval++;
066                                                   066   
067   return retval;                                  067   return retval;
068 }                                                 068 }
069                                                   069 
070                                                   070 
071 unsigned int strnlen(const char * s, sos_size_    071 unsigned int strnlen(const char * s, sos_size_t count)
072 {                                                 072 {
073   const char *sc;                                 073   const char *sc;
074                                                   074   
075   for (sc = s; count-- && *sc != '\0'; ++sc)      075   for (sc = s; count-- && *sc != '\0'; ++sc)
076     /* nothing */continue;                        076     /* nothing */continue;
077                                                   077 
078   return sc - s;                                  078   return sc - s;
079 }                                                 079 }
080                                                   080 
081                                                   081 
082 char *strzcpy(register char *dst, register con    082 char *strzcpy(register char *dst, register const char *src, register int len)
083 {                                                 083 {
084   int i;                                          084   int i;
085                                                   085 
086   if (len <= 0)                                   086   if (len <= 0)
087     return dst;                                   087     return dst;
088                                                   088   
089   for (i = 0; i < len; i++)                       089   for (i = 0; i < len; i++)
090     {                                             090     {
091       dst[i] = src[i];                            091       dst[i] = src[i];
092       if(src[i] == '\0')                          092       if(src[i] == '\0')
093         return dst;                               093         return dst;
094     }                                             094     }
095                                                   095   
096   dst[len-1] = '\0';                              096   dst[len-1] = '\0'; 
097   return dst;                                     097   return dst;
098 }                                                 098 }
099                                                   099 
100                                                   100 
101 char *strzcat (char *dest, const char *src, so    101 char *strzcat (char *dest, const char *src, sos_size_t n)
102 {                                                 102 {
103   char *res = dest;                               103   char *res = dest;
104                                                   104   
105   for ( ; *dest ; dest++);                        105   for ( ; *dest ; dest++);
106                                                   106   
107   for ( ; *src ; src++, dest++) {                 107   for ( ; *src ; src++, dest++) {
108     *dest = *src;                                 108     *dest = *src;
109     n--;                                          109     n--;
110     if (n <= 0)                                   110     if (n <= 0)
111       break;                                      111       break;
112   }                                               112   }
113                                                   113 
114   *dest = '\0';                                   114   *dest = '\0';
115   return res;                                     115   return res;
116 }                                                 116 }
117                                                   117 
118 int strcmp(register const char *s1, register c    118 int strcmp(register const char *s1, register const char *s2)
119 {                                                 119 {
120   while (*s1 == *s2++)                            120   while (*s1 == *s2++)
121     if (*s1++ == 0)                               121     if (*s1++ == 0)
122       return (0);                                 122       return (0);
123                                                   123   
124   return (*(const unsigned char *)s1 - *(const    124   return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
125 }                                                 125 }
126                                                   126 
127                                                   127 
128 int strncmp(register const char *s1, register     128 int strncmp(register const char *s1, register const char *s2, register int len)
129 {                                                 129 {
130   char c1 = '\0', c2 = '\0';                      130   char c1 = '\0', c2 = '\0';
131                                                   131   
132   while (len > 0)                                 132   while (len > 0)
133     {                                             133     {
134       c1 = (unsigned char) *s1++;                 134       c1 = (unsigned char) *s1++;
135       c2 = (unsigned char) *s2++;                 135       c2 = (unsigned char) *s2++;
136       if (c1 == '\0' || c1 != c2)                 136       if (c1 == '\0' || c1 != c2)
137         return c1 - c2;                           137         return c1 - c2;
138       len--;                                      138       len--;
139     }                                             139     }
140                                                   140   
141   return c1 - c2;                                 141   return c1 - c2;
142 }                                                 142 }
143                                                   143 
144                                                   144 
145 static unsigned long int _random_seed = 931867    145 static unsigned long int _random_seed = 93186752;
146                                                   146 
147 /**                                               147 /**
148  * The following code is borrowed from Glenn R    148  * The following code is borrowed from Glenn Rhoads.
149  * http://remus.rutgers.edu/~rhoads/Code/code.    149  * http://remus.rutgers.edu/~rhoads/Code/code.html
150  * License to be defined...                       150  * License to be defined...
151  */                                               151  */
152 unsigned long int random (void)                   152 unsigned long int random (void)
153 {                                                 153 {
154 /* The following parameters are recommended se    154 /* The following parameters are recommended settings based on research
155    uncomment the one you want. */                 155    uncomment the one you want. */
156                                                   156 
157 /* For RAND_MAX == 4294967291 */                  157 /* For RAND_MAX == 4294967291 */
158    static unsigned int a = 1588635695, q = 2,     158    static unsigned int a = 1588635695, q = 2, r = 1117695901;
159 /* static unsigned int a = 1223106847, m = 429    159 /* static unsigned int a = 1223106847, m = 4294967291U, q = 3, r = 625646750;*/
160 /* static unsigned int a = 279470273, m = 4294    160 /* static unsigned int a = 279470273, m = 4294967291U, q = 15, r = 102913196;*/
161                                                   161 
162 /* For RAND_MAX == 2147483647 */                  162 /* For RAND_MAX == 2147483647 */
163 /* static unsigned int a = 1583458089, m = 214    163 /* static unsigned int a = 1583458089, m = 2147483647, q = 1, r = 564025558; */
164 /* static unsigned int a = 784588716, m = 2147    164 /* static unsigned int a = 784588716, m = 2147483647, q = 2, r = 578306215;  */
165 /* static unsigned int a = 16807, m = 21474836    165 /* static unsigned int a = 16807, m = 2147483647, q = 127773, r = 2836;      */
166 /* static unsigned int a = 950706376, m = 2147    166 /* static unsigned int a = 950706376, m = 2147483647, q = 2, r = 246070895;  */
167                                                   167 
168    _random_seed = a*(_random_seed % q) - r*(_r    168    _random_seed = a*(_random_seed % q) - r*(_random_seed / q);
169    return _random_seed;                           169    return _random_seed;
170 }                                                 170 }
171                                                   171 
172                                                   172 
173 void srandom (unsigned long int seed)             173 void srandom (unsigned long int seed)
174 {                                                 174 {
175   _random_seed = seed;                            175   _random_seed = seed;
176 }                                                 176 }
177                                                   177 
178                                                   178 
179 /* I (d2) borrowed and rewrote this for Nachos    179 /* I (d2) borrowed and rewrote this for Nachos/INSA Rennes. Thanks to
180    them for having kindly allowed me to do so.    180    them for having kindly allowed me to do so. */
181 int vsnprintf(char *buff, sos_size_t len, cons    181 int vsnprintf(char *buff, sos_size_t len, const char * format, va_list ap)
182 {                                                 182 {
183   sos_size_t i, result;                           183   sos_size_t i, result;
                                                   >> 184   sos_bool_t fmt_modifiers = FALSE;
                                                   >> 185   sos_bool_t prefix_long = FALSE;
                                                   >> 186   sos_bool_t prefix_long_long = FALSE;
184                                                   187   
185   if (!buff || !format || (len < 0))              188   if (!buff || !format || (len < 0))
186     return -1;                                    189     return -1;
187                                                   190   
188 #define PUTCHAR(thechar) \                        191 #define PUTCHAR(thechar) \
189   do { \                                          192   do { \
190     if (result < len-1) \                         193     if (result < len-1) \
191       *buff++ = (thechar);  \                     194       *buff++ = (thechar);  \
192     result++; \                                   195     result++; \
193   } while (0)                                     196   } while (0)
194                                                   197   
195   result = 0;                                     198   result = 0;
196   for(i=0 ; format[i] != '\0' ; i++){          !! 199   for(i=0 ; format[i] != '\0' ; i++)
197     switch (format[i])                         !! 200     {
198       {                                        !! 201       if (!fmt_modifiers && (format[i] != '%'))
199       case '%':                                !! 202         {
200         i++;                                   !! 203           PUTCHAR(format[i]);
201         switch(format[i])                      !! 204           continue;
202           {                                    !! 205         }
203           case '%':                            !! 206         
                                                   >> 207       switch (format[i])
                                                   >> 208         {
                                                   >> 209         case '%':
                                                   >> 210           if (fmt_modifiers)
204             {                                     211             {
205               PUTCHAR('%');                       212               PUTCHAR('%');
                                                   >> 213               fmt_modifiers = FALSE;
206               break;                              214               break;
207             }                                     215             }
208           case 'i':;                           !! 216           
209           case 'd':                            !! 217           fmt_modifiers    = TRUE;
210             {                                  !! 218           prefix_long      = FALSE;
211               int integer = va_arg(ap,int);    !! 219           prefix_long_long = FALSE;
212               int cpt2 = 0;                    !! 220           break;
213               char buff_int[16];               !! 221           
214                                                !! 222         case 'l':
215               if (integer<0)                   !! 223           if (prefix_long)
216                 PUTCHAR('-');                  !! 224             prefix_long_long = TRUE;
217               /* Ne fait pas integer = -intege !! 225           else
218                  n'a pas d'equivalent positif  !! 226             prefix_long = TRUE;
219                                                !! 227           break;
220               do {                             !! 228           
221                 int m10 = integer%10;          !! 229         case 'u':
222                 m10 = (m10 < 0)? -m10:m10;     !! 230           {
223                 buff_int[cpt2++]=(char)('0'+ m !! 231             if (! prefix_long_long)
224                 integer=integer/10;            !! 232               {
225               } while(integer!=0);             !! 233                 unsigned int integer = va_arg(ap,unsigned int);
226                                                !! 234                 int cpt2 = 0;
227               for(cpt2 = cpt2 - 1 ; cpt2 >= 0  !! 235                 char buff_int[16];
228                 PUTCHAR(buff_int[cpt2]);       !! 236                 
229                                                !! 237                 do {
230               break;                           !! 238                   int m10 = integer%10;
231             }                                  !! 239                   buff_int[cpt2++]=(char)('0'+ m10);
                                                   >> 240                   integer=integer/10;
                                                   >> 241                 } while(integer!=0);
232                                                   242             
233           case 'c':                            !! 243                 for(cpt2 = cpt2 - 1 ; cpt2 >= 0 ; cpt2--)
234             {                                  !! 244                   PUTCHAR(buff_int[cpt2]);
235               int value = va_arg(ap,int);      !! 245               }
236               PUTCHAR((char)value);            !! 246             else
237               break;                           !! 247               {
238             }                                  !! 248                 unsigned long long int integer
                                                   >> 249                   = va_arg(ap,unsigned long long int);
                                                   >> 250                 int cpt2 = 0;
                                                   >> 251                 char buff_int[32];
                                                   >> 252                 
                                                   >> 253                 do {
                                                   >> 254                   int m10 = integer%10;
                                                   >> 255                   buff_int[cpt2++]=(char)('0'+ m10);
                                                   >> 256                   integer=integer/10;
                                                   >> 257                 } while(integer!=0);
239                                                   258             
240           case 's':                            !! 259                 for(cpt2 = cpt2 - 1 ; cpt2 >= 0 ; cpt2--)
241             {                                  !! 260                   PUTCHAR(buff_int[cpt2]);
242               char *string = va_arg(ap,char *) !! 261               }
243               if (! string)                    !! 262           }
244                 string = "(null)";             !! 263           fmt_modifiers = FALSE;
245               for( ; *string != '\0' ; string+ !! 264           break;
246                 PUTCHAR(*string);              << 
247               break;                           << 
248             }                                  << 
249                                                   265 
250           case 'p':                            !! 266         case 'i':
251             PUTCHAR('0');                      !! 267         case 'd':
252             PUTCHAR('x');                      !! 268           {
253           case 'x':                            !! 269             if (! prefix_long_long)
254             {                                  !! 270               {
255               unsigned int hexa = va_arg(ap,in !! 271                 int integer = va_arg(ap,int);
256               unsigned int nb;                 !! 272                 int cpt2 = 0;
257               int i, had_nonzero = 0;          !! 273                 char buff_int[16];
258               for(i=0 ; i < 8 ; i++)           !! 274                 
259                 {                              !! 275                 if (integer<0)
260                   nb = (unsigned int)(hexa <<  !! 276                   PUTCHAR('-');
261                   nb = (nb >> 28) & 0xf;       !! 277                 /* Ne fait pas integer = -integer ici parce que INT_MIN
262                   // Skip the leading zeros    !! 278                    n'a pas d'equivalent positif (int = [-2^31, 2^31-1]) */
263                   if (nb == 0)                 !! 279                 
264                     {                          !! 280                 do {
265                       if (had_nonzero)         !! 281                   int m10 = integer%10;
266                         PUTCHAR('0');          !! 282                   m10 = (m10 < 0)? -m10:m10;
267                     }                          !! 283                   buff_int[cpt2++]=(char)('0'+ m10);
268                   else                         !! 284                   integer=integer/10;
269                     {                          !! 285                 } while(integer!=0);
270                       had_nonzero = 1;         !! 286             
271                       if (nb < 10)             !! 287                 for(cpt2 = cpt2 - 1 ; cpt2 >= 0 ; cpt2--)
272                         PUTCHAR('0'+nb);       !! 288                   PUTCHAR(buff_int[cpt2]);
273                       else                     !! 289               }
274                         PUTCHAR('a'+(nb-10));  !! 290             else
275                     }                          !! 291               {
276                 }                              !! 292                 long long int integer = va_arg(ap,long long int);
277               if (! had_nonzero)               !! 293                 int cpt2 = 0;
278                 PUTCHAR('0');                  !! 294                 char buff_int[32];
279               break;                           !! 295                 
280             }                                  !! 296                 if (integer<0)
                                                   >> 297                   PUTCHAR('-');
                                                   >> 298                 /* Ne fait pas integer = -integer ici parce que INT_MIN
                                                   >> 299                    n'a pas d'equivalent positif (int = [-2^63, 2^63-1]) */
                                                   >> 300                 
                                                   >> 301                 do {
                                                   >> 302                   int m10 = integer%10;
                                                   >> 303                   m10 = (m10 < 0)? -m10:m10;
                                                   >> 304                   buff_int[cpt2++]=(char)('0'+ m10);
                                                   >> 305                   integer=integer/10;
                                                   >> 306                 } while(integer!=0);
                                                   >> 307             
                                                   >> 308                 for(cpt2 = cpt2 - 1 ; cpt2 >= 0 ; cpt2--)
                                                   >> 309                   PUTCHAR(buff_int[cpt2]);
                                                   >> 310               }
                                                   >> 311           }
                                                   >> 312           fmt_modifiers = FALSE;
                                                   >> 313           break;
                                                   >> 314           
                                                   >> 315         case 'c':
                                                   >> 316           {
                                                   >> 317             int value = va_arg(ap,int);
                                                   >> 318             PUTCHAR((char)value);
                                                   >> 319             fmt_modifiers = FALSE;
281             break;                                320             break;
282                                                << 
283           default:                             << 
284             PUTCHAR('%');                      << 
285             PUTCHAR(format[i]);                << 
286           }                                       321           }
287         break;                                 !! 322           
288                                                !! 323         case 's':
289       default:                                 !! 324           {
290         PUTCHAR(format[i]);                    !! 325             char *string = va_arg(ap,char *);
291       }                                        !! 326             if (! string)
292   }                                            !! 327               string = "(null)";
293                                                !! 328             for( ; *string != '\0' ; string++)
                                                   >> 329               PUTCHAR(*string);
                                                   >> 330             fmt_modifiers = FALSE;
                                                   >> 331             break;
                                                   >> 332           }
                                                   >> 333           
                                                   >> 334         case 'p':
                                                   >> 335           PUTCHAR('0');
                                                   >> 336           PUTCHAR('x');
                                                   >> 337         case 'x':
                                                   >> 338           {
                                                   >> 339             unsigned long long int hexa;
                                                   >> 340             unsigned long long int nb;
                                                   >> 341             int i, had_nonzero = 0;
                                                   >> 342             
                                                   >> 343             if (prefix_long_long)
                                                   >> 344               hexa = va_arg(ap,unsigned long long int);
                                                   >> 345             else
                                                   >> 346               hexa = va_arg(ap,unsigned int);
                                                   >> 347             
                                                   >> 348             for(i=0 ; i < 16 ; i++)
                                                   >> 349               {
                                                   >> 350                 nb = (unsigned long long int)(hexa << (i*4));
                                                   >> 351                 nb = (nb >> 60) & 0xf;
                                                   >> 352                 // Skip the leading zeros
                                                   >> 353                 if (nb == 0)
                                                   >> 354                   {
                                                   >> 355                     if (had_nonzero)
                                                   >> 356                       PUTCHAR('0');
                                                   >> 357                   }
                                                   >> 358                 else
                                                   >> 359                   {
                                                   >> 360                     had_nonzero = 1;
                                                   >> 361                     if (nb < 10)
                                                   >> 362                       PUTCHAR('0'+nb);
                                                   >> 363                     else
                                                   >> 364                       PUTCHAR('a'+(nb-10));
                                                   >> 365                   }
                                                   >> 366               }
                                                   >> 367             if (! had_nonzero)
                                                   >> 368               PUTCHAR('0');
                                                   >> 369           }
                                                   >> 370           fmt_modifiers = FALSE;
                                                   >> 371           break;
                                                   >> 372           
                                                   >> 373         default:
                                                   >> 374           PUTCHAR('%');
                                                   >> 375           if (prefix_long)
                                                   >> 376             PUTCHAR('l');
                                                   >> 377           if (prefix_long_long)
                                                   >> 378             PUTCHAR('l');
                                                   >> 379           PUTCHAR(format[i]);
                                                   >> 380           fmt_modifiers = FALSE;
                                                   >> 381         }
                                                   >> 382     }
                                                   >> 383 
294   *buff = '\0';                                   384   *buff = '\0';
295   return result;                                  385   return result;
296 }                                                 386 }
297                                                   387 
298                                                   388 
299 int snprintf(char * buff, sos_size_t len, cons    389 int snprintf(char * buff, sos_size_t len, const char *format, ...)
300 {                                                 390 {
301   va_list ap;                                     391   va_list ap;
302                                                   392  
303   va_start(ap, format);                           393   va_start(ap, format);
304   len = vsnprintf(buff, len, format, ap);         394   len = vsnprintf(buff, len, format, ap);
305   va_end(ap);                                     395   va_end(ap);
306                                                   396  
307   return len;                                     397   return len;
308 }                                                 398 }
                                                      

source navigation ] diff markup ] identifier search ] general search ]