xref: /freebsd/usr.bin/leave/leave.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <ctype.h>
33 #include <err.h>
34 #include <locale.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <time.h>
38 #include <unistd.h>
39 
40 #include <capsicum_helpers.h>
41 
42 static void doalarm(u_int);
43 static void usage(void) __dead2;
44 
45 /*
46  * leave [[+]hhmm]
47  *
48  * Reminds you when you have to leave.
49  * Leave prompts for input and goes away if you hit return.
50  * It nags you like a mother hen.
51  */
52 int
53 main(int argc, char **argv)
54 {
55 	u_int secs;
56 	int hours, minutes;
57 	char c, *cp = NULL;
58 	struct tm *t;
59 	time_t now;
60 	int plusnow, t_12_hour;
61 	char buf[50];
62 
63 	if (setlocale(LC_TIME, "") == NULL)
64 		warn("setlocale");
65 
66 	caph_cache_tzdata();
67 	caph_cache_catpages();
68 	if (caph_limit_stdio() < 0 || caph_enter() < 0)
69 		err(EXIT_FAILURE, "capsicum");
70 
71 	if (argc < 2) {
72 #define	MSG1	"When do you have to leave? "
73 		(void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
74 		cp = fgets(buf, sizeof(buf), stdin);
75 		if (cp == NULL || *cp == '\n')
76 			exit(EXIT_SUCCESS);
77 	} else if (argc > 2)
78 		usage();
79 	else
80 		cp = argv[1];
81 
82 	if (*cp == '+') {
83 		plusnow = 1;
84 		++cp;
85 	} else
86 		plusnow = 0;
87 
88 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
89 		if (!isdigit(c))
90 			usage();
91 		hours = hours * 10 + (c - '0');
92 	}
93 	minutes = hours % 100;
94 	hours /= 100;
95 
96 	if (minutes < 0 || minutes > 59)
97 		usage();
98 	if (plusnow)
99 		secs = hours * 60 * 60 + minutes * 60;
100 	else {
101 		(void)time(&now);
102 		t = localtime(&now);
103 
104 		if (hours > 23)
105 			usage();
106 
107 		/* Convert tol to 12 hr time (0:00...11:59) */
108 		if (hours > 11)
109 			hours -= 12;
110 
111 		/* Convert tm to 12 hr time (0:00...11:59) */
112 		if (t->tm_hour > 11)
113 			t_12_hour = t->tm_hour - 12;
114 		else
115 			t_12_hour = t->tm_hour;
116 
117 		if (hours < t_12_hour ||
118 		   (hours == t_12_hour && minutes <= t->tm_min))
119 			/* Leave time is in the past so we add 12 hrs */
120 			hours += 12;
121 
122 		secs = (hours - t_12_hour) * 60 * 60;
123 		secs += (minutes - t->tm_min) * 60;
124 		secs -= now % 60;	/* truncate (now + secs) to min */
125 	}
126 	doalarm(secs);
127 	exit(EXIT_SUCCESS);
128 }
129 
130 void
131 doalarm(u_int secs)
132 {
133 	int bother;
134 	time_t daytime;
135 	char tb[80];
136 	int pid;
137 
138 	if ((pid = fork())) {
139 		(void)time(&daytime);
140 		daytime += secs;
141 		strftime(tb, sizeof(tb), "%+", localtime(&daytime));
142 		printf("Alarm set for %s. (pid %d)\n", tb, pid);
143 		exit(EXIT_SUCCESS);
144 	}
145 	sleep((u_int)2);		/* let parent print set message */
146 	if (secs >= 2)
147 		secs -= 2;
148 
149 	/*
150 	 * if write fails, we've lost the terminal through someone else
151 	 * causing a vhangup by logging in.
152 	 */
153 #define	FIVEMIN	(5 * 60)
154 #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
155 	if (secs >= FIVEMIN) {
156 		sleep(secs - FIVEMIN);
157 		if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
158 			exit(EXIT_SUCCESS);
159 		secs = FIVEMIN;
160 	}
161 
162 #define	ONEMIN	(60)
163 #define	MSG3	"\07\07Just one more minute!\n"
164 	if (secs >= ONEMIN) {
165 		sleep(secs - ONEMIN);
166 		if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
167 			exit(EXIT_SUCCESS);
168 	}
169 
170 #define	MSG4	"\07\07Time to leave!\n"
171 	for (bother = 10; bother--;) {
172 		sleep((u_int)ONEMIN);
173 		if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
174 			exit(EXIT_SUCCESS);
175 	}
176 
177 #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
178 	(void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
179 	exit(EXIT_SUCCESS);
180 }
181 
182 static void
183 usage(void)
184 {
185 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
186 	exit(EXIT_FAILURE);
187 }
188