xref: /csrg-svn/usr.bin/leave/leave.c (revision 62055)
121564Sdist /*
2*62055Sbostic  * Copyright (c) 1980, 1988, 1993
3*62055Sbostic  *	The Regents of the University of California.  All rights reserved.
432738Sbostic  *
542735Sbostic  * %sccs.include.redist.c%
621564Sdist  */
721564Sdist 
821564Sdist #ifndef lint
9*62055Sbostic static char copyright[] =
10*62055Sbostic "@(#) Copyright (c) 1980, 1988, 1993\n\
11*62055Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1232738Sbostic #endif /* not lint */
1321564Sdist 
1421564Sdist #ifndef lint
15*62055Sbostic static char sccsid[] = "@(#)leave.c	8.1 (Berkeley) 06/06/93";
1632738Sbostic #endif /* not lint */
1721564Sdist 
1834973Sbostic #include <sys/param.h>
1934973Sbostic #include <sys/time.h>
201039Sbill #include <stdio.h>
2132738Sbostic #include <ctype.h>
2234973Sbostic 
231039Sbill /*
2416572Sralph  * leave [[+]hhmm]
251039Sbill  *
261039Sbill  * Reminds you when you have to leave.
271039Sbill  * Leave prompts for input and goes away if you hit return.
281039Sbill  * It nags you like a mother hen.
291039Sbill  */
main(argc,argv)3016572Sralph main(argc, argv)
3134973Sbostic 	int argc;
3234973Sbostic 	char **argv;
331039Sbill {
3434973Sbostic 	register u_int secs;
3534973Sbostic 	register int hours, minutes;
3634973Sbostic 	register char c, *cp;
3734973Sbostic 	struct tm *t, *localtime();
3834973Sbostic 	time_t now, time();
3934973Sbostic 	int plusnow;
4034973Sbostic 	char buf[50];
411039Sbill 
421039Sbill 	if (argc < 2) {
4334973Sbostic #define	MSG1	"When do you have to leave? "
4434973Sbostic 		(void)write(1, MSG1, sizeof(MSG1) - 1);
4534973Sbostic 		cp = fgets(buf, sizeof(buf), stdin);
4634973Sbostic 		if (*cp == '\n')
4734973Sbostic 			exit(0);
4816572Sralph 	} else
4916572Sralph 		cp = argv[1];
5034973Sbostic 
5116572Sralph 	if (*cp == '+') {
5234973Sbostic 		plusnow = 1;
5334973Sbostic 		++cp;
5434973Sbostic 	} else {
5534973Sbostic 		plusnow = 0;
5634973Sbostic 		(void)time(&now);
5734973Sbostic 		t = localtime(&now);
5834973Sbostic 	}
5934973Sbostic 
6034973Sbostic 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
6134973Sbostic 		if (!isdigit(c))
6216572Sralph 			usage();
6334973Sbostic 		hours = hours * 10 + (c - '0');
641039Sbill 	}
6534973Sbostic 	minutes = hours % 100;
6634973Sbostic 	hours /= 100;
671039Sbill 
6834973Sbostic 	if (minutes < 0 || minutes > 59)
6916572Sralph 		usage();
7034973Sbostic 	if (plusnow)
7134973Sbostic 		secs = hours * 60 * 60 + minutes * 60;
7234973Sbostic 	else {
7334973Sbostic 		if (hours > 23 || t->tm_hour > hours ||
7434973Sbostic 		    t->tm_hour == hours && minutes <= t->tm_min)
7534973Sbostic 			usage();
7634973Sbostic 		secs = (hours - t->tm_hour) * 60 * 60;
7734973Sbostic 		secs += (minutes - t->tm_min) * 60;
7816572Sralph 	}
7934973Sbostic 	doalarm(secs);
801039Sbill 	exit(0);
811039Sbill }
821039Sbill 
doalarm(secs)8334973Sbostic doalarm(secs)
8434973Sbostic 	u_int secs;
8516572Sralph {
8634973Sbostic 	register int bother;
8734973Sbostic 	time_t daytime, time();
8834973Sbostic 	int pid;
8934973Sbostic 	char *ctime();
901039Sbill 
9134973Sbostic 	if (pid = fork()) {
9234973Sbostic 		(void)time(&daytime);
9334973Sbostic 		daytime += secs;
9434973Sbostic 		printf("Alarm set for %.16s. (pid %d)\n",
9534973Sbostic 		    ctime(&daytime), pid);
9634973Sbostic 		exit(0);
9734973Sbostic 	}
9834973Sbostic 	sleep((u_int)2);		/* let parent print set message */
9932738Sbostic 
10034973Sbostic 	/*
10134973Sbostic 	 * if write fails, we've lost the terminal through someone else
10234973Sbostic 	 * causing a vhangup by logging in.
10334973Sbostic 	 */
10434973Sbostic #define	FIVEMIN	(5 * 60)
10534973Sbostic #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
10634973Sbostic 	if (secs >= FIVEMIN) {
10734973Sbostic 		sleep(secs - FIVEMIN);
10834973Sbostic 		if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
10934973Sbostic 			exit(0);
11034973Sbostic 		secs = FIVEMIN;
11132738Sbostic 	}
11232738Sbostic 
11334973Sbostic #define	ONEMIN	(60)
11434973Sbostic #define	MSG3	"\07\07Just one more minute!\n"
11534973Sbostic 	if (secs >= ONEMIN) {
11634973Sbostic 		sleep(secs - ONEMIN);
11734973Sbostic 		if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
11834973Sbostic 			exit(0);
1191039Sbill 	}
1201039Sbill 
12134973Sbostic #define	MSG4	"\07\07Time to leave!\n"
12234973Sbostic 	for (bother = 10; bother--;) {
12334973Sbostic 		sleep((u_int)ONEMIN);
12434973Sbostic 		if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
12534973Sbostic 			exit(0);
1261039Sbill 	}
1271039Sbill 
12834973Sbostic #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
12934973Sbostic 	(void)write(1, MSG5, sizeof(MSG5) - 1);
13016572Sralph 	exit(0);
1311039Sbill }
1321039Sbill 
usage()13334973Sbostic usage()
1341039Sbill {
13534973Sbostic 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
13634973Sbostic 	exit(1);
1371039Sbill }
138