home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / unrm.rm / unrm.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.5 KB  |  143 lines

  1. /**            unrm.c            **/
  2.  
  3. /** This is the companion program to the rm.c program, and will extract
  4.     files from the RM_DIR/login directory if they exist.  It checks to see 
  5.     that you are indeed the owner of the file before it'll let you copy
  6.     it AND it ensures that the file doesn't already exist in the current
  7.     directory (makes sense, eh?).
  8.  
  9.     This will not allow unrm'ing files that aren't owned by you, nor 
  10.     will it allow restores that replace a file of the same name in the
  11.     current directory (unless '-f' is specified, which will cause the
  12.     file to be replaced regardless).
  13.  
  14.     (C) Copyright 1986, Dave Taylor, Hewlett-Packard
  15. **/
  16.  
  17. #include <stdio.h>
  18. #include <errno.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21.  
  22. #define  RM_DIR        "/tmp/rm"
  23.  
  24. /** access modes for calls to 'access()' **/
  25.  
  26. #define  DIRACCESS    02 & 04
  27. #define  TOCOPY        04
  28. #define  TOREPLACE    02 & 04
  29.  
  30. #define  SLEN        80
  31.  
  32. int      force_overwrite = 0;        /* replace current regardless! */
  33.  
  34. char     *getenv(), *getlogin();
  35.  
  36. main(argc, argv)
  37. int argc;
  38. char **argv;
  39. {
  40.     extern int optind;    /* for getopt */
  41.     char   buffer[SLEN], login_name[SLEN], dirname[SLEN], *cp;
  42.     int    c;
  43.  
  44.     while ((c = getopt(argc, argv, "f")) != EOF) {
  45.       switch (c) {
  46.         case 'f' : force_overwrite++;    break;
  47.         case '?' : exit(fprintf(stderr,"Usage: unrm [-f] files\n"));
  48.       }
  49.     }
  50.  
  51.     if (argv[optind] == 0 || strlen(argv[optind]) == 0)
  52.       exit(0);
  53.  
  54.     if (access(RM_DIR, DIRACCESS)) {
  55.       fprintf(stderr,"Error: Directory %s doesn't exist!\n", RM_DIR);
  56.       exit(0);
  57.     }
  58.  
  59.     if ((cp = getenv("LOGNAME")) == NULL)
  60.       strcpy(login_name, getlogin());
  61.     else
  62.       strcpy(login_name, cp);
  63.  
  64.     sprintf(dirname, "%s/%s", RM_DIR, login_name);
  65.  
  66.     if (access(dirname, DIRACCESS)) {
  67.       fprintf(stderr,"Error: Directory %s doesn't exist!\n", dirname);
  68.       exit(0);
  69.     }
  70.  
  71.  
  72.     while (argv[optind] && strlen(argv[optind]) > 0) {
  73.       restore(dirname, argv[optind]);
  74.       optind++;
  75.     }
  76.  
  77.     exit(0);
  78. }
  79.  
  80. restore(directory, filename)
  81. char *directory, *filename;
  82. {
  83.     /** Try to link RM_DIR/filename to current directory.  If that
  84.         fails, try to copy it byte by byte... **/
  85.  
  86.     struct stat buffer;
  87.     char newfname[80], answer[80];
  88.  
  89.     sprintf(newfname,"%s/%s", directory, filename);
  90.  
  91.     if (access(newfname,TOCOPY) != 0)
  92.       return(fprintf(stderr,"Error: Can't find old copy of '%s'!\n", 
  93.          filename));
  94.     
  95.     if (stat(newfname, &buffer) != 0)
  96.       return(fprintf(stderr,"Error: Can't stat old copy of '%s'!\n", 
  97.          filename));
  98.  
  99.     if (buffer.st_uid != getuid())
  100.       return(fprintf(stderr,"Error: File '%s' isn't yours to restore!\n", 
  101.          filename));
  102.  
  103.     /** now we're ready to start some REAL work... **/
  104.  
  105.     if (access(filename,TOREPLACE) == 0) {    /* it exists! */
  106.       if (! force_overwrite)
  107.         printf(
  108.         "File %s already exists in this directory!  Replace it? (y/n) ",
  109.                filename);
  110.         gets(answer, 1);
  111.         if (tolower(answer[0]) != 'y') {
  112.           fprintf(stderr,"Restore of file %s cancelled\n", filename);
  113.           return;
  114.         }
  115.     }
  116.  
  117.     (void) unlink(filename);    /* blow it away, if it's here! */
  118.  
  119.     if (link(newfname, filename) != 0) {
  120.       FILE *infile, *outfile;    
  121.       int   c;
  122.       
  123.       if ((infile = fopen(newfname, "r")) == NULL)
  124.         exit(fprintf(stderr, 
  125.                  "Error: Can't read file '%s' to restore from!\n", 
  126.          newfname));
  127.  
  128.       if ((outfile = fopen(filename, "w")) == NULL)
  129.         exit(fprintf(stderr, "Error: Can't write to file '%s'!\n",
  130.          filename));
  131.  
  132.       while ((c = getc(infile)) != EOF)
  133.         putc(c, outfile);
  134.       
  135.       fclose(infile);
  136.       fclose(outfile);
  137.     }
  138.  
  139.     unlink(newfname);
  140.  
  141.     fprintf(stderr,"Restored file '%s'\n", filename);
  142. }
  143.