home *** CD-ROM | disk | FTP | other *** search
- /*
- * cpmod.c -- copy modes, ownerships and times from one file to others,
- * without affecting the data.
- *
- * by Steve Alter (13-Aug-87, Citicorp/TTI, Santa Monica, CA)
- *
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/time.h>
-
- main (argc, argv)
- int argc;
- char **argv;
- {
- char *prog_name, *rindex();
- char *source_file;
-
- short no_modes = 0;
- short no_owner = 0;
- short no_times = 0;
- short error = 0;
-
- struct stat source_buf, new_buf;
- struct timeval tvp[2];
-
- if (prog_name = rindex(argv[0], '/'))
- prog_name++;
- else
- prog_name = argv[0];
-
- for (argc--, argv++; (argc && **argv == '-'); argc--, argv++)
- switch ((*argv)[1]) {
- case 'm':
- no_modes++;
- break;
- case 'o':
- no_owner++;
- break;
- case 't':
- no_times++;
- break;
- default:
- fprintf(stderr,
- "%s: invalid option \"%s\"\n",
- prog_name, *argv);
- error++;
- }
-
- if (argc < 2 || error) {
- fprintf(stderr,
- "usage: %s [-m] [-o] [-t] file other-files ...\n",
- prog_name);
- exit(1);
- }
-
- if (no_modes && no_owner && no_times) {
- fprintf(stderr,
- "%s: all three options disabled, no work left.\n",
- prog_name);
- exit(1);
- }
-
- source_file = *argv++;
- argc--;
-
- if (stat(source_file, &source_buf)) {
- perror(source_file);
- exit(1);
- }
-
- while (argc--) {
- if (stat(*argv, &new_buf)) {
- perror(*argv++);
- continue;
- }
- if (!no_modes)
- if (chmod(*argv, (int)source_buf.st_mode))
- perror(*argv);
- if (!no_owner)
- if (chown(*argv, (int)source_buf.st_uid,
- (int)source_buf.st_gid))
- perror(*argv);
- if (!no_times) {
- tvp[0].tv_sec = source_buf.st_atime;
- tvp[1].tv_sec = source_buf.st_mtime;
- tvp[0].tv_usec = tvp[1].tv_usec = 0L;
- if (utimes(*argv, tvp))
- perror(*argv);
- }
- argv++;
- }
- } /* main */
-