Home | History | Annotate | Line # | Download | only in leave
leave.c revision 1.6
      1 /*	$NetBSD: leave.c,v 1.6 1997/07/15 02:31:13 mikel Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1988, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)leave.c	8.1 (Berkeley) 6/6/93";
     45 #else
     46 __RCSID("$NetBSD: leave.c,v 1.6 1997/07/15 02:31:13 mikel Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #include <ctype.h>
     53 #include <stdio.h>
     54 #include <unistd.h>
     55 
     56 /*
     57  * leave [[+]hhmm]
     58  *
     59  * Reminds you when you have to leave.
     60  * Leave prompts for input and goes away if you hit return.
     61  * It nags you like a mother hen.
     62  */
     63 
     64 int	main __P((int argc, char **argv));
     65 void	doalarm __P((u_int));
     66 void	usage __P((void));
     67 
     68 int
     69 main(argc, argv)
     70 	int argc;
     71 	char **argv;
     72 {
     73 	register u_int secs;
     74 	register int hours, minutes;
     75 	register char c, *cp;
     76 	struct tm *t;
     77 	time_t now;
     78 	int plusnow;
     79 	char buf[50];
     80 
     81 #ifdef __GNUC__
     82 	t = NULL;		/* XXX gcc -Wuninitialized */
     83 #endif
     84 
     85 	if (argc < 2) {
     86 #define	MSG1	"When do you have to leave? "
     87 		(void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
     88 		cp = fgets(buf, sizeof(buf), stdin);
     89 		if (cp == NULL || *cp == '\n')
     90 			exit(0);
     91 	} else
     92 		cp = argv[1];
     93 
     94 	if (*cp == '+') {
     95 		plusnow = 1;
     96 		++cp;
     97 	} else {
     98 		plusnow = 0;
     99 		(void)time(&now);
    100 		t = localtime(&now);
    101 	}
    102 
    103 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
    104 		if (!isdigit(c))
    105 			usage();
    106 		hours = hours * 10 + (c - '0');
    107 	}
    108 	minutes = hours % 100;
    109 	hours /= 100;
    110 
    111 	if (minutes < 0 || minutes > 59)
    112 		usage();
    113 	if (plusnow)
    114 		secs = hours * 60 * 60 + minutes * 60;
    115 	else {
    116 		if (hours > 23)
    117 			usage();
    118 		if (t->tm_hour >= 12)
    119 			t->tm_hour -= 12;
    120 		if (t->tm_hour > hours ||
    121 		    (t->tm_hour == hours && minutes <= t->tm_min))
    122 			hours += 12;
    123 		secs = (hours - t->tm_hour) * 60 * 60;
    124 		secs += (minutes - t->tm_min) * 60;
    125 	}
    126 	doalarm(secs);
    127 	exit(0);
    128 }
    129 
    130 void
    131 doalarm(secs)
    132 	u_int secs;
    133 {
    134 	register int bother;
    135 	time_t daytime;
    136 	int pid;
    137 
    138 	if ((pid = fork()) != 0) {
    139 		(void)time(&daytime);
    140 		daytime += secs;
    141 		printf("Alarm set for %.16s. (pid %d)\n",
    142 		    ctime(&daytime), pid);
    143 		exit(0);
    144 	}
    145 	sleep((u_int)2);		/* let parent print set message */
    146 
    147 	/*
    148 	 * if write fails, we've lost the terminal through someone else
    149 	 * causing a vhangup by logging in.
    150 	 */
    151 #define	FIVEMIN	(5 * 60)
    152 #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
    153 	if (secs >= FIVEMIN) {
    154 		sleep(secs - FIVEMIN);
    155 		if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) !=
    156 		    sizeof(MSG2) - 1)
    157 			exit(0);
    158 		secs = FIVEMIN;
    159 	}
    160 
    161 #define	ONEMIN	(60)
    162 #define	MSG3	"\07\07Just one more minute!\n"
    163 	if (secs >= ONEMIN) {
    164 		sleep(secs - ONEMIN);
    165 		if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) !=
    166 		    sizeof(MSG3) - 1)
    167 			exit(0);
    168 	}
    169 
    170 #define	MSG4	"\07\07Time to leave!\n"
    171 	for (bother = 10; bother--;) {
    172 		sleep((u_int)ONEMIN);
    173 		if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) !=
    174 		    sizeof(MSG4) - 1)
    175 			exit(0);
    176 	}
    177 
    178 #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
    179 	(void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
    180 	exit(0);
    181 }
    182 
    183 void
    184 usage()
    185 {
    186 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
    187 	exit(1);
    188 }
    189