aboutsummaryrefslogtreecommitdiff
path: root/os.c
blob: c147baba421d6a14d88b8d630947299972f9a2e2 (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
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include "api/vm.h"
#include "api/util.h"
#include "api/int.h"
#include "api/nil.h"
#include "api/str.h"
#include "common/err.h"

UwUVMValue uwu_exit(UwUVMArgs *args)
{
	uwuutil_require_max("nolambda:os:exit", args, 1);

	long exit_code = 0;

	if (args->num == 1)
		exit_code = uwuint_get(uwuvm_get_arg(args, 0));

	exit(exit_code);
	return uwunil_create();
}

UwUVMValue uwu_sleep(UwUVMArgs *args)
{
	uwuutil_require_exact("nolamda:os:sleep", args, 1);

	UwUVMValue value = uwuvm_get_arg(args, 0);

	if (value.type != &uwuint_type)
		error("type error: nolamda:os:sleep requires an integer as $0\n");

	long millis = uwuint_get(value);

	if (millis < 0)
		error("type error: nolamda:os:sleep requires a positive value as $0\n");

	struct timespec ts = {
		.tv_sec = millis / 1000,
		.tv_nsec = 1000000 * (millis % 1000),
	};

	while (nanosleep(&ts, &ts) != 0)
		if (errno != EINTR)
			syserror("nanosleep", NULL);

	return uwunil_create();
}

UwUVMValue uwu_execute(UwUVMArgs *args)
{
	uwuutil_require_exact("nolambda:os:execute", args, 1);

	char *command = uwustr_get(uwuvm_get_arg(args, 0));
	int ret = system(command);
	free(command);

	return uwuint_create(ret);
}

UwUVMValue uwu_time(UwUVMArgs *args)
{
	uwuutil_require_exact("nolamda:os:time", args, 0);

	struct timespec ts;
	clock_gettime(CLOCK_REALTIME, &ts);

	return uwuint_create(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}