2 * Copyright (c) 1980, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)leave.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
57 static void usage(void);
62 * Reminds you when you have to leave.
63 * Leave prompts for input and goes away if you hit return.
64 * It nags you like a mother hen.
72 register int hours, minutes;
73 register char c, *cp = NULL;
76 int plusnow, t_12_hour;
79 if (setlocale(LC_TIME, "") == NULL)
83 #define MSG1 "When do you have to leave? "
84 (void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
85 cp = fgets(buf, sizeof(buf), stdin);
86 if (cp == NULL || *cp == '\n')
99 for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
102 hours = hours * 10 + (c - '0');
104 minutes = hours % 100;
107 if (minutes < 0 || minutes > 59)
110 secs = hours * 60 * 60 + minutes * 60;
118 /* Convert tol to 12 hr time (0:00...11:59) */
122 /* Convert tm to 12 hr time (0:00...11:59) */
124 t_12_hour = t->tm_hour - 12;
126 t_12_hour = t->tm_hour;
128 if (hours < t_12_hour ||
129 (hours == t_12_hour && minutes <= t->tm_min))
130 /* Leave time is in the past so we add 12 hrs */
133 secs = (hours - t_12_hour) * 60 * 60;
134 secs += (minutes - t->tm_min) * 60;
135 secs -= now % 60; /* truncate (now + secs) to min */
150 if ((pid = fork())) {
151 (void)time(&daytime);
153 strftime(tb, sizeof(tb), "%+", localtime(&daytime));
154 printf("Alarm set for %s. (pid %d)\n", tb, pid);
157 sleep((u_int)2); /* let parent print set message */
162 * if write fails, we've lost the terminal through someone else
163 * causing a vhangup by logging in.
165 #define FIVEMIN (5 * 60)
166 #define MSG2 "\07\07You have to leave in 5 minutes.\n"
167 if (secs >= FIVEMIN) {
168 sleep(secs - FIVEMIN);
169 if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
175 #define MSG3 "\07\07Just one more minute!\n"
176 if (secs >= ONEMIN) {
177 sleep(secs - ONEMIN);
178 if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
182 #define MSG4 "\07\07Time to leave!\n"
183 for (bother = 10; bother--;) {
184 sleep((u_int)ONEMIN);
185 if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
189 #define MSG5 "\07\07That was the last time I'll tell you. Bye.\n"
190 (void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
197 fprintf(stderr, "usage: leave [[+]hhmm]\n");