home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / Editors / mjovesrc.zoo / recover.c < prev    next >
C/C++ Source or Header  |  1992-04-04  |  17KB  |  813 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7.  
  8. /* Recovers JOVE files after a system/editor crash.
  9.    Usage: recover [-d directory] [-syscrash]
  10.    The -syscrash option is specified in /etc/rc.  It directs recover to
  11.    move all the jove tmp files from TMP_DIR (/tmp) to REC_DIR (/usr/preserve).
  12.    recover -syscrash must be invoked in /ect/rc BEFORE /tmp gets cleared out.
  13.    (about the same place as expreserve gets invoked to save ed/vi/ex files.
  14.  
  15.    The -d option lets you specify the directory to search for tmp files when
  16.    the default isn't the right one.
  17.  
  18.    Look in Makefile to change the default directories. */
  19.  
  20. #include <stdio.h>    /* Do stdio first so it doesn't override OUR
  21.                definitions. */
  22. #include "jove.h"
  23. #include "temp.h"
  24. #include "rec.h"
  25. #include "rectune.h"
  26. #include "wait.h"
  27.  
  28. #ifdef UNIX
  29. # include <signal.h>
  30. # include <sys/file.h>
  31. # include <pwd.h>
  32. # include <time.h>
  33. #endif
  34.  
  35. #ifdef    SYSV
  36. # include <sys/utsname.h>
  37. #endif
  38.  
  39. #ifndef    L_SET
  40. # define L_SET    0
  41. # define L_INCR    1
  42. #endif
  43.  
  44. extern char    *ctime proto((const time_t *));
  45.  
  46. private char    blk_buf[JBUFSIZ];
  47. private int    nleft;
  48. private FILE    *ptrs_fp;
  49. private int    data_fd;
  50. private struct rec_head    Header;
  51. private long    Nchars,
  52.     Nlines;
  53. private char    tty[] = "/dev/tty";
  54. private int    UserID,
  55.     Verbose = 0;
  56. private char    *Directory = NULL;        /* the directory we're looking in */
  57.  
  58. private struct file_pair {
  59.     char    *file_data,
  60.         *file_rec;
  61. #define INSPECTED    01
  62.     int    file_flags;
  63.     struct file_pair    *file_next;
  64. } *First = NULL;
  65.  
  66. private struct rec_entry    *buflist[100];    /* system initializes to 0 */
  67.  
  68. #ifndef F_COMPLETION
  69. # define F_COMPLETION    /* since scandir.c is surrounded by an ifdef */
  70. #endif
  71.  
  72. /* simpler version of one in util.c, needed by scandir.c */
  73. UnivPtr
  74. emalloc(size)
  75. size_t size;
  76. {
  77.     register UnivPtr ptr;
  78.  
  79.     if ((ptr = malloc(size)) == NULL) {
  80.         fprintf(stderr, "couldn't malloc(%d)\n", size);
  81.         exit(1);
  82.     }
  83.     return ptr;
  84. }
  85.         
  86. /* simpler version of one in util.c, needed by scandir.c */
  87. UnivPtr
  88. erealloc(ptr, size)
  89. UnivPtr ptr;
  90. size_t size;
  91. {
  92.     if ((ptr = realloc(ptr, size)) == NULL) {
  93.         fprintf(stderr, "couldn't realloc(%d)\n", size);
  94.         exit(1);
  95.     }
  96.     return ptr;
  97. }
  98.  
  99. /* duplicated in util.c, needed by scandir.c */
  100. void
  101. null_ncpy(to, from, n)
  102. char    *to,
  103.     *from;
  104. size_t    n;
  105. {
  106.     (void) strncpy(to, from, n);
  107.     to[n] = '\0';
  108. }
  109.  
  110. #define complain printf    /* kludge! needed by scandir.c */
  111. #include "scandir.c"    /* to get dirent simulation and jscandir */
  112.  
  113. /* Get a line at `tl' in the tmp file into `buf' which should be LBSIZE
  114.    long. */
  115.  
  116. private char    *getblock proto((daddr atl));
  117.  
  118. void
  119. getline(tl, buf)
  120. daddr    tl;
  121. char    *buf;
  122. {
  123.     register char    *bp,
  124.             *lp;
  125.     register int    nl;
  126.  
  127.     lp = buf;
  128.     bp = getblock(tl >> 1);
  129.     nl = nleft;
  130.     tl = blk_round(tl);
  131.  
  132.     while ((*lp++ = *bp++) != '\0') {
  133.         if (--nl == 0) {
  134.             tl = forward_block(tl);
  135.             bp = getblock(tl >> 1);
  136.             nl = nleft;
  137.         }
  138.     }
  139. }
  140.  
  141. private char *
  142. getblock(atl)
  143. daddr    atl;
  144. {
  145.     int    bno,
  146.         off;
  147.     static int    curblock = -1;
  148.  
  149.     bno = da_to_bno(atl);
  150.     off = da_to_off(atl);
  151.     nleft = JBUFSIZ - off;
  152.  
  153.     if (bno != curblock) {
  154.         lseek(data_fd, (long) bno * JBUFSIZ, L_SET);
  155.         read(data_fd, (UnivPtr)blk_buf, (size_t)JBUFSIZ);
  156.         curblock = bno;
  157.     }
  158.     return blk_buf + off;
  159. }
  160.  
  161. char *
  162. copystr(s)
  163. char    *s;
  164. {
  165.     char    *str;
  166.  
  167.     str = malloc((size_t) (strlen(s) + 1));
  168.     strcpy(str, s);
  169.  
  170.     return str;
  171. }
  172.  
  173. private char    *CurDir;
  174.  
  175. /* Scan the DIRNAME directory for jove tmp files, and make a linked list
  176.    out of them. */
  177.  
  178. private int    add_name proto((char *));
  179.  
  180. private void
  181. get_files(dirname)
  182. char    *dirname;
  183. {
  184.     struct direct    **nmptr;
  185.  
  186.     CurDir = dirname;
  187.     First = NULL;
  188.     jscandir(dirname, &nmptr, add_name,
  189.         (int (*) proto((UnivConstPtr, UnivConstPtr)))NULL);
  190. }
  191.  
  192. private int
  193. add_name(fname)
  194. char *fname;
  195. {
  196.     char    dfile[128],
  197.         rfile[128];
  198.     struct file_pair    *fp;
  199.     struct rec_head        header;
  200.     int    fd;
  201.  
  202.     if (strncmp(fname, "jrec", (size_t)4) != 0)
  203.         return 0;
  204.     /* If we get here, we found a "recover" tmp file, so now
  205.        we look for the corresponding "data" tmp file.  First,
  206.        though, we check to see whether there is anything in
  207.        the "recover" file.  If it's 0 length, there's no point
  208.        in saving its name. */
  209.     (void) sprintf(rfile, "%s/%s", CurDir, fname);
  210.     (void) sprintf(dfile, "%s/jove%s", CurDir, fname + 4);
  211.     if ((fd = open(rfile, 0)) != -1) {
  212.         if ((read(fd, (UnivPtr) &header, sizeof header) != sizeof header)) {
  213.             close(fd);
  214.             return 0;
  215.         } else
  216.             close(fd);
  217.     }
  218.     if (access(dfile, 0) != 0) {
  219.         fprintf(stderr, "recover: can't find the data file for %s/%s\n", Directory, fname);
  220.         fprintf(stderr, "so deleting...\n");
  221.         (void) unlink(rfile);
  222.         (void) unlink(dfile);
  223.         return 0;
  224.     }
  225.     /* If we get here, we've found both files, so we put them
  226.        in the list. */
  227.     fp = (struct file_pair *) malloc (sizeof *fp);
  228.     if (fp == NULL) {
  229.         fprintf(stderr, "recover: cannot malloc for file_pair.\n");
  230.         exit(-1);
  231.     }
  232.     fp->file_data = copystr(dfile);
  233.     fp->file_rec = copystr(rfile);
  234.     fp->file_flags = 0;
  235.     fp->file_next = First;
  236.     First = fp;
  237.  
  238.     return 1;
  239. }
  240.  
  241. private void
  242. options()
  243. {
  244.     printf("Options are:\n");
  245.     printf("    ?        list options.\n");
  246.     printf("    get        get a buffer to a file.\n");
  247.     printf("    list        list known buffers.\n");
  248.     printf("    print        print a buffer to terminal.\n");
  249.     printf("    quit        quit and delete jove tmp files.\n");
  250.     printf("    restore        restore all buffers.\n");
  251. }
  252.  
  253. /* Returns a legitimate buffer # */
  254.  
  255. private void    tellme proto((char *, char *)),
  256.     list proto((void));
  257.  
  258. private struct rec_entry **
  259. getsrc()
  260. {
  261.     char    name[128];
  262.     int    number;
  263.  
  264.     for (;;) {
  265.         tellme("Which buffer ('?' for list)? ", name);
  266.         if (name[0] == '?')
  267.             list();
  268.         else if (name[0] == '\0')
  269.             return NULL;
  270.         else if ((number = atoi(name)) > 0 && number <= Header.Nbuffers)
  271.             return &buflist[number];
  272.         else {
  273.             int    i;
  274.  
  275.             for (i = 1; i <= Header.Nbuffers; i++)
  276.                 if (strcmp(buflist[i]->r_bname, name) == 0)
  277.                     return &buflist[i];
  278.             printf("%s: unknown buffer.\n", name);
  279.         }
  280.     }
  281. }
  282.  
  283. /* Get a destination file name. */
  284.  
  285. static char *
  286. getdest()
  287. {
  288.     static char    filebuf[256];
  289.  
  290.     tellme("Output file: ", filebuf);
  291.     if (filebuf[0] == '\0')
  292.         return NULL;
  293.     return filebuf;
  294. }
  295.  
  296. #include "ctype.h"
  297.  
  298. private char *
  299. readword(buf)
  300. char    *buf;
  301. {
  302.     int    c;
  303.     char    *bp = buf;
  304.  
  305.     do ; while (strchr(" \t\n", c = getchar()));
  306.  
  307.     do {
  308.         if (strchr(" \t\n", c))
  309.             break;
  310.         *bp++ = c;
  311.     } while ((c = getchar()) != EOF);
  312.     *bp = '\0';
  313.  
  314.     return buf;
  315. }
  316.  
  317. private void
  318. tellme(quest, answer)
  319. char    *quest,
  320.     *answer;
  321. {
  322.     if (stdin->_cnt <= 0) {
  323.         printf("%s", quest);
  324.         fflush(stdout);
  325.     }
  326.     readword(answer);
  327. }
  328.  
  329. /* Print the specified file to standard output. */
  330.  
  331. private jmp_buf    int_env;
  332.  
  333. private SIGRESULT
  334. catch(junk)
  335. int    junk;
  336. {
  337.     longjmp(int_env, 1);
  338.     /*NOTREACHED*/
  339. }
  340.  
  341. private void    get proto((struct rec_entry **src, char *dest));
  342.  
  343. private void
  344. restore()
  345. {
  346.     register int    i;
  347.     char    tofile[100],
  348.         answer[30];
  349.     int    nrecovered = 0;
  350.  
  351.     for (i = 1; i <= Header.Nbuffers; i++) {
  352.         (void) sprintf(tofile, "#%s", buflist[i]->r_bname);
  353. tryagain:
  354.         printf("Restoring %s to %s, okay?", buflist[i]->r_bname,
  355.                              tofile);
  356.         tellme(" ", answer);
  357.         switch (answer[0]) {
  358.         case 'y':
  359.             break;
  360.  
  361.         case 'n':
  362.             continue;
  363.  
  364.         default:
  365.             tellme("What file should I use instead? ", tofile);
  366.             goto tryagain;
  367.         }
  368.         get(&buflist[i], tofile);
  369.         nrecovered += 1;
  370.     }
  371.     printf("Recovered %d buffers.\n", nrecovered);
  372. }
  373.  
  374. private void    dump_file proto((int which, FILE *out));
  375.  
  376. private void
  377. get(src, dest)
  378. struct rec_entry    **src;
  379. char    *dest;
  380. {
  381.     FILE    *outfile;
  382.  
  383.     if (src == NULL || dest == NULL)
  384.         return;
  385.     (void) signal(SIGINT, catch);
  386.     if (setjmp(int_env) == 0) {
  387.         if (dest == tty)
  388.             outfile = stdout;
  389.         else {
  390.             if ((outfile = fopen(dest, "w")) == NULL) {
  391.                 printf("recover: cannot create %s.\n", dest);
  392.                 (void) signal(SIGINT, SIG_DFL);
  393.                 return;
  394.             }
  395.             printf("\"%s\"", dest);
  396.         }
  397.         dump_file(src - buflist, outfile);
  398.     } else
  399.         printf("\nAborted!\n");
  400.     (void) signal(SIGINT, SIG_DFL);
  401.     if (dest != tty) {
  402.         fclose(outfile);
  403.         printf(" %ld lines, %ld characters.\n", Nlines, Nchars);
  404.     }
  405. }
  406.  
  407. private char **
  408. scanvec(args, str)
  409. register char    **args,
  410.         *str;
  411. {
  412.     while (*args) {
  413.         if (strcmp(*args, str) == 0)
  414.             return args;
  415.         args += 1;
  416.     }
  417.     return NULL;
  418. }
  419.  
  420. private void
  421. read_rec(recptr)
  422. struct rec_entry    *recptr;
  423. {
  424.     if (fread((UnivPtr) recptr, sizeof *recptr, (size_t)1, ptrs_fp) != 1)
  425.         fprintf(stderr, "recover: cannot read record.\n");
  426. }
  427.  
  428. private void
  429. seekto(which)
  430. int    which;
  431. {
  432.     long    offset;
  433.     int    i;
  434.  
  435.     offset = sizeof (Header) + (Header.Nbuffers * sizeof (struct rec_entry));
  436.     for (i = 1; i < which; i++)
  437.         offset += buflist[i]->r_nlines * sizeof (daddr);
  438.     fseek(ptrs_fp, offset, L_SET);
  439. }
  440.  
  441. private void
  442. makblist()
  443. {
  444.     int    i;
  445.  
  446.     fseek(ptrs_fp, (long) sizeof (Header), L_SET);
  447.     for (i = 1; i <= Header.Nbuffers; i++) {
  448.         if (buflist[i] == NULL)
  449.             buflist[i] = (struct rec_entry *) malloc (sizeof (struct rec_entry));
  450.         read_rec(buflist[i]);
  451.     }
  452.     while (buflist[i]) {
  453.         free((UnivPtr) buflist[i]);
  454.         buflist[i] = NULL;
  455.         i += 1;
  456.     }
  457. }
  458.  
  459. private daddr
  460. getaddr(fp)
  461. register FILE    *fp;
  462. {
  463.     register int    nchars = sizeof (daddr);
  464.     daddr    addr;
  465.     register char    *cp = (char *) &addr;
  466.  
  467.     while (--nchars >= 0)
  468.         *cp++ = getc(fp);
  469.  
  470.     return addr;
  471. }
  472.  
  473. private void
  474. dump_file(which, out)
  475. int    which;
  476. FILE    *out;
  477. {
  478.     register int    nlines;
  479.     register daddr    addr;
  480.     char    buf[JBUFSIZ];
  481.  
  482.     seekto(which);
  483.     nlines = buflist[which]->r_nlines;
  484.     Nchars = Nlines = 0L;
  485.     while (--nlines >= 0) {
  486.         addr = getaddr(ptrs_fp);
  487.         getline(addr, buf);
  488.         Nlines += 1;
  489.         Nchars += 1 + strlen(buf);
  490.         fputs(buf, out);
  491.         if (nlines > 0)
  492.             fputc('\n', out);
  493.     }
  494. }
  495.  
  496. /* List all the buffers. */
  497.  
  498. private void
  499. list()
  500. {
  501.     int    i;
  502.  
  503.     for (i = 1; i <= Header.Nbuffers; i++)
  504.         printf("%d) buffer %s  \"%s\" (%d lines)\n", i,
  505.             buflist[i]->r_bname,
  506.             buflist[i]->r_fname,
  507.             buflist[i]->r_nlines);
  508. }
  509.  
  510. private void    ask_del proto((char *prompt, struct file_pair *fp));
  511.  
  512. private int
  513. doit(fp)
  514. struct file_pair    *fp;
  515. {
  516.     char    answer[30];
  517.     char    *datafile = fp->file_data,
  518.         *pntrfile = fp->file_rec;
  519.  
  520.     ptrs_fp = fopen(pntrfile, "r");
  521.     if (ptrs_fp == NULL) {
  522.         if (Verbose)
  523.             fprintf(stderr, "recover: cannot read rec file (%s).\n", pntrfile);
  524.         return 0;
  525.     }
  526.     fread((UnivPtr) &Header, sizeof Header, (size_t)1, ptrs_fp);
  527.     if (Header.Uid != UserID)
  528.         return 0;
  529.  
  530.     /* Don't ask about JOVE's that are still running ... */
  531. #ifdef    KILL0
  532.     if (kill(Header.Pid, 0) == 0)
  533.         return 0;
  534. #endif    /* KILL0 */
  535.  
  536.     if (Header.Nbuffers == 0) {
  537.         printf("There are no modified buffers in %s; should I delete the tmp file?", pntrfile);
  538.         ask_del(" ", fp);
  539.         return 1;
  540.     }
  541.  
  542.     if (Header.Nbuffers < 0) {
  543.         fprintf(stderr, "recover: %s doesn't look like a jove file.\n", pntrfile);
  544.         ask_del("Should I delete it? ", fp);
  545.         return 1;    /* We'll, we sort of found something. */
  546.     }
  547.     printf("Found %d buffer%s last updated: %s",
  548.         Header.Nbuffers,
  549.         Header.Nbuffers != 1 ? "s" : "",
  550.         ctime(&Header.UpdTime));
  551.     data_fd = open(datafile, 0);
  552.     if (data_fd == -1) {
  553.         fprintf(stderr, "recover: but I can't read the data file (%s).\n", datafile);
  554.         ask_del("Should I delete the tmp files? ", fp);
  555.         return 1;
  556.     }
  557.     makblist();
  558.     list();
  559.  
  560.     for (;;) {
  561.         tellme("(Type '?' for options): ", answer);
  562.         switch (answer[0]) {
  563.         case '\0':
  564.             continue;
  565.  
  566.         case '?':
  567.             options();
  568.             break;
  569.  
  570.         case 'l':
  571.             list();
  572.             break;
  573.  
  574.         case 'p':
  575.             get(getsrc(), tty);
  576.             break;
  577.  
  578.         case 'q':
  579.             ask_del("Shall I delete the tmp files? ", fp);
  580.             return 1;
  581.  
  582.         case 'g':
  583.             {    /* So it asks for src first. */
  584.             char    *dest;
  585.             struct rec_entry    **src;
  586.  
  587.             if ((src = getsrc()) == NULL)
  588.                 break;
  589.             dest = getdest();
  590.             get(src, dest);
  591.             break;
  592.             }
  593.  
  594.         case 'r':
  595.             restore();
  596.             break;
  597.  
  598.         default:
  599.             printf("I don't know how to \"%s\"!\n", answer);
  600.             break;
  601.         }
  602.     }
  603. }
  604.  
  605. private void    del_files proto((struct file_pair *fp));
  606.  
  607. private void
  608. ask_del(prompt, fp)
  609. char    *prompt;
  610. struct file_pair    *fp;
  611. {
  612.     char    yorn[20];
  613.  
  614.     tellme(prompt, yorn);
  615.     if (yorn[0] == 'y')
  616.         del_files(fp);
  617. }
  618.  
  619. private void
  620. del_files(fp)
  621. struct file_pair    *fp;
  622. {
  623.     (void) unlink(fp->file_data);
  624.     (void) unlink(fp->file_rec);
  625. }
  626.  
  627.  
  628.  
  629. private void
  630. MailUser(rec)
  631. struct rec_head *rec;
  632. {
  633. #ifdef    SYSV
  634.     struct utsname mach;
  635. #else
  636.     char mach[BUFSIZ];
  637. #endif
  638.     char mail_cmd[BUFSIZ];
  639.     char *last_update;
  640.     char *buf_string;
  641.     FILE *mail_pipe;
  642.     struct passwd *pw;
  643.     extern struct passwd *getpwuid();
  644.  
  645.     if ((pw = getpwuid(rec->Uid))== NULL)
  646.         return;
  647. #ifdef    SYSV
  648.     if (uname(&mach) < 0)
  649.         strcpy(mach.sysname, "unknown");
  650. #else
  651.     {
  652.         extern int    gethostname proto((const char *, size_t));
  653.  
  654.         gethostname(mach, sizeof(mach));
  655.     }
  656. #endif
  657.     last_update = ctime(&(rec->UpdTime));
  658.     /* Start up mail */
  659.     sprintf(mail_cmd, "/bin/mail %s", pw->pw_name);
  660.     setuid(getuid());
  661.     if ((mail_pipe = popen(mail_cmd, "w")) == NULL)
  662.         return;
  663.     setbuf(mail_pipe, mail_cmd);
  664.     /* Let's be grammatically correct! */
  665.     if (rec->Nbuffers == 1)
  666.         buf_string = "buffer";
  667.     else
  668.         buf_string = "buffers";
  669.     fprintf(mail_pipe, "Subject: System crash\n");
  670.     fprintf(mail_pipe, " \n");
  671.     fprintf(mail_pipe, "Jove saved %d %s when the system \"%s\"\n",
  672.      rec->Nbuffers, buf_string,
  673. #ifdef    SYSV
  674.      mach.sysname
  675. #else
  676.      mach
  677. #endif
  678.      );
  679.     fprintf(mail_pipe, "crashed on %s\n\n", last_update);
  680.     fprintf(mail_pipe, "You can retrieve the %s using Jove's -r\n",
  681.      buf_string);
  682.     fprintf(mail_pipe, "(recover option) i.e. give the command.\n");
  683.     fprintf(mail_pipe, "\tjove -r\n");
  684.     fprintf(mail_pipe, "See the Jove manual for more details\n");
  685.     pclose(mail_pipe);
  686. }
  687.  
  688.  
  689. private void
  690. savetmps()
  691. {
  692.     struct file_pair    *fp;
  693.     union wait    status;
  694.     int    pid,
  695.         fd;
  696.     struct rec_head        header;
  697.     char    buf[BUFSIZ];
  698.     char    *fname;
  699.     struct stat        stbuf;
  700.  
  701.     if (strcmp(TMP_DIR, REC_DIR) == 0)
  702.         return;        /* Files are moved to the same place. */
  703.     get_files(TMP_DIR);
  704.     for (fp = First; fp != NULL; fp = fp->file_next) {
  705.         stat(fp->file_data, &stbuf);
  706.         switch (pid = fork()) {
  707.         case -1:
  708.             fprintf(stderr, "recover: can't fork\n!");
  709.             exit(-1);
  710.             /*NOTREACHED*/
  711.  
  712.         case 0:
  713.             fprintf(stderr, "Recovering: %s, %s\n", fp->file_data,
  714.              fp->file_rec);
  715.             if ((fd = open(fp->file_rec, 0)) != -1) {
  716.                 if ((read(fd, (UnivPtr) &header, sizeof header) != sizeof header)) {
  717.                     close(fd);
  718.                     return;
  719.                 } else
  720.                     close(fd);
  721.             }
  722.             MailUser(&header);
  723.             execl("/bin/mv", "mv", fp->file_data, fp->file_rec,
  724.                   REC_DIR, (char *)NULL);
  725.             fprintf(stderr, "recover: cannot execl /bin/mv.\n");
  726.             exit(-1);
  727.             /*NOTREACHED*/
  728.  
  729.         default:
  730.             do ; while (wait(&status) != pid);
  731.             if (status.w_status != 0)
  732.                 fprintf(stderr, "recover: non-zero status (%d) returned from copy.\n", status.w_status);
  733.             fname = fp->file_data + strlen(TMP_DIR);
  734.             strcpy(buf, REC_DIR);
  735.             strcat(buf, fname);
  736.             if (chown(buf, (int) stbuf.st_uid, (int) stbuf.st_gid) != 0)
  737.                 perror("recover: chown failed.");
  738.             fname = fp->file_rec + strlen(TMP_DIR);
  739.             strcpy(buf, REC_DIR);
  740.             strcat(buf, fname);
  741.             if (chown(buf, (int) stbuf.st_uid, (int) stbuf.st_gid) != 0)
  742.                 perror("recover: chown failed.");
  743.         }
  744.     }
  745. }
  746.  
  747. private int
  748. lookup(dir)
  749. char    *dir;
  750. {
  751.     struct file_pair    *fp;
  752.     int    nfound = 0;
  753.  
  754.     printf("Checking %s ...\n", dir);
  755.     Directory = dir;
  756.     get_files(dir);
  757.     for (fp = First; fp != NULL; fp = fp->file_next) {
  758.         nfound += doit(fp);
  759.         if (ptrs_fp)
  760.             (void) fclose(ptrs_fp);
  761.         if (data_fd > 0)
  762.             (void) close(data_fd);
  763.     }
  764.     return nfound;
  765. }
  766.  
  767. int
  768. main(argc, argv)
  769. int    argc;
  770. char    *argv[];
  771. {
  772.     int    nfound;
  773.     char    **argvp;
  774.     char    *tmp_dir;
  775.  
  776.     UserID = getuid();
  777.  
  778.     if (scanvec(argv, "-help")) {
  779.         printf("recover: usage: recover [-d directory] [-syscrash]\n");
  780.         printf("Use \"jove -r\" after JOVE has died for some\n");
  781.         printf("unknown reason.\n\n");
  782.         printf("Use \"%s -syscrash\"\n", Recover);
  783.         printf("when the system is in the process of rebooting.");
  784.         printf("This is done automatically at reboot time\n");
  785.         printf("and so most of you don't have to worry about that.\n\n");
  786.         printf("Use \"recover -d directory\" when the tmp files are store\n");
  787.         printf("in DIRECTORY instead of the default one (/tmp).\n");
  788.         exit(0);
  789.     }
  790.     if (scanvec(argv, "-v"))
  791.         Verbose = YES;
  792.     if (scanvec(argv, "-syscrash")) {
  793.         printf("Recovering jove files ... ");
  794.         savetmps();
  795.         printf("Done.\n");
  796.         exit(0);
  797.     }
  798.     if ((argvp = scanvec(argv, "-uid")) != NULL)
  799.         UserID = atoi(argvp[1]);
  800.     if ((argvp = scanvec(argv, "-d")) != NULL)
  801.         tmp_dir = argvp[1];
  802.     else
  803.         tmp_dir = TmpFilePath;
  804.     /* Check default directory */
  805.     nfound = lookup(tmp_dir);
  806.     /* Check whether anything was saved when system died? */
  807.     if (strcmp(tmp_dir, REC_DIR) != 0)
  808.         nfound += lookup(REC_DIR);
  809.     if (nfound == 0)
  810.         printf("There's nothing to recover.\n");
  811.     return 0;
  812. }
  813.