Nice wrappers around ioctl() syscall
This commit is contained in:
24
ulib/system/ioctl.c
Normal file
24
ulib/system/ioctl.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <system/ioctl.h>
|
||||||
|
#include <sysdefs/ioctl.h>
|
||||||
|
#include <system/system.h>
|
||||||
|
|
||||||
|
int32_t ioctl_openfile(const char *path, uint64_t flags) {
|
||||||
|
IOCtlOF cfg = { .path = path, .flags = flags };
|
||||||
|
return ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, &cfg);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t ioctl_writefile(int32_t ioh, const uint8_t *const buffer, size_t len, size_t off) {
|
||||||
|
IOCtlWF cfg = { .buffer = buffer, .len = len, .off = off };
|
||||||
|
return ioctl(ioh, IOCTL_WRITE, &cfg);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t ioctl_readfile(int32_t ioh, uint8_t *const buffer, size_t len, size_t off) {
|
||||||
|
IOCtlRF cfg = { .buffer = buffer, .len = len, off = off };
|
||||||
|
return ioctl(ioh, IOCTL_READ, &cfg);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t ioctl_closefile(int32_t ioh) {
|
||||||
|
return ioctl(ioh, IOCTL_CLOSEF, NULL);
|
||||||
|
}
|
12
ulib/system/ioctl.h
Normal file
12
ulib/system/ioctl.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef ULIB_SYSTEM_IOCTL_H_
|
||||||
|
#define ULIB_SYSTEM_IOCTL_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
int32_t ioctl_openfile(const char *path, uint64_t flags);
|
||||||
|
int32_t ioctl_writefile(int32_t ioh, const uint8_t *const buffer, size_t len, size_t off);
|
||||||
|
int32_t ioctl_readfile(int32_t ioh, uint8_t *const buffer, size_t len, size_t off);
|
||||||
|
int32_t ioctl_closefile(int32_t ioh);
|
||||||
|
|
||||||
|
#endif // ULIB_SYSTEM_IOCTL_H_
|
@ -1,23 +1,22 @@
|
|||||||
#include <system/system.h>
|
#include <stdint.h>
|
||||||
#include <sysdefs/ioctl.h>
|
#include <sysdefs/ioctl.h>
|
||||||
|
#include <system/system.h>
|
||||||
|
#include <system/ioctl.h>
|
||||||
#include <string/string.h>
|
#include <string/string.h>
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
debugprint("Hello world from userspace in C");
|
debugprint("Hello world from userspace in C");
|
||||||
|
|
||||||
IOCtlOF ioctlof = { .path = "base:/hello.txt", .flags = IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE };
|
int32_t ioh = ioctl_openfile("base:/hello.txt", IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE);
|
||||||
int32_t ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, &ioctlof);
|
|
||||||
|
|
||||||
char *text = "Write to a file";
|
char *text = "Hello from the filesystem";
|
||||||
IOCtlWF ioctlwf = { .buffer = text, .len = string_len(text), .off = 0 };
|
ioctl_writefile(ioh, (const uint8_t *const)text, string_len(text), 0);
|
||||||
ioctl(ioh, IOCTL_WRITE, &ioctlwf);
|
|
||||||
|
|
||||||
char buf[0x100];
|
char buf[0x100];
|
||||||
IOCtlRF ioctlrf = { .buffer = buf, .len = string_len(text), .off = 0 };
|
ioctl_readfile(ioh, (uint8_t *const)buf, string_len(text), 0);
|
||||||
ioctl(ioh, IOCTL_READ, &ioctlrf);
|
|
||||||
|
|
||||||
debugprint(buf);
|
debugprint(buf);
|
||||||
|
|
||||||
ioctl(ioh, IOCTL_CLOSEF, NULL);
|
ioctl_closefile(ioh);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user