Home | History | Annotate | Line # | Download | only in leave
leave.c revision 1.11
      1 /*	$NetBSD: leave.c,v 1.11 2003/04/02 19:43:02 christos 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.11 2003/04/02 19:43:02 christos 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 <stdlib.h>
     55 #include <time.h>
     56 #include <err.h>
     57 #include <unistd.h>
     58 
     59 #define	SECOND	1
     60 #define MINUTE	(SECOND * 60)
     61 #define	HOUR	(MINUTE * 60)
     62 
     63 /*
     64  * leave [[+]hhmm]
     65  *
     66  * Reminds you when you have to leave.
     67  * Leave prompts for input and goes away if you hit return.
     68  * It nags you like a mother hen.
     69  */
     70 
     71 int main(int argc, char **argv);
     72 
     73 static void doalarm(u_int);
     74 static void usage(void);
     75 
     76 int
     77 main(int argc, char **argv)
     78 {
     79 	u_int secs;
     80 	int hours, minutes;
     81 	char c, *cp;
     82 	struct tm *t = NULL;
     83 	time_t now;
     84 	int plusnow;
     85 	char buf[50];
     86 
     87 	if (setvbuf(stdout, NULL, _IONBF, 0) != 0)
     88 		errx(1, "Cannot set stdout to unbuffered.");
     89 
     90 	if (argc < 2) {
     91 		(void)puts("When do you have to leave? ");
     92 		cp = fgets(buf, sizeof(buf), stdin);
     93 		if (cp == NULL || *cp == '\n')
     94 			exit(0);
     95 	} else
     96 		cp = argv[1];
     97 
     98 	if (*cp == '+') {
     99 		plusnow = 1;
    100 		++cp;
    101 	} else {
    102 		plusnow = 0;
    103 		(void)time(&now);
    104 		t = localtime(&now);
    105 	}
    106 
    107 	for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
    108 		if (!isdigit((unsigned char)c))
    109 			usage();
    110 		hours = hours * 10 + (c - '0');
    111 	}
    112 	minutes = hours % 100;
    113 	hours /= 100;
    114 
    115 	if (minutes < 0 || minutes > 59)
    116 		usage();
    117 	if (plusnow)
    118 		secs = (hours * HOUR) + (minutes * MINUTE);
    119 	else {
    120 		if (hours > 23)
    121 			usage();
    122 		if (t->tm_hour >= 12)
    123 			t->tm_hour -= 12;
    124 		if (hours >= 12)
    125 			hours -= 12;
    126 		if (t->tm_hour > hours ||
    127 		    (t->tm_hour == hours && minutes <= t->tm_min))
    128 			hours += 12;
    129 		secs = (hours - t->tm_hour) * HOUR;
    130 		secs += (minutes - t->tm_min) * MINUTE;
    131 	}
    132 	doalarm(secs);
    133 	exit(0);
    134 }
    135 
    136 static void
    137 doalarm(u_int secs)
    138 {
    139 	int bother;
    140 	time_t daytime;
    141 
    142 	switch (fork()) {
    143 	case 0:
    144 		break;
    145 	case -1:
    146 		err(1, "Fork failed");
    147 		/*NOTREACHED*/
    148 	default:
    149 		exit(0);
    150 	}
    151 
    152 	(void)time(&daytime);
    153 	daytime += secs;
    154 	printf("Alarm set for %.16s. (pid %u)\n",
    155 	    ctime(&daytime), (unsigned)getpid());
    156 
    157 	/*
    158 	 * if write fails, we've lost the terminal through someone else
    159 	 * causing a vhangup by logging in.
    160 	 */
    161 #define	FIVEMIN	(5 * MINUTE)
    162 	if (secs >= FIVEMIN) {
    163 		sleep(secs - FIVEMIN);
    164 		if (puts("\07\07You have to leave in 5 minutes.\n") == EOF)
    165 			exit(0);
    166 		secs = FIVEMIN;
    167 	}
    168 
    169 #define	ONEMIN	(MINUTE)
    170 	if (secs >= ONEMIN) {
    171 		sleep(secs - ONEMIN);
    172 		if (puts("\07\07Just one more minute!\n") == EOF)
    173 			exit(0);
    174 	}
    175 
    176 	for (bother = 10; bother--;) {
    177 		sleep((u_int)ONEMIN);
    178 		if (puts("\07\07Time to leave!\n") == EOF)
    179 			exit(0);
    180 	}
    181 
    182 	(void)puts("\07\07That was the last time I'll tell you.  Bye.\n");
    183 	exit(0);
    184 }
    185 
    186 static void
    187 usage(void)
    188 {
    189 	(void)fprintf(stderr, "Usage: %s [[+]hhmm]\n", getprogname());
    190 	exit(1);
    191 }
    192