ignore -t in -bd mode
[unix-history] / usr / src / usr.sbin / rmt / rmt.c
index 6a412a0..cedad28 100644 (file)
@@ -1,37 +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.4 (Berkeley) 84/11/15";
-#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;
 #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)
 
 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;
@@ -45,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;
@@ -57,7 +75,8 @@ top:
        case 'O':
                if (tape >= 0)
                        (void) close(tape);
        case 'O':
                if (tape >= 0)
                        (void) close(tape);
-               getstring(device); getstring(mode);
+               getstring(device);
+               getstring(mode);
                DEBUG2("rmtd: O %s %s\n", device, mode);
                tape = open(device, atoi(mode));
                if (tape < 0)
                DEBUG2("rmtd: O %s %s\n", device, mode);
                tape = open(device, atoi(mode));
                if (tape < 0)
@@ -73,9 +92,10 @@ top:
                goto respond;
 
        case 'L':
                goto respond;
 
        case 'L':
-               getstring(count); getstring(pos);
+               getstring(count);
+               getstring(pos);
                DEBUG2("rmtd: L %s %s\n", count, pos);
                DEBUG2("rmtd: L %s %s\n", count, pos);
-               rval = lseek(tape, (long) atoi(count), atoi(pos));
+               rval = lseek(tape, (off_t)atol(count), atoi(pos));
                if (rval < 0)
                        goto ioerror;
                goto respond;
                if (rval < 0)
                        goto ioerror;
                goto respond;
@@ -84,11 +104,12 @@ top:
                getstring(count);
                n = atoi(count);
                DEBUG1("rmtd: W %s\n", count);
                getstring(count);
                n = atoi(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) {
                                DEBUG("rmtd: premature eof\n");
                for (i = 0; i < n; i += cc) {
                        cc = read(0, &record[i], n - i);
                        if (cc <= 0) {
                                DEBUG("rmtd: premature eof\n");
-                               exit(1);
+                               exit(2);
                        }
                }
                rval = write(tape, record, n);
                        }
                }
                rval = write(tape, record, n);
@@ -100,18 +121,18 @@ top:
                getstring(count);
                DEBUG1("rmtd: R %s\n", count);
                n = atoi(count);
                getstring(count);
                DEBUG1("rmtd: R %s\n", 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) sprintf(resp, "A%d\n", rval);
-               (void) write(1, resp, strlen(resp));
-               (void) write(1, record, rval);
+               (void)sprintf(resp, "A%d\n", rval);
+               (void)write(1, resp, strlen(resp));
+               (void)write(1, record, rval);
                goto top;
 
        case 'I':
                goto top;
 
        case 'I':
-               getstring(op); getstring(count);
+               getstring(op);
+               getstring(count);
                DEBUG2("rmtd: I %s %s\n", op, count);
                { struct mtop mtop;
                  mtop.mt_op = atoi(op);
                DEBUG2("rmtd: I %s %s\n", op, count);
                { struct mtop mtop;
                  mtop.mt_op = atoi(op);
@@ -128,24 +149,27 @@ top:
                  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
                        goto ioerror;
                  rval = sizeof (mtget);
                  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
                        goto ioerror;
                  rval = sizeof (mtget);
-                 (void) write(1, (char *)&mtget, sizeof (mtget));
-                 goto respond;
+                 (void)sprintf(resp, "A%d\n", rval);
+                 (void)write(1, resp, strlen(resp));
+                 (void)write(1, (char *)&mtget, sizeof (mtget));
+                 goto top;
                }
 
        default:
                DEBUG1("rmtd: garbage command %c\n", c);
                }
 
        default:
                DEBUG1("rmtd: garbage command %c\n", c);
-               exit(1);
+               exit(3);
        }
 respond:
        DEBUG1("rmtd: A %d\n", rval);
        }
 respond:
        DEBUG1("rmtd: A %d\n", rval);
-       (void) sprintf(resp, "A%d\n", rval);
-       (void) write(1, resp, strlen(resp));
+       (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;
 }
 
+void
 getstring(bp)
        char *bp;
 {
 getstring(bp)
        char *bp;
 {
@@ -161,12 +185,34 @@ getstring(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;
 {
 
-       DEBUG2("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));
 }
 }
-