1: #ifndef lint
   2: static char *sccsid = "@(#)cp.c	4.1 (Berkeley) 10/1/80";
   3: #endif
   4: /*
   5:  * cp oldfile newfile
   6:  */
   7: 
   8: #define BSIZE   1024
   9: #include <stdio.h>
  10: #include <sys/types.h>
  11: #include <sys/stat.h>
  12: struct  stat    stbuf1, stbuf2;
  13: char    iobuf[BSIZE];
  14: int iflag = 0;  /* interactive flag. If this flag is set,
  15: 			 * the user is queried before files are
  16: 			 * destroyed by cp.
  17: 			 */
  18: 
  19: main(argc, argv)
  20: char *argv[];
  21: {
  22:     register i, r;
  23: 
  24:     /* get the flag(s) */
  25: 
  26:     if (argc < 2)
  27:         goto usage;
  28:     if (*argv[1] == '-') {
  29:         argc--;
  30:         while (*++argv[1] != '\0')
  31:             switch (*argv[1]) {
  32: 
  33:             /* interactive mode */
  34:             case 'i':
  35:                 iflag++;
  36:                 break;
  37: 
  38:             /* don't live with bad options */
  39:             default:
  40:                 goto usage;
  41:             }
  42:         argv++;
  43:     }
  44:     if (argc < 3)
  45:         goto usage;
  46:     if (argc > 3) {
  47:         if (stat(argv[argc-1], &stbuf2) < 0)
  48:             goto usage;
  49:         if ((stbuf2.st_mode&S_IFMT) != S_IFDIR)
  50:             goto usage;
  51:     }
  52:     r = 0;
  53:     for(i=1; i<argc-1;i++)
  54:         r |= copy(argv[i], argv[argc-1]);
  55:     exit(r);
  56: usage:
  57:     fprintf(stderr, "Usage: cp: f1 f2; or cp f1 ... fn d2\n");
  58:     exit(1);
  59: }
  60: 
  61: copy(from, to)
  62: char *from, *to;
  63: {
  64:     int fold, fnew, n;
  65:     register char *p1, *p2, *bp;
  66:     int mode;
  67:     int c;
  68:     char i;
  69:     if ((fold = open(from, 0)) < 0) {
  70:         fprintf(stderr, "cp: cannot open %s\n", from);
  71:         return(1);
  72:     }
  73:     fstat(fold, &stbuf1);
  74:     mode = stbuf1.st_mode;
  75:     /* is target a directory? */
  76:     if (stat(to, &stbuf2) >=0 &&
  77:        (stbuf2.st_mode&S_IFMT) == S_IFDIR) {
  78:         p1 = from;
  79:         p2 = to;
  80:         bp = iobuf;
  81:         while(*bp++ = *p2++)
  82:             ;
  83:         bp[-1] = '/';
  84:         p2 = bp;
  85:         while(*bp = *p1++)
  86:             if (*bp++ == '/')
  87:                 bp = p2;
  88:         to = iobuf;
  89:     }
  90:     if (stat(to, &stbuf2) >= 0) {
  91:         if (stbuf1.st_dev == stbuf2.st_dev &&
  92:            stbuf1.st_ino == stbuf2.st_ino) {
  93:             fprintf(stderr, "cp: cannot copy file to itself.\n");
  94:             return(1);
  95:         } else if (iflag) {
  96:             fprintf (stderr, "overwrite %s? ", to);
  97:             c = getchar();
  98:             i = (char) c;
  99:             while ((c & 0377) != '\n' && c != EOF)
 100:                 c = getchar();
 101:             if (i != 'y')
 102:                 return(1);
 103:         }
 104:     }
 105:     if ((fnew = creat(to, mode)) < 0) {
 106:         fprintf(stderr, "cp: cannot create %s\n", to);
 107:         close(fold);
 108:         return(1);
 109:     }
 110:     while(n = read(fold,  iobuf,  BSIZE)) {
 111:         if (n < 0) {
 112:             perror("cp: read");
 113:             close(fold);
 114:             close(fnew);
 115:             return(1);
 116:         } else
 117:             if (write(fnew, iobuf, n) != n) {
 118:                 perror("cp: write");
 119:                 close(fold);
 120:                 close(fnew);
 121:                 return(1);
 122:             }
 123:     }
 124:     close(fold);
 125:     close(fnew);
 126:     return(0);
 127: }

Defined functions

copy defined in line 61; used 1 times
  • in line 54
main defined in line 19; never used

Defined variables

iflag defined in line 14; used 2 times
iobuf defined in line 13; used 4 times
sccsid defined in line 2; never used
stbuf1 defined in line 12; used 4 times
stbuf2 defined in line 12; used 7 times

Defined macros

BSIZE defined in line 8; used 2 times
Last modified: 1982-09-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 760
Valid CSS Valid XHTML 1.0 Strict