home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / apps / text_ed / elv16b2 / st / elvrec.c < prev    next >
C/C++ Source or Header  |  1992-05-13  |  4KB  |  217 lines

  1. /* elvrec.c */
  2.  
  3. /* This file contains the file recovery program */
  4.  
  5. /* Author:
  6.  *    Steve Kirkendall
  7.  *    14407 SW Teal Blvd. #C
  8.  *    Beaverton, OR 97005
  9.  *    kirkenda@cs.pdx.edu
  10.  */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include "config.h"
  15. #include "vi.h"
  16.  
  17. void recover P_((char *, char *));
  18. void main P_((int, char **));
  19.  
  20.  
  21. #if MINT
  22. extern    int __mint;
  23. #endif
  24.  
  25. void recover(basename, outname)
  26.     char    *basename;    /* the name of the file to recover */
  27.     char    *outname;    /* the name of the file to write to */
  28. {
  29.     char    pathname[500];    /* full pathname of the file to recover */
  30.     char    line[600];    /* a line from the /usr/preserve/Index file */
  31.     int    ch;        /* a character from the text being recovered */
  32.     FILE    *from;        /* the /usr/preserve file, or /usr/preserve/Index */
  33.     FILE    *to;        /* the user's text file */
  34.     char    *ptr;
  35. #if OSK
  36.     int        uid;
  37. #endif
  38.  
  39.     /* convert basename to a full pathname */
  40.     if (basename)
  41.     {
  42. #ifndef CRUNCH
  43. # if MSDOS || TOS || MINT
  44.         if (!basename[0] || basename[1] != ':')
  45. # else
  46.         if (basename[0] != SLASH)
  47. # endif
  48.         {
  49.             ptr = getcwd(pathname, (int) sizeof pathname);
  50.             if (ptr != pathname)
  51.             {
  52.                 strcpy(pathname, ptr);
  53.             }
  54.             ptr = pathname + strlen(pathname);
  55. #if MINT
  56.             if (__mint && (*basename == '/' || *basename == '\\') &&
  57.                 (*pathname | 0x20) == 'u' && pathname[1] == ':')
  58.                 ptr = pathname;
  59. #endif
  60.             *ptr++ = SLASH;
  61.             strcpy(ptr, basename);
  62.         }
  63.         else
  64. #endif
  65.         {
  66.             strcpy(pathname, basename);
  67.         }
  68.     }
  69.  
  70. #if OSK
  71.     uid = getuid();
  72.     if(setuid(0))
  73.         exit(_errmsg(errno, "Can't set uid\n"));
  74. #endif
  75.     /* scan the /usr/preserve/Index file, for the *oldest* unrecovered
  76.      * version of this file.
  77.      */
  78.     from = fopen(PRSVINDEX, "r");
  79.     while (from && fgets(line, sizeof line, from))
  80.     {
  81.         /* strip off the newline from the end of the string */
  82.         line[strlen(line) - 1] = '\0';
  83.  
  84.         /* parse the line into a "preserve" name and a "text" name */
  85.         for (ptr = line; *ptr != ' '; ptr++)
  86.         {
  87.         }
  88.         *ptr++ = '\0';
  89.  
  90.         /* If the "preserve" file is missing, then ignore this line
  91.          * because it describes a file that has already been recovered.
  92.          */
  93.         if (access(line, 0) < 0)
  94.         {
  95.             continue;
  96.         }
  97.  
  98.         /* are we looking for a specific file? */
  99.         if (basename)
  100.         {
  101.             /* quit if we found it */
  102.             if (!strcmp(ptr, pathname))
  103.             {
  104.                 break;
  105.             }
  106.         }
  107.         else
  108.         {
  109.             /* list this file as "available for recovery" */
  110.             puts(ptr);
  111.         }
  112.     }
  113.  
  114.     /* file not found? */
  115.     if (!basename || !from || feof(from))
  116.     {
  117.         if (from != NULL) fclose(from);
  118.         if (basename)
  119.         {
  120.             fprintf(stderr, "%s: no recovered file has that exact name\n", pathname);
  121.         }
  122.         return;
  123.     }
  124.     if (from != NULL) fclose(from);
  125.  
  126.     /* copy the recovered text back into the user's file... */
  127.  
  128.     /* open the /usr/preserve file for reading */
  129.     from = fopen(line, "r");
  130.     if (!from)
  131.     {
  132.         perror(line);
  133.         exit(2);
  134.     }
  135.  
  136. #if ANY_UNIX
  137.     /* Be careful about user-id.  We want to be running under the user's
  138.      * real id when we open/create the user's text file... but we want
  139.      * to be superuser when we delete the /usr/preserve file.  For UNIX,
  140.      * we accomplish this by deleting the /usr/preserve file *now*,
  141.      * when it is open but before we've read it.  Then we revert to the
  142.      * user's real id.
  143.      */
  144.     unlink(line);
  145.     setuid(getuid());
  146. #endif
  147. #if OSK
  148.     setuid(uid);
  149. #endif
  150.  
  151.     if (outname == NULL) return;
  152.  
  153.     /* open the user's file for writing */
  154.     to = fopen(outname, "w");
  155.     if (!to)
  156.     {
  157.         perror(ptr);
  158.         exit(2);
  159.     }
  160.  
  161.     /* copy the text */
  162.     while ((ch = getc(from)) != EOF)
  163.     {
  164.         putc(ch, to);
  165.     }
  166.  
  167. #if !ANY_UNIX
  168. #if OSK
  169.     fclose(from);
  170.     setuid(0);
  171. #endif
  172. #if TOS || MINT
  173.     /* GEMDOS doesn't know how to unlink a file thats still open...
  174.      *  it might just trash the filesystem instead :-(  -nox */
  175.     fclose(from);
  176. #endif
  177.     /* delete the /usr/preserve file */
  178.     unlink(line);
  179. #if OSK
  180.     setuid(uid);
  181. #endif
  182. #endif
  183. }
  184.  
  185. void
  186. main(argc, argv)
  187.     int    argc;
  188.     char    **argv;
  189. {
  190.     /* check arguments */
  191.     if (argc > 3)
  192.     {
  193.         fprintf(stderr, "usage: %s [preserved_file [recovery_file]]\n", argv[0]);
  194.         exit(2);
  195.     }
  196.  
  197.     /* recover the requested file, or list recoverable files */
  198.     if (argc == 3)
  199.     {
  200.         /* recover the file, but write it to a different filename */
  201.         recover (argv[1], argv[2]);
  202.     }
  203.     else if (argc == 2)
  204.     {
  205.         /* recover the file */
  206.         recover(argv[1], argv[1]);
  207.     }
  208.     else
  209.     {
  210.         /* list the recoverable files */
  211.         recover((char *)0, (char *)0);
  212.     }
  213.  
  214.     /* success! */
  215.     exit(0);
  216. }
  217.