]> git.wh0rd.org - dump.git/blobdiff - dump/optr.c
Added CVS Id.
[dump.git] / dump / optr.c
index 4d232422bde8af2c6bd757f69a8b32a8d0a9e162..6c2efb703287a6ad2d80ef52c7b33b5dfd680285 100644 (file)
@@ -1,7 +1,8 @@
 /*
  *     Ported to Linux's Second Extended File System as part of the
  *     dump and restore backup suit
- *     Remy Card <card@Linux.EU.Org>, 1994, 1995, 1996
+ *     Remy Card <card@Linux.EU.Org>, 1994-1997
+ *      Stelian Pop <pop@cybercable.fr>, 1999 
  *
  */
 
  * 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.
+ *
+ * $Id: optr.c,v 1.5 1999/10/11 13:31:11 stelian Exp $
  */
 
-#ifndef lint
-static char sccsid[] = "@(#)optr.c     8.2 (Berkeley) 1/6/94";
-#endif /* not lint */
-
 #include <sys/param.h>
 #include <sys/wait.h>
 #include <sys/time.h>
@@ -49,34 +48,27 @@ static char sccsid[] = "@(#)optr.c  8.2 (Berkeley) 1/6/94";
 #include <errno.h>
 #include <fstab.h>
 #include <grp.h>
-#include <signal.h>
 #include <stdio.h>
-#ifdef __STDC__
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
-#endif
-#include <tzfile.h>
-#ifdef __STDC__
 #include <unistd.h>
-#endif
 #include <utmp.h>
-#ifndef __STDC__
-#include <varargs.h>
-#endif
+#include <sys/stat.h>
 
 #ifdef __linux__
 #include <linux/ext2_fs.h>
 #include <ext2fs/ext2fs.h>
 #include <bsdcompat.h>
+#include <signal.h>
 #endif
 
 #include "dump.h"
 #include "pathnames.h"
 
-void   alarmcatch __P((/* int, int */));
+static void alarmcatch __P((int));
 int    datesort __P((const void *, const void *));
-static void sendmes __P((char *, char *));
+static void sendmes __P((const char *, const char *));
 
 /*
  *     Query the operator; This previously-fascist piece of code
@@ -90,21 +82,27 @@ static      void sendmes __P((char *, char *));
  *     that dump needs attention.
  */
 static int timeout;
-static char *attnmessage;              /* attention message */
+static const char *attnmessage;                /* attention message */
 
 int
-query(question)
-       char    *question;
+query(const char *question)
 {
        char    replybuffer[64];
        int     back, errcount;
        FILE    *mytty;
+       time_t  firstprompt, when_answered;
+
+#ifdef __linux__
+       (void)time4(&(firstprompt));
+#else
+       (void)time((time_t *)&(firstprompt));
+#endif
 
        if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
                quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
        attnmessage = question;
        timeout = 0;
-       alarmcatch();
+       alarmcatch(0);
        back = -1;
        errcount = 0;
        do {
@@ -131,18 +129,30 @@ query(question)
        if (signal(SIGALRM, sig) == SIG_IGN)
                signal(SIGALRM, SIG_IGN);
        (void) fclose(mytty);
+#ifdef __linux__
+       (void)time4(&(when_answered));
+#else
+       (void)time((time_t *)&(when_answered));
+#endif
+       /*
+        * Adjust the base for time estimates to ignore time we spent waiting
+        * for operator input.
+        */
+       if ((tstart_writing != 0) && (when_answered != (time_t)-1) && (firstprompt != (time_t)-1))
+               tstart_writing += (when_answered - firstprompt);
        return(back);
 }
 
-char lastmsg[100];
+char lastmsg[BUFSIZ];
 
 /*
  *     Alert the console operator, and enable the alarm clock to
  *     sleep for 2 minutes in case nobody comes to satisfy dump
  */
-void
-alarmcatch()
+static void
+alarmcatch(int signo)
 {
+       int save_errno = errno;
        if (notify == 0) {
                if (timeout == 0)
                        (void) fprintf(stderr,
@@ -161,14 +171,14 @@ alarmcatch()
        signal(SIGALRM, alarmcatch);
        (void) alarm(120);
        timeout = 1;
+       errno = save_errno;
 }
 
 /*
  *     Here if an inquisitive operator interrupts the dump program
  */
 void
-interrupt(signo)
-       int signo;
+interrupt(int signo)
 {
        msg("Interrupt received.\n");
        if (query("Do you want to abort dump?"))
@@ -184,9 +194,9 @@ struct      group *gp;
 
 /*
  *     Get the names from the group entry "operator" to notify.
- */    
+ */
 void
-set_operators()
+set_operators(void)
 {
        if (!notify)            /*not going to notify*/
                return;
@@ -206,8 +216,7 @@ struct tm *localclock;
  *     that the process control groups are not messed up
  */
 void
-broadcast(message)
-       char    *message;
+broadcast(const char *message)
 {
        time_t          clock;
        FILE    *f_utmp;
@@ -262,16 +271,15 @@ broadcast(message)
 }
 
 static void
-sendmes(tty, message)
-       char *tty, *message;
+sendmes(const char *tty, const char *message)
 {
-       char t[50], buf[BUFSIZ];
-       register char *cp;
+       char t[MAXPATHLEN], buf[BUFSIZ];
+       register const char *cp;
        int lmsg = 1;
        FILE *f_tty;
 
        (void) strcpy(t, _PATH_DEV);
-       (void) strcat(t, tty);
+       (void) strncat(t, tty, sizeof t - strlen(_PATH_DEV) - 1);
 
        if ((f_tty = fopen(t, "w")) != NULL) {
                setbuf(f_tty, buf);
@@ -284,7 +292,7 @@ DUMP: NEEDS ATTENTION: ",
                        if (*cp == '\0') {
                                if (lmsg) {
                                        cp = message;
-                                       if (*cp == '\0')
+                                       if (!(cp && *cp != '\0'))
                                                break;
                                        lmsg = 0;
                                } else
@@ -305,15 +313,19 @@ DUMP: NEEDS ATTENTION: ",
 time_t tschedule = 0;
 
 void
-timeest()
+timeest(void)
 {
        time_t  tnow, deltat;
 
+#ifdef __linux__
+       (void) time4(&tnow);
+#else
        (void) time((time_t *) &tnow);
+#endif
        if (tnow >= tschedule) {
                tschedule = tnow + 300;
                if (blockswritten < 500)
-                       return; 
+                       return;
                deltat = tstart_writing - tnow +
                        (1.0 * (tnow - tstart_writing))
                        / blockswritten * tapesize;
@@ -324,7 +336,7 @@ timeest()
 }
 
 void
-#if __STDC__
+#ifdef __STDC__
 msg(const char *fmt, ...)
 #else
 msg(fmt, va_alist)
@@ -338,7 +350,7 @@ msg(fmt, va_alist)
 #ifdef TDEBUG
        (void) fprintf(stderr, "pid=%d ", getpid());
 #endif
-#if __STDC__
+#ifdef __STDC__
        va_start(ap, fmt);
 #else
        va_start(ap);
@@ -346,12 +358,12 @@ msg(fmt, va_alist)
        (void) vfprintf(stderr, fmt, ap);
        (void) fflush(stdout);
        (void) fflush(stderr);
-       (void) vsprintf(lastmsg, fmt, ap);
+       (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
        va_end(ap);
 }
 
 void
-#if __STDC__
+#ifdef __STDC__
 msgtail(const char *fmt, ...)
 #else
 msgtail(fmt, va_alist)
@@ -360,7 +372,7 @@ msgtail(fmt, va_alist)
 #endif
 {
        va_list ap;
-#if __STDC__
+#ifdef __STDC__
        va_start(ap, fmt);
 #else
        va_start(ap);
@@ -370,7 +382,7 @@ msgtail(fmt, va_alist)
 }
 
 void
-#if __STDC__
+#ifdef __STDC__
 quit(const char *fmt, ...)
 #else
 quit(fmt, va_alist)
@@ -384,7 +396,7 @@ quit(fmt, va_alist)
 #ifdef TDEBUG
        (void) fprintf(stderr, "pid=%d ", getpid());
 #endif
-#if __STDC__
+#ifdef __STDC__
        va_start(ap, fmt);
 #else
        va_start(ap);
@@ -401,9 +413,8 @@ quit(fmt, va_alist)
  *     we don't actually do it
  */
 
-struct fstab *
-allocfsent(fs)
-       register struct fstab *fs;
+static struct fstab *
+allocfsent(struct fstab *fs)
 {
        register struct fstab *new;
 
@@ -426,7 +437,7 @@ struct      pfstab {
 static struct pfstab *table;
 
 void
-getfstab()
+getfstab(void)
 {
        register struct fstab *fs;
        register struct pfstab *pf;
@@ -463,8 +474,7 @@ getfstab()
  * The file name can omit the leading '/'.
  */
 struct fstab *
-fstabsearch(key)
-       char *key;
+fstabsearch(const char *key)
 {
        register struct pfstab *pf;
        register struct fstab *fs;
@@ -492,14 +502,16 @@ fstabsearch(key)
 
 #ifdef __linux__
 struct fstab *
-fstabsearchdir(key, directory)
-       char *key;
-       char *directory;
+fstabsearchdir(const char *key, char *directory)
 {
        register struct pfstab *pf;
        register struct fstab *fs;
        register struct fstab *found_fs = NULL;
        unsigned int size = 0;
+       struct stat buf;
+
+       if (stat(key, &buf) == 0 && S_ISBLK(buf.st_mode))
+               return NULL;
 
        for (pf = table; pf != NULL; pf = pf->pf_next) {
                fs = pf->pf_fstab;
@@ -529,51 +541,51 @@ fstabsearchdir(key, directory)
  *     Tell the operator what to do
  */
 void
-lastdump(arg)
-       char    arg;    /* w ==> just what to do; W ==> most recent dumps */
+lastdump(char arg) /* w ==> just what to do; W ==> most recent dumps */
 {
        register int i;
        register struct fstab *dt;
-       register struct dumpdates *dtwalk;
+       register struct dumpdates *dtwalk=NULL;
        char *lastname, *date;
        int dumpme;
        time_t tnow;
 
        (void) time(&tnow);
        getfstab();             /* /etc/fstab input */
-       initdumptimes();        /* /etc/dumpdates input */
-       qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
-
-       if (arg == 'w')
-               (void) printf("Dump these file systems:\n");
-       else
-               (void) printf("Last dump(s) done (Dump '>' file systems):\n");
-       lastname = "??";
-       ITITERATE(i, dtwalk) {
-               if (strncmp(lastname, dtwalk->dd_name,
-                   sizeof(dtwalk->dd_name)) == 0)
-                       continue;
-               date = (char *)ctime(&dtwalk->dd_ddate);
-               date[16] = '\0';        /* blast away seconds and year */
-               lastname = dtwalk->dd_name;
-               dt = fstabsearch(dtwalk->dd_name);
-               dumpme = (dt != NULL &&
-                   dt->fs_freq != 0 &&
-                   dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
-               if (arg != 'w' || dumpme)
-                       (void) printf(
-                           "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
-                           dumpme && (arg != 'w') ? '>' : ' ',
-                           dtwalk->dd_name,
-                           dt ? dt->fs_file : "",
-                           dtwalk->dd_level,
-                           date);
+       initdumptimes(0);       /* dumpdates input */
+       if (ddatev != NULL) {
+               qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
+
+               if (arg == 'w')
+                       (void) printf("Dump these file systems:\n");
+               else
+                       (void) printf("Last dump(s) done (Dump '>' file systems):\n");
+               lastname = "??";
+               ITITERATE(i, dtwalk) {
+                       if (strncmp(lastname, dtwalk->dd_name,
+                               sizeof(dtwalk->dd_name)) == 0)
+                               continue;
+                       date = (char *)ctime(&dtwalk->dd_ddate);
+                       date[16] = '\0';        /* blast away seconds and year */
+                       lastname = dtwalk->dd_name;
+                       dt = fstabsearch(dtwalk->dd_name);
+                       dumpme = (dt != NULL &&
+                               dt->fs_freq != 0 &&
+                               dtwalk->dd_ddate < tnow - (dt->fs_freq * 86400));
+                       if (arg != 'w' || dumpme)
+                               (void) printf(
+                                       "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
+                                       dumpme && (arg != 'w') ? '>' : ' ',
+                                       dtwalk->dd_name,
+                                       dt ? dt->fs_file : "",
+                                       dtwalk->dd_level,
+                                       date);
+               }
        }
 }
 
 int
-datesort(a1, a2)
-       const void *a1, *a2;
+datesort(const void *a1, const void *a2)
 {
        struct dumpdates *d1 = *(struct dumpdates **)a1;
        struct dumpdates *d2 = *(struct dumpdates **)a2;