home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / elvis184.zip / src / elvrec.c < prev    next >
C/C++ Source or Header  |  1994-03-26  |  4KB  |  209 lines

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