root/ulib.c

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

DEFINITIONS

This source file includes following definitions.
  1. strcpy
  2. strcmp
  3. strlen
  4. memset
  5. strchr
  6. gets
  7. stat
  8. atoi
  9. memmove

   1 #include "types.h"
   2 #include "stat.h"
   3 #include "fcntl.h"
   4 #include "user.h"
   5 #include "x86.h"
   6 
   7 char*
   8 strcpy(char *s, const char *t)
     /* [previous][next][first][last][top][bottom][index][help]  */
   9 {
  10   char *os;
  11 
  12   os = s;
  13   while((*s++ = *t++) != 0)
  14     ;
  15   return os;
  16 }
  17 
  18 int
  19 strcmp(const char *p, const char *q)
     /* [previous][next][first][last][top][bottom][index][help]  */
  20 {
  21   while(*p && *p == *q)
  22     p++, q++;
  23   return (uchar)*p - (uchar)*q;
  24 }
  25 
  26 uint
  27 strlen(const char *s)
     /* [previous][next][first][last][top][bottom][index][help]  */
  28 {
  29   int n;
  30 
  31   for(n = 0; s[n]; n++)
  32     ;
  33   return n;
  34 }
  35 
  36 void*
  37 memset(void *dst, int c, uint n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  38 {
  39   stosb(dst, c, n);
  40   return dst;
  41 }
  42 
  43 char*
  44 strchr(const char *s, char c)
     /* [previous][next][first][last][top][bottom][index][help]  */
  45 {
  46   for(; *s; s++)
  47     if(*s == c)
  48       return (char*)s;
  49   return 0;
  50 }
  51 
  52 char*
  53 gets(char *buf, int max)
     /* [previous][next][first][last][top][bottom][index][help]  */
  54 {
  55   int i, cc;
  56   char c;
  57 
  58   for(i=0; i+1 < max; ){
  59     cc = read(0, &c, 1);
  60     if(cc < 1)
  61       break;
  62     buf[i++] = c;
  63     if(c == '\n' || c == '\r')
  64       break;
  65   }
  66   buf[i] = '\0';
  67   return buf;
  68 }
  69 
  70 int
  71 stat(const char *n, struct stat *st)
     /* [previous][next][first][last][top][bottom][index][help]  */
  72 {
  73   int fd;
  74   int r;
  75 
  76   fd = open(n, O_RDONLY);
  77   if(fd < 0)
  78     return -1;
  79   r = fstat(fd, st);
  80   close(fd);
  81   return r;
  82 }
  83 
  84 int
  85 atoi(const char *s)
     /* [previous][next][first][last][top][bottom][index][help]  */
  86 {
  87   int n;
  88 
  89   n = 0;
  90   while('0' <= *s && *s <= '9')
  91     n = n*10 + *s++ - '0';
  92   return n;
  93 }
  94 
  95 void*
  96 memmove(void *vdst, const void *vsrc, int n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  97 {
  98   char *dst;
  99   const char *src;
 100 
 101   dst = vdst;
 102   src = vsrc;
 103   while(n-- > 0)
 104     *dst++ = *src++;
 105   return vdst;
 106 }

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