home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / sgi / 13554 < prev    next >
Encoding:
Text File  |  1992-09-11  |  3.5 KB  |  154 lines

  1. Newsgroups: comp.sys.sgi
  2. Path: sparky!uunet!sun-barr!ames!sgi!fido!zola!zuni!anchor!olson
  3. From: olson@anchor.esd.sgi.com (Dave Olson)
  4. Subject: Re: setting file access time
  5. Message-ID: <pnsrijk@zuni.esd.sgi.com>
  6. Sender: news@zuni.esd.sgi.com (Net News)
  7. Organization:  Silicon Graphics, Inc.  Mountain View, CA
  8. References: <32519@adm.brl.mil>
  9. Date: Fri, 11 Sep 92 22:40:36 GMT
  10. Lines: 142
  11.  
  12. In <32519@adm.brl.mil> beyer@bflsgu.fl.bs.dlr.de (R. Beyer) writes:
  13.  
  14. | I am looking for a command which can be used on the command line or in
  15. | a script to set the file access time like, for instance, utime(2).
  16.  
  17. Here you go; it does quite a bit more, actually.  I don't remember
  18. just why all of it was in one program, but...
  19.  
  20. /*    get time date stamp from a coff binary and print it in
  21.     human-readable form (like odump -fv).
  22.     Checks to see if f_magic looks like it could be a COFF
  23.     header.  Given all the different magic #'s, this is
  24.     rather difficult.  The best (easiest) I can see to do is to
  25.     look and see if f_magic&0500 == 0500.  Could look at opthdr
  26.     too, but this is  just a quick and dirty tool.  Word order
  27.     is also a problem that the coff format doesn't really
  28.     address...  So for now, I will just check for 'native' order.
  29.     Otherwise have to worry about word and byte order in the
  30.     f_timdat also.
  31.     Dave Olson, 5/88
  32.  
  33.     Added -s option to set st_mtime from this field. 2/89
  34. */
  35. #include "sys/types.h"
  36. #include "filehdr.h"
  37. #include "stdio.h"
  38. #include "fcntl.h"
  39. #include <unistd.h>
  40.  
  41. char *prog;
  42. int errcnt;
  43. char *ctime();
  44. extern int errno;
  45. void error(), showtime();
  46.  
  47. main(cnt, args)
  48. int cnt;
  49. char **args;
  50. {
  51.     int c, setit = 0, verbose = 0, numeric = 0;
  52.     extern int optind, opterr;
  53.     extern char *optarg;
  54.  
  55.     opterr = 0;    /* do our own errors */
  56.     prog = *args;
  57.  
  58.     while((c=getopt(cnt, args, "svn")) != -1) {
  59.         switch(c) {
  60.         case '?':
  61.             usage();
  62.             break;
  63.         case 's':
  64.             setit = 1;
  65.             break;
  66.         case 'v':
  67.             verbose++;
  68.             break;
  69.         case 'n':    /* output the stamp in numeric form */
  70.             numeric++;
  71.             break;
  72.         }
  73.     }
  74.  
  75.     if(cnt == optind)
  76.         showtime(0, "stdin", 0, 1);
  77.     else for( ; optind < cnt; optind++) {
  78.         int fd = open(args[optind], O_RDONLY);
  79.         if(fd == -1)
  80.             error(0, "Couldn't open %s", args[optind]);
  81.         else {
  82.             showtime(fd, args[optind], setit, verbose, numeric);
  83.             close(fd);
  84.         }
  85.     }
  86.     exit(errcnt);
  87. }
  88.  
  89. usage()
  90. {
  91.     fprintf(stderr, "Usage: %s [-v] [-s] [-n] [file] ...\n", prog);
  92.     exit(1);
  93. }
  94.  
  95.  
  96. void
  97. error(exval, fmt, a1, a2, a3)
  98. int exval;
  99. char *fmt;
  100. {
  101.     char buf[1024];
  102.  
  103.     strcpy(buf, prog);
  104.     strcat(buf, ": ");
  105.  
  106.     sprintf(buf+strlen(buf), fmt, a1, a2, a3);
  107.  
  108.     if(errno)
  109.         perror(buf);
  110.     else
  111.         fprintf(stderr, "%s\n", buf);
  112.     if(exval)
  113.         exit(exval);
  114.     errcnt++;
  115. }
  116.  
  117.  
  118. void
  119. showtime(fd, name, set, verbose, numeric)
  120. int fd, set;
  121. {
  122.     struct filehdr fhdr;
  123.     struct utimbuf ut;
  124.  
  125.     if(read(fd, &fhdr, sizeof(fhdr)) != sizeof(fhdr)) {
  126.         error(0, "Couldn't read coff filehdr of %s", name);
  127.         return;
  128.     }
  129.     if((fhdr.f_magic & 0500) != 0500) {
  130.         if(verbose)
  131.             errno=0, error(0, "%s doesn't appear to be an coff binary", name);
  132.         return;
  133.     }
  134.     if(set) {
  135.         ut.actime = fhdr.f_timdat;
  136.         ut.modtime = fhdr.f_timdat;
  137.         if(utime(name, &ut) == -1) {
  138.             error(0, "couldn't set times on %s", name);
  139.             return;
  140.         }
  141.     }
  142.     if(!set || verbose > 1) {
  143.         if(numeric)    /* for comparing compile times in scripts */
  144.             printf("%s: %u\n", name, fhdr.f_timdat);
  145.         else
  146.             printf("%s: %.24s\n", name, ctime(&fhdr.f_timdat));
  147.     }
  148. }
  149. --
  150. Let no one tell me that silence gives consent,  |   Dave Olson
  151. because whoever is silent dissents.             |   Silicon Graphics, Inc.
  152.     Maria Isabel Barreno                        |   olson@sgi.com
  153.