aboutsummaryrefslogtreecommitdiff
path: root/fs.c
blob: 0c64c39ca70b7b9c6924f0226aa2ef123ddd9f1b (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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "common/err.h"
#include "common/file.h"
#include "api/vm.h"
#include "api/str.h"
#include "api/nil.h"
#include "api/bool.h"
#include "api/util.h"

UwUVMValue uwu_read(UwUVMArgs *args)
{
	uwuutil_require_exact("nolambda:fs:read", args, 1);

	char *filename = uwustr_get(uwuvm_get_arg(args, 0));

	FILE *file = fopen(filename, "r");
	if (! file) syserror("fopen", file);
	if (fseek(file, 0, SEEK_END) == -1) syserror("fseek", file);

	size_t size = ftell(file);
	if (size == 1) syserror("ftell", file);
	if (fseek(file, 0, SEEK_SET) == -1) syserror("fseek", file);

	char contents[size];
	if (fread(contents, 1, size, file) != size) syserror("fread", file);

	fclose(file);
	free(filename);

	return uwustr_create(contents);
}

UwUVMValue uwu_write(UwUVMArgs *args)
{
	uwuutil_require_exact("nolambda:fs:write", args, 2);
	
	char *filename = uwustr_get(uwuvm_get_arg(args, 0));
	char *contents = uwustr_get(uwuvm_get_arg(args, 1));
	
	FILE *file = fopen(filename, "w");
	if (! file) syserror("fopen", file);

	size_t size = strlen(contents);
	if (fwrite(contents, 1, size, file) != size) syserror("fwrite", file);

	fclose(file);
	free(filename);
	free(contents);

	return uwunil_create();
}

UwUVMValue uwu_remove(UwUVMArgs *args)
{
	uwuutil_require_min("nolambda:fs:remove", args, 1);

	for (size_t i = 0; i < args->num; i++) {
		char *filename = uwustr_get(uwuvm_get_arg(args, i));

		if (remove(filename) != 0) syserror("remove", NULL);

		free(filename);
	}

	return uwunil_create();
}

UwUVMValue uwu_exists(UwUVMArgs *args)
{
	uwuutil_require_min("nolambda:fs:exists", args, 1);

	for (size_t i = 0; i < args->num; i++) {
		char *filename = uwustr_get(uwuvm_get_arg(args, i));
		bool exists = file_exists(filename);
		free(filename);

		if (! exists)
			return uwubool_create(false);
	}

	return uwubool_create(true);
}