home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1622 < prev    next >
Internet Message Format  |  1990-12-28  |  2KB

  1. From: chet@cwns1.CWRU.EDU (Chet Ramey)
  2. Newsgroups: alt.sources
  3. Subject: [comp.unix.questions] Re: how to compare file modification time in bourne shell script
  4. Message-ID: <1990Jul26.001648.3365@math.lsa.umich.edu>
  5. Date: 26 Jul 90 00:16:48 GMT
  6.  
  7. Archive-name: newer/25-Jul-90
  8. Original-posting-by: chet@cwns1.CWRU.EDU (Chet Ramey)
  9. Original-subject: Re: how to compare file modification time in bourne shell script
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.unix.questions.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. In article <1990Jul23.233044.2729@silma.com> aab@silma.UUCP () writes:
  16.  
  17. >newer file1 file2
  18. >that returns 0 if file1 is newer than file2 else returns 1
  19.  
  20. Here's something I picked up a while back...
  21.  
  22. /*
  23.  * From Henry Spencer
  24.  *
  25.  * > There doesn't appear to be any decent way to compare the last modified
  26.  * > times of files from the shell...
  27.  *
  28.  * Before everybody starts inventing their own names for this, it should be
  29.  * noted that V8 already has a program for this, newer(1).  It takes two
  30.  * filenames as arguments, and exits with status 0 if and only if either
  31.  * (a) the first exists and the second does not, or (b) both exist and the
  32.  * first's modification time is at least as recent as the second's.  Other-
  33.  * wise it exits with non-zero status.  (The preceding two sentences are
  34.  * essentially the whole of the manual page for it.)
  35.  * 
  36.  * Relatively few people have V8, but in the absence of any other precedent
  37.  * for what this facility should like look, it seems reasonable to follow
  38.  * V8's lead.
  39.  * 
  40.  * Here is an independent rewrite, done from the manual page and not the
  41.  * code, by me, hereby placed in the public domain:
  42.  */
  43.  
  44. /*
  45.  * newer - is first file newer than second?
  46.  *
  47.  * newer file1 file2
  48.  *
  49.  * exit with 0 status if file1 exists and file2 does not, or if file1's last
  50.  * modified time is at least as recent as file2's.
  51.  */
  52.  
  53. #include <stdio.h>
  54. #include <sys/types.h>
  55. #include <sys/stat.h>
  56.  
  57. main(argc, argv)
  58. int argc;
  59. char *argv[];
  60. {
  61.     struct stat file1;
  62.     struct stat file2;
  63.  
  64.     if (argc != 3) {
  65.         fprintf(stderr, "Usage: %s file1 file2\n", argv[0]);
  66.         exit(2);
  67.     }
  68.  
  69.     if (stat(argv[1], &file1) < 0)
  70.         exit(1);
  71.     if (stat(argv[2], &file2) < 0)
  72.         exit(0);
  73.     if (file1.st_mtime >= file2.st_mtime)
  74.         exit(0);
  75.     exit(1);
  76. }
  77. -- 
  78. Chet Ramey                    ``See Figure 1.''
  79. Network Services Group
  80. Case Western Reserve University    
  81. chet@ins.CWRU.Edu        
  82.