Home | History | Annotate | Line # | Download | only in leave
leave.c revision 1.5
      1 /*	$NetBSD: leave.c,v 1.5 1997/07/15 02:14:35 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.5 1997/07/15 02:14:35 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 || t->tm_hour > hours ||
    117 		    (t->tm_hour == hours && minutes <= t->tm_min))
    118 			usage();
    119 		secs = (hours - t->tm_hour) * 60 * 60;
    120 		secs += (minutes - t->tm_min) * 60;
    121 	}
    122 	doalarm(secs);
    123 	exit(0);
    124 }
    125 
    126 void
    127 doalarm(secs)
    128 	u_int secs;
    129 {
    130 	register int bother;
    131 	time_t daytime;
    132 	int pid;
    133 
    134 	if ((pid = fork()) != 0) {
    135 		(void)time(&daytime);
    136 		daytime += secs;
    137 		printf("Alarm set for %.16s. (pid %d)\n",
    138 		    ctime(&daytime), pid);
    139 		exit(0);
    140 	}
    141 	sleep((u_int)2);		/* let parent print set message */
    142 
    143 	/*
    144 	 * if write fails, we've lost the terminal through someone else
    145 	 * causing a vhangup by logging in.
    146 	 */
    147 #define	FIVEMIN	(5 * 60)
    148 #define	MSG2	"\07\07You have to leave in 5 minutes.\n"
    149 	if (secs >= FIVEMIN) {
    150 		sleep(secs - FIVEMIN);
    151 		if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) !=
    152 		    sizeof(MSG2) - 1)
    153 			exit(0);
    154 		secs = FIVEMIN;
    155 	}
    156 
    157 #define	ONEMIN	(60)
    158 #define	MSG3	"\07\07Just one more minute!\n"
    159 	if (secs >= ONEMIN) {
    160 		sleep(secs - ONEMIN);
    161 		if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) !=
    162 		    sizeof(MSG3) - 1)
    163 			exit(0);
    164 	}
    165 
    166 #define	MSG4	"\07\07Time to leave!\n"
    167 	for (bother = 10; bother--;) {
    168 		sleep((u_int)ONEMIN);
    169 		if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) !=
    170 		    sizeof(MSG4) - 1)
    171 			exit(0);
    172 	}
    173 
    174 #define	MSG5	"\07\07That was the last time I'll tell you.  Bye.\n"
    175 	(void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
    176 	exit(0);
    177 }
    178 
    179 void
    180 usage()
    181 {
    182 	fprintf(stderr, "usage: leave [[+]hhmm]\n");
    183 	exit(1);
    184 }
    185