do case insensitive comparison on domain name
[unix-history] / usr / src / usr.bin / leave / leave.c
index 96d2936..d6563e5 100644 (file)
@@ -1,21 +1,35 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1980, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+"@(#) Copyright (c) 1980, 1988 Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)leave.c    5.1 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)leave.c    5.4 (Berkeley) %G%";
+#endif /* not lint */
 
 
+#include <sys/param.h>
+#include <sys/time.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <signal.h>
+#include <ctype.h>
+
 /*
  * leave [[+]hhmm]
  *
 /*
  * leave [[+]hhmm]
  *
@@ -23,179 +37,113 @@ static char sccsid[] = "@(#)leave.c       5.1 (Berkeley) %G%";
  * Leave prompts for input and goes away if you hit return.
  * It nags you like a mother hen.
  */
  * Leave prompts for input and goes away if you hit return.
  * It nags you like a mother hen.
  */
-char origlogin[20];
-char *getlogin();
-char *whenleave;
-char *ctime();
-char buff[100];
-
 main(argc, argv)
 main(argc, argv)
-char **argv;
+       int argc;
+       char **argv;
 {
 {
-       long when, tod, now, diff, hours, minutes;
-       char *cp;
-       int *nv;
-       int atoi();
-       int *localtime();
+       register u_int secs;
+       register int hours, minutes;
+       register char c, *cp;
+       struct tm *t, *localtime();
+       time_t now, time();
+       int plusnow;
+       char buf[50];
 
 
-       strcpy(origlogin, getlogin());
        if (argc < 2) {
        if (argc < 2) {
-               printf("When do you have to leave? ");
-               fflush(stdout);
-               buff[read(0, buff, sizeof buff)] = 0;
-               cp = buff;
+#define        MSG1    "When do you have to leave? "
+               (void)write(1, MSG1, sizeof(MSG1) - 1);
+               cp = fgets(buf, sizeof(buf), stdin);
+               if (*cp == '\n')
+                       exit(0);
        } else
                cp = argv[1];
        } else
                cp = argv[1];
-       if (*cp == '\n')
-               exit(0);
+
        if (*cp == '+') {
        if (*cp == '+') {
-               cp++;
-               if (*cp < '0' || *cp > '9')
-                       usage();
-               tod = atoi(cp);
-               hours = tod / 100;
-               minutes = tod % 100;
-               if (minutes < 0 || minutes > 59)
+               plusnow = 1;
+               ++cp;
+       } else {
+               plusnow = 0;
+               (void)time(&now);
+               t = localtime(&now);
+       }
+
+       for (hours = 0; (c = *cp) && c != '\n'; ++cp) {
+               if (!isdigit(c))
                        usage();
                        usage();
-               diff = 60*hours+minutes;
-               doalarm(diff);
-               exit(0);
+               hours = hours * 10 + (c - '0');
        }
        }
-       if (*cp < '0' || *cp > '9')
-               usage();
-       tod = atoi(cp);
-       hours = tod / 100;
-       if (hours > 12)
-               hours -= 12;
-       if (hours == 12)
-               hours = 0;
-       minutes = tod % 100;
+       minutes = hours % 100;
+       hours /= 100;
 
 
-       if (hours < 0 || hours > 12 || minutes < 0 || minutes > 59)
+       if (minutes < 0 || minutes > 59)
                usage();
                usage();
-
-       time(&now);
-       nv = localtime(&now);
-       when = 60*hours+minutes;
-       if (nv[2] > 12)
-               nv[2] -= 12;    /* do am/pm bit */
-       now = 60*nv[2] + nv[1];
-       diff = when - now;
-       while (diff < 0)
-               diff += 12*60;
-       if (diff > 11*60) {
-               printf("That time has already passed!\n");
-               exit(1);
+       if (plusnow)
+               secs = hours * 60 * 60 + minutes * 60;
+       else {
+               if (hours > 23 || t->tm_hour > hours ||
+                   t->tm_hour == hours && minutes <= t->tm_min)
+                       usage();
+               secs = (hours - t->tm_hour) * 60 * 60;
+               secs += (minutes - t->tm_min) * 60;
        }
        }
-       doalarm(diff);
+       doalarm(secs);
        exit(0);
 }
 
        exit(0);
 }
 
-usage()
-{
-       printf("usage: leave [[+]hhmm]\n");
-       exit(1);
-}
-
-doalarm(nmins)
-long nmins;
+static
+doalarm(secs)
+       u_int secs;
 {
 {
-       char *msg1, *msg2, *msg3, *msg4;
-       register int i;
-       int slp1, slp2, slp3, slp4;
-       int seconds, gseconds;
-       long daytime;
-
-       seconds = 60 * nmins;
-       if (seconds <= 0)
-               seconds = 1;
-       gseconds = seconds;
-
-       msg1 = "You have to leave in 5 minutes";
-       if (seconds <= 60*5) {
-               slp1 = 0;
-       } else {
-               slp1 = seconds - 60*5;
-               seconds = 60*5;
+       register int bother;
+       time_t daytime, time();
+       int pid;
+       char *ctime();
+
+       if (pid = fork()) {
+               (void)time(&daytime);
+               daytime += secs;
+               printf("Alarm set for %.16s. (pid %d)\n",
+                   ctime(&daytime), pid);
+               exit(0);
        }
        }
-
-       msg2 = "Just one more minute!";
-       if (seconds <= 60) {
-               slp2 = 0;
-       } else {
-               slp2 = seconds - 60;
-               seconds = 60;
+       sleep((u_int)2);                /* let parent print set message */
+
+       /*
+        * if write fails, we've lost the terminal through someone else
+        * causing a vhangup by logging in.
+        */
+#define        FIVEMIN (5 * 60)
+#define        MSG2    "\07\07You have to leave in 5 minutes.\n"
+       if (secs >= FIVEMIN) {
+               sleep(secs - FIVEMIN);
+               if (write(1, MSG2, sizeof(MSG2) - 1) != sizeof(MSG2) - 1)
+                       exit(0);
+               secs = FIVEMIN;
        }
 
        }
 
-       msg3 = "Time to leave!";
-       slp3 = seconds;
-
-       msg4 = "You're going to be late!";
-       slp4 = 60;
+#define        ONEMIN  (60)
+#define        MSG3    "\07\07Just one more minute!\n"
+       if (secs >= ONEMIN) {
+               sleep(secs - ONEMIN);
+               if (write(1, MSG3, sizeof(MSG3) - 1) != sizeof(MSG3) - 1)
+                       exit(0);
+       }
 
 
-       time(&daytime);
-       daytime += gseconds;
-       whenleave = ctime(&daytime);
-       printf("Alarm set for %s", whenleave);
-       if (fork())
-               exit(0);
-       signal(SIGINT, SIG_IGN);
-       signal(SIGQUIT, SIG_IGN);
-       signal(SIGTERM, SIG_IGN);
-       signal(SIGTTOU, SIG_IGN);
+#define        MSG4    "\07\07Time to leave!\n"
+       for (bother = 10; bother--;) {
+               sleep((u_int)ONEMIN);
+               if (write(1, MSG4, sizeof(MSG4) - 1) != sizeof(MSG4) - 1)
+                       exit(0);
+       }
 
 
-       if (slp1)
-               bother(slp1, msg1);
-       if (slp2)
-               bother(slp2, msg2);
-       bother(slp3, msg3);
-       for (i = 0; i < 10; i++)
-               bother(slp4, msg4);
-       printf("That was the last time I'll tell you. Bye.\n");
+#define        MSG5    "\07\07That was the last time I'll tell you.  Bye.\n"
+       (void)write(1, MSG5, sizeof(MSG5) - 1);
        exit(0);
 }
 
        exit(0);
 }
 
-bother(slp, msg)
-int slp;
-char *msg;
-{
-
-       delay(slp);
-       printf("\7\7\7%s\n", msg);
-}
-
-/*
- * delay is like sleep but does it in 100 sec pieces and
- * knows what zero means.
- */
-delay(secs)
-int secs;
+static
+usage()
 {
 {
-       int n;
-
-       while (secs > 0) {
-               n = 100;
-               if (secs < n)
-                       n = secs;
-               secs -= n;
-               if (n > 0)
-                       sleep(n);
-               if (strcmp(origlogin, getlogin()))
-                       exit(0);
-       }
-}
-
-#ifdef V6
-char *getlogin() {
-#include <utmp.h>
-
-       static struct utmp ubuf;
-       int ufd;
-
-       ufd = open("/etc/utmp",0);
-       seek(ufd, ttyn(0)*sizeof(ubuf), 0);
-       read(ufd, &ubuf, sizeof(ubuf));
-       ubuf.ut_name[sizeof(ubuf.ut_name)] = 0;
-       return(&ubuf.ut_name);
+       fprintf(stderr, "usage: leave [[+]hhmm]\n");
+       exit(1);
 }
 }
-#endif