PatchworkOS
3984a1d
A non-POSIX operating system.
Theme:
Default
Round
Robot
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
11
static
size_t
read_fd
(
fd_t
fd,
const
char
* name,
bool
hexOutput
)
12
{
13
while
(1)
14
{
15
char
buffer
[
BUFFER_SIZE
];
16
uint64_t
count
=
read
(fd,
buffer
,
BUFFER_SIZE
- 1);
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
swrite
(
STDOUT_FILENO
,
F
(
"%02x "
, (
unsigned
char
)
buffer
[
i
]));
33
}
34
continue
;
35
}
36
37
write
(
STDOUT_FILENO
,
buffer
,
count
);
38
}
39
40
return
0;
41
}
42
43
int
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
}
main
int main(void)
Definition
main.c:5
y
int64_t y
Definition
main.c:153
errno.h
errno
#define errno
Error number variable.
Definition
errno.h:27
open
fd_t open(const char *path)
System call for opening files.
Definition
open.c:8
close
uint64_t close(fd_t fd)
System call for closing files.
Definition
close.c:8
F
#define F(format,...)
Allocates a formatted string on the stack.
Definition
io.h:66
write
size_t write(fd_t fd, const void *buffer, size_t count)
System call for writing to files.
Definition
write.c:8
read
size_t read(fd_t fd, void *buffer, size_t count)
System call for reading from files.
Definition
read.c:8
swrite
size_t swrite(fd_t fd, const char *string)
Wrapper for writing a null-terminated string to a file.
Definition
swrite.c:4
STDOUT_FILENO
#define STDOUT_FILENO
Standard output file descriptor.
Definition
io.h:35
STDIN_FILENO
#define STDIN_FILENO
Standard input file descriptor.
Definition
io.h:34
ERR
#define ERR
Integer error value.
Definition
ERR.h:17
fd_t
__UINT64_TYPE__ fd_t
A file descriptor.
Definition
fd_t.h:12
io.h
buffer
EFI_PHYSICAL_ADDRESS buffer
Definition
mem.c:15
BUFFER_SIZE
#define BUFFER_SIZE
Definition
main.c:9
read_fd
static size_t read_fd(fd_t fd, const char *name, bool hexOutput)
Definition
main.c:11
count
static atomic_long count
Definition
main.c:11
stdbool.h
stdint.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
printf
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition
printf.c:3
stdlib.h
EXIT_SUCCESS
#define EXIT_SUCCESS
Definition
stdlib.h:46
EXIT_FAILURE
#define EXIT_FAILURE
Definition
stdlib.h:47
string.h
strerror
_PUBLIC char * strerror(int errnum)
Definition
strerror.c:6
strcmp
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition
strcmp.c:3
src
programs
utils
cat
main.c
Generated on Sat Jan 10 2026 00:03:15 for PatchworkOS by
1.9.8