xv6-riscv-kernel/user/ulib.c

146 lines
1.8 KiB
C
Raw Normal View History

#include "kernel/types.h"
#include "kernel/stat.h"
#include "kernel/fcntl.h"
#include "user/user.h"
//
// wrapper so that it's OK if main() does not call exit().
//
void
_main()
{
extern int main();
main();
exit(0);
}
2024-06-15 16:55:06 +02:00
char *
2018-08-31 15:21:19 +02:00
strcpy(char *s, const char *t)
2006-07-16 18:00:03 +02:00
{
char *os;
2006-09-06 19:27:19 +02:00
os = s;
2024-08-07 16:07:20 +02:00
while((*s++ = *t++) != 0) {}
return os;
2006-07-16 18:00:03 +02:00
}
int
strcmp(const char *p, const char *q)
{
2006-09-06 19:27:19 +02:00
while(*p && *p == *q)
p++, q++;
2024-05-24 11:26:40 +02:00
return (u8)*p - (u8)*q;
}
2024-05-24 11:26:40 +02:00
u32
2018-08-31 15:21:19 +02:00
strlen(const char *s)
{
2007-08-10 19:17:42 +02:00
int n;
2024-08-07 16:07:20 +02:00
for(n = 0; s[n]; n++) {}
return n;
}
2024-06-15 16:55:06 +02:00
void *
2024-05-24 11:26:40 +02:00
memset(void *dst, int c, u32 n)
{
2024-06-15 16:55:06 +02:00
char *cdst = (char *)dst;
int i;
for(i = 0; i < n; i++) {
2019-05-31 17:45:42 +02:00
cdst[i] = c;
}
return dst;
}
2024-06-15 16:55:06 +02:00
char *
strchr(const char *s, char c)
{
2006-09-06 19:27:19 +02:00
for(; *s; s++)
if(*s == c)
2024-06-15 16:55:06 +02:00
return (char *)s;
return 0;
}
2024-06-15 16:55:06 +02:00
char *
gets(char *buf, int max)
{
2024-06-15 16:55:06 +02:00
int i, cc;
char c;
2006-09-06 19:27:19 +02:00
2024-06-15 16:55:06 +02: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 05:00:13 +02:00
int
2018-08-31 15:21:19 +02:00
stat(const char *n, struct stat *st)
2006-08-14 05:00:13 +02:00
{
int fd;
2006-08-14 05:00:13 +02:00
int r;
fd = open(n, O_RDONLY);
2006-09-06 19:57:47 +02:00
if(fd < 0)
return -1;
2006-08-14 05:00:13 +02:00
r = fstat(fd, st);
close(fd);
return r;
}
2007-08-08 10:56:09 +02:00
int
atoi(const char *s)
{
int n;
n = 0;
while('0' <= *s && *s <= '9')
2024-06-15 16:55:06 +02:00
n = n * 10 + *s++ - '0';
2007-08-08 10:56:09 +02:00
return n;
}
2024-06-15 16:55:06 +02:00
void *
2018-08-31 15:21:19 +02:00
memmove(void *vdst, const void *vsrc, int n)
{
2024-06-15 16:55:06 +02:00
char *dst;
2018-08-31 15:21:19 +02:00
const char *src;
dst = vdst;
src = vsrc;
2024-06-15 16:55:06 +02:00
if(src > dst) {
while(n-- > 0)
*dst++ = *src++;
} else {
dst += n;
src += n;
while(n-- > 0)
*--dst = *--src;
}
return vdst;
}
int
2024-05-24 11:26:40 +02:00
memcmp(const void *s1, const void *s2, u32 n)
{
const char *p1 = s1, *p2 = s2;
2024-06-15 16:55:06 +02:00
while(n-- > 0) {
if(*p1 != *p2) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}
void *
2024-05-24 11:26:40 +02:00
memcpy(void *dst, const void *src, u32 n)
{
return memmove(dst, src, n);
}