date and time created 88/07/22 16:08:01 by bostic
[unix-history] / usr / src / usr.sbin / rmt / rmt.c
index 07b7741..73adc31 100644 (file)
@@ -1,6 +1,29 @@
+/*
+ * Copyright (c) 1983 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[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rmt.c      4.5 (Berkeley) 84/12/19";
-#endif
+static char sccsid[] = "@(#)rmt.c      5.4 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * rmt
 
 /*
  * rmt
@@ -14,8 +37,9 @@ static char sccsid[] = "@(#)rmt.c     4.5 (Berkeley) 84/12/19";
 
 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;
+char   *checkbuf();
 
 #define        SSIZE   64
 char   device[SSIZE];
 
 #define        SSIZE   64
 char   device[SSIZE];
@@ -25,7 +49,6 @@ extern        errno;
 char   *sys_errlist[];
 char   resp[BUFSIZ];
 
 char   *sys_errlist[];
 char   resp[BUFSIZ];
 
-char   *sprintf();
 long   lseek();
 
 FILE   *debug;
 long   lseek();
 
 FILE   *debug;
@@ -48,8 +71,6 @@ main(argc, argv)
                        exit(1);
                (void) setbuf(debug, (char *)0);
        }
                        exit(1);
                (void) setbuf(debug, (char *)0);
        }
-       n = MAXRECSIZ;
-       (void) setsockopt(0, SOL_SOCKET, SO_RCVBUF, &n, sizeof (n));
 top:
        errno = 0;
        rval = 0;
 top:
        errno = 0;
        rval = 0;
@@ -87,11 +108,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);
@@ -103,8 +125,7 @@ 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;
@@ -131,13 +152,15 @@ 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) sprintf(resp, "A%d\n", rval);
+                 (void) write(1, resp, strlen(resp));
                  (void) write(1, (char *)&mtget, sizeof (mtget));
                  (void) write(1, (char *)&mtget, sizeof (mtget));
-                 goto respond;
+                 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);
@@ -164,6 +187,29 @@ getstring(bp)
        cp[i] = '\0';
 }
 
        cp[i] = '\0';
 }
 
+char *
+checkbuf(record, size)
+       char *record;
+       int size;
+{
+       extern char *malloc();
+
+       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);
+}
+
 error(num)
        int num;
 {
 error(num)
        int num;
 {
@@ -172,4 +218,3 @@ error(num)
        (void) sprintf(resp, "E%d\n%s\n", num, sys_errlist[num]);
        (void) write(1, resp, strlen (resp));
 }
        (void) sprintf(resp, "E%d\n%s\n", num, sys_errlist[num]);
        (void) write(1, resp, strlen (resp));
 }
-