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

1.7     ! millert     1: /*     $OpenBSD: leave.c,v 1.6 2001/11/19 19:02:14 mpech 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.7     ! millert    47: static char rcsid[] = "$OpenBSD: leave.c,v 1.6 2001/11/19 19:02:14 mpech 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.7     ! millert    57: void   usage(void);
        !            58: void   doalarm(u_int secs);
1.3       deraadt    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: {
1.6       mpech      72:        u_int secs;
                     73:        int hours, minutes;
                     74:        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);
1.5       pjanzen    86:        } else if (argc > 2)
                     87:                usage();
                     88:        else
1.1       deraadt    89:                cp = argv[1];
                     90:
                     91:        if (*cp == '+') {
                     92:                plusnow = 1;
                     93:                ++cp;
                     94:        } else {
                     95:                plusnow = 0;
                     96:                (void)time(&now);
                     97:                t = localtime(&now);
                     98:        }
                     99:
                    100:        for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
                    101:                if (!isdigit(c))
                    102:                        usage();
                    103:                hours = hours * 10 + (c - '0');
                    104:        }
                    105:        minutes = hours % 100;
                    106:        hours /= 100;
                    107:
                    108:        if (minutes < 0 || minutes > 59)
                    109:                usage();
                    110:        if (plusnow)
                    111:                secs = hours * 60 * 60 + minutes * 60;
                    112:        else {
1.3       deraadt   113:                if (hours > 23)
1.1       deraadt   114:                        usage();
1.4       espie     115:                if (t->tm_hour > hours ||
                    116:                    (t->tm_hour == hours && t->tm_min >= minutes)) {
                    117:                        /* determine 24 hours mode */
                    118:                        if (hours >= 13)
                    119:                                hours += 24;
                    120:                        else
                    121:                                hours += 12;
                    122:                }
                    123:
1.1       deraadt   124:                secs = (hours - t->tm_hour) * 60 * 60;
                    125:                secs += (minutes - t->tm_min) * 60;
                    126:        }
                    127:        doalarm(secs);
                    128:        exit(0);
                    129: }
                    130:
1.3       deraadt   131: void
1.1       deraadt   132: doalarm(secs)
                    133:        u_int secs;
                    134: {
1.6       mpech     135:        int bother;
1.3       deraadt   136:        time_t daytime;
1.1       deraadt   137:        int pid;
                    138:
1.3       deraadt   139:        if ((pid = fork())) {
1.1       deraadt   140:                (void)time(&daytime);
                    141:                daytime += secs;
                    142:                printf("Alarm set for %.16s. (pid %d)\n",
                    143:                    ctime(&daytime), pid);
                    144:                exit(0);
                    145:        }
                    146:        sleep((u_int)2);                /* let parent print set message */
                    147:
                    148:        /*
                    149:         * if write fails, we've lost the terminal through someone else
                    150:         * causing a vhangup by logging in.
                    151:         */
                    152: #define        FIVEMIN (5 * 60)
                    153: #define        MSG2    "\07\07You have to leave in 5 minutes.\n"
                    154:        if (secs >= FIVEMIN) {
                    155:                sleep(secs - FIVEMIN);
1.3       deraadt   156:                if (write(STDOUT_FILENO, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
1.1       deraadt   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);
1.3       deraadt   165:                if (write(STDOUT_FILENO, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
1.1       deraadt   166:                        exit(0);
                    167:        }
                    168:
                    169: #define        MSG4    "\07\07Time to leave!\n"
                    170:        for (bother = 10; bother--;) {
                    171:                sleep((u_int)ONEMIN);
1.3       deraadt   172:                if (write(STDOUT_FILENO, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
1.1       deraadt   173:                        exit(0);
                    174:        }
                    175:
                    176: #define        MSG5    "\07\07That was the last time I'll tell you.  Bye.\n"
1.3       deraadt   177:        (void)write(STDOUT_FILENO, MSG5, sizeof(MSG5) - 1);
1.1       deraadt   178:        exit(0);
                    179: }
                    180:
1.3       deraadt   181: void
1.1       deraadt   182: usage()
                    183: {
                    184:        fprintf(stderr, "usage: leave [[+]hhmm]\n");
                    185:        exit(1);
                    186: }