summaryrefslogtreecommitdiff
path: root/src/user/app/shell/builtins.c
blob: fef46a5a38b96a206ffc6cdc3046bcd1d7276b75 (plain)
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
#include "builtins.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

void cmd_cat_ls(const char *args, bool ls) {
	FILE *file;
	static char buf[512];
	int len; // first used for strlen(args), then length of buffer

	if (args) {
		len = strlen(args);
		memcpy(buf, args, len + 1); // no overflow check - the shell is just a PoC

		if (ls) { // paths to directories always have a trailing slash
			if (buf[len-1] != '/') {
				buf[len] = '/';
				buf[len+1] = '\0';
			}
		}

		file = fopen(buf, "r");
	} else if (ls) { /* ls default argument */
		file = fopen("/", "r");
	} else { /* cat default argument */
		file = file_clone(stdin);
	}

	if (!file) {
		printf("couldn't open.\n");
		return;
	}

	while (!feof(file)) {
		int len = fread(buf, 1, sizeof buf, file);
		if (len <= 0) break;

		if (ls) {
			for (int i = 0; i < len; i++)
				if (buf[i] == '\0') buf[i] = '\n';
		}
		fwrite(buf, 1, len, stdout);
	}
	fclose(file);
}

void cmd_hexdump(const char *args) {
	static uint8_t buf[512];
	int fd, len;

	fd = _syscall_open(args, strlen(args), 0);
	if (fd < 0) {
		printf("couldn't open.\n");
		return;
	}

	len = _syscall_read(fd, buf, sizeof buf, 0);
	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");
	}

	close(fd);
}

void cmd_touch(const char *args) {
	int fd = _syscall_open(args, strlen(args), OPEN_CREATE);
	if (fd < 0) {
		printf("couldn't create file.\n");
		return;
	}
	close(fd);
}