PatchworkOS
Loading...
Searching...
No Matches
ctl.c
Go to the documentation of this file.
1#include <kernel/fs/ctl.h>
2
3#include <kernel/mem/pmm.h>
5
6#include <errno.h>
7#include <sys/argsplit.h>
8
10{
11 if (ctls == NULL || file == NULL || buffer == NULL || count == 0)
12 {
13 errno = EINVAL;
14 return ERR;
15 }
16
17 if (count > MAX_PATH)
18 {
19 errno = E2BIG;
20 return ERR;
21 }
22
23 uint8_t argBuffer[MAX_PATH];
24
25 uint64_t argc;
26 const char** argv = argsplit_buf(argBuffer, MAX_PATH, buffer, count, &argc);
27 if (argv == NULL)
28 {
29 return ERR;
30 }
31 if (argc == 0)
32 {
34 return ERR;
35 }
36
37 ctl_t* ctl = &ctls[0];
38 while (ctl->name != NULL)
39 {
40 if (strcmp(ctl->name, argv[0]) == 0)
41 {
42 if (argc < ctl->argcMin || argc > ctl->argcMax)
43 {
45 return ERR;
46 }
47
48 if (ctl->func(file, argc, argv) == ERR)
49 {
50 return ERR;
51 }
52 return count;
53 }
54
55 ctl++;
56 }
57
58 errno = ENOENT;
59 return ERR;
60}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
uint64_t ctl_dispatch(ctl_array_t ctls, file_t *file, const void *buffer, uint64_t count)
Dispatch a ctl command.
Definition ctl.c:9
ctl_t ctl_array_t[]
Type definition for an array of ctl commands.
Definition ctl.h:72
#define ENOENT
No such file or directory.
Definition errno.h:42
#define EINVAL
Invalid argument.
Definition errno.h:142
#define EUNKNOWNCTL
Invalid or unknown control request.
Definition errno.h:697
#define errno
Error number variable.
Definition errno.h:27
#define E2BIG
Argument list too long.
Definition errno.h:67
const char ** argsplit_buf(void *buf, uint64_t size, const char *str, uint64_t maxLen, uint64_t *count)
Standardized argument parsing function using a provided buffer.
Definition argsplit_buf.c:3
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
static dentry_t * file
Definition log_file.c:17
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static atomic_long count
Definition main.c:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
Structure defining a ctl command.
Definition ctl.h:62
ctl_func_t func
The function to call for the command.
Definition ctl.h:64
uint64_t argcMax
The maximum number of arguments accepted by func.
Definition ctl.h:66
const char * name
The name of the command.
Definition ctl.h:63
File structure.
Definition file.h:37