xref: /csrg-svn/usr.bin/leave/leave.c (revision 32738)
121564Sdist /*
221564Sdist  * Copyright (c) 1980 Regents of the University of California.
3*32738Sbostic  * All rights reserved.
4*32738Sbostic  *
5*32738Sbostic  * Redistribution and use in source and binary forms are permitted
6*32738Sbostic  * provided that this notice is preserved and that due credit is given
7*32738Sbostic  * to the University of California at Berkeley. The name of the University
8*32738Sbostic  * may not be used to endorse or promote products derived from this
9*32738Sbostic  * software without specific written prior permission. This software
10*32738Sbostic  * is provided ``as is'' without express or implied warranty.
1121564Sdist  */
1221564Sdist 
1321564Sdist #ifndef lint
1421564Sdist char copyright[] =
1521564Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1621564Sdist  All rights reserved.\n";
17*32738Sbostic #endif /* not lint */
1821564Sdist 
1921564Sdist #ifndef lint
20*32738Sbostic static char sccsid[] = "@(#)leave.c	5.2 (Berkeley) 12/02/87";
21*32738Sbostic #endif /* not lint */
2221564Sdist 
231039Sbill #include <stdio.h>
24*32738Sbostic #include <ctype.h>
2511804Sedward #include <signal.h>
261039Sbill /*
2716572Sralph  * leave [[+]hhmm]
281039Sbill  *
291039Sbill  * Reminds you when you have to leave.
301039Sbill  * Leave prompts for input and goes away if you hit return.
311039Sbill  * It nags you like a mother hen.
321039Sbill  */
3316572Sralph char origlogin[20];
341039Sbill char *getlogin();
351039Sbill char *whenleave;
361039Sbill char *ctime();
371039Sbill char buff[100];
381039Sbill 
3916572Sralph main(argc, argv)
401039Sbill char **argv;
411039Sbill {
42*32738Sbostic 	long when, now, diff, hours, minutes;
4316572Sralph 	char *cp;
441039Sbill 	int *nv;
45*32738Sbostic 	int gethm();
461039Sbill 	int *localtime();
471039Sbill 
48*32738Sbostic 	if ((cp = getlogin()) == NULL) {
49*32738Sbostic 		fputs("leave: You are not logged in.\n", stderr);
50*32738Sbostic 		exit(1);
51*32738Sbostic 	}
52*32738Sbostic 	strcpy(origlogin, cp);
531039Sbill 	if (argc < 2) {
541039Sbill 		printf("When do you have to leave? ");
551039Sbill 		fflush(stdout);
5616572Sralph 		buff[read(0, buff, sizeof buff)] = 0;
5716572Sralph 		cp = buff;
5816572Sralph 	} else
5916572Sralph 		cp = argv[1];
6016572Sralph 	if (*cp == '\n')
611039Sbill 		exit(0);
6216572Sralph 	if (*cp == '+') {
6316572Sralph 		cp++;
64*32738Sbostic 		if (!gethm(cp, &hours, &minutes))
6516572Sralph 			usage();
6616572Sralph 		if (minutes < 0 || minutes > 59)
6716572Sralph 			usage();
6816572Sralph 		diff = 60*hours+minutes;
691039Sbill 		doalarm(diff);
7016572Sralph 		exit(0);
711039Sbill 	}
72*32738Sbostic 	if (!gethm(cp, &hours, &minutes))
7316572Sralph 		usage();
741039Sbill 	if (hours > 12)
751039Sbill 		hours -= 12;
761039Sbill 	if (hours == 12)
771039Sbill 		hours = 0;
781039Sbill 
7916572Sralph 	if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59)
8016572Sralph 		usage();
811039Sbill 
821039Sbill 	time(&now);
831039Sbill 	nv = localtime(&now);
841039Sbill 	when = 60*hours+minutes;
8516572Sralph 	if (nv[2] > 12)
8616572Sralph 		nv[2] -= 12;	/* do am/pm bit */
871039Sbill 	now = 60*nv[2] + nv[1];
881039Sbill 	diff = when - now;
891039Sbill 	while (diff < 0)
901039Sbill 		diff += 12*60;
9116572Sralph 	if (diff > 11*60) {
92*32738Sbostic 		fprintf(stderr, "That time has already passed!\n");
9316572Sralph 		exit(1);
9416572Sralph 	}
951039Sbill 	doalarm(diff);
961039Sbill 	exit(0);
971039Sbill }
981039Sbill 
9916572Sralph usage()
10016572Sralph {
101*32738Sbostic 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
10216572Sralph 	exit(1);
10316572Sralph }
1041039Sbill 
105*32738Sbostic int
106*32738Sbostic gethm(cp, hp, mp)
107*32738Sbostic register char *cp;
108*32738Sbostic int *hp, *mp;
109*32738Sbostic {
110*32738Sbostic 	register char c;
111*32738Sbostic 	register int tod;
112*32738Sbostic 
113*32738Sbostic 	tod = 0;
114*32738Sbostic 	while ((c = *cp++) != '\0') {
115*32738Sbostic 		if (!isdigit(c))
116*32738Sbostic 			return(0);
117*32738Sbostic 		tod = tod * 10 + (c - '0');
118*32738Sbostic 	}
119*32738Sbostic 	*hp = tod / 100;
120*32738Sbostic 	*mp = tod % 100;
121*32738Sbostic 	return(1);
122*32738Sbostic }
123*32738Sbostic 
1241039Sbill doalarm(nmins)
1251039Sbill long nmins;
1261039Sbill {
1271039Sbill 	char *msg1, *msg2, *msg3, *msg4;
1281039Sbill 	register int i;
1291039Sbill 	int slp1, slp2, slp3, slp4;
1301039Sbill 	int seconds, gseconds;
1311039Sbill 	long daytime;
1321039Sbill 
1331039Sbill 	seconds = 60 * nmins;
1341039Sbill 	if (seconds <= 0)
1351039Sbill 		seconds = 1;
1361039Sbill 	gseconds = seconds;
1371039Sbill 
1381039Sbill 	msg1 = "You have to leave in 5 minutes";
1391039Sbill 	if (seconds <= 60*5) {
1401039Sbill 		slp1 = 0;
1411039Sbill 	} else {
1421039Sbill 		slp1 = seconds - 60*5;
1431039Sbill 		seconds = 60*5;
1441039Sbill 	}
1451039Sbill 
1461039Sbill 	msg2 = "Just one more minute!";
1471039Sbill 	if (seconds <= 60) {
1481039Sbill 		slp2 = 0;
1491039Sbill 	} else {
1501039Sbill 		slp2 = seconds - 60;
1511039Sbill 		seconds = 60;
1521039Sbill 	}
1531039Sbill 
1541039Sbill 	msg3 = "Time to leave!";
1551039Sbill 	slp3 = seconds;
1561039Sbill 
1571039Sbill 	msg4 = "You're going to be late!";
1581039Sbill 	slp4 = 60;
1591039Sbill 
1601039Sbill 	time(&daytime);
1611039Sbill 	daytime += gseconds;
1621039Sbill 	whenleave = ctime(&daytime);
16316572Sralph 	printf("Alarm set for %s", whenleave);
1641039Sbill 	if (fork())
1651039Sbill 		exit(0);
16611804Sedward 	signal(SIGINT, SIG_IGN);
16711804Sedward 	signal(SIGQUIT, SIG_IGN);
16811804Sedward 	signal(SIGTERM, SIG_IGN);
16911804Sedward 	signal(SIGTTOU, SIG_IGN);
1701039Sbill 
1711039Sbill 	if (slp1)
17216572Sralph 		bother(slp1, msg1);
1731039Sbill 	if (slp2)
17416572Sralph 		bother(slp2, msg2);
17516572Sralph 	bother(slp3, msg3);
17616572Sralph 	for (i = 0; i < 10; i++)
17716572Sralph 		bother(slp4, msg4);
17816572Sralph 	printf("That was the last time I'll tell you. Bye.\n");
17916572Sralph 	exit(0);
1801039Sbill }
1811039Sbill 
18216572Sralph bother(slp, msg)
1831039Sbill int slp;
1841039Sbill char *msg;
1851039Sbill {
1861039Sbill 
1871039Sbill 	delay(slp);
18816572Sralph 	printf("\7\7\7%s\n", msg);
1891039Sbill }
1901039Sbill 
1911039Sbill /*
1921039Sbill  * delay is like sleep but does it in 100 sec pieces and
1931039Sbill  * knows what zero means.
1941039Sbill  */
19516572Sralph delay(secs)
19616572Sralph int secs;
19716572Sralph {
1981039Sbill 	int n;
199*32738Sbostic 	register char *l;
2001039Sbill 
20116572Sralph 	while (secs > 0) {
2021039Sbill 		n = 100;
20316572Sralph 		if (secs < n)
20416572Sralph 			n = secs;
20516572Sralph 		secs -= n;
2061039Sbill 		if (n > 0)
2071039Sbill 			sleep(n);
208*32738Sbostic 		l = getlogin();
209*32738Sbostic 		if (l == NULL)
2101039Sbill 			exit(0);
211*32738Sbostic 		if (strcmp(origlogin, l) != 0)
212*32738Sbostic 			exit(0);
2131039Sbill 	}
2141039Sbill }
2151039Sbill 
2161039Sbill #ifdef V6
2171039Sbill char *getlogin() {
2181039Sbill #include <utmp.h>
2191039Sbill 
2201039Sbill 	static struct utmp ubuf;
2211039Sbill 	int ufd;
2221039Sbill 
2231039Sbill 	ufd = open("/etc/utmp",0);
2241039Sbill 	seek(ufd, ttyn(0)*sizeof(ubuf), 0);
2251039Sbill 	read(ufd, &ubuf, sizeof(ubuf));
2261039Sbill 	ubuf.ut_name[sizeof(ubuf.ut_name)] = 0;
2271039Sbill 	return(&ubuf.ut_name);
2281039Sbill }
2291039Sbill #endif
230