libc: Implement wrappers for sys_{open,read,write}
read() and close() are in unistd.h, but open() in fnctl.h. I thought only the definitions for O_SOMETHING were in fnctl.h, but it is as it is. Don't know why, but let's not anger the Unix gods. The FILE* C API is pending as well.
This commit is contained in:
parent
1b84c443fe
commit
9e0bd39964
@ -1,3 +1,4 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
#include <luna.h>
|
#include <luna.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -31,17 +32,16 @@ int main()
|
|||||||
|
|
||||||
printf("Opening %s for reading...\n", filename);
|
printf("Opening %s for reading...\n", filename);
|
||||||
|
|
||||||
int fd = syscall(SYS_open, filename, 1);
|
int fd = open(filename, O_RDONLY);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
perror("open");
|
perror("open");
|
||||||
// return 1;
|
// return 1;
|
||||||
}
|
}
|
||||||
else { printf("Got fd %d\n", fd); }
|
|
||||||
|
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
|
|
||||||
ssize_t nread = syscall(SYS_read, fd, sizeof(buf), buf);
|
ssize_t nread = read(fd, buf, sizeof(buf));
|
||||||
if (nread < 0)
|
if (nread < 0)
|
||||||
{
|
{
|
||||||
perror("read");
|
perror("read");
|
||||||
@ -56,7 +56,7 @@ int main()
|
|||||||
printf("%s", buf);
|
printf("%s", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (syscall(SYS_close, fd) < 0)
|
if (close(fd) < 0)
|
||||||
{
|
{
|
||||||
perror("close");
|
perror("close");
|
||||||
// return 1;
|
// return 1;
|
||||||
|
17
libs/libc/include/fcntl.h
Normal file
17
libs/libc/include/fcntl.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef _FCNTL_H
|
||||||
|
#define _FCNTL_H
|
||||||
|
|
||||||
|
#define O_RDONLY 1
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int open(const char*, int);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
@ -1,4 +1,8 @@
|
|||||||
#ifndef _SYS_TYPES_H
|
#ifndef _SYS_TYPES_H
|
||||||
#define _SYS_TYPES_H
|
#define _SYS_TYPES_H
|
||||||
|
|
||||||
typedef long int pid_t;
|
typedef long int pid_t;
|
||||||
|
typedef unsigned long int size_t;
|
||||||
|
typedef long int ssize_t;
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,10 +1,13 @@
|
|||||||
#ifndef _UNISTD_H
|
#ifndef _UNISTD_H
|
||||||
#define _UNISTD_H
|
#define _UNISTD_H
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int execv(const char*, char* const[]);
|
int execv(const char*, char* const[]);
|
||||||
int execve(const char*, char* const[], char* const[]);
|
int execve(const char*, char* const[], char* const[]);
|
||||||
int execvp(const char*, char* const[]);
|
int execvp(const char*, char* const[]);
|
||||||
@ -12,7 +15,11 @@ extern "C"
|
|||||||
long syscall(long, ...);
|
long syscall(long, ...);
|
||||||
unsigned int sleep(unsigned int);
|
unsigned int sleep(unsigned int);
|
||||||
|
|
||||||
|
ssize_t read(int, void*, size_t);
|
||||||
|
int close(int);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
11
libs/libc/src/fcntl.cpp
Normal file
11
libs/libc/src/fcntl.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/syscall.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
int open(const char* pathname, int flags)
|
||||||
|
{
|
||||||
|
return (int)syscall(SYS_open, pathname, flags);
|
||||||
|
}
|
||||||
|
}
|
@ -74,4 +74,14 @@ extern "C"
|
|||||||
{
|
{
|
||||||
return msleep(seconds * 1000);
|
return msleep(seconds * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssize_t read(int fd, void* buf, size_t count)
|
||||||
|
{
|
||||||
|
return syscall(SYS_read, fd, count, buf); // yes, our read() syscall is in the wrong order.
|
||||||
|
}
|
||||||
|
|
||||||
|
int close(int fd)
|
||||||
|
{
|
||||||
|
return (int)syscall(SYS_close, fd);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user