19b50d902SRodney W. Grimes /*
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1980, 1988, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329b50d902SRodney W. Grimes #include <ctype.h>
33f7a6cf3aSFaraz Vahedi #include <err.h>
342d3b8b64SWolfgang Helbig #include <locale.h>
35492dbf73SPhilippe Charnier #include <stdio.h>
36948a3f54SDima Dorfman #include <stdlib.h>
372d3b8b64SWolfgang Helbig #include <time.h>
38492dbf73SPhilippe Charnier #include <unistd.h>
39492dbf73SPhilippe Charnier
4024a73038SFaraz Vahedi #include <capsicum_helpers.h>
4124a73038SFaraz Vahedi
4249fc7960SXin LI static void doalarm(u_int);
43a1b6427aSAlfonso Gregory static void usage(void) __dead2;
449b50d902SRodney W. Grimes
459b50d902SRodney W. Grimes /*
469b50d902SRodney W. Grimes * leave [[+]hhmm]
479b50d902SRodney W. Grimes *
489b50d902SRodney W. Grimes * Reminds you when you have to leave.
499b50d902SRodney W. Grimes * Leave prompts for input and goes away if you hit return.
509b50d902SRodney W. Grimes * It nags you like a mother hen.
519b50d902SRodney W. Grimes */
52492dbf73SPhilippe Charnier int
main(int argc,char ** argv)53f4ac32deSDavid Malone main(int argc, char **argv)
549b50d902SRodney W. Grimes {
55f4ac32deSDavid Malone u_int secs;
56f4ac32deSDavid Malone int hours, minutes;
57f4ac32deSDavid Malone char c, *cp = NULL;
589b1fd327SDima Dorfman struct tm *t;
599b1fd327SDima Dorfman time_t now;
602d3b8b64SWolfgang Helbig int plusnow, t_12_hour;
619b50d902SRodney W. Grimes char buf[50];
629b50d902SRodney W. Grimes
632d3b8b64SWolfgang Helbig if (setlocale(LC_TIME, "") == NULL)
642d3b8b64SWolfgang Helbig warn("setlocale");
652d3b8b64SWolfgang Helbig
66*08a38bb0SMariusz Zaborski caph_cache_tzdata();
6724a73038SFaraz Vahedi caph_cache_catpages();
6824a73038SFaraz Vahedi if (caph_limit_stdio() < 0 || caph_enter() < 0)
6924a73038SFaraz Vahedi err(EXIT_FAILURE, "capsicum");
7024a73038SFaraz Vahedi
719b50d902SRodney W. Grimes if (argc < 2) {
729b50d902SRodney W. Grimes #define MSG1 "When do you have to leave? "
73e1b4d8d0SSheldon Hearn (void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
749b50d902SRodney W. Grimes cp = fgets(buf, sizeof(buf), stdin);
75492dbf73SPhilippe Charnier if (cp == NULL || *cp == '\n')
76f7a6cf3aSFaraz Vahedi exit(EXIT_SUCCESS);
77345edac5SKris Kennaway } else if (argc > 2)
78345edac5SKris Kennaway usage();
79345edac5SKris Kennaway else
809b50d902SRodney W. Grimes cp = argv[1];
819b50d902SRodney W. Grimes
829b50d902SRodney W. Grimes if (*cp == '+') {
839b50d902SRodney W. Grimes plusnow = 1;
849b50d902SRodney W. Grimes ++cp;
852d3b8b64SWolfgang Helbig } else
869b50d902SRodney W. Grimes plusnow = 0;
879b50d902SRodney W. Grimes
889b50d902SRodney W. Grimes for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
899b50d902SRodney W. Grimes if (!isdigit(c))
909b50d902SRodney W. Grimes usage();
919b50d902SRodney W. Grimes hours = hours * 10 + (c - '0');
929b50d902SRodney W. Grimes }
939b50d902SRodney W. Grimes minutes = hours % 100;
949b50d902SRodney W. Grimes hours /= 100;
959b50d902SRodney W. Grimes
969b50d902SRodney W. Grimes if (minutes < 0 || minutes > 59)
979b50d902SRodney W. Grimes usage();
989b50d902SRodney W. Grimes if (plusnow)
999b50d902SRodney W. Grimes secs = hours * 60 * 60 + minutes * 60;
1009b50d902SRodney W. Grimes else {
1012d3b8b64SWolfgang Helbig (void)time(&now);
1022d3b8b64SWolfgang Helbig t = localtime(&now);
1032d3b8b64SWolfgang Helbig
1042d3b8b64SWolfgang Helbig if (hours > 23)
1059b50d902SRodney W. Grimes usage();
1062d3b8b64SWolfgang Helbig
1072d3b8b64SWolfgang Helbig /* Convert tol to 12 hr time (0:00...11:59) */
1082d3b8b64SWolfgang Helbig if (hours > 11)
1092d3b8b64SWolfgang Helbig hours -= 12;
1102d3b8b64SWolfgang Helbig
1112d3b8b64SWolfgang Helbig /* Convert tm to 12 hr time (0:00...11:59) */
1122d3b8b64SWolfgang Helbig if (t->tm_hour > 11)
1132d3b8b64SWolfgang Helbig t_12_hour = t->tm_hour - 12;
1142d3b8b64SWolfgang Helbig else
1152d3b8b64SWolfgang Helbig t_12_hour = t->tm_hour;
1162d3b8b64SWolfgang Helbig
1172d3b8b64SWolfgang Helbig if (hours < t_12_hour ||
1182d3b8b64SWolfgang Helbig (hours == t_12_hour && minutes <= t->tm_min))
1192d3b8b64SWolfgang Helbig /* Leave time is in the past so we add 12 hrs */
1202d3b8b64SWolfgang Helbig hours += 12;
1212d3b8b64SWolfgang Helbig
1222d3b8b64SWolfgang Helbig secs = (hours - t_12_hour) * 60 * 60;
1239b50d902SRodney W. Grimes secs += (minutes - t->tm_min) * 60;
1242d3b8b64SWolfgang Helbig secs -= now % 60; /* truncate (now + secs) to min */
1259b50d902SRodney W. Grimes }
1269b50d902SRodney W. Grimes doalarm(secs);
127f7a6cf3aSFaraz Vahedi exit(EXIT_SUCCESS);
1289b50d902SRodney W. Grimes }
1299b50d902SRodney W. Grimes
130492dbf73SPhilippe Charnier void
doalarm(u_int secs)131f4ac32deSDavid Malone doalarm(u_int secs)
1329b50d902SRodney W. Grimes {
133f4ac32deSDavid Malone int bother;
1349b1fd327SDima Dorfman time_t daytime;
1352d3b8b64SWolfgang Helbig char tb[80];
1369b50d902SRodney W. Grimes int pid;
1379b50d902SRodney W. Grimes
138492dbf73SPhilippe Charnier if ((pid = fork())) {
1399b50d902SRodney W. Grimes (void)time(&daytime);
1409b50d902SRodney W. Grimes daytime += secs;
1412d3b8b64SWolfgang Helbig strftime(tb, sizeof(tb), "%+", localtime(&daytime));
1422d3b8b64SWolfgang Helbig printf("Alarm set for %s. (pid %d)\n", tb, pid);
143f7a6cf3aSFaraz Vahedi exit(EXIT_SUCCESS);
1449b50d902SRodney W. Grimes }
1459b50d902SRodney W. Grimes sleep((u_int)2); /* let parent print set message */
1462a453c23SSteve Price if (secs >= 2)
1472d3b8b64SWolfgang Helbig secs -= 2;
1489b50d902SRodney W. Grimes
1499b50d902SRodney W. Grimes /*
1509b50d902SRodney W. Grimes * if write fails, we've lost the terminal through someone else
1519b50d902SRodney W. Grimes * causing a vhangup by logging in.
1529b50d902SRodney W. Grimes */
1539b50d902SRodney W. Grimes #define FIVEMIN (5 * 60)
1549b50d902SRodney W. Grimes #define MSG2 "\07\07You have to leave in 5 minutes.\n"
1559b50d902SRodney W. Grimes if (secs >= FIVEMIN) {
1569b50d902SRodney W. Grimes sleep(secs - FIVEMIN);
157e1b4d8d0SSheldon Hearn if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
158f7a6cf3aSFaraz Vahedi exit(EXIT_SUCCESS);
1599b50d902SRodney W. Grimes secs = FIVEMIN;
1609b50d902SRodney W. Grimes }
1619b50d902SRodney W. Grimes
1629b50d902SRodney W. Grimes #define ONEMIN (60)
1639b50d902SRodney W. Grimes #define MSG3 "\07\07Just one more minute!\n"
1649b50d902SRodney W. Grimes if (secs >= ONEMIN) {
1659b50d902SRodney W. Grimes sleep(secs - ONEMIN);
166e1b4d8d0SSheldon Hearn if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
167f7a6cf3aSFaraz Vahedi exit(EXIT_SUCCESS);
1689b50d902SRodney W. Grimes }
1699b50d902SRodney W. Grimes
1709b50d902SRodney W. Grimes #define MSG4 "\07\07Time to leave!\n"
1719b50d902SRodney W. Grimes for (bother = 10; bother--;) {
1729b50d902SRodney W. Grimes sleep((u_int)ONEMIN);
173e1b4d8d0SSheldon Hearn if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
174f7a6cf3aSFaraz Vahedi exit(EXIT_SUCCESS);
1759b50d902SRodney W. Grimes }
1769b50d902SRodney W. Grimes
1779b50d902SRodney W. Grimes #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n"
178e1b4d8d0SSheldon Hearn (void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
179f7a6cf3aSFaraz Vahedi exit(EXIT_SUCCESS);
1809b50d902SRodney W. Grimes }
1819b50d902SRodney W. Grimes
182492dbf73SPhilippe Charnier static void
usage(void)183f4ac32deSDavid Malone usage(void)
1849b50d902SRodney W. Grimes {
1859b50d902SRodney W. Grimes fprintf(stderr, "usage: leave [[+]hhmm]\n");
186f7a6cf3aSFaraz Vahedi exit(EXIT_FAILURE);
1879b50d902SRodney W. Grimes }
188