jrstu@cbnewsd.ATT.COM (james.stuhlmacher) (10/03/89)
Here are two patch files for cp(1) and touch(1). When one or more of cp's source arguments are directories, cp(1) will treat the directory as a file and copy it to a file. I noticed this when I wanted to save some files in the current directory to the subdirectory "sav" with the command: cp * sav This resulted in a file with the name "sav" in the directory "sav". The patch causes cp(1) to check if the source is a directory before copying it. If it is a directory, then it will not copy the directory, but instead print a message and continue on. I also added exit status when there are multiple sources. When touch(1) was given an argument of a file that did not exist, it would call creat(2) with a mask of 0777. This causes the file to be executable if umask does not force it otherwise. Since the file is empty it should not be executable. A mask of 0666 is be used instead. #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of shell archive." # Contents: cp.cdif touch.cdif # Wrapped by jims on Mon Oct 2 21:02:16 1989 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'cp.cdif' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'cp.cdif'\" else echo shar: Extracting \"'cp.cdif'\" \(1991 characters\) sed "s/^X//" >'cp.cdif' <<'END_OF_FILE' X*** ocp.c Sun Oct 1 18:27:57 1989 X--- cp.c Mon Oct 2 20:58:10 1989 X*************** X*** 22,28 **** X m = sbuf.st_mode & S_IFMT; X if (s >= 0 && m == S_IFDIR) { X /* Last argument is a directory. */ X! cp_to_dir(argc, argv); X } else if (argc > 3) { X /* More than 2 arguments and last one is not a directory. */ X usage(); X--- 22,28 ---- X m = sbuf.st_mode & S_IFMT; X if (s >= 0 && m == S_IFDIR) { X /* Last argument is a directory. */ X! exit(cp_to_dir(argc, argv)); X } else if (argc > 3) { X /* More than 2 arguments and last one is not a directory. */ X usage(); X*************** X*** 37,42 **** X--- 37,47 ---- X fd1 = open(argv[1], 0); X if (fd1 < 0) {stderr3("cannot open ", argv[1], "\n"); exit(1);} X fstat(fd1, &sbuf); X+ m = sbuf.st_mode & S_IFMT; X+ if (m == S_IFDIR) { X+ stderr3("<", argv[1], "> directory\n"); X+ exit(1); X+ } X fd2 = creat(argv[2], sbuf.st_mode & 0777); X if (fd2 < 0) {stderr3("cannot create ", argv[2], "\n"); exit(2);} X fstat(fd2, &sbuf2); X*************** X*** 57,63 **** X int argc; X char *argv[]; X { X! int i, fd1, fd2; X char dirname[256], *ptr, *dp; X struct stat sbuf; X X--- 62,68 ---- X int argc; X char *argv[]; X { X! int i, mode, fd1, fd2, exit_status = 0; X char dirname[256], *ptr, *dp; X struct stat sbuf; X X*************** X*** 65,70 **** X--- 70,76 ---- X fd1 = open(argv[i], 0); X if (fd1 < 0) { X stderr3("cannot open ", argv[i], "\n"); X+ exit_status = 1; X continue; X } X X*************** X*** 82,94 **** X--- 88,110 ---- X while (*ptr != 0) *dp++ = *ptr++; X *dp++ = 0; X fstat(fd1, &sbuf); X+ mode = sbuf.st_mode & S_IFMT; X+ if (mode == S_IFDIR) { X+ stderr3("<", argv[i], "> directory\n"); X+ exit_status = 1; X+ close(fd1); X+ continue; X+ } X fd2 = creat(dirname, sbuf.st_mode & 0777); X if (fd2 < 0) { X stderr3("cannot create ", dirname, "\n"); X+ exit_status = 2; X+ close(fd1); X continue; X } X copyfile(fd1, fd2, dirname); X } X+ return(exit_status); X } X X END_OF_FILE if test 1991 -ne `wc -c <'cp.cdif'`; then echo shar: \"'cp.cdif'\" unpacked with wrong size! fi # end of 'cp.cdif' fi if test -f 'touch.cdif' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'touch.cdif'\" else echo shar: Extracting \"'touch.cdif'\" \(449 characters\) sed "s/^X//" >'touch.cdif' <<'END_OF_FILE' X*** otouch.c Sun Oct 1 18:09:37 1989 X--- touch.c Sun Oct 1 18:12:07 1989 X*************** X*** 63,69 **** X /* file does not exist */ X if (no_creat == 1) X return(0); X! else if ( (fd = creat(name, 0777)) < 0) { X return(1); X } else { X close(fd); X--- 63,69 ---- X /* file does not exist */ X if (no_creat == 1) X return(0); X! else if ( (fd = creat(name, 0666)) < 0) { X return(1); X } else { X close(fd); END_OF_FILE if test 449 -ne `wc -c <'touch.cdif'`; then echo shar: \"'touch.cdif'\" unpacked with wrong size! fi # end of 'touch.cdif' fi echo shar: End of shell archive. exit 0 James Stuhlmacher j.stuhlmacher@ATT.com ..!att!ihlpb!jims