aboutsummaryrefslogtreecommitdiff
path: root/cronscan.c
blob: 16ebb0980c57d066e878716cbd96097c2611b5f1 (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
/* SPDX-License-Identifier: ISC */
#include "gcrond.h"

int cronscan(const char *directory, crontab_t **list)
{
	crontab_t *cron, *tail = NULL;
	struct dirent *ent;
	int dfd, ret = 0;
	DIR *dir;

	dir = opendir(directory);
	if (dir == NULL) {
		perror(directory);
		return -1;
	}

	dfd = dirfd(dir);
	if (dfd < 0) {
		perror(directory);
		closedir(dir);
		return -1;
	}

	for (;;) {
		errno = 0;
		ent = readdir(dir);

		if (ent == NULL) {
			if (errno != 0) {
				perror(directory);
				ret = -1;
			}
			break;
		}

		if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
			continue;

		cron = rdcron(dfd, ent->d_name);
		if (cron == NULL)
			continue;

		if (tail == NULL) {
			*list = cron;
			tail = cron;
		} else {
			tail->next = cron;
		}

		while (tail->next != NULL)
			tail = tail->next;
	}

	closedir(dir);
	return ret;
}