root/string.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. memset
  2. memcmp
  3. memmove
  4. memcpy
  5. strncmp
  6. strncpy
  7. safestrcpy
  8. strlen

   1 #include "types.h"
   2 #include "x86.h"
   3 
   4 void*
   5 memset(void *dst, int c, uint n)
     /* [previous][next][first][last][top][bottom][index][help]  */
   6 {
   7   if ((int)dst%4 == 0 && n%4 == 0){
   8     c &= 0xFF;
   9     stosl(dst, (c<<24)|(c<<16)|(c<<8)|c, n/4);
  10   } else
  11     stosb(dst, c, n);
  12   return dst;
  13 }
  14 
  15 int
  16 memcmp(const void *v1, const void *v2, uint n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  17 {
  18   const uchar *s1, *s2;
  19 
  20   s1 = v1;
  21   s2 = v2;
  22   while(n-- > 0){
  23     if(*s1 != *s2)
  24       return *s1 - *s2;
  25     s1++, s2++;
  26   }
  27 
  28   return 0;
  29 }
  30 
  31 void*
  32 memmove(void *dst, const void *src, uint n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  33 {
  34   const char *s;
  35   char *d;
  36 
  37   s = src;
  38   d = dst;
  39   if(s < d && s + n > d){
  40     s += n;
  41     d += n;
  42     while(n-- > 0)
  43       *--d = *--s;
  44   } else
  45     while(n-- > 0)
  46       *d++ = *s++;
  47 
  48   return dst;
  49 }
  50 
  51 // memcpy exists to placate GCC.  Use memmove.
  52 void*
  53 memcpy(void *dst, const void *src, uint n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  54 {
  55   return memmove(dst, src, n);
  56 }
  57 
  58 int
  59 strncmp(const char *p, const char *q, uint n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  60 {
  61   while(n > 0 && *p && *p == *q)
  62     n--, p++, q++;
  63   if(n == 0)
  64     return 0;
  65   return (uchar)*p - (uchar)*q;
  66 }
  67 
  68 char*
  69 strncpy(char *s, const char *t, int n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  70 {
  71   char *os;
  72 
  73   os = s;
  74   while(n-- > 0 && (*s++ = *t++) != 0)
  75     ;
  76   while(n-- > 0)
  77     *s++ = 0;
  78   return os;
  79 }
  80 
  81 // Like strncpy but guaranteed to NUL-terminate.
  82 char*
  83 safestrcpy(char *s, const char *t, int n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  84 {
  85   char *os;
  86 
  87   os = s;
  88   if(n <= 0)
  89     return os;
  90   while(--n > 0 && (*s++ = *t++) != 0)
  91     ;
  92   *s = 0;
  93   return os;
  94 }
  95 
  96 int
  97 strlen(const char *s)
     /* [previous][next][first][last][top][bottom][index][help]  */
  98 {
  99   int n;
 100 
 101   for(n = 0; s[n]; n++)
 102     ;
 103   return n;
 104 }
 105 

/* [previous][next][first][last][top][bottom][index][help]  */