BSD 4_4_Lite2 release
[unix-history] / usr / src / sbin / dump / dumprmt.c
index 1aaeb10..9c5273e 100644 (file)
@@ -1,12 +1,38 @@
 /*-
 /*-
- * Copyright (c) 1980 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * 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[] = "@(#)dumprmt.c  5.16 (Berkeley) %G%";
+static char sccsid[] = "@(#)dumprmt.c  8.3 (Berkeley) 4/28/95";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -28,6 +54,7 @@ static char sccsid[] = "@(#)dumprmt.c 5.16 (Berkeley) %G%";
 #include <protocols/dumprestore.h>
 
 #include <ctype.h>
 #include <protocols/dumprestore.h>
 
 #include <ctype.h>
+#include <err.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <signal.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <signal.h>
@@ -79,16 +106,15 @@ static void
 rmtconnaborted()
 {
 
 rmtconnaborted()
 {
 
-       (void) fprintf(stderr, "rdump: Lost connection to remote host.\n");
-       exit(1);
+       errx(1, "Lost connection to remote host.");
 }
 
 void
 rmtgetconn()
 {
        register char *cp;
 }
 
 void
 rmtgetconn()
 {
        register char *cp;
-       static struct servent *sp = 0;
-       static struct passwd *pwd = 0;
+       static struct servent *sp = NULL;
+       static struct passwd *pwd = NULL;
 #ifdef notdef
        static int on = 1;
 #endif
 #ifdef notdef
        static int on = 1;
 #endif
@@ -96,20 +122,15 @@ rmtgetconn()
        int size;
        int maxseg;
 
        int size;
        int maxseg;
 
-       if (sp == 0) {
+       if (sp == NULL) {
                sp = getservbyname("shell", "tcp");
                sp = getservbyname("shell", "tcp");
-               if (sp == 0) {
-                       (void) fprintf(stderr,
-                           "rdump: shell/tcp: unknown service\n");
-                       exit(1);
-               }
+               if (sp == NULL)
+                       errx(1, "shell/tcp: unknown service");
                pwd = getpwuid(getuid());
                pwd = getpwuid(getuid());
-               if (pwd == 0) {
-                       (void) fprintf(stderr, "rdump: who are you?\n");
-                       exit(1);
-               }
+               if (pwd == NULL)
+                       errx(1, "who are you?");
        }
        }
-       if (cp = index(rmtpeer, '@')) {
+       if ((cp = strchr(rmtpeer, '@')) != NULL) {
                tuser = rmtpeer;
                *cp = '\0';
                if (!okname(tuser))
                tuser = rmtpeer;
                *cp = '\0';
                if (!okname(tuser))
@@ -149,8 +170,7 @@ okname(cp0)
        for (cp = cp0; *cp; cp++) {
                c = *cp;
                if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
        for (cp = cp0; *cp; cp++) {
                c = *cp;
                if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
-                       (void) fprintf(stderr, "rdump: invalid user name %s\n",
-                           cp0);
+                       warnx("invalid user name: %s", cp0);
                        return (0);
                }
        }
                        return (0);
                }
        }
@@ -261,13 +281,25 @@ rmtstatus()
        register char *cp;
 
        if (rmtstate != TS_OPEN)
        register char *cp;
 
        if (rmtstate != TS_OPEN)
-               return (0);
+               return (NULL);
        rmtcall("status", "S\n");
        for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
                *cp++ = rmtgetb();
        return (&mts);
 }
 
        rmtcall("status", "S\n");
        for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
                *cp++ = rmtgetb();
        return (&mts);
 }
 
+int
+rmtioctl(cmd, count)
+       int cmd, count;
+{
+       char buf[256];
+
+       if (count < 0)
+               return (-1);
+       (void)sprintf(buf, "I%d\n%d\n", cmd, count);
+       return (rmtcall("ioctl", buf));
+}
+
 static int
 rmtcall(cmd, buf)
        char *cmd, *buf;
 static int
 rmtcall(cmd, buf)
        char *cmd, *buf;
@@ -282,12 +314,13 @@ static int
 rmtreply(cmd)
        char *cmd;
 {
 rmtreply(cmd)
        char *cmd;
 {
+       register char *cp;
        char code[30], emsg[BUFSIZ];
 
        rmtgets(code, sizeof (code));
        if (*code == 'E' || *code == 'F') {
                rmtgets(emsg, sizeof (emsg));
        char code[30], emsg[BUFSIZ];
 
        rmtgets(code, sizeof (code));
        if (*code == 'E' || *code == 'F') {
                rmtgets(emsg, sizeof (emsg));
-               msg("%s: %s\n", cmd, emsg, code + 1);
+               msg("%s: %s", cmd, emsg);
                if (*code == 'F') {
                        rmtstate = TS_CLOSED;
                        return (-1);
                if (*code == 'F') {
                        rmtstate = TS_CLOSED;
                        return (-1);
@@ -295,7 +328,12 @@ rmtreply(cmd)
                return (-1);
        }
        if (*code != 'A') {
                return (-1);
        }
        if (*code != 'A') {
-               msg("Protocol to remote tape server botched (code %s?).\n",
+               /* Kill trailing newline */
+               cp = code + strlen(code);
+               if (cp > code && *--cp == '\n')
+                       *cp = '\0';
+
+               msg("Protocol to remote tape server botched (code \"%s\").\n",
                    code);
                rmtconnaborted();
        }
                    code);
                rmtconnaborted();
        }
@@ -312,6 +350,7 @@ rmtgetb()
        return (c);
 }
 
        return (c);
 }
 
+/* Get a line (guaranteed to have a trailing newline). */
 void
 rmtgets(line, len)
        char *line;
 void
 rmtgets(line, len)
        char *line;
@@ -322,13 +361,13 @@ rmtgets(line, len)
        while (len > 1) {
                *cp = rmtgetb();
                if (*cp == '\n') {
        while (len > 1) {
                *cp = rmtgetb();
                if (*cp == '\n') {
-                       cp[1] = 0;
+                       cp[1] = '\0';
                        return;
                }
                cp++;
                len--;
        }
                        return;
                }
                cp++;
                len--;
        }
-       *cp = 0;
+       *cp = '\0';
        msg("Protocol to remote tape server botched.\n");
        msg("(rmtgets got \"%s\").\n", line);
        rmtconnaborted();
        msg("Protocol to remote tape server botched.\n");
        msg("(rmtgets got \"%s\").\n", line);
        rmtconnaborted();