386BSD 0.1 development
[unix-history] / usr / src / usr.sbin / rmt / rmt.c
index 6a412a0..3a4d621 100644 (file)
@@ -1,6 +1,45 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rmt.c      4.4 (Berkeley) 84/11/15";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)rmt.c      5.6 (Berkeley) 6/1/90";
+#endif /* not lint */
 
 /*
  * rmt
 
 /*
  * rmt
@@ -8,23 +47,23 @@ static char sccsid[] = "@(#)rmt.c    4.4 (Berkeley) 84/11/15";
 #include <stdio.h>
 #include <sgtty.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <sgtty.h>
 #include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/mtio.h>
 #include <errno.h>
 #include <sys/mtio.h>
 #include <errno.h>
+#include <string.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;
+char   *checkbuf();
 
 #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;
 long   lseek();
 
 FILE   *debug;
@@ -84,11 +123,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,8 +140,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;
@@ -128,13 +167,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);
@@ -161,12 +202,34 @@ 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;
 {
 
-       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));
 }
 }
-