home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / mod.std.unix.v8 / text0015.txt < prev    next >
Encoding:
Internet Message Format  |  1987-06-30  |  1.5 KB

  1. From: pyramid!utzoo!henry (Henry Spencer)
  2. Date: Fri, 31 Oct 86 21:25:31 CST
  3.  
  4. > There doesn't appear to be any decent way to compare the last modified
  5. > times of files from the shell...
  6.  
  7. Before everybody starts inventing their own names for this, it should be
  8. noted that V8 already has a program for this, newer(1).  It takes two
  9. filenames as arguments, and exits with status 0 if and only if either
  10. (a) the first exists and the second does not, or (b) both exist and the
  11. first's modification time is at least as recent as the second's.  Other-
  12. wise it exits with non-zero status.  (The preceding two sentences are
  13. essentially the whole of the manual page for it.)
  14.  
  15. Relatively few people have V8, but in the absence of any other precedent
  16. for what this facility should like look, it seems reasonable to follow
  17. V8's lead.
  18.  
  19. Here is an independent rewrite, done from the manual page and not the
  20. code, by me, hereby placed in the public domain:
  21.  
  22. /*
  23.  * newer - is first file newer than second?
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     struct stat file1;
  35.     struct stat file2;
  36.  
  37.     if (argc != 3) {
  38.         fprintf(stderr, "Usage: %s file1 file2\n", argv[0]);
  39.         exit(2);
  40.     }
  41.  
  42.     if (stat(argv[1], &file1) < 0)
  43.         exit(1);
  44.     if (stat(argv[2], &file2) < 0)
  45.         exit(0);
  46.     if (file1.st_mtime >= file2.st_mtime)
  47.         exit(0);
  48.     exit(1);
  49. }
  50.  
  51.                 Henry Spencer @ U of Toronto Zoology
  52.                 {allegra,ihnp4,decvax,pyramid}!utzoo!henry
  53.  
  54. Volume-Number: Volume 8, Number 16
  55.  
  56.