break out special local mail processing (e.g., mapping to the
[unix-history] / usr / src / usr.sbin / rmt / rmt.c
index 86c8da7..cedad28 100644 (file)
@@ -1,34 +1,55 @@
+/*
+ * Copyright (c) 1983, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char copyright[] =
+"@(#) Copyright (c) 1983, 1993\n\
+       The Regents of the University of California.  All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rmt.c      4.2 82/04/19";
-#endif
+static char sccsid[] = "@(#)rmt.c      8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * rmt
  */
 
 /*
  * rmt
  */
-#include <stdio.h>
-#include <sgtty.h>
 #include <sys/types.h>
 #include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/mtio.h>
 #include <errno.h>
 #include <sys/mtio.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <sgtty.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 int    tape = -1;
 
 
 int    tape = -1;
 
-#define        MAXRECSIZ       (10*1024)       /* small enuf for pdp-11's too */
-char   record[MAXRECSIZ];
+char   *record;
+int    maxrecsize = -1;
 
 #define        SSIZE   64
 char   device[SSIZE];
 char   count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
 
 
 #define        SSIZE   64
 char   device[SSIZE];
 char   count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
 
-extern errno;
-char   *sys_errlist[];
 char   resp[BUFSIZ];
 
 char   resp[BUFSIZ];
 
-char   *sprintf();
-long   lseek();
-
 FILE   *debug;
 FILE   *debug;
+#define        DEBUG(f)        if (debug) fprintf(debug, f)
+#define        DEBUG1(f,a)     if (debug) fprintf(debug, f, a)
+#define        DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
+
+char   *checkbuf __P((char *, int));
+void    error __P((int));
+void    getstring __P((char *));
 
 
+int
 main(argc, argv)
        int argc;
        char **argv;
 main(argc, argv)
        int argc;
        char **argv;
@@ -42,7 +63,7 @@ main(argc, argv)
                debug = fopen(*argv, "w");
                if (debug == 0)
                        exit(1);
                debug = fopen(*argv, "w");
                if (debug == 0)
                        exit(1);
-               (void) setbuf(debug, (char *)0);
+               (void)setbuf(debug, (char *)0);
        }
 top:
        errno = 0;
        }
 top:
        errno = 0;
@@ -54,60 +75,65 @@ top:
        case 'O':
                if (tape >= 0)
                        (void) close(tape);
        case 'O':
                if (tape >= 0)
                        (void) close(tape);
-               gets(device); gets(mode);
-if (debug) fprintf(debug, "rmtd: O %s %s\n", device, mode);
+               getstring(device);
+               getstring(mode);
+               DEBUG2("rmtd: O %s %s\n", device, mode);
                tape = open(device, atoi(mode));
                if (tape < 0)
                        goto ioerror;
                tape = open(device, atoi(mode));
                if (tape < 0)
                        goto ioerror;
-               break;
+               goto respond;
 
        case 'C':
 
        case 'C':
-if (debug) fprintf(debug, "rmtd: C\n");
-               gets(device);           /* discard */
+               DEBUG("rmtd: C\n");
+               getstring(device);              /* discard */
                if (close(tape) < 0)
                        goto ioerror;
                tape = -1;
                if (close(tape) < 0)
                        goto ioerror;
                tape = -1;
-               break;
+               goto respond;
 
        case 'L':
 
        case 'L':
-               gets(count); gets(pos);
-if (debug) fprintf(debug, "rmtd: L %s %s\n", count, pos);
-               rval = lseek(tape, (long) atoi(count), atoi(pos));
+               getstring(count);
+               getstring(pos);
+               DEBUG2("rmtd: L %s %s\n", count, pos);
+               rval = lseek(tape, (off_t)atol(count), atoi(pos));
                if (rval < 0)
                        goto ioerror;
                if (rval < 0)
                        goto ioerror;
-               break;
+               goto respond;
 
        case 'W':
 
        case 'W':
-               gets(count);
+               getstring(count);
                n = atoi(count);
                n = atoi(count);
-if (debug) fprintf(debug, "rmtd: W %s\n", count);
+               DEBUG1("rmtd: W %s\n", count);
+               record = checkbuf(record, n);
                for (i = 0; i < n; i += cc) {
                        cc = read(0, &record[i], n - i);
                        if (cc <= 0) {
                for (i = 0; i < n; i += cc) {
                        cc = read(0, &record[i], n - i);
                        if (cc <= 0) {
-if (debug) fprintf(debug, "rmtd: premature eof\n");
-                               exit(1);
+                               DEBUG("rmtd: premature eof\n");
+                               exit(2);
                        }
                }
                rval = write(tape, record, n);
                if (rval < 0)
                        goto ioerror;
                        }
                }
                rval = write(tape, record, n);
                if (rval < 0)
                        goto ioerror;
-               break;
+               goto respond;
 
        case 'R':
 
        case 'R':
-               gets(count);
-if (debug) fprintf(debug, "rmtd: R %s\n", count);
+               getstring(count);
+               DEBUG1("rmtd: R %s\n", count);
                n = atoi(count);
                n = atoi(count);
-               if (n > sizeof (record))
-                       n = sizeof (record);
+               record = checkbuf(record, n);
                rval = read(tape, record, n);
                if (rval < 0)
                        goto ioerror;
                rval = read(tape, record, n);
                if (rval < 0)
                        goto ioerror;
-               (void) write(1, record, n);
-               break;
+               (void)sprintf(resp, "A%d\n", rval);
+               (void)write(1, resp, strlen(resp));
+               (void)write(1, record, rval);
+               goto top;
 
        case 'I':
 
        case 'I':
-               gets(op); gets(count);
-if (debug) fprintf(debug, "rmtd: I %s %s\n", op, count);
+               getstring(op);
+               getstring(count);
+               DEBUG2("rmtd: I %s %s\n", op, count);
                { struct mtop mtop;
                  mtop.mt_op = atoi(op);
                  mtop.mt_count = atoi(count);
                { struct mtop mtop;
                  mtop.mt_op = atoi(op);
                  mtop.mt_count = atoi(count);
@@ -115,32 +141,36 @@ if (debug) fprintf(debug, "rmtd: I %s %s\n", op, count);
                        goto ioerror;
                  rval = mtop.mt_count;
                }
                        goto ioerror;
                  rval = mtop.mt_count;
                }
-               break;
+               goto respond;
 
        case 'S':               /* status */
 
        case 'S':               /* status */
-if (debug) fprintf(debug, "rmtd: S\n");
+               DEBUG("rmtd: S\n");
                { struct mtget mtget;
                  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
                        goto ioerror;
                  rval = sizeof (mtget);
                { struct mtget mtget;
                  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
                        goto ioerror;
                  rval = sizeof (mtget);
-                 (void) write(1, (char *)&mtget, sizeof (mtget));
-                 break;
+                 (void)sprintf(resp, "A%d\n", rval);
+                 (void)write(1, resp, strlen(resp));
+                 (void)write(1, (char *)&mtget, sizeof (mtget));
+                 goto top;
                }
 
        default:
                }
 
        default:
-if (debug) fprintf(debug, "rmtd: garbage command %c\n", c);
-               exit(1);
+               DEBUG1("rmtd: garbage command %c\n", c);
+               exit(3);
        }
        }
-if (debug) fprintf(debug, "rmtd: A %d\n", rval);
-       (void) sprintf(resp, "A%d\n", rval);
-       (void) write(1, resp, strlen(resp));
+respond:
+       DEBUG1("rmtd: A %d\n", rval);
+       (void)sprintf(resp, "A%d\n", rval);
+       (void)write(1, resp, strlen(resp));
        goto top;
 ioerror:
        error(errno);
        goto top;
 }
 
        goto top;
 ioerror:
        error(errno);
        goto top;
 }
 
-gets(bp)
+void
+getstring(bp)
        char *bp;
 {
        int i;
        char *bp;
 {
        int i;
@@ -155,12 +185,34 @@ gets(bp)
        cp[i] = '\0';
 }
 
        cp[i] = '\0';
 }
 
+char *
+checkbuf(record, size)
+       char *record;
+       int size;
+{
+
+       if (size <= maxrecsize)
+               return (record);
+       if (record != 0)
+               free(record);
+       record = malloc(size);
+       if (record == 0) {
+               DEBUG("rmtd: cannot allocate buffer space\n");
+               exit(4);
+       }
+       maxrecsize = size;
+       while (size > 1024 &&
+              setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
+               size -= 1024;
+       return (record);
+}
+
+void
 error(num)
        int num;
 {
 
 error(num)
        int num;
 {
 
-if (debug) fprintf(debug, "rmtd: E %d (%s)\n", num, sys_errlist[num]);
-       (void) sprintf(resp, "E%d\n%s\n", num, sys_errlist[num]);
-       (void) write(1, resp, strlen (resp));
+       DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
+       (void)sprintf(resp, "E%d\n%s\n", num, strerror(num));
+       (void)write(1, resp, strlen(resp));
 }
 }
-