2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
|
|
|
#include "kernel/fcntl.h"
|
|
|
|
#include "user/user.h"
|
2006-07-16 15:36:31 +00:00
|
|
|
|
2022-08-10 10:37:21 +00:00
|
|
|
//
|
|
|
|
// wrapper so that it's OK if main() does not call exit().
|
|
|
|
//
|
|
|
|
void
|
|
|
|
_main()
|
|
|
|
{
|
|
|
|
extern int main();
|
|
|
|
main();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2006-07-16 16:00:03 +00:00
|
|
|
char*
|
2018-08-31 13:21:19 +00:00
|
|
|
strcpy(char *s, const char *t)
|
2006-07-16 16:00:03 +00:00
|
|
|
{
|
2006-09-06 17:04:06 +00:00
|
|
|
char *os;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-09-06 17:04:06 +00:00
|
|
|
os = s;
|
|
|
|
while((*s++ = *t++) != 0)
|
|
|
|
;
|
|
|
|
return os;
|
2006-07-16 16:00:03 +00:00
|
|
|
}
|
2006-08-11 13:55:18 +00:00
|
|
|
|
2006-08-23 01:09:24 +00:00
|
|
|
int
|
|
|
|
strcmp(const char *p, const char *q)
|
|
|
|
{
|
2006-09-06 17:27:19 +00:00
|
|
|
while(*p && *p == *q)
|
2006-09-06 17:04:06 +00:00
|
|
|
p++, q++;
|
2007-08-08 09:30:48 +00:00
|
|
|
return (uchar)*p - (uchar)*q;
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2007-08-08 09:30:48 +00:00
|
|
|
uint
|
2018-08-31 13:21:19 +00:00
|
|
|
strlen(const char *s)
|
2006-08-11 13:55:18 +00:00
|
|
|
{
|
2007-08-10 17:17:42 +00:00
|
|
|
int n;
|
|
|
|
|
2006-08-11 13:55:18 +00:00
|
|
|
for(n = 0; s[n]; n++)
|
|
|
|
;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-09-06 17:04:06 +00:00
|
|
|
void*
|
2007-08-08 09:30:48 +00:00
|
|
|
memset(void *dst, int c, uint n)
|
2006-08-12 11:38:57 +00:00
|
|
|
{
|
2019-05-31 15:45:42 +00:00
|
|
|
char *cdst = (char *) dst;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
cdst[i] = c;
|
|
|
|
}
|
2006-08-12 11:38:57 +00:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2006-09-06 17:04:06 +00:00
|
|
|
char*
|
2006-08-23 01:09:24 +00:00
|
|
|
strchr(const char *s, char c)
|
|
|
|
{
|
2006-09-06 17:27:19 +00:00
|
|
|
for(; *s; s++)
|
|
|
|
if(*s == c)
|
2011-01-11 18:01:13 +00:00
|
|
|
return (char*)s;
|
2006-09-06 17:04:06 +00:00
|
|
|
return 0;
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
2006-09-06 17:04:06 +00:00
|
|
|
char*
|
2006-08-11 13:55:18 +00:00
|
|
|
gets(char *buf, int max)
|
|
|
|
{
|
2007-08-10 17:17:42 +00:00
|
|
|
int i, cc;
|
2006-08-11 13:55:18 +00:00
|
|
|
char c;
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2007-08-10 17:17:42 +00:00
|
|
|
for(i=0; i+1 < max; ){
|
2006-08-11 13:55:18 +00:00
|
|
|
cc = read(0, &c, 1);
|
|
|
|
if(cc < 1)
|
|
|
|
break;
|
2007-08-08 08:04:20 +00:00
|
|
|
buf[i++] = c;
|
2006-08-11 13:55:18 +00:00
|
|
|
if(c == '\n' || c == '\r')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
2006-08-14 03:00:13 +00:00
|
|
|
|
|
|
|
int
|
2018-08-31 13:21:19 +00:00
|
|
|
stat(const char *n, struct stat *st)
|
2006-08-14 03:00:13 +00:00
|
|
|
{
|
2006-08-14 14:13:52 +00:00
|
|
|
int fd;
|
2006-08-14 03:00:13 +00:00
|
|
|
int r;
|
|
|
|
|
2006-08-14 14:13:52 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-08-22 06:01:32 +00:00
|
|
|
|
|
|
|
void*
|
2018-08-31 13:21:19 +00:00
|
|
|
memmove(void *vdst, const void *vsrc, int n)
|
2007-08-22 06:01:32 +00:00
|
|
|
{
|
2018-08-31 13:21:19 +00:00
|
|
|
char *dst;
|
|
|
|
const char *src;
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
dst = vdst;
|
|
|
|
src = vsrc;
|
2019-10-09 01:24:03 +00:00
|
|
|
if (src > dst) {
|
|
|
|
while(n-- > 0)
|
|
|
|
*dst++ = *src++;
|
|
|
|
} else {
|
|
|
|
dst += n;
|
|
|
|
src += n;
|
|
|
|
while(n-- > 0)
|
|
|
|
*--dst = *--src;
|
|
|
|
}
|
2007-08-22 06:01:32 +00:00
|
|
|
return vdst;
|
|
|
|
}
|
2019-10-09 01:18:54 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
memcmp(const void *s1, const void *s2, uint n)
|
|
|
|
{
|
|
|
|
const char *p1 = s1, *p2 = s2;
|
|
|
|
while (n-- > 0) {
|
|
|
|
if (*p1 != *p2) {
|
|
|
|
return *p1 - *p2;
|
|
|
|
}
|
|
|
|
p1++;
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
memcpy(void *dst, const void *src, uint n)
|
|
|
|
{
|
|
|
|
return memmove(dst, src, n);
|
|
|
|
}
|