xref: /freebsd/usr.bin/leave/leave.c (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
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 <err.h>
33 #include <ctype.h>
34 #include <locale.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <time.h>
38 #include <unistd.h>
39 
40 static void doalarm(u_int);
41 static void usage(void) __dead2;
42 
43 /*
44  * leave [[+]hhmm]
45  *
46  * Reminds you when you have to leave.
47  * Leave prompts for input and goes away if you hit return.
48  * It nags you like a mother hen.
49  */
50 int
51 main(int argc, char **argv)
52 {
53 	u_int secs;
54 	int hours, minutes;
55 	char c, *cp = NULL;
56 	struct tm *t;
57 	time_t now;
58 	int plusnow, t_12_hour;
59 	char buf[50];
60 
61 	if (setlocale(LC_TIME, "") == NULL)
62 		warn("setlocale");
63 
64 	if (argc < 2) {
65 #define	MSG1	"When do you have to leave? "
66 		(void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
67 		cp = fgets(buf, sizeof(buf), stdin);
68 		if (cp == NULL || *cp == '\n')
69 			exit(0);
70 	} else if (argc > 2)
71 		usage();
72 	else
73 		cp = argv[1];
74 
75 	if (*cp == '+') {
76 		plusnow = 1;
77 		++cp;
78 	} else
79 		plusnow = 0;
80 
81 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
82 		if (!isdigit(c))
83 			usage();
84 		hours = hours * 10 + (c - '0');
85 	}
86 	minutes = hours % 100;
87 	hours /= 100;
88 
89 	if (minutes < 0 || minutes > 59)
90 		usage();
91 	if (plusnow)
92 		secs = hours * 60 * 60 + minutes * 60;
93 	else {
94 		(void)time(&now);
95 		t = localtime(&now);
96 
97 		if (hours > 23)
98 			usage();
99 
100 		/* Convert tol to 12 hr time (0:00...11:59) */
101 		if (hours > 11)
102 			hours -= 12;
103 
104 		/* Convert tm to 12 hr time (0:00...11:59) */
105 		if (t->tm_hour > 11)
106 			t_12_hour = t->tm_hour - 12;
107 		else
108 			t_12_hour = t->tm_hour;
109 
110 		if (hours < t_12_hour ||
111 	 	   (hours == t_12_hour && minutes <= t->tm_min))
112 			/* Leave time is in the past so we add 12 hrs */
113 			hours += 12;
114 
115 		secs = (hours - t_12_hour) * 60 * 60;
116 		secs += (minutes - t->tm_min) * 60;
117 		secs -= now % 60;	/* truncate (now + secs) to min */
118 	}
119 	doalarm(secs);
120 	exit(0);
121 }
122 
123 void
124 doalarm(u_int secs)
125 {
126 	int bother;
127 	time_t daytime;
128 	char tb[80];
129 	int pid;
130 
131 	if ((pid = fork())) {
132 		(void)time(&daytime);
133 		daytime += secs;
134 		strftime(tb, sizeof(tb), "%+", localtime(&daytime));
135 		printf("Alarm set for %s. (pid %d)\n", tb, pid);
136 		exit(0);
137 	}
138 	sleep((u_int)2);		/* let parent print set message */
139 	if (secs >= 2)
140 		secs -= 2;
141 
142 	/*
143 	 * if write fails, we've lost the terminal through someone else
144 	 * causing a vhangup by logging in.
145 	 */
146 #define	FIVEMIN	(5 * 60)
147 #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
148 	if (secs >= FIVEMIN) {
149 		sleep(secs - FIVEMIN);
150 		if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
151 			exit(0);
152 		secs = FIVEMIN;
153 	}
154 
155 #define	ONEMIN	(60)
156 #define	MSG3	"\07\07Just one more minute!\n"
157 	if (secs >= ONEMIN) {
158 		sleep(secs - ONEMIN);
159 		if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
160 			exit(0);
161 	}
162 
163 #define	MSG4	"\07\07Time to leave!\n"
164 	for (bother = 10; bother--;) {
165 		sleep((u_int)ONEMIN);
166 		if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
167 			exit(0);
168 	}
169 
170 #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
171 	(void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
172 	exit(0);
173 }
174 
175 static void
176 usage(void)
177 {
178 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
179 	exit(1);
180 }
181