commit 9b691a623cfdcd32b9a90b917171611133126fa0
parent 522f38dfa17e3bf5d49920d855d662cfc40d36e3
Author: Ari Malinen <ari.malinen@gmail.com>
Date: Fri, 13 Jun 2014 01:16:27 +0300
cleaned code
Diffstat:
3 files changed, 18 insertions(+), 26 deletions(-)
diff --git a/README b/README
@@ -12,5 +12,5 @@ usage: dcron [options]
config:
# tm_min, tm_hour, tm_mday, tm_mon, tm_wday, command
-0 5 * * * exec updatedb
-9 5 * * * exec mandb --quiet
+0 5 * * * updatedb
+9 5 * * * mandb --quiet
diff --git a/dcron.c b/dcron.c
@@ -6,7 +6,7 @@
#include <unistd.h>
#include <syslog.h>
-#define LEN 100
+#define LEN 80
int main(int argc, char *argv[]) {
char *col, line[LEN+1], config[LEN+1] = "/etc/dcron.conf";
@@ -19,19 +19,13 @@ int main(int argc, char *argv[]) {
for (i = 1; i < argc; i++) {
if (!strcmp("-d", argv[i])) {
- switch (fork()) {
- case -1:
- fprintf(stderr, "error: failed to daemonize\n");
- syslog(LOG_NOTICE, "error: failed to daemonize");
- return 1;
- case 0:
- if (setsid() == -1)
- return 1;
- fclose(stdout);
- fclose(stderr);
- break;
- default:
- return 0;
+ if (fork() == 0) {
+ setsid();
+ fclose(stdout);
+ fclose(stderr);
+ fclose(stdin);
+ } else {
+ return 0;
}
} else if (!strcmp("-f", argv[i])) {
if (argv[i+1] == NULL || argv[i+1][0] == '-') {
@@ -40,8 +34,6 @@ int main(int argc, char *argv[]) {
return 1;
}
strncpy(config, argv[++i], LEN);
- printf("config: %s\n", config);
- syslog(LOG_NOTICE, "config: %s", config);
} else {
fprintf(stderr, "usage: %s [options]\n", argv[0]);
fprintf(stderr, "-h help\n");
@@ -51,7 +43,9 @@ int main(int argc, char *argv[]) {
}
}
- for (t = time(NULL), sleep(60 - t % 60); 1; t = time(NULL), sleep(60 - t % 60)) {
+ while (1) {
+ t = time(NULL);
+ sleep(60 - t % 60);
t = time(NULL);
tm = localtime(&t);
@@ -61,11 +55,11 @@ int main(int argc, char *argv[]) {
continue;
}
- for (i = 0; fgets(line, LEN+1, fp) != NULL; i++) {
+ for (i = 0; fgets(line, LEN+1, fp); i++) {
if (line[0] == '#' || line[1] == '\0')
continue;
- for (j = 0, col = strtok(line,"\t"); col != NULL; j++, col = strtok(NULL, "\t")) {
+ for (j = 0, col = strtok(line,"\t"); col; j++, col = strtok(NULL, "\t")) {
if ((j == 0 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_min)) ||
(j == 1 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_hour)) ||
(j == 2 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_mday)) ||
@@ -77,10 +71,8 @@ int main(int argc, char *argv[]) {
syslog(LOG_NOTICE, "run: %s", col);
if (fork() == 0) {
execl("/bin/sh", "/bin/sh", "-c", col, (char *)NULL);
- fprintf(stderr, "error: job failed\n");
- syslog(LOG_NOTICE, "error: job failed");
}
- } else if (!isdigit(col[0]) || j > 5) {
+ } else if (!isdigit(col[0])) {
fprintf(stderr, "error: %s line %d column %d\n", config, i+1, j+1);
syslog(LOG_NOTICE, "error: %s line %d column %d", config, i+1, j+1);
}
diff --git a/dcron.conf b/dcron.conf
@@ -1,3 +1,3 @@
# tm_min, tm_hour, tm_mday, tm_mon, tm_wday, command
-0 5 * * * exec updatedb
-9 5 * * * exec mandb --quiet
+0 5 * * * updatedb
+9 5 * * * mandb --quiet