Back to index

tetex-bin  3.0
newer.c
Go to the documentation of this file.
00001 /****************************************************************
00002 Copyright 1990 - 1995 by AT&T Bell Laboratories.
00003 
00004 Permission to use, copy, modify, and distribute this software
00005 and its documentation for any purpose and without fee is hereby
00006 granted, provided that the above copyright notice appear in all
00007 copies and that both that the copyright notice and this
00008 permission notice and warranty disclaimer appear in supporting
00009 documentation, and that the names of AT&T Bell Laboratories or
00010 any of its entities not be used in advertising or publicity
00011 pertaining to distribution of the software without specific,
00012 written prior permission.
00013 
00014 AT&T disclaims all warranties with regard to this software,
00015 including all implied warranties of merchantability and fitness.
00016 In no event shall AT&T be liable for any special, indirect or
00017 consequential damages or any damages whatsoever resulting from
00018 loss of use, data or profits, whether in an action of contract,
00019 negligence or other tortious action, arising out of or in
00020 connection with the use or performance of this software.
00021 ****************************************************************/
00022 
00023 #include "c-auto.h"     /* In case we need, e.g., _POSIX_SOURCE */
00024 #include "config.h"
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #ifndef WIN32
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 #endif
00031 
00032 /*
00033  *      newer x y
00034  *
00035  *      returns 0 if x exists and y does not, or if
00036  *      files x and y both exist and x was modified
00037  *      at least as recently as y, and nonzero otherwise.
00038  *      (i.e., it's really ``is x older than y'', not ``is y newer than x'')
00039  *      Perhaps this program should be replaced by ls -lt, but then two
00040  *      files with the same mtime couldn't be handled right ...
00041  */
00042 
00043 int main P2C(int,argc, char**,argv)
00044 {
00045         struct stat x, y;
00046 
00047         if (argc > 1 && strcmp (argv[1], "--help") == 0) {
00048           fputs ("Usage: newer [OPTION]... FILE1 FILE2\n\
00049   Exit successfully if FILE1 exists and is at least as recent as FILE2.\n\
00050 \n\
00051 --help      display this help and exit\n\
00052 --version   output version information and exit\n\n", stdout);
00053           fputs ("Email bug reports to tex-k@mail.tug.org.\n", stdout);
00054           exit(0);
00055         } else if (argc > 1 && strcmp (argv[1], "--version") == 0) {
00056           printf ("newer%s 0.63\n\
00057 Copyright (C) 1996 AT&T Bell Laboratories.\n\
00058 There is NO warranty.  You may redistribute this software\n\
00059 under the terms of the GNU General Public License\n\
00060 and the newer copyright.\n\
00061 For more information about these matters, see the files\n\
00062 named COPYING and the newer source.\n\
00063 Primary author of newer: John Hobby.\n",
00064 WEB2CVERSION);
00065           exit (0);
00066         }
00067 
00068         /* insist on exactly two args */
00069         if (argc != 3) {
00070           fputs ("newer: Need exactly two arguments.\n", stderr);
00071           fputs ("Try `newer --help' for more information.\n", stderr);
00072           exit(1);
00073         }
00074         
00075         /* does the first file exist? */
00076         if (stat (argv[1], &x) < 0)
00077                 exit(1);
00078         
00079         /* does the second file exist? */
00080         if (stat (argv[2], &y) < 0)
00081                 exit(0);
00082         
00083         /* fail if the first file is older than the second */
00084         if (x.st_mtime < y.st_mtime)
00085                 exit(1);
00086         
00087         /* otherwise, succeed */
00088         exit(0);
00089 }