must flush stderr before reading from the terminal
[unix-history] / usr / src / sbin / restore / tape.c
index 0174d06..8615f6e 100644 (file)
@@ -1,7 +1,7 @@
 /* Copyright (c) 1983 Regents of the University of California */
 
 #ifndef lint
 /* Copyright (c) 1983 Regents of the University of California */
 
 #ifndef lint
-static char sccsid[] = "@(#)tape.c     3.9     (Berkeley)      83/03/27";
+static char sccsid[] = "@(#)tape.c     3.15    (Berkeley)      83/05/06";
 #endif
 
 #include "restore.h"
 #endif
 
 #include "restore.h"
@@ -50,9 +50,19 @@ nohost:
                done(1);
        setuid(getuid());       /* no longer need or want root privileges */
 #else
                done(1);
        setuid(getuid());       /* no longer need or want root privileges */
 #else
-       if (strcmp(source, "-") == 0) {
+       if (strcmp(source, "-") != 0) {
+               terminal = stdin;
+       } else {
+               /*
+                * Since input is coming from a pipe we must establish
+                * our own connection to the terminal.
+                */
+               terminal = fopen("/dev/tty", "r");
+               if (terminal == NULL) {
+                       perror("open(\"/dev/tty\")");
+                       done(1);
+               }
                pipein++;
                pipein++;
-               yflag++;
        }
        magtape = source;
 #endif RRESTOR
        }
        magtape = source;
 #endif RRESTOR
@@ -104,8 +114,10 @@ setup()
                while (--j);
                endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
        }
                while (--j);
                endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
        }
-       vprintf(stdout, "Dump   date: %s", ctime(&spcl.c_date));
-       vprintf(stdout, "Dumped from: %s", ctime(&spcl.c_ddate));
+       if (vflag || command == 't') {
+               fprintf(stdout, "Dump   date: %s", ctime(&spcl.c_date));
+               fprintf(stdout, "Dumped from: %s", ctime(&spcl.c_ddate));
+       }
        dumptime = spcl.c_ddate;
        dumpdate = spcl.c_date;
        if (stat(".", &stbuf) < 0) {
        dumptime = spcl.c_ddate;
        dumpdate = spcl.c_date;
        if (stat(".", &stbuf) < 0) {
@@ -133,9 +145,9 @@ setup()
        map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
        if (map == (char *)NIL)
                panic("no memory for file removal list\n");
        map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
        if (map == (char *)NIL)
                panic("no memory for file removal list\n");
+       clrimap = map;
        curfile.action = USING;
        getfile(xtrmap, xtrmapskip);
        curfile.action = USING;
        getfile(xtrmap, xtrmapskip);
-       clrimap = map;
        if (checktype(&spcl, TS_BITS) == FAIL) {
                fprintf(stderr, "Cannot find file dump list\n");
                done(1);
        if (checktype(&spcl, TS_BITS) == FAIL) {
                fprintf(stderr, "Cannot find file dump list\n");
                done(1);
@@ -143,9 +155,9 @@ setup()
        map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
        if (map == (char *)NULL)
                panic("no memory for file dump list\n");
        map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
        if (map == (char *)NULL)
                panic("no memory for file dump list\n");
+       dumpmap = map;
        curfile.action = USING;
        getfile(xtrmap, xtrmapskip);
        curfile.action = USING;
        getfile(xtrmap, xtrmapskip);
-       dumpmap = map;
 }
 
 getvol(nextvol)
 }
 
 getvol(nextvol)
@@ -172,9 +184,13 @@ again:
        else 
                newvol = 0;
        while (newvol <= 0) {
        else 
                newvol = 0;
        while (newvol <= 0) {
-               fprintf(stderr, "Specify next volume #: ");
-               if (gets(tbf) == NULL)
-                       continue;
+               do      {
+                       fprintf(stderr, "Specify next volume #: ");
+                       (void) fflush(stderr);
+                       (void) fgets(tbf, BUFSIZ, terminal);
+               } while (!feof(terminal) && tbf[0] == '\n');
+               if (feof(terminal))
+                       done(1);
                newvol = atoi(tbf);
                if (newvol <= 0) {
                        fprintf(stderr,
                newvol = atoi(tbf);
                if (newvol <= 0) {
                        fprintf(stderr,
@@ -185,7 +201,8 @@ again:
                return;
        closemt();
        fprintf(stderr, "Mount tape volume %d then type return ", newvol);
                return;
        closemt();
        fprintf(stderr, "Mount tape volume %d then type return ", newvol);
-       while (getchar() != '\n')
+       (void) fflush(stderr);
+       while (getc(terminal) != '\n')
                ;
 #ifdef RRESTOR
        if ((mt = rmtopen(magtape, 0)) == -1)
                ;
 #ifdef RRESTOR
        if ((mt = rmtopen(magtape, 0)) == -1)
@@ -211,8 +228,9 @@ gethdr:
                goto again;
        }
        if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
                goto again;
        }
        if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
-               fprintf(stderr, "Wrong dump date (%s)\n",
+               fprintf(stderr, "Wrong dump date\n\tgot: %s",
                        ctime(&tmpbuf.c_date));
                        ctime(&tmpbuf.c_date));
+               fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
                volno = 0;
                goto again;
        }
                volno = 0;
                goto again;
        }
@@ -249,7 +267,7 @@ setdumpnum()
 #ifdef RRESTOR
        rmtioctl(MTFSF, dumpnum - 1);
 #else
 #ifdef RRESTOR
        rmtioctl(MTFSF, dumpnum - 1);
 #else
-       if (ioctl(mt, MTIOCTOP, &tcom) < 0)
+       if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
                perror("ioctl MTFSF");
 #endif
 }
                perror("ioctl MTFSF");
 #endif
 }
@@ -454,7 +472,7 @@ xtrlnkfile(buf, size)
                    curfile.name, lnkbuf, buf, pathlen);
                done(1);
        }
                    curfile.name, lnkbuf, buf, pathlen);
                done(1);
        }
-       strcat(lnkbuf, buf);
+       (void) strcat(lnkbuf, buf);
 }
 
 xtrlnkskip(buf, size)
 }
 
 xtrlnkskip(buf, size)
@@ -476,6 +494,7 @@ xtrmap(buf, size)
 {
 
        bcopy(buf, map, size);
 {
 
        bcopy(buf, map, size);
+       map += size;
 }
 
 xtrmapskip(buf, size)
 }
 
 xtrmapskip(buf, size)
@@ -485,9 +504,9 @@ xtrmapskip(buf, size)
 
 #ifdef lint
        buf = buf;
 
 #ifdef lint
        buf = buf;
-       size = size;
 #endif
        panic("hole in map\n");
 #endif
        panic("hole in map\n");
+       map += size;
 }
 
 null() {;}
 }
 
 null() {;}
@@ -628,14 +647,14 @@ gethead(buf)
                        long    c_ddate;
                        long    c_volume;
                        long    c_tapea;
                        long    c_ddate;
                        long    c_volume;
                        long    c_tapea;
-                       short   c_inumber;
+                       u_short c_inumber;
                        long    c_magic;
                        long    c_checksum;
                        struct odinode {
                                unsigned short odi_mode;
                        long    c_magic;
                        long    c_checksum;
                        struct odinode {
                                unsigned short odi_mode;
-                               short   odi_nlink;
-                               short   odi_uid;
-                               short   odi_gid;
+                               u_short odi_nlink;
+                               u_short odi_uid;
+                               u_short odi_gid;
                                long    odi_size;
                                long    odi_rdev;
                                char    odi_addr[36];
                                long    odi_size;
                                long    odi_rdev;
                                char    odi_addr[36];
@@ -803,7 +822,7 @@ findinode(header, complain)
                        skipcnt++;
        }
        if (skipcnt > 0 && complain)
                        skipcnt++;
        }
        if (skipcnt > 0 && complain)
-               fprintf(stderr, "resync restor, skipped %d blocks\n", skipcnt);
+               fprintf(stderr, "resync restore, skipped %d blocks\n", skipcnt);
        skipcnt = 0;
 }
 
        skipcnt = 0;
 }
 
@@ -847,33 +866,6 @@ checksum(b)
        return(GOOD);
 }
 
        return(GOOD);
 }
 
-/*
- * respond to interrupts
- */
-onintr()
-{
-       if (pipein || reply("restore interrupted, continue") == FAIL)
-               done(1);
-       if (signal(SIGINT, onintr) == SIG_IGN)
-               (void) signal(SIGINT, SIG_IGN);
-       if (signal(SIGTERM, onintr) == SIG_IGN)
-               (void) signal(SIGTERM, SIG_IGN);
-}
-
-/*
- * handle unexpected inconsistencies
- */
-/* VARARGS1 */
-panic(msg, d1, d2)
-       char *msg;
-       long d1, d2;
-{
-
-       fprintf(stderr, msg, d1, d2);
-       if (pipein || reply("abort") == GOOD)
-               abort();
-}
-
 #ifdef RRESTOR
 /* VARARGS1 */
 msg(cp, a1, a2, a3)
 #ifdef RRESTOR
 /* VARARGS1 */
 msg(cp, a1, a2, a3)