[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.4

1.4     ! espie       1: /*     $OpenBSD: leave.c,v 1.3 1997/09/08 09:34:44 deraadt 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.4     ! espie      47: static char rcsid[] = "$OpenBSD: leave.c,v 1.3 1997/09/08 09:34:44 deraadt Exp $";
1.1       deraadt    48: #endif /* not lint */
                     49:
                     50: #include <sys/param.h>
                     51: #include <sys/time.h>
1.3       deraadt    52: #include <unistd.h>
                     53: #include <stdlib.h>
1.1       deraadt    54: #include <stdio.h>
                     55: #include <ctype.h>
                     56:
1.3       deraadt    57: void   usage __P((void));
                     58: void   doalarm __P((u_int secs));
                     59:
1.1       deraadt    60: /*
                     61:  * leave [[+]hhmm]
                     62:  *
                     63:  * Reminds you when you have to leave.
                     64:  * Leave prompts for input and goes away if you hit return.
                     65:  * It nags you like a mother hen.
                     66:  */
1.3       deraadt    67: int
1.1       deraadt    68: main(argc, argv)
                     69:        int argc;
                     70:        char **argv;
                     71: {
                     72:        register u_int secs;
                     73:        register int hours, minutes;
                     74:        register char c, *cp;
1.3       deraadt    75:        struct tm *t;
                     76:        time_t now;
1.1       deraadt    77:        int plusnow;
                     78:        char buf[50];
                     79:
                     80:        if (argc < 2) {
                     81: #define        MSG1    "When do you have to leave? "
1.3       deraadt    82:                (void)write(STDOUT_FILENO, MSG1, sizeof(MSG1) - 1);
1.1       deraadt    83:                cp = fgets(buf, sizeof(buf), stdin);
                     84:                if (cp == NULL || *cp == '\n')
                     85:                        exit(0);
                     86:        } else
                     87:                cp = argv[1];
                     88:
                     89:        if (*cp == '+') {
                     90:                plusnow = 1;
                     91:                ++cp;
                     92:        } else {
                     93:                plusnow = 0;
                     94:                (void)time(&now);
                     95:                t = localtime(&now);
                     96:        }
                     97:
                     98:        for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
                     99:                if (!isdigit(c))
                    100:                        usage();
                    101:                hours = hours * 10 + (c - '0');
                    102:        }
                    103:        minutes = hours % 100;
                    104:        hours /= 100;
                    105:
                    106:        if (minutes < 0 || minutes > 59)
                    107:                usage();
                    108:        if (plusnow)
                    109:                secs = hours * 60 * 60 + minutes * 60;
                    110:        else {
1.3       deraadt   111:                if (hours > 23)
1.1       deraadt   112:                        usage();
1.4     ! espie     113:                if (t->tm_hour > hours ||
        !           114:                    (t->tm_hour == hours && t->tm_min >= minutes)) {
        !           115:                        /* determine 24 hours mode */
        !           116:                        if (hours >= 13)
        !           117:                                hours += 24;
        !           118:                        else
        !           119:                                hours += 12;
        !           120:                }
        !           121:
1.1       deraadt   122:                secs = (hours - t->tm_hour) * 60 * 60;
                    123:                secs += (minutes - t->tm_min) * 60;
                    124:        }
                    125:        doalarm(secs);
                    126:        exit(0);
                    127: }
                    128:
1.3       deraadt   129: void
1.1       deraadt   130: doalarm(secs)
                    131:        u_int secs;
                    132: {
                    133:        register int bother;
1.3       deraadt   134:        time_t daytime;
1.1       deraadt   135:        int pid;
                    136:
1.3       deraadt   137:        if ((pid = fork())) {
1.1       deraadt   138:                (void)time(&daytime);
                    139:                daytime += secs;
                    140:                printf("Alarm set for %.16s. (pid %d)\n",
                    141:                    ctime(&daytime), pid);
                    142:                exit(0);
                    143:        }
                    144:        sleep((u_int)2);                /* let parent print set message */
                    145:
                    146:        /*
                    147:         * if write fails, we've lost the terminal through someone else
                    148:         * causing a vhangup by logging in.
                    149:         */
                    150: #define        FIVEMIN (5 * 60)
                    151: #define        MSG2    "\07\07You have to leave in 5 minutes.\n"
                    152:        if (secs >= FIVEMIN) {
                    153:                sleep(secs - FIVEMIN);
1.3       deraadt   154:                if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
1.1       deraadt   155:                        exit(0);
                    156:                secs = FIVEMIN;
                    157:        }
                    158:
                    159: #define        ONEMIN  (60)
                    160: #define        MSG3    "\07\07Just one more minute!\n"
                    161:        if (secs >= ONEMIN) {
                    162:                sleep(secs - ONEMIN);
1.3       deraadt   163:                if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
1.1       deraadt   164:                        exit(0);
                    165:        }
                    166:
                    167: #define        MSG4    "\07\07Time to leave!\n"
                    168:        for (bother = 10; bother--;) {
                    169:                sleep((u_int)ONEMIN);
1.3       deraadt   170:                if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
1.1       deraadt   171:                        exit(0);
                    172:        }
                    173:
                    174: #define        MSG5    "\07\07That was the last time I'll tell you.  Bye.\n"
1.3       deraadt   175:        (void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
1.1       deraadt   176:        exit(0);
                    177: }
                    178:
1.3       deraadt   179: void
1.1       deraadt   180: usage()
                    181: {
                    182:        fprintf(stderr, "usage: leave [[+]hhmm]\n");
                    183:        exit(1);
                    184: }