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 * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3127572Scharnierstatic const char copyright[] =
321590Srgrimes"@(#) Copyright (c) 1980, 1988, 1993\n\
331590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341590Srgrimes#endif /* not lint */
351590Srgrimes
361590Srgrimes#ifndef lint
3727572Scharnier#if 0
381590Srgrimesstatic char sccsid[] = "@(#)leave.c	8.1 (Berkeley) 6/6/93";
3927572Scharnier#endif
401590Srgrimes#endif /* not lint */
4199112Sobrien#include <sys/cdefs.h>
4299112Sobrien__FBSDID("$FreeBSD$");
431590Srgrimes
4432295Shelbig#include <err.h>
4527572Scharnier#include <ctype.h>
4632295Shelbig#include <locale.h>
471590Srgrimes#include <stdio.h>
4878717Sdd#include <stdlib.h>
4932295Shelbig#include <time.h>
5027572Scharnier#include <unistd.h>
511590Srgrimes
52198847Sdelphijstatic void doalarm(u_int);
5392920Simpstatic void usage(void);
5427572Scharnier
551590Srgrimes/*
561590Srgrimes * leave [[+]hhmm]
571590Srgrimes *
581590Srgrimes * Reminds you when you have to leave.
591590Srgrimes * Leave prompts for input and goes away if you hit return.
601590Srgrimes * It nags you like a mother hen.
611590Srgrimes */
6227572Scharnierint
63102944Sdwmalonemain(int argc, char **argv)
641590Srgrimes{
65102944Sdwmalone	u_int secs;
66102944Sdwmalone	int hours, minutes;
67102944Sdwmalone	char c, *cp = NULL;
6877451Sdd	struct tm *t;
6977451Sdd	time_t now;
7032295Shelbig	int plusnow, t_12_hour;
711590Srgrimes	char buf[50];
721590Srgrimes
7332295Shelbig	if (setlocale(LC_TIME, "") == NULL)
7432295Shelbig		warn("setlocale");
7532295Shelbig
761590Srgrimes	if (argc < 2) {
771590Srgrimes#define	MSG1	"When do you have to leave? "
7880381Ssheldonh		(void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
791590Srgrimes		cp = fgets(buf, sizeof(buf), stdin);
8027572Scharnier		if (cp == NULL || *cp == '\n')
811590Srgrimes			exit(0);
8275906Skris	} else if (argc > 2)
8375906Skris		usage();
8475906Skris	else
851590Srgrimes		cp = argv[1];
861590Srgrimes
871590Srgrimes	if (*cp == '+') {
881590Srgrimes		plusnow = 1;
891590Srgrimes		++cp;
9032295Shelbig	} else
911590Srgrimes		plusnow = 0;
921590Srgrimes
931590Srgrimes	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
941590Srgrimes		if (!isdigit(c))
951590Srgrimes			usage();
961590Srgrimes		hours = hours * 10 + (c - '0');
971590Srgrimes	}
981590Srgrimes	minutes = hours % 100;
991590Srgrimes	hours /= 100;
1001590Srgrimes
1011590Srgrimes	if (minutes < 0 || minutes > 59)
1021590Srgrimes		usage();
1031590Srgrimes	if (plusnow)
1041590Srgrimes		secs = hours * 60 * 60 + minutes * 60;
1051590Srgrimes	else {
10632295Shelbig		(void)time(&now);
10732295Shelbig		t = localtime(&now);
10832295Shelbig
10932295Shelbig		if (hours > 23)
1101590Srgrimes			usage();
11132295Shelbig
11232295Shelbig		/* Convert tol to 12 hr time (0:00...11:59) */
11332295Shelbig		if (hours > 11)
11432295Shelbig			hours -= 12;
11532295Shelbig
11632295Shelbig		/* Convert tm to 12 hr time (0:00...11:59) */
11732295Shelbig		if (t->tm_hour > 11)
11832295Shelbig			t_12_hour = t->tm_hour - 12;
11932295Shelbig		else
12032295Shelbig			t_12_hour = t->tm_hour;
12132295Shelbig
12232295Shelbig		if (hours < t_12_hour ||
12332295Shelbig	 	   (hours == t_12_hour && minutes <= t->tm_min))
12432295Shelbig			/* Leave time is in the past so we add 12 hrs */
12532295Shelbig			hours += 12;
12632295Shelbig
12732295Shelbig		secs = (hours - t_12_hour) * 60 * 60;
1281590Srgrimes		secs += (minutes - t->tm_min) * 60;
12932295Shelbig		secs -= now % 60;	/* truncate (now + secs) to min */
1301590Srgrimes	}
1311590Srgrimes	doalarm(secs);
1321590Srgrimes	exit(0);
1331590Srgrimes}
1341590Srgrimes
13527572Scharniervoid
136102944Sdwmalonedoalarm(u_int secs)
1371590Srgrimes{
138102944Sdwmalone	int bother;
13977451Sdd	time_t daytime;
14032295Shelbig	char tb[80];
1411590Srgrimes	int pid;
1421590Srgrimes
14327572Scharnier	if ((pid = fork())) {
1441590Srgrimes		(void)time(&daytime);
1451590Srgrimes		daytime += secs;
14632295Shelbig		strftime(tb, sizeof(tb), "%+", localtime(&daytime));
14732295Shelbig		printf("Alarm set for %s. (pid %d)\n", tb, pid);
1481590Srgrimes		exit(0);
1491590Srgrimes	}
1501590Srgrimes	sleep((u_int)2);		/* let parent print set message */
15135182Ssteve	if (secs >= 2)
15235182Ssteve		secs -= 2;
1531590Srgrimes
1541590Srgrimes	/*
1551590Srgrimes	 * if write fails, we've lost the terminal through someone else
1561590Srgrimes	 * causing a vhangup by logging in.
1571590Srgrimes	 */
1581590Srgrimes#define	FIVEMIN	(5 * 60)
1591590Srgrimes#define	MSG2	"\07\07You have to leave in 5 minutes.\n"
1601590Srgrimes	if (secs >= FIVEMIN) {
1611590Srgrimes		sleep(secs - FIVEMIN);
16280381Ssheldonh		if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
1631590Srgrimes			exit(0);
1641590Srgrimes		secs = FIVEMIN;
1651590Srgrimes	}
1661590Srgrimes
1671590Srgrimes#define	ONEMIN	(60)
1681590Srgrimes#define	MSG3	"\07\07Just one more minute!\n"
1691590Srgrimes	if (secs >= ONEMIN) {
1701590Srgrimes		sleep(secs - ONEMIN);
17180381Ssheldonh		if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
1721590Srgrimes			exit(0);
1731590Srgrimes	}
1741590Srgrimes
1751590Srgrimes#define	MSG4	"\07\07Time to leave!\n"
1761590Srgrimes	for (bother = 10; bother--;) {
1771590Srgrimes		sleep((u_int)ONEMIN);
17880381Ssheldonh		if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
1791590Srgrimes			exit(0);
1801590Srgrimes	}
1811590Srgrimes
1821590Srgrimes#define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
18380381Ssheldonh	(void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
1841590Srgrimes	exit(0);
1851590Srgrimes}
1861590Srgrimes
18727572Scharnierstatic void
188102944Sdwmaloneusage(void)
1891590Srgrimes{
1901590Srgrimes	fprintf(stderr, "usage: leave [[+]hhmm]\n");
1911590Srgrimes	exit(1);
1921590Srgrimes}
193