Cloning File Protection?

Scott Weitzenkamp scott at talarian.UUCP
Sat Oct 6 02:58:07 AEST 1990


  I am trying to write a shell script (either in sh or csh on SunOS 4.0.3)
that can clone the file protection from one file to another.  I'd like
to do something like this:

chmod `get_protection old_file_name` new_file_name

  Is there a (easy) way from sh or csh to retrieve the file protection
of a file in a format that chmod can understand?  It doesn't look to
me like ls(1) will do the trick.  What I wound up doing was to write
a C program to print a file's protection in octal:

/* getmod.c -- print file protection of a file in octal */

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

int main(argc, argv)
int argc;
char **argv;
{
  struct stat stat_buf;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    return 1;
  } /* if */

  if (stat(argv[0], &stat_buf) != 0) {
    perror("stat");
    return 1;
  } /* if */

  printf("0%o\n", stat_buf.st_mode & 0777);

  return 0;
} /* main */

  Now I can do what I want:

chmod `getmod old_foo.c` new_foo.c

  I have a feeling this is probably easy to do in Perl, but I not
really interested in a Perl solution because I cannot guarantee that
our customers will have Perl (I suppose I could put Perl on our
product tape, though).  
  
  Do anybody see an easier way to do this?

-- 
Thanks in advance...
Scott Weitzenkamp, Talarian Corporation, Mountain View, CA
uunet!talarian!scott             (415) 965-8050
"Welcome to the late show, starring NULL and void" -- Men At Work



More information about the Comp.unix.questions mailing list