1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include "builtins.h"
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define DEFAULT_ARGV(...) \
char *_argv_default[] = {argv[0], __VA_ARGS__}; \
if (argc <= 1) { \
argc = sizeof(_argv_default) / sizeof(*_argv_default); \
argv = _argv_default; \
}
static void cmd_cat(int argc, char **argv) {
const size_t buflen = 4096;
char *buf = malloc(buflen);
DEFAULT_ARGV("!stdin");
for (int i = 1; i < argc; i++) {
FILE *file = fopen(argv[i], "r");
if (!file) {
eprintf("couldn't open %s", argv[i]);
return;
}
for (;;) {
int len = fread(buf, 1, sizeof buf, file);
if (len <= 0) break;
fwrite(buf, 1, len, stdout);
}
fclose(file);
}
}
static void cmd_echo(int argc, char **argv) {
bool newline = true;
int i = 1;
if (!strcmp("-n", argv[i])) {
i++;
newline = false;
}
printf("%s", argv[i++]);
for (; i < argc; i++)
printf(" %s", argv[i]);
if (newline)
printf("\n");
}
void cmd_hexdump(int argc, char **argv) {
const size_t buflen = 512;
uint8_t *buf = malloc(buflen);
FILE *file;
int len;
DEFAULT_ARGV("!stdin");
for (int i = 1; i < argc; i++) {
file = fopen(argv[i], "r");
if (!file) {
eprintf("couldn't open %s", argv[i]);
return;
}
len = fread(buf, 1, buflen, file);
fclose(file);
for (int i = 0; i < len; i += 16) {
printf("%08x ", i);
for (int j = i; j < i + 8 && j < len; j++)
printf("%02x ", buf[j]);
printf(" ");
for (int j = i + 8; j < i + 16 && j < len; j++)
printf("%02x ", buf[j]);
printf(" |");
for (int j = i; j < i + 16 && j < len; j++) {
char c = '.';
if (0x20 <= buf[j] && buf[j] < 0x7f) c = buf[j];
printf("%c", c);
}
printf("|\n");
}
}
}
static void cmd_ls(int argc, char **argv) {
FILE *file;
const size_t buflen = 4096;
char *buf = malloc(buflen);
DEFAULT_ARGV("/");
for (int i = 1; i < argc; i++) {
char *path = (void*)argv[i];
int pathlen = strlen(path);
if (!pathlen || path[pathlen - 1] != '/') {
memcpy(buf, path, pathlen);
buf[pathlen] = '/';
buf[pathlen+1] = '\0';
path = buf;
}
file = fopen(path, "r");
if (!file) {
eprintf("couldn't open %s", argv[i]);
return;
}
for (;;) {
int len = fread(buf, 1, sizeof buf, file);
if (len <= 0) break;
for (int i = 0; i < len; i++)
if (buf[i] == '\0') buf[i] = '\n';
fwrite(buf, 1, len, stdout);
}
fclose(file);
}
}
static void cmd_sleep(int argc, char **argv) {
if (argc < 2) {
eprintf("no arguments");
return;
}
_syscall_sleep(strtol(argv[1], NULL, 0) * 1000);
}
static void cmd_touch(int argc, char **argv) {
if (argc <= 1) {
eprintf("no arguments");
return;
}
for (int i = 1; i < argc; i++) {
FILE *f = fopen(argv[i], "a");
if (!f) eprintf("couldn't touch %s\n", argv[i]);
fclose(f);
}
}
struct builtin builtins[] = {
{"cat", cmd_cat},
{"echo", cmd_echo},
{"hexdump", cmd_hexdump},
{"ls", cmd_ls},
{"sleep", cmd_sleep},
{"touch", cmd_touch},
{NULL, NULL},
};
|