xref: /netbsd-src/usr.bin/leave/leave.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: leave.c,v 1.9 2000/07/03 02:51:22 matt 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.9 2000/07/03 02:51:22 matt 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 /*
59  * leave [[+]hhmm]
60  *
61  * Reminds you when you have to leave.
62  * Leave prompts for input and goes away if you hit return.
63  * It nags you like a mother hen.
64  */
65 
66 int	main __P((int argc, char **argv));
67 void	doalarm __P((u_int));
68 void	usage __P((void));
69 
70 int
71 main(argc, argv)
72 	int argc;
73 	char **argv;
74 {
75 	register u_int secs;
76 	register int hours, minutes;
77 	register char c, *cp;
78 	struct tm *t;
79 	time_t now;
80 	int plusnow;
81 	char buf[50];
82 
83 #ifdef __GNUC__
84 	t = NULL;		/* XXX gcc -Wuninitialized */
85 #endif
86 
87 	if (argc < 2) {
88 #define	MSG1	"When do you have to leave? "
89 		(void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
90 		cp = fgets(buf, sizeof(buf), stdin);
91 		if (cp == NULL || *cp == '\n')
92 			exit(0);
93 	} else
94 		cp = argv[1];
95 
96 	if (*cp == '+') {
97 		plusnow = 1;
98 		++cp;
99 	} else {
100 		plusnow = 0;
101 		(void)time(&now);
102 		t = localtime(&now);
103 	}
104 
105 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
106 		if (!isdigit((unsigned char)c))
107 			usage();
108 		hours = hours * 10 + (c - '0');
109 	}
110 	minutes = hours % 100;
111 	hours /= 100;
112 
113 	if (minutes < 0 || minutes > 59)
114 		usage();
115 	if (plusnow)
116 		secs = hours * 60 * 60 + minutes * 60;
117 	else {
118 		if (hours > 23)
119 			usage();
120 		if (t->tm_hour >= 12)
121 			t->tm_hour -= 12;
122 		if (t->tm_hour > hours ||
123 		    (t->tm_hour == hours && minutes <= t->tm_min))
124 			hours += 12;
125 		secs = (hours - t->tm_hour) * 60 * 60;
126 		secs += (minutes - t->tm_min) * 60;
127 	}
128 	doalarm(secs);
129 	exit(0);
130 }
131 
132 void
133 doalarm(secs)
134 	u_int secs;
135 {
136 	register int bother;
137 	time_t daytime;
138 	int pid;
139 
140 	if ((pid = fork()) != 0) {
141 		(void)time(&daytime);
142 		daytime += secs;
143 		printf("Alarm set for %.16s. (pid %d)\n",
144 		    ctime(&daytime), pid);
145 		exit(0);
146 	}
147 	sleep((u_int)2);		/* let parent print set message */
148 
149 	/*
150 	 * if write fails, we've lost the terminal through someone else
151 	 * causing a vhangup by logging in.
152 	 */
153 #define	FIVEMIN	(5 * 60)
154 #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
155 	if (secs >= FIVEMIN) {
156 		sleep(secs - FIVEMIN);
157 		if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) !=
158 		    sizeof(MSG2) - 1)
159 			exit(0);
160 		secs = FIVEMIN;
161 	}
162 
163 #define	ONEMIN	(60)
164 #define	MSG3	"\07\07Just one more minute!\n"
165 	if (secs >= ONEMIN) {
166 		sleep(secs - ONEMIN);
167 		if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) !=
168 		    sizeof(MSG3) - 1)
169 			exit(0);
170 	}
171 
172 #define	MSG4	"\07\07Time to leave!\n"
173 	for (bother = 10; bother--;) {
174 		sleep((u_int)ONEMIN);
175 		if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) !=
176 		    sizeof(MSG4) - 1)
177 			exit(0);
178 	}
179 
180 #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
181 	(void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
182 	exit(0);
183 }
184 
185 void
186 usage()
187 {
188 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
189 	exit(1);
190 }
191