/* * SPDsoft 1995 * SPDsoft Feb, 1998 * Tue Feb 17 17:17:28 MET 1998 * * Copy file attributes * :set tabstop=4 * */ #include #include #include #include #include #include #include #include #include #include #include #include #define PATH_SEP '/' #ifdef _BSD_ extern char *sys_errlist[]; # define raise(s) kill(getpid(),(s)) # define strerror(e) sys_errlist[(e)] #endif static char __ident[] = "@(#)v 0.1 (c)SPDsoft, Fri Feb 16, 1998"; #define VERS_STR ((char*)&__ident[4]) void usage(void); int cp(char *in, char *out); extern int errno; int Stat(const char *path, struct stat *buf); char *app; int Stat(const char *path, struct stat *buf) { int ret; int fd; if ( -1 == (fd=open(path,O_RDONLY))) { perror(path); exit(1); } else { ret=fstat(fd,buf); close(fd); } } void usage(void) { fprintf(stderr,"Use: %s [-hxXtTsrb] model file [...]\n", app); fprintf(stderr,"%s: -h : This text\n", app); fprintf(stderr,"%s: -X : Expand owner permisions\n", app); fprintf(stderr,"%s: -x : Expand owner permisions -w\n", app); fprintf(stderr,"%s: -t : Copy mtime and atime\n", app); fprintf(stderr,"%s: -T : Copy mtime, atime AND ctime\n", app); fprintf(stderr,"%s: -s : Copy size\n", app); fprintf(stderr,"%s: -r : Replace model with file\n", app); fprintf(stderr,"%s: -f : Use fstat\n", app); fprintf(stderr,"%s: -b dir: Backup model to dir\n", app); exit(0); } main( int argc, char *argv[]) { struct stat sbp; struct stat sbo; int i; int opt; int (*my_stat)(const char *path, struct stat *buf); int expand=0; int time=0; int size=0; int replace=0; char *dir=NULL; char *name; mode_t om; FILE *f; off_t diff_size, j; struct utimbuf times; struct timeval otime, ntime; struct timezone tzp; extern char *optarg; extern int optind,opterr; if ((app = strrchr(argv[0], PATH_SEP)) != NULL) app = app+1; else app = argv[0]; my_stat=&stat; while ( (opt=getopt(argc,argv,"hfxXtTsrb:")) != EOF ) { switch(opt) { case 'f': my_stat=&Stat; break; case 'X': expand=2; break; case 'x': expand=1; break; case 'T': time=2; break; case 't': time=1; break; case 's': size=1; break; case 'r': replace=1; break; case 'b': dir=optarg; break; case 'h': default: usage(); break; } } if ((argc-optind)<2) usage(); if ( my_stat(argv[optind],&sbp)==-1) { fprintf(stderr,"%s: can't stat %s (%s)\n", app, argv[optind],strerror(errno)); exit(1); } else { switch ( expand ) { case 1: om = ( sbp.st_mode & 0500 ) >> 6 ; sbp.st_mode |= om | (om << 3 ) ; break; case 2: om = ( sbp.st_mode & 0700 ) >> 6 ; sbp.st_mode |= om | (om << 3 ) ; break; default: break; } for( i=optind+1; i < argc ; i ++) { if ( size != 0 ) { if ( my_stat(argv[i],&sbo)==-1) { fprintf(stderr,"%s: can't stat %s (%s)\n", app, argv[i],strerror(errno)); exit(1); } if ( sbp.st_size < sbo.st_size ) fprintf(stderr,"%s: can't fix size %s\n", app, argv[i]); else { if ((f = fopen(argv[i], "r+b")) == NULL) { perror(argv[i]); exit(1); } fseek(f,0L,SEEK_END); for( j=sbo.st_size; j