use syslog for errors, use getopt
[unix-history] / usr / src / usr.bin / uucp / acucntrl / acucntrl.c
index 7bf4746..4735e2c 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)acucntrl.c 5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)acucntrl.c 5.11 (Berkeley) %G%";
 #endif
 
 /*  acucntrl - turn around tty line between dialin and dialout
 #endif
 
 /*  acucntrl - turn around tty line between dialin and dialout
@@ -46,20 +46,24 @@ static char sccsid[] = "@(#)acucntrl.c      5.4 (Berkeley) %G%";
 /* #define SENSECARRIER */
 
 #include "uucp.h"
 /* #define SENSECARRIER */
 
 #include "uucp.h"
+#ifdef DIALINOUT
 #include <sys/buf.h>
 #include <signal.h>
 #include <sys/conf.h>
 #include <sys/buf.h>
 #include <signal.h>
 #include <sys/conf.h>
+#ifdef vax
 #ifdef BSD4_2
 #include <vaxuba/ubavar.h>
 #else
 #include <sys/ubavar.h>
 #endif
 #ifdef BSD4_2
 #include <vaxuba/ubavar.h>
 #else
 #include <sys/ubavar.h>
 #endif
+#endif /* vax */
 #include <sys/stat.h>
 #include <nlist.h>
 #include <sgtty.h>
 #include <utmp.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <nlist.h>
 #include <sgtty.h>
 #include <utmp.h>
 #include <pwd.h>
 #include <stdio.h>
+#include <sys/file.h>
 
 #define NDZLINE        8       /* lines/dz */
 #define NDHLINE        16      /* lines/dh */
 
 #define NDZLINE        8       /* lines/dz */
 #define NDHLINE        16      /* lines/dh */
@@ -111,6 +115,7 @@ struct nlist nl[] = {
 char Etcutmp[] = "/etc/utmp";
 char Etcttys[] = "/etc/ttys";
 #ifdef BSD4_3
 char Etcutmp[] = "/etc/utmp";
 char Etcttys[] = "/etc/ttys";
 #ifdef BSD4_3
+FILE *ttysfile, *nttysfile;
 char NEtcttys[] = "/etc/ttys.new";
 extern long ftell();
 #endif BSD4_3
 char NEtcttys[] = "/etc/ttys.new";
 extern long ftell();
 #endif BSD4_3
@@ -170,8 +175,10 @@ int argc; char *argv[];
 
        opnttys(device);
 
 
        opnttys(device);
 
+#ifdef vax
        /* Get nlist info */
        nlist("/vmunix", nl);
        /* Get nlist info */
        nlist("/vmunix", nl);
+#endif vax
 
        /* Chdir to /dev */
        if(chdir(Devhome) < 0) {
 
        /* Chdir to /dev */
        if(chdir(Devhome) < 0) {
@@ -226,13 +233,23 @@ int argc; char *argv[];
                exit(2);
        }
 
                exit(2);
        }
 
+#ifndef sequent
        /* Disable modem control */
        if (setmodem(device, DISABLE) < 0) {
                fprintf(stderr, "Unable to disable modem control\n");
                exit(1);
        }
        /* Disable modem control */
        if (setmodem(device, DISABLE) < 0) {
                fprintf(stderr, "Unable to disable modem control\n");
                exit(1);
        }
+#endif !sequent
 
        if (enable) {
 
        if (enable) {
+#ifdef sequent
+               if (setmodem(device, ENABLE) < 0) {
+                       fprintf(stderr, "Cannot Enable modem control\n");
+                       (void)setmodem(device, i);
+                       exit(1);
+               }
+#endif sequent
+#ifndef sequent
                if((devfile = open(device, 1)) < 0) {
                        fprintf(stderr, "On open of %s: %s\n",
                                device, sys_errlist[errno]);
                if((devfile = open(device, 1)) < 0) {
                        fprintf(stderr, "On open of %s: %s\n",
                                device, sys_errlist[errno]);
@@ -254,13 +271,16 @@ int argc; char *argv[];
                            "Cannot set hangup on close on %s: %s\n",
                                device, sys_errlist[errno]);
 
                            "Cannot set hangup on close on %s: %s\n",
                                device, sys_errlist[errno]);
 
+#endif !sequent
                i = resetmodem;
 
                i = resetmodem;
 
+#ifndef sequent
                if (setmodem(device, ENABLE) < 0) {
                        fprintf(stderr, "Cannot Enable modem control\n");
                        (void)setmodem(device, i);
                        exit(1);
                }
                if (setmodem(device, ENABLE) < 0) {
                        fprintf(stderr, "Cannot Enable modem control\n");
                        (void)setmodem(device, i);
                        exit(1);
                }
+#endif sequent
                resetmodem=i;
 
                if (settys(ENABLE)) {
                resetmodem=i;
 
                if (settys(ENABLE)) {
@@ -309,7 +329,14 @@ int argc; char *argv[];
                        pokeinit(device, Uname, enable);
                }
                post(device, Uname);
                        pokeinit(device, Uname, enable);
                }
                post(device, Uname);
-               if((devfile = open(device, 1)) < 0) {
+#ifdef sequent
+       /* Disable modem control */
+       if (setmodem(device, DISABLE) < 0) {
+               fprintf(stderr, "Unable to disable modem control\n");
+               exit(1);
+       }
+#endif sequent
+               if((devfile = open(device, O_RDWR|O_NDELAY)) < 0) {
                        fprintf(stderr, "On %s open: %s\n",
                                device, sys_errlist[errno]);
                } else {
                        fprintf(stderr, "On %s open: %s\n",
                                device, sys_errlist[errno]);
                } else {
@@ -341,10 +368,10 @@ char *device, *name;
        (void)time((time_t *)&utmp.ut_time);
        strncpy(utmp.ut_line, device, LINSIZ);
        strncpy(utmp.ut_name, name,  NAMSIZ);
        (void)time((time_t *)&utmp.ut_time);
        strncpy(utmp.ut_line, device, LINSIZ);
        strncpy(utmp.ut_name, name,  NAMSIZ);
-       if (lseek(etcutmp, utmploc, 0)<0)
+       if (lseek(etcutmp, utmploc, 0) < 0)
                fprintf(stderr, "on lseek in /etc/utmp: %s",
                        sys_errlist[errno]);
                fprintf(stderr, "on lseek in /etc/utmp: %s",
                        sys_errlist[errno]);
-       if (write(etcutmp, (char *)&utmp, sizeof(utmp))<0)
+       if (write(etcutmp, (char *)&utmp, sizeof(utmp)) < 0)
                fprintf(stderr, "on write in /etc/utmp: %s",
                        sys_errlist[errno]);
 }
                fprintf(stderr, "on write in /etc/utmp: %s",
                        sys_errlist[errno]);
 }
@@ -372,13 +399,13 @@ char *uname, *device; int enable;
                return;
 
        /* wait till init has responded, clearing the utmp entry */
                return;
 
        /* wait till init has responded, clearing the utmp entry */
-       i=100;
+       i = 100;
        do {
                sleep(1);
        do {
                sleep(1);
-               if (lseek(etcutmp, utmploc, 0)<0)
+               if (lseek(etcutmp, utmploc, 0) < 0)
                        fprintf(stderr, "On lseek in /etc/utmp: %s",
                                sys_errlist[errno]);
                        fprintf(stderr, "On lseek in /etc/utmp: %s",
                                sys_errlist[errno]);
-               if (read(etcutmp, (char *)&utmp, sizeof utmp)<0)
+               if (read(etcutmp, (char *)&utmp, sizeof utmp) < 0)
                        fprintf(stderr, "On read from /etc/utmp: %s",
                                sys_errlist[errno]);
        } while (utmp.ut_name[0] != '\0' && --i > 0);
                        fprintf(stderr, "On read from /etc/utmp: %s",
                                sys_errlist[errno]);
        } while (utmp.ut_name[0] != '\0' && --i > 0);
@@ -389,18 +416,24 @@ char *uname, *device; int enable;
 opnttys(device)
 char *device;
 {
 opnttys(device)
 char *device;
 {
-       register FILE *ttysfile, *nttysfile;
        register int  ndevice; 
        register char *p;
        char *index();
        char linebuf[BUFSIZ];
 
        register int  ndevice; 
        register char *p;
        char *index();
        char linebuf[BUFSIZ];
 
-       ttysfile = fopen(Etcttys, "r");
-       if(ttysfile == NULL) {
-               fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
-                       sys_errlist[errno]);
-               exit(1);
-       }
+       ttysfile = NULL;
+       do {
+               if (ttysfile != NULL) {
+                       fclose(ttysfile);
+                       sleep(5);
+               }
+               ttysfile = fopen(Etcttys, "r");
+               if(ttysfile == NULL) {
+                       fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
+                               sys_errlist[errno]);
+                       exit(1);
+               }
+       } while (flock(fileno(ttysfile), LOCK_NB|LOCK_EX) < 0);
        nttysfile = fopen(NEtcttys, "w");
        if(nttysfile == NULL) {
                fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
        nttysfile = fopen(NEtcttys, "w");
        if(nttysfile == NULL) {
                fprintf(stderr, "Cannot open %s: %s\n", Etcttys,
@@ -409,17 +442,18 @@ char *device;
        }
 
        ndevice = strlen(device);
        }
 
        ndevice = strlen(device);
+#ifndef BRL4_2
+       utmploc = sizeof(utmp);
+#else BRL4_2
        utmploc = 0;
        utmploc = 0;
+#endif BRL4_2
 
 
-       ttyslnbeg = 0;
        while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
        while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
-               utmploc += sizeof(utmp);
-               if(strncmp(device, linebuf, ndevice) == 0) {
-                       fclose(ttysfile);
-                       fclose(nttysfile);
+               if(strncmp(device, linebuf, ndevice) == 0)
                        return;
                        return;
-               }
-               ttyslnbeg = ftell(ttysfile);
+               ttyslnbeg += strlen(linebuf);
+               if (linebuf[0] != '#' && linebuf[0] != '\0')
+                       utmploc += sizeof(utmp);
                if (fputs(linebuf, nttysfile) == NULL) {
                        fprintf(stderr, "On %s write: %s\n",
                                Etcttys, sys_errlist[errno]);
                if (fputs(linebuf, nttysfile) == NULL) {
                        fprintf(stderr, "On %s write: %s\n",
                                Etcttys, sys_errlist[errno]);
@@ -436,35 +470,31 @@ settys(enable)
 int enable;
 {
        register char *cp, *cp2;
 int enable;
 {
        register char *cp, *cp2;
-       FILE *ittysfil, *nttysfil;
        char lbuf[BUFSIZ];
        int i;
        char c1, c2;
 
        char lbuf[BUFSIZ];
        int i;
        char c1, c2;
 
-       ittysfil = fopen(Etcttys, "r");
-       if(ittysfil == NULL) {
-               fprintf(stderr, "Cannot open %s for output: %s\n",
-                       Etcttys, sys_errlist[errno]);
-               exit(1);
-       }
-       nttysfil = fopen(NEtcttys, "a");
-       if(nttysfil == NULL) {
-               fprintf(stderr, "Cannot open %s for output: %s\n",
-                       NEtcttys, sys_errlist[errno]);
-               exit(1);
-       }
-       (void)fseek(ittysfil, ttyslnbeg, 0);
-       if(fgets(lbuf, BUFSIZ, ittysfil) == NULL) {
+       (void) fseek(ttysfile, ttyslnbeg, 0);
+       if(fgets(lbuf, BUFSIZ, ttysfile) == NULL) {
                fprintf(stderr, "On %s read: %s\n",
                        Etcttys, sys_errlist[errno]);
                exit(1);
        }
        /* format is now */
        /* ttyd0 std.100 dialup on secure # comment */
                fprintf(stderr, "On %s read: %s\n",
                        Etcttys, sys_errlist[errno]);
                exit(1);
        }
        /* format is now */
        /* ttyd0 std.100 dialup on secure # comment */
+       /* except, 2nd item may have embedded spaces inside quotes, Hubert */
        cp = lbuf;
        for (i=0;*cp && i<3;i++) {
        cp = lbuf;
        for (i=0;*cp && i<3;i++) {
-               while (*cp && *cp != ' ' && *cp != '\t')
+               if (*cp == '"') {
                        cp++;
                        cp++;
+                       while (*cp && *cp != '"')
+                               cp++;
+                       if (*cp != '\0')
+                               cp++;
+               }else {
+                       while (*cp && *cp != ' ' && *cp != '\t')
+                               cp++;
+               }
                while (*cp && (*cp == ' ' || *cp == '\t'))
                        cp++;
        }
                while (*cp && (*cp == ' ' || *cp == '\t'))
                        cp++;
        }
@@ -486,14 +516,14 @@ int enable;
        while (*cp && (*cp == ' ' || *cp == '\t'))
                cp++;
        resettty = strcmp("on", cp2) != 0;
        while (*cp && (*cp == ' ' || *cp == '\t'))
                cp++;
        resettty = strcmp("on", cp2) != 0;
-       fprintf(nttysfil,"%s%c%s%c%s", lbuf, c1, enable ? "on" : "off", c2, cp);
-       if (ferror(nttysfil)) {
+       fprintf(nttysfile,"%s%c%s%c%s", lbuf, c1, enable ? "on" : "off", c2, cp);
+       if (ferror(nttysfile)) {
                fprintf(stderr, "On %s fprintf: %s\n",
                        NEtcttys, sys_errlist[errno]);
                exit(1);
        }
                fprintf(stderr, "On %s fprintf: %s\n",
                        NEtcttys, sys_errlist[errno]);
                exit(1);
        }
-       while(fgets(lbuf, sizeof(lbuf) - 1, ittysfil) != NULL) {
-               if (fputs(lbuf, nttysfil) == NULL) {
+       while(fgets(lbuf, sizeof(lbuf) - 1, ttysfile) != NULL) {
+               if (fputs(lbuf, nttysfile) == NULL) {
                        fprintf(stderr, "On %s write: %s\n",
                                NEtcttys, sys_errlist[errno]);
                        exit(1);
                        fprintf(stderr, "On %s write: %s\n",
                                NEtcttys, sys_errlist[errno]);
                        exit(1);
@@ -505,13 +535,13 @@ int enable;
        else {
                struct stat statb;
                if (stat(Etcttys, &statb) == 0) {
        else {
                struct stat statb;
                if (stat(Etcttys, &statb) == 0) {
-                       fchmod(fileno(nttysfil) ,statb.st_mode);
-                       fchown(fileno(nttysfil), statb.st_uid, statb.st_gid);
+                       fchmod(fileno(nttysfile) ,statb.st_mode);
+                       fchown(fileno(nttysfile), statb.st_uid, statb.st_gid);
                }
                (void) rename(NEtcttys, Etcttys);
        }
                }
                (void) rename(NEtcttys, Etcttys);
        }
-       (void) fclose(ittysfil);
-       (void) fclose(nttysfil);
+       (void) fclose(nttysfile);
+       (void) fclose(ttysfile);
        return enable^resettty;
 }
 
        return enable^resettty;
 }
 
@@ -539,15 +569,19 @@ char *device;
        utmploc = 0;
 
        while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
        utmploc = 0;
 
        while(fgets(linebuf, sizeof(linebuf) - 1, ttysfile) != NULL) {
-               utmploc += sizeof(utmp);
                lnsiz = strlen(linebuf);
                if ((p = index(linebuf, '\n')) != NULL)
                        *p = '\0';
                if(strncmp(device, &linebuf[2], ndevice) == 0) {
                        (void)fclose(ttysfile);
                lnsiz = strlen(linebuf);
                if ((p = index(linebuf, '\n')) != NULL)
                        *p = '\0';
                if(strncmp(device, &linebuf[2], ndevice) == 0) {
                        (void)fclose(ttysfile);
+#ifdef sequent
+                       /* Why is the sequent off by one? */
+                       utmploc += sizeof(utmp);
+#endif sequent
                        return;
                }
                ttyslnbeg += lnsiz;
                        return;
                }
                ttyslnbeg += lnsiz;
+               utmploc += sizeof(utmp);
        }
        fprintf(stderr, "%s not found in %s\n", device, Etcttys);
        exit(1);
        }
        fprintf(stderr, "%s not found in %s\n", device, Etcttys);
        exit(1);
@@ -585,6 +619,17 @@ int enable;
 }
 #endif !BSD4_3
 
 }
 #endif !BSD4_3
 
+#ifdef sequent
+setmodem(ttyline, enable)
+char *ttyline; int enable;
+{
+       char *sysbuf[BUFSIZ];
+       sprintf(sysbuf,"/etc/ttyconfig /dev/%s -special %s", ttyline,
+               enable ? "-carrier" : "-nocarrier");
+       system(sysbuf);
+}
+#endif /* sequent */
+#ifdef vax
 /*
  * Excerpted from (June 8, 1983 W.Sebok)
  * > ttymodem.c - enable/disable modem control for tty lines.
 /*
  * Excerpted from (June 8, 1983 W.Sebok)
  * > ttymodem.c - enable/disable modem control for tty lines.
@@ -712,6 +757,7 @@ char *ttyline; int enable;
        }
        return(0);
 }
        }
        return(0);
 }
+#endif /* vax */
 
 prefix(s1, s2)
        register char *s1, *s2;
 
 prefix(s1, s2)
        register char *s1, *s2;
@@ -723,3 +769,9 @@ prefix(s1, s2)
                        return (1);
        return (c == '\0');
 }
                        return (1);
        return (c == '\0');
 }
+#else  /* !DIALINOUT */
+main()
+{
+       fprintf(stderr,"acucntrl is not supported on this system\n");
+}
+#endif /* !DIALINOUT */