Blame


1 502e1ef8 2024-06-16 benni #include <sys/types.h>
2 502e1ef8 2024-06-16 benni #include <sys/stat.h>
3 6bf04703 2024-04-05 benni #include <sys/mman.h>
4 502e1ef8 2024-06-16 benni #include <stdbool.h>
5 e46639b3 2024-02-20 benni #include <unistd.h>
6 6bf04703 2024-04-05 benni #include <stdlib.h>
7 e46639b3 2024-02-20 benni #include <string.h>
8 b4cef989 2024-02-21 benni #include <fcntl.h>
9 e46639b3 2024-02-20 benni #include <stdio.h>
10 b4cef989 2024-02-21 benni #include <err.h>
11 e46639b3 2024-02-20 benni
12 e46639b3 2024-02-20 benni __attribute__((always_inline))
13 e46639b3 2024-02-20 benni inline static void ebreak (void)
14 e46639b3 2024-02-20 benni {
15 e46639b3 2024-02-20 benni __asm __volatile__ ("ebreak");
16 e46639b3 2024-02-20 benni }
17 e46639b3 2024-02-20 benni
18 6bf04703 2024-04-05 benni __attribute__((always_inline))
19 6bf04703 2024-04-05 benni inline static void *sys_brk (void *ptr)
20 6bf04703 2024-04-05 benni {
21 6bf04703 2024-04-05 benni register void *a0 asm ("a0") = ptr;
22 6bf04703 2024-04-05 benni register int a7 asm ("a7") = 214;
23 6bf04703 2024-04-05 benni __asm __volatile__ ("ecall" : "=r"(a0) : "r"(a0), "r"(a7));
24 6bf04703 2024-04-05 benni return a0;
25 6bf04703 2024-04-05 benni }
26 6bf04703 2024-04-05 benni
27 5c65abd0 2024-06-11 benni unsigned mul(unsigned a, unsigned b)
28 5c65abd0 2024-06-11 benni {
29 5c65abd0 2024-06-11 benni return a / b;
30 5c65abd0 2024-06-11 benni }
31 5c65abd0 2024-06-11 benni
32 e46639b3 2024-02-20 benni int main (int argc, char *argv[]) {
33 fdc4e5ea 2024-06-11 benni FILE *in, *out;
34 fdc4e5ea 2024-06-11 benni int ch, *ptr;
35 c2b4813c 2024-02-24 benni
36 cc17802d 2024-06-10 benni ptr = malloc (4);
37 cc17802d 2024-06-10 benni *ptr = 1;
38 cc17802d 2024-06-10 benni free (ptr);
39 c2b4813c 2024-02-24 benni
40 fdc4e5ea 2024-06-11 benni in = fopen ("/test.txt", "r");
41 fdc4e5ea 2024-06-11 benni if (in == NULL)
42 fdc4e5ea 2024-06-11 benni err (1, "open('test.txt')");
43 fdc4e5ea 2024-06-11 benni
44 fdc4e5ea 2024-06-11 benni out = fopen ("/output.txt", "w");
45 fdc4e5ea 2024-06-11 benni if (out == NULL)
46 fdc4e5ea 2024-06-11 benni err (1, "open('output.txt')");
47 fdc4e5ea 2024-06-11 benni
48 fdc4e5ea 2024-06-11 benni while ((ch = fgetc (in)) != EOF)
49 fdc4e5ea 2024-06-11 benni fputc (ch, out);
50 fdc4e5ea 2024-06-11 benni
51 fdc4e5ea 2024-06-11 benni fclose (out);
52 fdc4e5ea 2024-06-11 benni fclose (in);
53 728f3bb1 2024-06-11 benni
54 728f3bb1 2024-06-11 benni in = fopen ("/output.txt", "r");
55 728f3bb1 2024-06-11 benni if (in == NULL)
56 728f3bb1 2024-06-11 benni err (1, "reopen('output.txt')");
57 728f3bb1 2024-06-11 benni
58 728f3bb1 2024-06-11 benni while ((ch = fgetc (in)) != EOF)
59 728f3bb1 2024-06-11 benni putchar (ch);
60 728f3bb1 2024-06-11 benni
61 728f3bb1 2024-06-11 benni fclose (in);
62 728f3bb1 2024-06-11 benni
63 5c65abd0 2024-06-11 benni printf ("Result: %u\n", mul (3, 2));
64 5c65abd0 2024-06-11 benni
65 502e1ef8 2024-06-16 benni execlp ("ksh", "ksh", NULL);
66 6341b1fd 2024-06-15 benni
67 e46639b3 2024-02-20 benni return 0;
68 e46639b3 2024-02-20 benni }