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