leave.c revision 27572
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1980, 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527572Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1980, 1988, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127572Scharnier#if 0
421590Srgrimesstatic char sccsid[] = "@(#)leave.c	8.1 (Berkeley) 6/6/93";
4327572Scharnier#endif
4427572Scharnierstatic const char rcsid[] =
4527572Scharnier	"$Id$";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes#include <sys/param.h>
491590Srgrimes#include <sys/time.h>
5027572Scharnier#include <ctype.h>
511590Srgrimes#include <stdio.h>
5227572Scharnier#include <unistd.h>
531590Srgrimes
5427572Scharniervoid doalarm __P((u_int));
5527572Scharnierstatic void usage __P((void));
5627572Scharnier
571590Srgrimes/*
581590Srgrimes * leave [[+]hhmm]
591590Srgrimes *
601590Srgrimes * Reminds you when you have to leave.
611590Srgrimes * Leave prompts for input and goes away if you hit return.
621590Srgrimes * It nags you like a mother hen.
631590Srgrimes */
6427572Scharnierint
651590Srgrimesmain(argc, argv)
661590Srgrimes	int argc;
671590Srgrimes	char **argv;
681590Srgrimes{
691590Srgrimes	register u_int secs;
701590Srgrimes	register int hours, minutes;
711590Srgrimes	register char c, *cp;
721590Srgrimes	struct tm *t, *localtime();
731590Srgrimes	time_t now, time();
741590Srgrimes	int plusnow;
751590Srgrimes	char buf[50];
761590Srgrimes
771590Srgrimes	if (argc < 2) {
781590Srgrimes#define	MSG1	"When do you have to leave? "
791590Srgrimes		(void)write(1, MSG1, sizeof(MSG1) - 1);
801590Srgrimes		cp = fgets(buf, sizeof(buf), stdin);
8127572Scharnier		if (cp == NULL || *cp == '\n')
821590Srgrimes			exit(0);
831590Srgrimes	} else
841590Srgrimes		cp = argv[1];
851590Srgrimes
861590Srgrimes	if (*cp == '+') {
871590Srgrimes		plusnow = 1;
881590Srgrimes		++cp;
891590Srgrimes	} else {
901590Srgrimes		plusnow = 0;
911590Srgrimes		(void)time(&now);
921590Srgrimes		t = localtime(&now);
931590Srgrimes	}
941590Srgrimes
951590Srgrimes	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
961590Srgrimes		if (!isdigit(c))
971590Srgrimes			usage();
981590Srgrimes		hours = hours * 10 + (c - '0');
991590Srgrimes	}
1001590Srgrimes	minutes = hours % 100;
1011590Srgrimes	hours /= 100;
1021590Srgrimes
1031590Srgrimes	if (minutes < 0 || minutes > 59)
1041590Srgrimes		usage();
1051590Srgrimes	if (plusnow)
1061590Srgrimes		secs = hours * 60 * 60 + minutes * 60;
1071590Srgrimes	else {
1081590Srgrimes		if (hours > 23 || t->tm_hour > hours ||
10927572Scharnier		    (t->tm_hour == hours && minutes <= t->tm_min))
1101590Srgrimes			usage();
1111590Srgrimes		secs = (hours - t->tm_hour) * 60 * 60;
1121590Srgrimes		secs += (minutes - t->tm_min) * 60;
1131590Srgrimes	}
1141590Srgrimes	doalarm(secs);
1151590Srgrimes	exit(0);
1161590Srgrimes}
1171590Srgrimes
11827572Scharniervoid
1191590Srgrimesdoalarm(secs)
1201590Srgrimes	u_int secs;
1211590Srgrimes{
1221590Srgrimes	register int bother;
1231590Srgrimes	time_t daytime, time();
1241590Srgrimes	int pid;
1251590Srgrimes	char *ctime();
1261590Srgrimes
12727572Scharnier	if ((pid = fork())) {
1281590Srgrimes		(void)time(&daytime);
1291590Srgrimes		daytime += secs;
1301590Srgrimes		printf("Alarm set for %.16s. (pid %d)\n",
1311590Srgrimes		    ctime(&daytime), pid);
1321590Srgrimes		exit(0);
1331590Srgrimes	}
1341590Srgrimes	sleep((u_int)2);		/* let parent print set message */
1351590Srgrimes
1361590Srgrimes	/*
1371590Srgrimes	 * if write fails, we've lost the terminal through someone else
1381590Srgrimes	 * causing a vhangup by logging in.
1391590Srgrimes	 */
1401590Srgrimes#define	FIVEMIN	(5 * 60)
1411590Srgrimes#define	MSG2	"\07\07You have to leave in 5 minutes.\n"
1421590Srgrimes	if (secs >= FIVEMIN) {
1431590Srgrimes		sleep(secs - FIVEMIN);
1441590Srgrimes		if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
1451590Srgrimes			exit(0);
1461590Srgrimes		secs = FIVEMIN;
1471590Srgrimes	}
1481590Srgrimes
1491590Srgrimes#define	ONEMIN	(60)
1501590Srgrimes#define	MSG3	"\07\07Just one more minute!\n"
1511590Srgrimes	if (secs >= ONEMIN) {
1521590Srgrimes		sleep(secs - ONEMIN);
1531590Srgrimes		if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
1541590Srgrimes			exit(0);
1551590Srgrimes	}
1561590Srgrimes
1571590Srgrimes#define	MSG4	"\07\07Time to leave!\n"
1581590Srgrimes	for (bother = 10; bother--;) {
1591590Srgrimes		sleep((u_int)ONEMIN);
1601590Srgrimes		if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
1611590Srgrimes			exit(0);
1621590Srgrimes	}
1631590Srgrimes
1641590Srgrimes#define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
1651590Srgrimes	(void)write(1, MSG5, sizeof(MSG5) - 1);
1661590Srgrimes	exit(0);
1671590Srgrimes}
1681590Srgrimes
16927572Scharnierstatic void
1701590Srgrimesusage()
1711590Srgrimes{
1721590Srgrimes	fprintf(stderr, "usage: leave [[+]hhmm]\n");
1731590Srgrimes	exit(1);
1741590Srgrimes}
175