more ANSI fixes
[unix-history] / usr / src / libexec / getty / main.c
index 91d3590..09ba98f 100644 (file)
@@ -1,18 +1,37 @@
-#ifndef lint
-static char sccsid[] = "@(#)main.c     4.6 (Berkeley) 83/12/13";
-#endif
-
-/*
- * getty -- adapt to terminal speed on dialup, and call login
+/*-
+ * Copyright (c) 1980 The Regents of the University of California.
+ * All rights reserved.
  *
  *
- * Melbourne getty, June 83, kre.
+ * %sccs.include.redist.c%
  */
 
  */
 
-#include <sgtty.h>
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)main.c     5.14 (Berkeley) %G%";
+#endif /* not lint */
+
+#define USE_OLD_TTY
+
+#include <sys/param.h>
+#include <sys/stat.h>
 #include <signal.h>
 #include <signal.h>
+#include <fcntl.h>
+#include <sgtty.h>
+#include <time.h>
 #include <ctype.h>
 #include <setjmp.h>
 #include <ctype.h>
 #include <setjmp.h>
+#include <syslog.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
 #include "gettytab.h"
 #include "gettytab.h"
+#include "pathnames.h"
 
 struct sgttyb tmode = {
        0, 0, CERASE, CKILL, 0
 
 struct sgttyb tmode = {
        0, 0, CERASE, CKILL, 0
@@ -26,14 +45,14 @@ struct      ltchars ltc = {
        CFLUSH, CWERASE, CLNEXT
 };
 
        CFLUSH, CWERASE, CLNEXT
 };
 
-int    crmod;
-int    upper;
-int    lower;
-int    digit;
+int crmod, digit, lower, upper;
 
 
-char   hostname[32];
+char   hostname[MAXHOSTNAMELEN];
 char   name[16];
 char   name[16];
+char   dev[] = _PATH_DEV;
+char   ttyn[32];
 char   *portselector();
 char   *portselector();
+char   *ttyname();
 
 #define        OBUFSIZ         128
 #define        TABBUFSIZ       512
 
 #define        OBUFSIZ         128
 #define        TABBUFSIZ       512
@@ -70,6 +89,7 @@ char partab[] = {
 
 jmp_buf timeout;
 
 
 jmp_buf timeout;
 
+static void
 dingdong()
 {
 
 dingdong()
 {
 
@@ -80,6 +100,7 @@ dingdong()
 
 jmp_buf        intrupt;
 
 
 jmp_buf        intrupt;
 
+static void
 interrupt()
 {
 
 interrupt()
 {
 
@@ -88,18 +109,56 @@ interrupt()
 }
 
 main(argc, argv)
 }
 
 main(argc, argv)
-       char *argv[];
+       int argc;
+       char **argv;
 {
 {
+       extern  char **environ;
        char *tname;
        long allflags;
        char *tname;
        long allflags;
+       int repcnt = 0;
 
        signal(SIGINT, SIG_IGN);
 /*
        signal(SIGQUIT, SIG_DFL);
 */
 
        signal(SIGINT, SIG_IGN);
 /*
        signal(SIGQUIT, SIG_DFL);
 */
+       openlog("getty", LOG_ODELAY|LOG_CONS, LOG_AUTH);
        gethostname(hostname, sizeof(hostname));
        if (hostname[0] == '\0')
                strcpy(hostname, "Amnesiac");
        gethostname(hostname, sizeof(hostname));
        if (hostname[0] == '\0')
                strcpy(hostname, "Amnesiac");
+       /*
+        * The following is a work around for vhangup interactions
+        * which cause great problems getting window systems started.
+        * If the tty line is "-", we do the old style getty presuming
+        * that the file descriptors are already set up for us. 
+        * J. Gettys - MIT Project Athena.
+        */
+       if (argc <= 2 || strcmp(argv[2], "-") == 0)
+           strcpy(ttyn, ttyname(0));
+       else {
+           int i;
+
+           strcpy(ttyn, dev);
+           strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
+           if (strcmp(argv[0], "+") != 0) {
+               chown(ttyn, 0, 0);
+               chmod(ttyn, 0600);
+               revoke(ttyn);
+               /*
+                * Delay the open so DTR stays down long enough to be detected.
+                */
+               sleep(2);
+               while ((i = open(ttyn, O_RDWR)) == -1) {
+                       if (repcnt % 10 == 0) {
+                               syslog(LOG_ERR, "%s: %m", ttyn);
+                               closelog();
+                       }
+                       repcnt++;
+                       sleep(60);
+               }
+               login_tty(i);
+           }
+       }
+
        gettable("default", defent, defstrs);
        gendefaults();
        tname = "default";
        gettable("default", defent, defstrs);
        gendefaults();
        tname = "default";
@@ -107,12 +166,15 @@ main(argc, argv)
                tname = argv[1];
        for (;;) {
                int ldisp = OTTYDISC;
                tname = argv[1];
        for (;;) {
                int ldisp = OTTYDISC;
+               int off = 0;
 
                gettable(tname, tabent, tabstrs);
                if (OPset || EPset || APset)
                        APset++, OPset++, EPset++;
                setdefaults();
                ioctl(0, TIOCFLUSH, 0);         /* clear out the crap */
 
                gettable(tname, tabent, tabstrs);
                if (OPset || EPset || APset)
                        APset++, OPset++, EPset++;
                setdefaults();
                ioctl(0, TIOCFLUSH, 0);         /* clear out the crap */
+               ioctl(0, FIONBIO, &off);        /* turn off non-blocking mode */
+               ioctl(0, FIOASYNC, &off);       /* ditto for async mode */
                if (IS)
                        tmode.sg_ispeed = speed(IS);
                else if (SP)
                if (IS)
                        tmode.sg_ispeed = speed(IS);
                else if (SP)
@@ -125,9 +187,14 @@ main(argc, argv)
                ioctl(0, TIOCSETP, &tmode);
                setchars();
                ioctl(0, TIOCSETC, &tc);
                ioctl(0, TIOCSETP, &tmode);
                setchars();
                ioctl(0, TIOCSETC, &tc);
-               ioctl(0, TIOCSETD, &ldisp);
                if (HC)
                        ioctl(0, TIOCHPCL, 0);
                if (HC)
                        ioctl(0, TIOCHPCL, 0);
+               if (AB) {
+                       extern char *autobaud();
+
+                       tname = autobaud();
+                       continue;
+               }
                if (PS) {
                        tname = portselector();
                        continue;
                if (PS) {
                        tname = portselector();
                        continue;
@@ -147,8 +214,15 @@ main(argc, argv)
                        alarm(TO);
                }
                if (getname()) {
                        alarm(TO);
                }
                if (getname()) {
+                       register int i;
+
+                       oflush();
                        alarm(0);
                        signal(SIGALRM, SIG_DFL);
                        alarm(0);
                        signal(SIGALRM, SIG_DFL);
+                       if (name[0] == '-') {
+                               puts("user names may not start with '-'.");
+                               continue;
+                       }
                        if (!(upper || lower || digit))
                                continue;
                        allflags = setflags(2);
                        if (!(upper || lower || digit))
                                continue;
                        allflags = setflags(2);
@@ -163,11 +237,18 @@ main(argc, argv)
                        ioctl(0, TIOCSETP, &tmode);
                        ioctl(0, TIOCSLTC, &ltc);
                        ioctl(0, TIOCLSET, &allflags);
                        ioctl(0, TIOCSETP, &tmode);
                        ioctl(0, TIOCSLTC, &ltc);
                        ioctl(0, TIOCLSET, &allflags);
-                       putchr('\n');
-                       oflush();
-                       makeenv(env);
                        signal(SIGINT, SIG_DFL);
                        signal(SIGINT, SIG_DFL);
-                       execle(LO, "login", name, (char *)0, env);
+                       for (i = 0; environ[i] != (char *)0; i++)
+                               env[i] = environ[i];
+                       makeenv(&env[i]);
+
+                       /* 
+                        * this is what login was doing anyway.
+                        * soon we rewrite getty completely.
+                        */
+                       set_ttydefaults(0);
+                       execle(LO, "login", "-p", name, (char *) 0, env);
+                       syslog(LOG_ERR, "%s: %m", LO);
                        exit(1);
                }
                alarm(0);
                        exit(1);
                }
                alarm(0);
@@ -180,8 +261,8 @@ main(argc, argv)
 
 getname()
 {
 
 getname()
 {
+       register int c;
        register char *np;
        register char *np;
-       register c;
        char cs;
 
        /*
        char cs;
 
        /*
@@ -202,27 +283,25 @@ getname()
                PF = 0;
        }
        ioctl(0, TIOCSETP, &tmode);
                PF = 0;
        }
        ioctl(0, TIOCSETP, &tmode);
-       crmod = 0;
-       upper = 0;
-       lower = 0;
-       digit = 0;
+       crmod = digit = lower = upper = 0;
        np = name;
        for (;;) {
                oflush();
        np = name;
        for (;;) {
                oflush();
-               if (read(0, &cs, 1) <= 0)
+               if (read(STDIN_FILENO, &cs, 1) <= 0)
                        exit(0);
                if ((c = cs&0177) == 0)
                        return (0);
                if (c == EOT)
                        exit(1);
                        exit(0);
                if ((c = cs&0177) == 0)
                        return (0);
                if (c == EOT)
                        exit(1);
-               if (c == '\r' || c == '\n' || np >= &name[16])
+               if (c == '\r' || c == '\n' || np >= &name[sizeof name]) {
+                       putf("\r\n");
                        break;
                        break;
-
-               if (c >= 'a' && c <= 'z')
-                       lower++;
-               else if (c >= 'A' && c <= 'Z') {
-                       upper++;
-               else if (c == ERASE || c == '#' || c == '\b') {
+               }
+               if (islower(c))
+                       lower = 1;
+               else if (isupper(c))
+                       upper = 1;
+               else if (c == ERASE || c == '#' || c == '\b') {
                        if (np > name) {
                                np--;
                                if (tmode.sg_ospeed >= B1200)
                        if (np > name) {
                                np--;
                                if (tmode.sg_ospeed >= B1200)
@@ -242,11 +321,9 @@ getname()
                        prompt();
                        np = name;
                        continue;
                        prompt();
                        np = name;
                        continue;
-               } else if (c == ' ')
-                       c = '_';
-               else if (c >= '0' && c <= '9')
+               } else if (isdigit(c))
                        digit++;
                        digit++;
-               if (IG && (c < ' ' || c > 0176))
+               if (IG && (c <= ' ' || c > 0176))
                        continue;
                *np++ = c;
                putchr(cs);
                        continue;
                *np++ = c;
                putchr(cs);
@@ -254,7 +331,7 @@ getname()
        signal(SIGINT, SIG_IGN);
        *np = 0;
        if (c == '\r')
        signal(SIGINT, SIG_IGN);
        *np = 0;
        if (c == '\r')
-               crmod++;
+               crmod = 1;
        if (upper && !lower && !LC || UC)
                for (np = name; *np; np++)
                        if (isupper(*np))
        if (upper && !lower && !LC || UC)
                for (np = name; *np; np++)
                        if (isupper(*np))
@@ -297,11 +374,10 @@ putpad(s)
                return;
 
        /*
                return;
 
        /*
-        * Round up by a half a character frame,
-        * and then do the delay.
+        * Round up by a half a character frame, and then do the delay.
         * Too bad there are no user program accessible programmed delays.
         * Too bad there are no user program accessible programmed delays.
-        * Transmitting pad characters slows many
-        * terminals down and also loads the system.
+        * Transmitting pad characters slows many terminals down and also
+        * loads the system.
         */
        mspc10 = tmspc10[tmode.sg_ospeed];
        pad += mspc10 / 2;
         */
        mspc10 = tmspc10[tmode.sg_ospeed];
        pad += mspc10 / 2;
@@ -312,7 +388,6 @@ putpad(s)
 puts(s)
        register char *s;
 {
 puts(s)
        register char *s;
 {
-
        while (*s)
                putchr(*s++);
 }
        while (*s)
                putchr(*s++);
 }
@@ -333,13 +408,13 @@ putchr(cc)
                if (obufcnt >= OBUFSIZ)
                        oflush();
        } else
                if (obufcnt >= OBUFSIZ)
                        oflush();
        } else
-               write(1, &c, 1);
+               write(STDOUT_FILENO, &c, 1);
 }
 
 oflush()
 {
        if (obufcnt)
 }
 
 oflush()
 {
        if (obufcnt)
-               write(1, outbuf, obufcnt);
+               write(STDOUT_FILENO, outbuf, obufcnt);
        obufcnt = 0;
 }
 
        obufcnt = 0;
 }
 
@@ -354,9 +429,9 @@ prompt()
 putf(cp)
        register char *cp;
 {
 putf(cp)
        register char *cp;
 {
-       char *tp;
        extern char editedhost[];
        extern char editedhost[];
-       extern char *ttyname();
+       time_t t;
+       char *slash, db[100];
 
        while (*cp) {
                if (*cp != '%') {
 
        while (*cp) {
                if (*cp != '%') {
@@ -365,18 +440,27 @@ putf(cp)
                }
                switch (*++cp) {
 
                }
                switch (*++cp) {
 
+               case 't':
+                       slash = rindex(ttyn, '/');
+                       if (slash == (char *) 0)
+                               puts(ttyn);
+                       else
+                               puts(&slash[1]);
+                       break;
+
                case 'h':
                        puts(editedhost);
                        break;
 
                case 'h':
                        puts(editedhost);
                        break;
 
-               case 't':
-                       tp = ttyname(0);
-                       if (tp != (char *)0) {
-                               if (strncmp(tp, "/dev/", 5) == 0)
-                                       tp += 5;
-                               puts(tp);
-                       }
+               case 'd': {
+                       char fmt[] = "%l:% %P on %A, %d %B %Y";
+
+                       fmt[4] = 'M';           /* I *hate* SCCS... */
+                       (void)time(&t);
+                       (void)strftime(db, sizeof(db), fmt, localtime(&t));
+                       puts(db);
                        break;
                        break;
+               }
 
                case '%':
                        putchr('%');
 
                case '%':
                        putchr('%');