[net.sources] Copy the mode of a file to other files

ok@edai.UUCP (05/07/84)

Run this through "sh".

cat >copymode.1 <<'EOF'
.TH COPYMODE 1 local
.SH NAME
copymode \- copy mode from file
.SH SYNOPSIS
.B copymode
FromFile ToFile ...
.SH DESCRIPTION
The mode of
each named
.I ToFile
is changed
to be exactly the same as that of the
.I FromFile.
.PP
The point of this command is that when you transform a file using
one or more filters, you would very often like the transformed
file to have the same access permissions as the original.  But as
the filters never know what files they are working on (and indeed,
if you use pipes, there is no place to carry the permission bits
from one stage to the next), it is not possible for the filters to
do this, and they create the transformed file with the same default
that output redirection would use.  With this command you can copy
the permissions of the original file to the transformed file.
.PP
Only the owner of a file (or the super-user) may change its mode.
.SH EXAMPLES
.IP
crypt key <cypher >clear; copymode cypher clear
.PP
This is the case that copymode was created for.  The decrypted
file "clear" gets the same permissions as the encrypted file "crypt".
.IP
split -250 bigfile piece
.br
copymode bigfile piece*
.PP
Splits a file into pieces and ensures that the pieces have the
same permissions as the original file.  (Note that these must
be separate lines for filename expansion to work.)
.SH "SEE ALSO"
chmod(1),
ls(1),
chmod(2),
stat(2),
umask(2)
.SH BUGS
This should be another option ("-f FromFile") in chmod(1).
'EOF'
cat >copymode.c <<'EOF'
/*  File   : copymode.c
    Author : Richard A. O'Keefe
    Updated: 28 April 1984

    copymode FromFile ToFile1 ... ToFilen

    copies the mode (access permission) bits from FromFile to
    ToFile1 ... ToFilen.  The exit code is
    0) all went well
    1) there were too few parameters
    2) stat(2) wasn't happy with FromFile
    3) chmod(2) wasn't happy with one or more of the ToFiles.
    All the ToFiles which can be changed will be changed, even
    if a file earlier in the list cannot be.  E.g. suppose
	copymode from a		# would succeed
	copymode from b		# would fail
	copymode from c		# would succeed
    then
	copymode from a b c	# will change a and c and will fail.

    This program is for use with filters.  A filter cannot make its
    output have the same protection as its input, because it has no
    access to the files as such, only to their contents.  Given a
    filter, to make something which preserves permissions, define
    Filter =
	a=$1
	b=$2
	shift;shift
	filter <$a >$b $*
	copymode $a $b
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

main(argc, argv)
    int argc;
    char **argv;
    {
	int mode;
	int errs = 0;

	if (argc < 3) {
	    fprintf(stderr, "Usage: copymode From To...\n");
	    exit(1);
	}
	{
	    struct stat statbuf;
	    if (stat(argv[1], &statbuf)) {
		perror(argv[1]);
		exit(2);
	    }
	    mode = statbuf.st_mode & 07777;
	}
	argv++;
	while (*++argv) {
	    if (chmod(*argv, mode)) {
		perror(*argv);
		errs++;
	    }
	}
	exit(errs ? 3 : 0);
    }

'EOF'