root/ulib.c

/* [<][>][^][v][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, char *t)
   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)
  20 {
  21   while(*p && *p == *q)
  22     p++, q++;
  23   return (uchar)*p - (uchar)*q;
  24 }
  25 
  26 uint
  27 strlen(char *s)
  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)
  38 {
  39   stosb(dst, c, n);
  40   return dst;
  41 }
  42 
  43 char*
  44 strchr(const char *s, char c)
  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)
  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(char *n, struct stat *st)
  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)
  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, void *vsrc, int n)
  97 {
  98   char *dst, *src;
  99   
 100   dst = vdst;
 101   src = vsrc;
 102   while(n-- > 0)
 103     *dst++ = *src++;
 104   return vdst;
 105 }

/* [<][>][^][v][top][bottom][index][help] */