BSD 4 release
[unix-history] / usr / src / cmd / cp.c
index 7af6362..3e1f1fb 100644 (file)
@@ -1,19 +1,44 @@
+static char *sccsid = "@(#)cp.c        4.1 (Berkeley) 10/1/80";
 /*
  * cp oldfile newfile
  */
 
 /*
  * cp oldfile newfile
  */
 
-#define        BSIZE   512
+#define        BSIZE   1024
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 struct stat    stbuf1, stbuf2;
 char   iobuf[BSIZE];
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 struct stat    stbuf1, stbuf2;
 char   iobuf[BSIZE];
+int    iflag = 0;      /* interactive flag. If this flag is set,
+                        * the user is queried before files are
+                        * destroyed by cp.
+                        */
 
 main(argc, argv)
 char *argv[];
 {
        register i, r;
 
 
 main(argc, argv)
 char *argv[];
 {
        register i, r;
 
+       /* get the flag(s) */
+
+       if (argc < 2)
+               goto usage;
+       if (*argv[1] == '-') {
+               argc--;
+               while (*++argv[1] != '\0')
+                       switch (*argv[1]) {
+
+                       /* interactive mode */
+                       case 'i':
+                               iflag++;
+                               break;
+
+                       /* don't live with bad options */
+                       default:
+                               goto usage;
+                       }
+               argv++;
+       }
        if (argc < 3) 
                goto usage;
        if (argc > 3) {
        if (argc < 3) 
                goto usage;
        if (argc > 3) {
@@ -37,6 +62,7 @@ char *from, *to;
        int fold, fnew, n;
        register char *p1, *p2, *bp;
        int mode;
        int fold, fnew, n;
        register char *p1, *p2, *bp;
        int mode;
+       char c,i;
        if ((fold = open(from, 0)) < 0) {
                fprintf(stderr, "cp: cannot open %s\n", from);
                return(1);
        if ((fold = open(from, 0)) < 0) {
                fprintf(stderr, "cp: cannot open %s\n", from);
                return(1);
@@ -63,6 +89,13 @@ char *from, *to;
                   stbuf1.st_ino == stbuf2.st_ino) {
                        fprintf(stderr, "cp: cannot copy file to itself.\n");
                        return(1);
                   stbuf1.st_ino == stbuf2.st_ino) {
                        fprintf(stderr, "cp: cannot copy file to itself.\n");
                        return(1);
+               } else if (iflag) {
+                       fprintf (stderr, "overwrite %s? ", to);
+                       i = c = getchar();
+                       while (c != '\n' && c != EOF)
+                               c = getchar();
+                       if (i != 'y')
+                               return(1);
                }
        }
        if ((fnew = creat(to, mode)) < 0) {
                }
        }
        if ((fnew = creat(to, mode)) < 0) {
@@ -72,13 +105,13 @@ char *from, *to;
        }
        while(n = read(fold,  iobuf,  BSIZE)) {
                if (n < 0) {
        }
        while(n = read(fold,  iobuf,  BSIZE)) {
                if (n < 0) {
-                       fprintf(stderr, "cp: read error\n");
+                       perror("cp: read");
                        close(fold);
                        close(fnew);
                        return(1);
                } else
                        if (write(fnew, iobuf, n) != n) {
                        close(fold);
                        close(fnew);
                        return(1);
                } else
                        if (write(fnew, iobuf, n) != n) {
-                               fprintf(stderr, "cp: write error.\n");
+                               perror("cp: write");
                                close(fold);
                                close(fnew);
                                return(1);
                                close(fold);
                                close(fnew);
                                return(1);