home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / s1 / cp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  1.0 KB  |  58 lines

  1. /*
  2.  * cp oldfile newfile
  3.  */
  4.  
  5. main(argc,argv)
  6. char **argv;
  7. {
  8.     static int buf[256];
  9.     int fold, fnew, n;
  10.     register char *p1, *p2, *bp;
  11.     int mode;
  12.  
  13.     if(argc != 3) {
  14.         write(1, "Usage: cp oldfile newfile\n", 26);
  15.         exit(1);
  16.     }
  17.     if((fold = open(argv[1], 0)) < 0) {
  18.         write(1, "Cannot open old file.\n", 22);
  19.         exit(1);
  20.     }
  21.     fstat(fold, buf);
  22.     mode = buf[2];
  23.     /* is target a directory? */
  24.     if (stat(argv[2], buf+50)>=0 && (buf[52]&060000)==040000) {
  25.         p1 = argv[1];
  26.         p2 = argv[2];
  27.         bp = buf+100;
  28.         while(*bp++ = *p2++);
  29.         bp[-1] = '/';
  30.         p2 = bp;
  31.         while(*bp = *p1++)
  32.             if(*bp++ == '/')
  33.                 bp = p2;
  34.         argv[2] = buf+100;
  35.     }
  36.     if (stat(argv[2], buf+50) >= 0) {
  37.         if (buf[0]==buf[50] && buf[1]==buf[51]) {
  38.             write(1, "Copying file to itself.\n", 24);
  39.             exit(1);
  40.         }
  41.     }
  42.     if ((fnew = creat(argv[2], mode)) < 0) {
  43.         write(1, "Can't create new file.\n", 23);
  44.         exit(1);
  45.     }
  46.     while(n = read(fold,  buf,  512)) {
  47.     if(n < 0) {
  48.         write(1, "Read error\n", 11);
  49.         exit(1);
  50.     } else
  51.         if(write(fnew, buf, n) != n){
  52.             write(1, "Write error.\n", 13);
  53.             exit(1);
  54.         }
  55.     }
  56.     exit(0);
  57. }
  58.