home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / can2 / xdevcan.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  1.6 KB  |  62 lines

  1. /***********************************************************************
  2. *
  3. *    XDEVCAN.C
  4. *        this is called and the file is copied via read and write to 
  5. *        the other device, sure it may be slow but it solves a problem 
  6. *        of cross device links crossdevcan(OLDFILE, NEWFILE)
  7. *
  8. ***********************************************************************/
  9.  
  10. #include "can.h"
  11.  
  12. crossdevcan(OLDFILE, NEWFILE)
  13. char *OLDFILE, *NEWFILE;
  14. {
  15.     int oldFD, newFD;    /* File Descriptors for access */
  16.     char NEXTCHAR[512];    /* used to transfer the char to new place */
  17.     int RWSIZE = 512;    /* amount to read and write */
  18.     struct stat BUF;    /* used to transfer all stats of old file to new */
  19.     int n_read;    /* the number of bytes successfully read */
  20.  
  21.     /* create the new file in the trashcan */
  22.     if((newFD = creat(NEWFILE, 00777)) < 0)
  23.     {
  24.         perror("Cross Device can");
  25.         return;
  26.     }
  27.     /* open the old one to be read */
  28.     if((oldFD = open(OLDFILE, O_RDONLY)) < 0)
  29.     {
  30.         perror("Cross Device can");
  31.         return;
  32.     }
  33.     /* repeatedly read and write until whole file is copied */
  34.     while((n_read = read(oldFD, NEXTCHAR, RWSIZE)) != 0)
  35.     {
  36.         if((write(newFD, NEXTCHAR, n_read)) != n_read)
  37.         {
  38.             perror("Cross Device can");
  39.             return;
  40.         }
  41.     }
  42.     /* close the files */
  43.     close(oldFD);
  44.     close(newFD);
  45.     /* get info concerning the old file */
  46.     stat(OLDFILE, &BUF);
  47.     /* make the new file's modes the same as the old one's */
  48.     if(chmod(NEWFILE, (int)(BUF.st_mode & 0777)) != 0)
  49.     {
  50.         perror("Cross Device can");
  51.         return;
  52.     }
  53.     /* make the new file's times the same as the old one's */
  54.     TIMES.actime= BUF.st_atime;
  55.     TIMES.modtime= BUF.st_mtime;
  56.     if(utime(NEWFILE, &TIMES) != 0)
  57.     {
  58.         perror("Cross Device can");
  59.         return;
  60.     }
  61. }
  62.