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

1.15    ! deraadt     1: /*     $OpenBSD: leave.c,v 1.14 2014/11/26 18:34:51 millert 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.
1.9       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #include <sys/time.h>
1.11      otto       34: #include <ctype.h>
                     35: #include <err.h>
                     36: #include <stdio.h>
                     37: #include <stdlib.h>
1.3       deraadt    38: #include <unistd.h>
1.1       deraadt    39:
1.11      otto       40: static __dead void     usage(void);
                     41: static void            doalarm(u_int secs);
                     42:
                     43: #define SECOND  1
                     44: #define MINUTE  (SECOND * 60)
                     45: #define        FIVEMIN (5 * MINUTE)
                     46: #define HOUR    (MINUTE * 60)
1.3       deraadt    47:
1.1       deraadt    48: /*
                     49:  * leave [[+]hhmm]
                     50:  *
                     51:  * Reminds you when you have to leave.
                     52:  * Leave prompts for input and goes away if you hit return.
                     53:  * It nags you like a mother hen.
                     54:  */
1.3       deraadt    55: int
1.10      deraadt    56: main(int argc, char *argv[])
1.1       deraadt    57: {
1.6       mpech      58:        u_int secs;
                     59:        int hours, minutes;
                     60:        char c, *cp;
1.3       deraadt    61:        struct tm *t;
                     62:        time_t now;
1.11      otto       63:        int plusnow = 0, twentyfour;
1.1       deraadt    64:        char buf[50];
1.11      otto       65:
1.14      millert    66:        if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
1.11      otto       67:                errx(1, "Cannot set stdout to line buffered.");
1.1       deraadt    68:
                     69:        if (argc < 2) {
1.11      otto       70:                (void)fputs("When do you have to leave? ", stdout);
1.1       deraadt    71:                cp = fgets(buf, sizeof(buf), stdin);
                     72:                if (cp == NULL || *cp == '\n')
                     73:                        exit(0);
1.5       pjanzen    74:        } else if (argc > 2)
                     75:                usage();
                     76:        else
1.1       deraadt    77:                cp = argv[1];
                     78:
                     79:        if (*cp == '+') {
                     80:                plusnow = 1;
                     81:                ++cp;
                     82:        }
                     83:
                     84:        for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
1.13      deraadt    85:                if (!isdigit((unsigned char)c))
1.1       deraadt    86:                        usage();
                     87:                hours = hours * 10 + (c - '0');
                     88:        }
                     89:        minutes = hours % 100;
                     90:        hours /= 100;
1.11      otto       91:        /* determine 24 hours mode */
                     92:        twentyfour = hours > 12;
1.1       deraadt    93:
                     94:        if (minutes < 0 || minutes > 59)
                     95:                usage();
                     96:        if (plusnow)
1.11      otto       97:                secs = (hours * HOUR) + (minutes * MINUTE);
1.1       deraadt    98:        else {
1.3       deraadt    99:                if (hours > 23)
1.1       deraadt   100:                        usage();
1.11      otto      101:                (void)time(&now);
                    102:                t = localtime(&now);
                    103:                while (t->tm_hour > hours ||
1.4       espie     104:                    (t->tm_hour == hours && t->tm_min >= minutes)) {
1.11      otto      105:                        if (twentyfour)
1.4       espie     106:                                hours += 24;
                    107:                        else
                    108:                                hours += 12;
                    109:                }
                    110:
1.11      otto      111:                secs = (hours - t->tm_hour) * HOUR;
                    112:                secs += (minutes - t->tm_min) * MINUTE;
1.1       deraadt   113:        }
                    114:        doalarm(secs);
                    115:        exit(0);
                    116: }
                    117:
1.11      otto      118: static void
1.10      deraadt   119: doalarm(u_int secs)
1.1       deraadt   120: {
1.6       mpech     121:        int bother;
1.3       deraadt   122:        time_t daytime;
1.8       mpech     123:        pid_t pid;
1.1       deraadt   124:
1.11      otto      125:        switch (pid = fork()) {
                    126:        case 0:
                    127:                break;
                    128:        case -1:
                    129:                err(1, "Fork failed");
                    130:                /* NOTREACHED */
                    131:        default:
1.1       deraadt   132:                (void)time(&daytime);
                    133:                daytime += secs;
1.8       mpech     134:                printf("Alarm set for %.16s. (pid %ld)\n",
                    135:                    ctime(&daytime), (long)pid);
1.1       deraadt   136:                exit(0);
                    137:        }
1.11      otto      138:        sleep(2);                       /* let parent print set message */
1.1       deraadt   139:
                    140:        /*
                    141:         * if write fails, we've lost the terminal through someone else
                    142:         * causing a vhangup by logging in.
                    143:         */
                    144:        if (secs >= FIVEMIN) {
                    145:                sleep(secs - FIVEMIN);
1.11      otto      146:                if (puts("\a\aYou have to leave in 5 minutes.") == EOF)
1.1       deraadt   147:                        exit(0);
                    148:                secs = FIVEMIN;
                    149:        }
                    150:
1.11      otto      151:        if (secs >= MINUTE) {
                    152:                sleep(secs - MINUTE);
                    153:                if (puts("\a\aJust one more minute!") == EOF)
1.1       deraadt   154:                        exit(0);
                    155:        }
                    156:
                    157:        for (bother = 10; bother--;) {
1.11      otto      158:                sleep(MINUTE);
                    159:                if (puts("\a\aTime to leave!") == EOF)
1.1       deraadt   160:                        exit(0);
                    161:        }
                    162:
1.11      otto      163:        puts("\a\aThat was the last time I'll tell you.  Bye.");
1.1       deraadt   164:        exit(0);
                    165: }
                    166:
1.11      otto      167: static __dead void
1.10      deraadt   168: usage(void)
1.1       deraadt   169: {
                    170:        fprintf(stderr, "usage: leave [[+]hhmm]\n");
                    171:        exit(1);
                    172: }