summaryrefslogtreecommitdiff
path: root/module/wrapper.c
blob: 6b15a25b3fc075c4d9541aaaec19a27bef39646b (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
/* Wrapper around module API so we can run/test it on non-RO machines */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "header.h"

#ifndef __riscos__

int main(int argc, char **argv)
{
	_kernel_oserror *error;
	char *buf = NULL;
	int buf_alloc = 0, buf_len = 0;

	for (int i = 1; i < argc; i++) {
		/* Need a leading space for all but the first argument */
		int req = buf_len + (buf_len > 0 ? 1 : 0) + strlen(argv[i]) + 1;

		while (req > buf_alloc) {
			char *temp = realloc(buf, buf_alloc + 4096);
			if (temp == NULL) {
				printf("Insufficient memory for parameters");
				return 1;
			}

			buf = temp;
			buf_alloc += 4096;
		}

		if (buf_len > 0) {
			memcpy(buf + buf_len, " ", 1);
			buf_len++;
		}

		memcpy(buf + buf_len, argv[i], strlen(argv[i]));

		buf_len += strlen(argv[i]);

		buf[buf_len] = '\0';
	}

	error = mod_init(NULL, 0, NULL);
	if (error != NULL) {
		free(buf);
		printf("mod_init: %s (%d)\n", error->errmess, error->errnum);
		return 1;
	}

	error = command_handler(buf, argc - 1, CMD_Iconv, NULL);
	if (error != NULL) {
		free(buf);
		printf("command_handler: %s (%d)\n", error->errmess, 
				error->errnum);
		return 1;
	}

	error = mod_fini(0, 0, NULL);
	if (error != NULL) {
		free(buf);
		printf("mod_fini: %s (%d)\n", error->errmess, error->errnum);
		return 1;
	}

	free(buf);

	return 0;
}

#endif