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