[BACK]Return to leave.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / leave

Annotation of src/usr.bin/leave/leave.c, Revision 1.2

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