PatchworkOS
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1#include <errno.h>
2#include <stdbool.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/io.h>
8
9#define BUFFER_SIZE 1024
10
11static uint64_t read_fd(fd_t fd, const char* name, bool hexOutput)
12{
13 while (1)
14 {
15 char buffer[BUFFER_SIZE];
17 if (count == ERR)
18 {
19 printf("cat: failed to read %s (%s)\n", name, strerror(errno));
20 close(fd);
21 return ERR;
22 }
23 if (count == 0)
24 {
25 break;
26 }
27
28 if (hexOutput)
29 {
30 for (uint64_t i = 0; i < count; i++)
31 {
32 writef(STDOUT_FILENO, "%02x ", (unsigned char)buffer[i]);
33 }
34 continue;
35 }
36
38 }
39
40 return 0;
41}
42
43int main(int argc, char** argv)
44{
45 if (argc == 1)
46 {
47 return read_fd(STDIN_FILENO, "stdin", false);
48 }
49
50 bool hexOutput = false;
51 int i = 1;
52 if (strcmp(argv[1], "-hex") == 0)
53 {
54 hexOutput = true;
55 i++;
56 }
57
58 for (; i < argc; i++)
59 {
60 fd_t fd = open(argv[i]);
61 if (fd == ERR)
62 {
63 printf("cat: failed to open %s (%s)\n", argv[i], strerror(errno));
64 return EXIT_FAILURE;
65 }
66
67 if (read_fd(fd, argv[i], hexOutput) == ERR)
68 {
69 return EXIT_FAILURE;
70 }
71
72 close(fd);
73 }
74
75 return EXIT_SUCCESS;
76}
#define errno
Error number variable.
Definition errno.h:27
uint64_t writef(fd_t fd, const char *_RESTRICT format,...)
Wrapper for writing a formatted string to a file.
Definition writef.c:9
fd_t open(const char *path)
System call for opening files.
Definition open.c:9
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
#define STDOUT_FILENO
Definition io.h:45
uint64_t read(fd_t fd, void *buffer, uint64_t count)
System call for reading from files.
Definition read.c:9
uint64_t write(fd_t fd, const void *buffer, uint64_t count)
System call for writing to files.
Definition write.c:9
#define STDIN_FILENO
Definition io.h:44
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
#define ERR
Definition main.c:44
int main()
Definition main.c:97
#define BUFFER_SIZE
Definition main.c:9
static uint64_t read_fd(fd_t fd, const char *name, bool hexOutput)
Definition main.c:11
static atomic_long count
Definition main.c:9
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:5
#define EXIT_SUCCESS
Definition stdlib.h:46
#define EXIT_FAILURE
Definition stdlib.h:47
_PUBLIC char * strerror(int errnum)
Definition strerror.c:6
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3