xref: /netbsd-src/usr.bin/leave/leave.c (revision 6818646ac81b767d53e3d0745d2267de0bb68492)
1*6818646aSjoerg /*	$NetBSD: leave.c,v 1.15 2011/09/16 15:39:27 joerg Exp $	*/
231ac284aSjtc 
361f28255Scgd /*
431ac284aSjtc  * Copyright (c) 1980, 1988, 1993
531ac284aSjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3287d6b38cSmikel #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\
3598e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3931ac284aSjtc #if 0
4031ac284aSjtc static char sccsid[] = "@(#)leave.c	8.1 (Berkeley) 6/6/93";
4187d6b38cSmikel #else
42*6818646aSjoerg __RCSID("$NetBSD: leave.c,v 1.15 2011/09/16 15:39:27 joerg Exp $");
4331ac284aSjtc #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd #include <sys/param.h>
4761f28255Scgd #include <sys/time.h>
4861f28255Scgd #include <ctype.h>
4987d6b38cSmikel #include <stdio.h>
50fcd0fb11Smatt #include <stdlib.h>
51a12b3b40Skleink #include <time.h>
5228ce846dSchristos #include <err.h>
5387d6b38cSmikel #include <unistd.h>
5461f28255Scgd 
5588c16e89Sfair #define	SECOND	1
5688c16e89Sfair #define MINUTE	(SECOND * 60)
5788c16e89Sfair #define	HOUR	(MINUTE * 60)
5888c16e89Sfair 
5961f28255Scgd /*
6061f28255Scgd  * leave [[+]hhmm]
6161f28255Scgd  *
6261f28255Scgd  * Reminds you when you have to leave.
6361f28255Scgd  * Leave prompts for input and goes away if you hit return.
6461f28255Scgd  * It nags you like a mother hen.
6561f28255Scgd  */
6687d6b38cSmikel 
67*6818646aSjoerg __dead static void doalarm(u_int);
68*6818646aSjoerg __dead static void usage(void);
6987d6b38cSmikel 
7087d6b38cSmikel int
main(int argc,char ** argv)7128ce846dSchristos main(int argc, char **argv)
7261f28255Scgd {
7328ce846dSchristos 	u_int secs;
7428ce846dSchristos 	int hours, minutes;
7528ce846dSchristos 	char c, *cp;
7628ce846dSchristos 	struct tm *t = NULL;
7787d6b38cSmikel 	time_t now;
7861f28255Scgd 	int plusnow;
7961f28255Scgd 	char buf[50];
8061f28255Scgd 
8128ce846dSchristos 	if (setvbuf(stdout, NULL, _IONBF, 0) != 0)
8228ce846dSchristos 		errx(1, "Cannot set stdout to unbuffered.");
8387d6b38cSmikel 
8461f28255Scgd 	if (argc < 2) {
8528ce846dSchristos 		(void)puts("When do you have to leave? ");
8661f28255Scgd 		cp = fgets(buf, sizeof(buf), stdin);
872e960887Sphil 		if (cp == NULL || *cp == '\n')
8861f28255Scgd 			exit(0);
8961f28255Scgd 	} else
9061f28255Scgd 		cp = argv[1];
9161f28255Scgd 
9261f28255Scgd 	if (*cp == '+') {
9361f28255Scgd 		plusnow = 1;
9461f28255Scgd 		++cp;
9561f28255Scgd 	} else {
9661f28255Scgd 		plusnow = 0;
9761f28255Scgd 		(void)time(&now);
9861f28255Scgd 		t = localtime(&now);
9961f28255Scgd 	}
10061f28255Scgd 
10161f28255Scgd 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
1028f5ca4eeSchristos 		if (!isdigit((unsigned char)c))
10361f28255Scgd 			usage();
10461f28255Scgd 		hours = hours * 10 + (c - '0');
10561f28255Scgd 	}
10661f28255Scgd 	minutes = hours % 100;
10761f28255Scgd 	hours /= 100;
10861f28255Scgd 
10961f28255Scgd 	if (minutes < 0 || minutes > 59)
11061f28255Scgd 		usage();
11161f28255Scgd 	if (plusnow)
11288c16e89Sfair 		secs = (hours * HOUR) + (minutes * MINUTE);
11361f28255Scgd 	else {
114688f0668Smikel 		if (hours > 23)
11561f28255Scgd 			usage();
116688f0668Smikel 		if (t->tm_hour >= 12)
117688f0668Smikel 			t->tm_hour -= 12;
11888c16e89Sfair 		if (hours >= 12)
11988c16e89Sfair 			hours -= 12;
120688f0668Smikel 		if (t->tm_hour > hours ||
121688f0668Smikel 		    (t->tm_hour == hours && minutes <= t->tm_min))
122688f0668Smikel 			hours += 12;
12388c16e89Sfair 		secs = (hours - t->tm_hour) * HOUR;
12488c16e89Sfair 		secs += (minutes - t->tm_min) * MINUTE;
12561f28255Scgd 	}
12661f28255Scgd 	doalarm(secs);
12761f28255Scgd 	exit(0);
12861f28255Scgd }
12961f28255Scgd 
13028ce846dSchristos static void
doalarm(u_int secs)13128ce846dSchristos doalarm(u_int secs)
13261f28255Scgd {
13328ce846dSchristos 	int bother;
13487d6b38cSmikel 	time_t daytime;
13561f28255Scgd 
13628ce846dSchristos 	switch (fork()) {
13728ce846dSchristos 	case 0:
13828ce846dSchristos 		break;
13928ce846dSchristos 	case -1:
14028ce846dSchristos 		err(1, "Fork failed");
14128ce846dSchristos 		/*NOTREACHED*/
14228ce846dSchristos 	default:
14361f28255Scgd 		exit(0);
14461f28255Scgd 	}
14528ce846dSchristos 
14628ce846dSchristos 	(void)time(&daytime);
14728ce846dSchristos 	daytime += secs;
14828ce846dSchristos 	printf("Alarm set for %.16s. (pid %u)\n",
14928ce846dSchristos 	    ctime(&daytime), (unsigned)getpid());
15061f28255Scgd 
15161f28255Scgd 	/*
15261f28255Scgd 	 * if write fails, we've lost the terminal through someone else
15361f28255Scgd 	 * causing a vhangup by logging in.
15461f28255Scgd 	 */
15588c16e89Sfair #define	FIVEMIN	(5 * MINUTE)
15661f28255Scgd 	if (secs >= FIVEMIN) {
15761f28255Scgd 		sleep(secs - FIVEMIN);
15828ce846dSchristos 		if (puts("\07\07You have to leave in 5 minutes.\n") == EOF)
15961f28255Scgd 			exit(0);
16061f28255Scgd 		secs = FIVEMIN;
16161f28255Scgd 	}
16261f28255Scgd 
16388c16e89Sfair #define	ONEMIN	(MINUTE)
16461f28255Scgd 	if (secs >= ONEMIN) {
16561f28255Scgd 		sleep(secs - ONEMIN);
16628ce846dSchristos 		if (puts("\07\07Just one more minute!\n") == EOF)
16761f28255Scgd 			exit(0);
16861f28255Scgd 	}
16961f28255Scgd 
17061f28255Scgd 	for (bother = 10; bother--;) {
17161f28255Scgd 		sleep((u_int)ONEMIN);
17228ce846dSchristos 		if (puts("\07\07Time to leave!\n") == EOF)
17361f28255Scgd 			exit(0);
17461f28255Scgd 	}
17561f28255Scgd 
17628ce846dSchristos 	(void)puts("\07\07That was the last time I'll tell you.  Bye.\n");
17761f28255Scgd 	exit(0);
17861f28255Scgd }
17961f28255Scgd 
18028ce846dSchristos static void
usage(void)18128ce846dSchristos usage(void)
18261f28255Scgd {
183b635f565Sjmmv 	(void)fprintf(stderr, "usage: %s [[+]hhmm]\n", getprogname());
18461f28255Scgd 	exit(1);
18561f28255Scgd }
186