Compare commits
No commits in common. "3cc2e4b2a4f41702dc73aa9d954dd664c85fec1a" and "d66506256db309b94583973a01891728c16e66bf" have entirely different histories.
3cc2e4b2a4
...
d66506256d
15
apps/app.c
15
apps/app.c
@ -15,20 +15,23 @@ int main()
|
|||||||
atexit(bye);
|
atexit(bye);
|
||||||
printf("Welcome to %s from userspace (pid %d)!\n", "Luna", getpid());
|
printf("Welcome to %s from userspace (pid %d)!\n", "Luna", getpid());
|
||||||
|
|
||||||
FILE* f = fopen("/etc/motd", "r");
|
mkdir("/home", 0755);
|
||||||
if (!f)
|
mkdir("/home/user", 0755);
|
||||||
|
|
||||||
|
int fd = open("/home/user/notes.txt", O_RDWR | O_CREAT, 0644);
|
||||||
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
perror("fopen");
|
perror("open");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[512];
|
char buffer[512];
|
||||||
size_t nread = fread(buffer, 1, sizeof(buffer), f);
|
ssize_t nread = read(fd, buffer, sizeof(buffer));
|
||||||
buffer[nread] = 0;
|
buffer[nread] = 0;
|
||||||
|
|
||||||
printf("/etc/motd says: %s", buffer);
|
printf("/home/user/notes.txt says: %s", buffer);
|
||||||
|
|
||||||
fclose(f);
|
close(fd);
|
||||||
|
|
||||||
time_t now = time(NULL);
|
time_t now = time(NULL);
|
||||||
printf("date: %s", ctime(&now));
|
printf("date: %s", ctime(&now));
|
||||||
|
@ -10,23 +10,6 @@
|
|||||||
|
|
||||||
FILE* stderr = nullptr;
|
FILE* stderr = nullptr;
|
||||||
|
|
||||||
static int fopen_parse_mode(const char* mode)
|
|
||||||
{
|
|
||||||
int result = 0;
|
|
||||||
switch (*mode)
|
|
||||||
{
|
|
||||||
case 'r': result |= O_RDONLY; break;
|
|
||||||
case 'w': result |= (O_WRONLY | O_CREAT | O_TRUNC); break;
|
|
||||||
case 'a': result |= (O_WRONLY | O_CREAT | O_APPEND); break;
|
|
||||||
|
|
||||||
default: errno = EINVAL; return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strchr(mode, '+')) result |= O_RDWR;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
int console_write(const char* str, size_t size)
|
int console_write(const char* str, size_t size)
|
||||||
@ -35,13 +18,10 @@ extern "C"
|
|||||||
__errno_return(rc, int);
|
__errno_return(rc, int);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* fopen(const char* path, const char* mode)
|
FILE* fopen(const char* path, const char*)
|
||||||
{
|
{
|
||||||
int flags;
|
// FIXME: Parse the mode string.
|
||||||
|
int fd = open(path, O_RDWR);
|
||||||
if ((flags = fopen_parse_mode(mode)) < 0) return nullptr;
|
|
||||||
|
|
||||||
int fd = open(path, flags, 0666);
|
|
||||||
if (fd < 0) return nullptr;
|
if (fd < 0) return nullptr;
|
||||||
|
|
||||||
FILE* f = (FILE*)malloc(sizeof(FILE));
|
FILE* f = (FILE*)malloc(sizeof(FILE));
|
||||||
|
Loading…
Reference in New Issue
Block a user