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