home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / cpmod / cpmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-31  |  1.7 KB  |  96 lines

  1. /*
  2.  *  cpmod.c -- copy modes, ownerships and times from one file to others,
  3.  *           without affecting the data.
  4.  *
  5.  *  by Steve Alter (13-Aug-87, Citicorp/TTI, Santa Monica, CA)
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <sys/time.h>
  13.  
  14. main (argc, argv)
  15.     int argc;
  16.     char **argv;
  17. {
  18.     char *prog_name, *rindex();
  19.     char *source_file;
  20.  
  21.     short no_modes = 0;
  22.     short no_owner = 0;
  23.     short no_times = 0;
  24.     short error = 0;
  25.  
  26.     struct stat source_buf, new_buf;
  27.     struct timeval tvp[2];
  28.  
  29.     if (prog_name = rindex(argv[0], '/'))
  30.         prog_name++;
  31.     else
  32.         prog_name = argv[0];
  33.  
  34.     for (argc--, argv++; (argc && **argv == '-'); argc--, argv++)
  35.         switch ((*argv)[1]) {
  36.             case 'm':
  37.                 no_modes++;
  38.                 break;
  39.             case 'o':
  40.                 no_owner++;
  41.                 break;
  42.             case 't':
  43.                 no_times++;
  44.                 break;
  45.             default:
  46.                 fprintf(stderr,
  47.                     "%s: invalid option \"%s\"\n",
  48.                     prog_name, *argv);
  49.                 error++;
  50.         }
  51.  
  52.     if (argc < 2 || error) {
  53.         fprintf(stderr,
  54.             "usage: %s [-m] [-o] [-t] file other-files ...\n",
  55.             prog_name);
  56.         exit(1);
  57.     }
  58.  
  59.     if (no_modes && no_owner && no_times) {
  60.         fprintf(stderr,
  61.         "%s: all three options disabled, no work left.\n",
  62.             prog_name);
  63.         exit(1);
  64.     }
  65.  
  66.     source_file = *argv++;
  67.     argc--;
  68.  
  69.     if (stat(source_file, &source_buf)) {
  70.         perror(source_file);
  71.         exit(1);
  72.     }
  73.  
  74.     while (argc--) {
  75.         if (stat(*argv, &new_buf)) {
  76.             perror(*argv++);
  77.             continue;
  78.         }
  79.         if (!no_modes)
  80.             if (chmod(*argv, (int)source_buf.st_mode))
  81.                 perror(*argv);
  82.         if (!no_owner)
  83.             if (chown(*argv, (int)source_buf.st_uid,
  84.                      (int)source_buf.st_gid))
  85.                 perror(*argv);
  86.         if (!no_times) {
  87.             tvp[0].tv_sec = source_buf.st_atime;
  88.             tvp[1].tv_sec = source_buf.st_mtime;
  89.             tvp[0].tv_usec = tvp[1].tv_usec = 0L;
  90.             if (utimes(*argv, tvp))
  91.                 perror(*argv);
  92.         }
  93.         argv++;
  94.     }
  95. }  /* main */
  96.