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