xref: /csrg-svn/usr.bin/leave/leave.c (revision 1039)
1*1039Sbill static char *sccsid = "@(#)leave.c	4.1 (Berkeley) 10/01/80";
2*1039Sbill #include <stdio.h>
3*1039Sbill /*
4*1039Sbill  * leave [hhmm]
5*1039Sbill  *
6*1039Sbill  * Reminds you when you have to leave.
7*1039Sbill  * Leave prompts for input and goes away if you hit return.
8*1039Sbill  * It nags you like a mother hen.
9*1039Sbill  */
10*1039Sbill char origlogin[20], thislogin[20];
11*1039Sbill char *getlogin();
12*1039Sbill char *whenleave;
13*1039Sbill char *ctime();
14*1039Sbill char buff[100];
15*1039Sbill 
16*1039Sbill main(argc,argv)
17*1039Sbill char **argv;
18*1039Sbill {
19*1039Sbill 	long when, tod, now, diff, hours, minutes;
20*1039Sbill 	int *nv;
21*1039Sbill 	int atoi();
22*1039Sbill 	int *localtime();
23*1039Sbill 
24*1039Sbill 	if (argc < 2) {
25*1039Sbill 		printf("When do you have to leave? ");
26*1039Sbill 		fflush(stdout);
27*1039Sbill 		buff[read(0,buff,sizeof buff)] = 0;
28*1039Sbill 	} else {
29*1039Sbill 		strcpy(buff,argv[1]);
30*1039Sbill 	}
31*1039Sbill 
32*1039Sbill 	if (buff[0] == '\n')
33*1039Sbill 		exit(0);
34*1039Sbill 	if (buff[0] == '+') {
35*1039Sbill 		diff = atoi(buff+1);
36*1039Sbill 		doalarm(diff);
37*1039Sbill 	}
38*1039Sbill 	if (buff[0] < '0' || buff[0] > '9') {
39*1039Sbill 		printf("usage: %s [hhmm]\n",argv[0]);
40*1039Sbill 		exit(1);
41*1039Sbill 	}
42*1039Sbill 	strcpy(origlogin,getlogin());
43*1039Sbill 
44*1039Sbill 	tod = atoi(buff);
45*1039Sbill 	hours = tod / 100;
46*1039Sbill 	if (hours > 12)
47*1039Sbill 		hours -= 12;
48*1039Sbill 	if (hours == 12)
49*1039Sbill 		hours = 0;
50*1039Sbill 	minutes = tod % 100;
51*1039Sbill 
52*1039Sbill 	if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59) {
53*1039Sbill 		printf("usage: %s [hhmm]\n",argv[0]);
54*1039Sbill 		exit(1);
55*1039Sbill 	}
56*1039Sbill 
57*1039Sbill 	setexit();	/* refigure time if killed */
58*1039Sbill 	time(&now);
59*1039Sbill 	nv = localtime(&now);
60*1039Sbill 	when = 60*hours+minutes;
61*1039Sbill 	if (nv[2] > 12) nv[2] -= 12;	/* do am/pm bit */
62*1039Sbill 	now = 60*nv[2] + nv[1];
63*1039Sbill 	diff = when - now;
64*1039Sbill 	while (diff < 0)
65*1039Sbill 		diff += 12*60;
66*1039Sbill 	if (diff > 11*60) printf("That time has already passed!\n");
67*1039Sbill 	doalarm(diff);
68*1039Sbill 	exit(0);
69*1039Sbill }
70*1039Sbill 
71*1039Sbill 
72*1039Sbill doalarm(nmins)
73*1039Sbill long nmins;
74*1039Sbill {
75*1039Sbill 	char *msg1, *msg2, *msg3, *msg4;
76*1039Sbill 	register int i;
77*1039Sbill 	int slp1, slp2, slp3, slp4;
78*1039Sbill 	int seconds, gseconds;
79*1039Sbill 	long daytime;
80*1039Sbill 
81*1039Sbill 	seconds = 60 * nmins;
82*1039Sbill 	if (seconds <= 0)
83*1039Sbill 		seconds = 1;
84*1039Sbill 	gseconds = seconds;
85*1039Sbill 
86*1039Sbill 	msg1 = "You have to leave in 5 minutes";
87*1039Sbill 	if (seconds <= 60*5) {
88*1039Sbill 		slp1 = 0;
89*1039Sbill 	} else {
90*1039Sbill 		slp1 = seconds - 60*5;
91*1039Sbill 		seconds = 60*5;
92*1039Sbill 	}
93*1039Sbill 
94*1039Sbill 	msg2 = "Just one more minute!";
95*1039Sbill 	if (seconds <= 60) {
96*1039Sbill 		slp2 = 0;
97*1039Sbill 	} else {
98*1039Sbill 		slp2 = seconds - 60;
99*1039Sbill 		seconds = 60;
100*1039Sbill 	}
101*1039Sbill 
102*1039Sbill 	msg3 = "Time to leave!";
103*1039Sbill 	slp3 = seconds;
104*1039Sbill 
105*1039Sbill 	msg4 = "You're going to be late!";
106*1039Sbill 	slp4 = 60;
107*1039Sbill 
108*1039Sbill 	time(&daytime);
109*1039Sbill 	daytime += gseconds;
110*1039Sbill 	whenleave = ctime(&daytime);
111*1039Sbill 	printf("Alarm set for %s\n",whenleave);
112*1039Sbill 	if (fork())
113*1039Sbill 		exit(0);
114*1039Sbill 	signal(2,1);
115*1039Sbill 	signal(3,1);
116*1039Sbill 	signal(15,1);
117*1039Sbill 
118*1039Sbill 	if (slp1)
119*1039Sbill 		bother(slp1,msg1);
120*1039Sbill 	if (slp2)
121*1039Sbill 		bother(slp2,msg2);
122*1039Sbill 	bother(slp3,msg3);
123*1039Sbill 	for (;;) {
124*1039Sbill 		bother(slp4,msg4);
125*1039Sbill 	}
126*1039Sbill }
127*1039Sbill 
128*1039Sbill bother(slp,msg)
129*1039Sbill int slp;
130*1039Sbill char *msg;
131*1039Sbill {
132*1039Sbill 
133*1039Sbill 	delay(slp);
134*1039Sbill 	printf("\7\7\7");
135*1039Sbill 	printf("%s\n",msg);
136*1039Sbill }
137*1039Sbill 
138*1039Sbill /*
139*1039Sbill  * delay is like sleep but does it in 100 sec pieces and
140*1039Sbill  * knows what zero means.
141*1039Sbill  */
142*1039Sbill delay(secs) int secs; {
143*1039Sbill 	int n;
144*1039Sbill 
145*1039Sbill 	while(secs>0) {
146*1039Sbill 		n = 100;
147*1039Sbill 		secs = secs - 100;
148*1039Sbill 		if (secs < 0) {
149*1039Sbill 			n = n + secs;
150*1039Sbill 		}
151*1039Sbill 		if (n > 0)
152*1039Sbill 			sleep(n);
153*1039Sbill 		strcpy(thislogin,getlogin());
154*1039Sbill 		if (strcmp(origlogin, thislogin))
155*1039Sbill 			exit(0);
156*1039Sbill 	}
157*1039Sbill }
158*1039Sbill 
159*1039Sbill #ifdef V6
160*1039Sbill char *getlogin() {
161*1039Sbill #include <utmp.h>
162*1039Sbill 
163*1039Sbill 	static struct utmp ubuf;
164*1039Sbill 	int ufd;
165*1039Sbill 
166*1039Sbill 	ufd = open("/etc/utmp",0);
167*1039Sbill 	seek(ufd, ttyn(0)*sizeof(ubuf), 0);
168*1039Sbill 	read(ufd, &ubuf, sizeof(ubuf));
169*1039Sbill 	ubuf.ut_name[sizeof(ubuf.ut_name)] = 0;
170*1039Sbill 	return(&ubuf.ut_name);
171*1039Sbill }
172*1039Sbill #endif
173