# 2.11BSD#431 - /usr/src/ucb/leave.c

##### Functions

    bother            X
    delay             X
    doalarm           X
    main              X
    usage             X

##### Variables

    buff              X
    copyright         X
    origlogin         X
    whenleave         X

       1: /*
       2:  * Copyright (c) 1980 Regents of the University of California.
       3:  * All rights reserved.  The Berkeley software License Agreement
       4:  * specifies the terms and conditions for redistribution.
       5:  */
       6:
       7: #if !defined(lint) && defined(DOSCCS)
       8: char copyright[] =
       9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
      10:  All rights reserved.\n";
      11:
      12: static char sccsid[] = "@(#)leave.c	5.1.1 (2.11BSD) 1996/11/17";
      13: #endif
      14:
      15: #include <stdio.h>
      16: #include <signal.h>
      17: #include <time.h>
      18: #include <stdlib.h>
      19: #include <unistd.h>
      20:
      21: /*
      22:  * leave [[+]hhmm]
      23:  *
      24:  * Reminds you when you have to leave.
      25:  * Leave prompts for input and goes away if you hit return.
      26:  * It nags you like a mother hen.
      27:  */
      28: char origlogin[20];
      29: char *whenleave;
      30: char buff[100];
      31:
      32: main(argc, argv)
      33: char **argv;
      34: {
      35:     long when, tod, now, diff, hours, minutes;
      36:     char *cp;
      37:     register struct tm *nv;
      38:
      39:     strcpy(origlogin, getlogin());
      40:     if (argc < 2) {
      41:         printf("When do you have to leave? ");
      42:         fflush(stdout);
      43:         buff[read(0, buff, sizeof buff)] = 0;
      44:         cp = buff;
      45:     } else
      46:         cp = argv[1];
      47:     if (*cp == '\n')
      48:         exit(0);
      49:     if (*cp == '+') {
      50:         cp++;
      51:         if (*cp < '0' || *cp > '9')
      52:             usage();
      53:         tod = atoi(cp);
      54:         hours = tod / 100;
      55:         minutes = tod % 100;
      56:         if (minutes < 0 || minutes > 59)
      57:             usage();
      58:         diff = 60*hours+minutes;
      59:         doalarm(diff);
      60:         exit(0);
      61:     }
      62:     if (*cp < '0' || *cp > '9')
      63:         usage();
      64:     tod = atoi(cp);
      65:     hours = tod / 100;
      66:     if (hours > 12)
      67:         hours -= 12;
      68:     if (hours == 12)
      69:         hours = 0;
      70:     minutes = tod % 100;
      71:
      72:     if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59)
      73:         usage();
      74:
      75:     time(&now);
      76:     nv = localtime(&now);
      77:     when = 60*hours+minutes;
      78:     if (nv->tm_hour > 12)
      79:         nv->tm_hour -= 12;  /* do am/pm bit */
      80:     now = (60 * nv->tm_hour) + nv->tm_min;
      81:     diff = when - now;
      82:     while (diff < 0)
      83:         diff += 12*60;
      84:     if (diff > 11*60) {
      85:         printf("That time has already passed!\n");
      86:         exit(1);
      87:     }
      88:     doalarm(diff);
      89:     exit(0);
      90: }
      91:
      92: usage()
      93: {
      94:     printf("usage: leave [[+]hhmm]\n");
      95:     exit(1);
      96: }
      97:
      98: doalarm(nmins)
      99: long nmins;
     100: {
     101:     char *msg1, *msg2, *msg3, *msg4;
     102:     register int i;
     103:     int slp1, slp2, slp3, slp4;
     104:     int seconds, gseconds;
     105:     long daytime;
     106:
     107:     seconds = 60 * nmins;
     108:     if (seconds <= 0)
     109:         seconds = 1;
     110:     gseconds = seconds;
     111:
     112:     msg1 = "You have to leave in 5 minutes";
     113:     if (seconds <= 60*5) {
     114:         slp1 = 0;
     115:     } else {
     116:         slp1 = seconds - 60*5;
     117:         seconds = 60*5;
     118:     }
     119:
     120:     msg2 = "Just one more minute!";
     121:     if (seconds <= 60) {
     122:         slp2 = 0;
     123:     } else {
     124:         slp2 = seconds - 60;
     125:         seconds = 60;
     126:     }
     127:
     128:     msg3 = "Time to leave!";
     129:     slp3 = seconds;
     130:
     131:     msg4 = "You're going to be late!";
     132:     slp4 = 60;
     133:
     134:     time(&daytime);
     135:     daytime += gseconds;
     136:     whenleave = ctime(&daytime);
     137:     printf("Alarm set for %s", whenleave);
     138:     if (fork())
     139:         exit(0);
     140:     signal(SIGINT, SIG_IGN);
     141:     signal(SIGQUIT, SIG_IGN);
     142:     signal(SIGTERM, SIG_IGN);
     143:     signal(SIGTTOU, SIG_IGN);
     144:
     145:     if (slp1)
     146:         bother(slp1, msg1);
     147:     if (slp2)
     148:         bother(slp2, msg2);
     149:     bother(slp3, msg3);
     150:     for (i = 0; i < 10; i++)
     151:         bother(slp4, msg4);
     152:     printf("That was the last time I'll tell you. Bye.\n");
     153:     exit(0);
     154: }
     155:
     156: bother(slp, msg)
     157: int slp;
     158: char *msg;
     159: {
     160:
     161:     delay(slp);
     162:     printf("\7\7\7%s\n", msg);
     163: }
     164:
     165: /*
     166:  * delay is like sleep but does it in 100 sec pieces and
     167:  * knows what zero means.
     168:  */
     169: delay(secs)
     170: int secs;
     171: {
     172:     int n;
     173:
     174:     while (secs > 0) {
     175:         n = 100;
     176:         if (secs < n)
     177:             n = secs;
     178:         secs -= n;
     179:         if (n > 0)
     180:             sleep(n);
     181:         if (strcmp(origlogin, getlogin()))
     182:             exit(0);
     183:     }
     184: }

#### Defined functions

bother defined in line 156; used 4 times

- in line 146-151(4)

delay defined in line 169; used 1 times

- in line 161

doalarm defined in line 98; used 2 times

- in line 59, 88

main defined in line 32; never used
usage defined in line 92; used 4 times

- in line 52-57(2), 63, 73

#### Defined variables

buff defined in line 30; used 4 times

- in line 43-44(4)

copyright defined in line 8; never used
origlogin defined in line 28; used 2 times

- in line 39, 181

sccsid defined in line 12; never used
whenleave defined in line 29; used 2 times

- in line 136-137(2)

Last modified: 1996-11-18 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 702 |