aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 50beaaa7fdee6879ecf12c96493fbf44f5f475b8 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/syslog.h>
#include <syslog.h>

int main(int argc, char **argv) {
    char *str = NULL;
	size_t n = 0;

	openlog(NULL, LOG_PID, LOG_LOCAL0);

	for (int i = 0; i < argc; i++)
		n += strlen(*argv+i);

    if ((str = malloc(n + 4 * argc + 1)) == NULL) {
		perror("main: malloc failed");
		exit(EXIT_FAILURE);
    }

	strcpy(str, "[0] ");
	strcat(str, argv[0]);

	for (int i = 1; i < argc; i++) {
		char *tmp = NULL;

		if ((tmp = malloc(strlen(argv[i]) + 4)) == NULL) {
			perror("main: malloc failed");
			exit(EXIT_FAILURE);
		}

		sprintf(tmp, "[%d] %s ", i, argv[i]);
		strcat(str, tmp);

		free(tmp);
	}
	str[strlen(str)] = '\0';

    syslog(LOG_DEBUG, "args: %s\n", str);

	closelog();
	free(str);

	return 0;
}