xv6-65oo2/ulib.c

106 lines
1.2 KiB
C
Raw Normal View History

2006-08-14 03:00:13 +00:00
#include "types.h"
#include "stat.h"
#include "fcntl.h"
#include "user.h"
2009-03-08 21:38:30 +00:00
#include "x86.h"
2006-07-16 16:00:03 +00:00
char*
strcpy(char *s, char *t)
{
char *os;
2006-09-06 17:27:19 +00:00
os = s;
while((*s++ = *t++) != 0)
;
return os;
2006-07-16 16:00:03 +00:00
}
int
strcmp(const char *p, const char *q)
{
2006-09-06 17:27:19 +00:00
while(*p && *p == *q)
p++, q++;
return (uchar)*p - (uchar)*q;
}
uint
strlen(char *s)
{
2007-08-10 17:17:42 +00:00
int n;
for(n = 0; s[n]; n++)
;
return n;
}
void*
memset(void *dst, int c, uint n)
{
2009-03-08 21:38:30 +00:00
stosb(dst, c, n);
return dst;
}
char*
strchr(const char *s, char c)
{
2006-09-06 17:27:19 +00:00
for(; *s; s++)
if(*s == c)
return (char*) s;
return 0;
}
char*
gets(char *buf, int max)
{
2007-08-10 17:17:42 +00:00
int i, cc;
char c;
2006-09-06 17:27:19 +00:00
2007-08-10 17:17:42 +00:00
for(i=0; i+1 < max; ){
cc = read(0, &c, 1);
if(cc < 1)
break;
buf[i++] = c;
if(c == '\n' || c == '\r')
break;
}
buf[i] = '\0';
return buf;
}
2006-08-14 03:00:13 +00:00
int
stat(char *n, struct stat *st)
{
int fd;
2006-08-14 03:00:13 +00:00
int r;
fd = open(n, O_RDONLY);
2006-09-06 17:57:47 +00:00
if(fd < 0)
return -1;
2006-08-14 03:00:13 +00:00
r = fstat(fd, st);
close(fd);
return r;
}
2007-08-08 08:56:09 +00:00
int
atoi(const char *s)
{
int n;
n = 0;
while('0' <= *s && *s <= '9')
n = n*10 + *s++ - '0';
return n;
}
void*
memmove(void *vdst, void *vsrc, int n)
{
char *dst, *src;
dst = vdst;
src = vsrc;
while(n-- > 0)
*dst++ = *src++;
return vdst;
}