about summary refs log tree commit diff
path: root/snooze.c
diff options
context:
space:
mode:
authorAndrew Benson <abenson+void@gmail.com>2020-10-01 18:36:42 -0600
committerLeah Neukirchen <leah@vuxu.org>2020-10-04 16:17:04 +0200
commit610e6b35ec614764770d47b5974b30fd90dce6fe (patch)
treea6efc19488d7d9a9d9c613c78a5956dc526d010d /snooze.c
parentea43661178aff50b3c3da885abe529e47c5b01d8 (diff)
downloadsnooze-610e6b35ec614764770d47b5974b30fd90dce6fe.tar.gz
snooze-610e6b35ec614764770d47b5974b30fd90dce6fe.tar.xz
snooze-610e6b35ec614764770d47b5974b30fd90dce6fe.zip
Add option for jitter, fix wording of randdelay
Diffstat (limited to 'snooze.c')
-rw-r--r--snooze.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/snooze.c b/snooze.c
index 8bbd93f..06329e3 100644
--- a/snooze.c
+++ b/snooze.c
@@ -28,6 +28,7 @@ static int nflag, vflag;
 
 static int timewait = -1;
 static int randdelay = 0;
+static int jitter = 0;
 static char *timefile;
 
 static sig_atomic_t alarm_rang = 0;
@@ -215,6 +216,14 @@ next_day:
 			goto next_day;
 	}
 
+	if (jitter && !nflag) {
+		long delay;
+		delay = lrand48() % jitter;
+		if (vflag)
+			printf("adding %lds for jitter.\n", delay);
+		t += delay;
+	}
+
 	return t;
 }
 
@@ -246,7 +255,7 @@ main(int argc, char *argv[])
 
 	setvbuf(stdout, 0, _IOLBF, 0);
 
-	while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
+	while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:J:d:m:ns:t:vw:")) != -1)
 		switch (c) {
 		case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
 		case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
@@ -266,8 +275,9 @@ main(int argc, char *argv[])
 		case 'T': timewait = parse_dur(optarg); break;
 		case 't': timefile = optarg; break;
 		case 'R': randdelay = parse_dur(optarg); break;
+		case 'J': jitter = parse_dur(optarg); break;
 		default:
-			fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
+			fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-J jitter] [-s slack]\n"
 			    "  [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
 			    "Timespec: exact: 1,3,5\n"
 			    "          range: 1-7\n"
@@ -296,18 +306,11 @@ main(int argc, char *argv[])
 		}
 	}
 
+	srand48(getpid() ^ start);
+
 	if (randdelay) {
 		long delay;
-#ifdef __linux__
-		long rnd = getauxval(AT_RANDOM);
-		if (rnd > 0)
-			delay = rnd % randdelay;
-		else
-#endif
-		{
-			srand48(getpid() ^ start);
-			delay = lrand48() % randdelay;
-		}
+		delay = lrand48() % randdelay;
 		if (vflag)
 			printf("randomly delaying by %lds.\n", delay);
 		start += delay;
@@ -326,13 +329,18 @@ main(int argc, char *argv[])
 			char weekstr[4];
 			struct tm *tm = localtime(&t);
 			strftime(weekstr, sizeof weekstr, "%a", tm);
-			printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
+			printf("%s %s %2ldd%3ldh%3ldm%3lds ",
 			    isotime(tm),
 			    weekstr,
 			    ((t - now) / (60*60*24)),
 			    ((t - now) / (60*60)) % 24,
 			    ((t - now) / 60) % 60,
 			    (t - now) % 60);
+			if(jitter) {
+				printf("(plus up to %ds for jitter)\n", jitter);
+			} else {
+				printf("\n");
+			}
 			t = find_next(t + 1);
 			if (t < 0) {
 				fprintf(stderr,