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

  1. From: taylor@limbo.Intuitive.Com (Dave Taylor)
  2. Newsgroups: alt.sources
  3. Subject: TIMESTAMP: a useful file filter by last modified time
  4. Message-ID: <926@limbo.Intuitive.Com>
  5. Date: 30 Jun 90 19:44:08 GMT
  6.  
  7. Timestamp is a handy utility I recently whipped up for some local
  8. administrative needs that lists the subset of files given on the command 
  9. line that are older (or newer) than the specified timestamp delta.
  10.  
  11. Timestamp can be included in shell scripts where the resolution of the 
  12. 'last modified' check of the "find" utility doesn't offer sufficiently 
  13. high resolution.  Specifically, "timestamp" can be used to check for 
  14. files that are older than "any" time, from seconds to days.  Further, 
  15. with the use of the optional '-r' flag (to reverse the meaning of the 
  16. command) "timestamp" can be used to find files newer than the specified 
  17. time too.  (indeed, in conjunction, you could find all files that were
  18. created during an specific 'slice' of time, even weeks ago).
  19.  
  20.     Enjoy!
  21.  
  22.                         -- Dave Taylor
  23. Intuitive Systems
  24. Mountain View, California
  25.  
  26. taylor@limbo.intuitive.com    or   {uunet!}{decwrl,apple}!limbo!taylor
  27.  
  28. -- Shell archive:
  29.  
  30. # This is a shell archive.  Remove anything before this line,
  31. # then unpack it by saving it in a file and typing "sh file".
  32. #
  33. # Wrapped by Dave Taylor <taylor@intuitive.com> on Sat Jun 30 12:38:08 1990
  34. #
  35. # This archive contains:
  36. #    timestamp.1    timestamp.c    
  37. #
  38. # Error checking via wc(1) will be performed.
  39. # Error checking via sum(1) will be performed.
  40.  
  41. LANG=""; export LANG
  42. PATH=/bin:/usr/bin:$PATH; export PATH
  43.  
  44. if sum -r </dev/null >/dev/null 2>&1
  45. then
  46.     sumopt='-r'
  47. else
  48.     sumopt=''
  49. fi
  50.  
  51. echo x - timestamp.1
  52. cat >timestamp.1 <<'@EOF'
  53. .TH TIMESTAMP 1L "" "" 
  54. .SH NAME
  55. timestamp \- check file modification times
  56. .SH SYNOPSIS
  57. .B timestamp 
  58. [
  59. .I \-q
  60. ] [
  61. .I delta
  62. ]
  63. .I filelist
  64. .sp
  65. .B timestamp 
  66. [
  67. .I \-q
  68. ] [
  69. .I \-r
  70. ] [
  71. .I delta
  72. ]
  73. .I filelist 
  74. .SH DESCRIPTION
  75. .I Timestamp
  76. is a utility that lists the subset of files given on the command
  77. line that are older (or newer) than the specified timestamp delta.
  78. .PP
  79. This simple application is designed to be included in shell scripts
  80. where the resolution of the 'last modified' check of the 
  81. .I find
  82. utility doesn't offer sufficiently high resolution.  Specifically,
  83. .I timestamp
  84. can be used to check for files that are older than 
  85. .I any
  86. time, from seconds to days.  Further, with the use of the optional
  87. .I \-r
  88. flag (to reverse the meaning of the command) 
  89. .I timestamp
  90. can be used to find files 
  91. .I newer
  92. than the specified time.
  93. .SH OPTIONS
  94. The 
  95. .I timestamp
  96. program has the following set of options available:
  97. .TP
  98. .B "-d N"
  99. Add the specified number of days to the timestamp delta.
  100. .TP
  101. .B "-h N"
  102. Add the specified number of hours to the timestamp delta.
  103. .TP
  104. .B "-m N"
  105. Add the specified number of minutes to the timestamp delta.
  106. .TP
  107. .B "-s N"
  108. Add the specified number of seconds to the timestamp delta.
  109. .TP
  110. .B "-q"
  111. Quiet: don't complain about files that we can't open.
  112. .TP
  113. .B "-r"
  114. Reverse the logic of the program; this will cause it to show
  115. all files newer than the specified timestamp delta, rather
  116. than older.
  117. .SH EXAMPLES
  118. Consider the case when you want to remove spurious lock files
  119. from the 
  120. .I sendmail
  121. queue: we don't want to wait until they're a day or more old
  122. to remove them (because that'd be a pretty slow delivery time
  123. for email), so instead:
  124. .nf
  125. .in +0.5i
  126.  
  127. timestamp -q -m 30 /usr/spool/mqueue/l*
  128.  
  129. .in -0.5i
  130. .fi
  131. which would output the lock files that are at least 30 minutes
  132. old in the spool directory.
  133. .PP
  134. By the same token, if we had our machine broken into, we could
  135. find out which files in 
  136. .I /tmp
  137. had been modified in the past few minutes with:
  138. .nf
  139. .in +0.5i
  140.  
  141. timestamp -r -m 5 /tmp/*
  142.  
  143. .in -0.5i
  144. .fi
  145. as appropriate.
  146. .SH SEE ALSO
  147. find(1)
  148. .SH AUTHOR
  149. Dave Taylor, Intuitive Systems
  150. .br
  151. <taylor@limbo.Intuitive.Com>
  152. .SH NOTES
  153. This command checks the last modified time of the files, not
  154. the created or last accessed times.  It's possible that we've
  155. choosen the wrong option in this case.
  156. .PP
  157. The 
  158. .I \-q
  159. option might well be something that should be there as a
  160. default value too...
  161. @EOF
  162. set `sum $sumopt <timestamp.1`; if test $1 -ne 15993
  163. then
  164.     echo ERROR: timestamp.1 checksum is $1 should be 15993
  165. fi
  166. set `wc -lwc <timestamp.1`
  167. if test $1$2$3 != 1084372406
  168. then
  169.     echo ERROR: wc results of timestamp.1 are $* should be 108 437 2406
  170. fi
  171.  
  172. chmod 644 timestamp.1
  173.  
  174. echo x - timestamp.c
  175. cat >timestamp.c <<'@EOF'
  176. /**                timestamp.c                **/
  177.  
  178. /** This program prints the names of all files in the argument list that
  179.     are at least as old as the time specified (with no default)
  180.  
  181.     (C) Copyright 1990 Dave Taylor  <taylor@intuitive.com>
  182.     (C) Copyright 1990 Intuitive Systems
  183.  
  184.     You may do whatever you want with this program so long as the 
  185.     copyright notice above remains intact.
  186. **/
  187.  
  188. #include <stdio.h>
  189. #include <sys/types.h>
  190. #include <sys/stat.h>
  191. #include <sys/time.h>
  192.  
  193. #ifndef TRUE
  194. # define TRUE            1
  195. # define FALSE            0
  196. #endif
  197.  
  198. #define SLEN            128
  199.  
  200. #define SECONDS_PER_MINUTE    60
  201. #define SECONDS_PER_HOUR    3600        /*     60 * 60     */
  202. #define SECONDS_PER_DAY        86400        /*    60 * 60 * 24    */
  203.  
  204. #define compare_times(ft,ct,test)     (test ? ft <= ct : ft > ct)
  205.  
  206. int getopt();
  207. char *ctime();
  208.  
  209. main(argc, argv)
  210. char **argv;
  211. {
  212.     extern int optind;
  213.     extern char *optarg;
  214.  
  215.     int c, older = TRUE, quiet = FALSE;
  216.     long hours = 0L, minutes = 0L, seconds = 0L, days = 0L;
  217.     long current_time, comparator;
  218.     char  buffer[SLEN];
  219.     struct stat statbuf;
  220.     
  221.     while ((c = getopt(argc, argv, "d:h:m:qrs:")) != EOF) {
  222.       switch (c) {
  223.          case 'd' : days = atoi(optarg);        break;
  224.          case 'h' : hours = atoi(optarg);        break;
  225.          case 'm' : minutes = atoi(optarg);        break;
  226.          case 'q' : quiet = TRUE;            break;
  227.          case 'r' : older = FALSE;            break;
  228.          case 's' : seconds = atoi(optarg);        break;
  229.           default : usage();
  230.       }
  231.     }
  232.  
  233.     /** now compute our offset from the current time **/
  234.  
  235.     current_time = time(0);
  236.     comparator = current_time - (days * SECONDS_PER_DAY)
  237.                   - (hours * SECONDS_PER_HOUR)
  238.                   - (minutes * SECONDS_PER_MINUTE)
  239.                   - seconds;
  240.  
  241.     while (optind < argc) {
  242.  
  243.       if ((stat(argv[optind], &statbuf) < 0) && (! quiet))  {
  244.         sprintf(buffer, "Failed trying to stat file '%s'", argv[optind]);
  245.         perror(buffer);
  246.       }
  247.  
  248.       if (compare_times(statbuf.st_mtime, comparator, older))
  249.         printf("%s\n", argv[optind]);
  250.  
  251.       optind++;
  252.     }
  253.  
  254.     exit(0);
  255. }
  256.  
  257. usage()
  258. {
  259.     fprintf(stderr, "Usage: timestamp [delta] [-q] [-r] filenames\n");
  260.     fprintf(stderr, "\nWhere delta can be any combination of:\n");
  261.     fprintf(stderr, "\t-d N  \tOlder than N days in the past\n");
  262.     fprintf(stderr, "\t-h N  \tOlder than N hours in the past\n");
  263.     fprintf(stderr, "\t-m N  \tOlder than N minutes in the past\n");
  264.     fprintf(stderr, 
  265.         "\t-q    \tQuiet: don't complain about nonexistent files\n");
  266.     fprintf(stderr, 
  267.         "\t-r    \tReverse logic: show newer, rather than older\n");
  268.     fprintf(stderr, "\t-s N  \tOlder than N seconds in the past\n");
  269.  
  270.     exit(1);
  271. }
  272. @EOF
  273. set `sum $sumopt <timestamp.c`; if test $1 -ne 39407
  274. then
  275.     echo ERROR: timestamp.c checksum is $1 should be 39407
  276. fi
  277. set `wc -lwc <timestamp.c`
  278. if test $1$2$3 != 963562510
  279. then
  280.     echo ERROR: wc results of timestamp.c are $* should be 96 356 2510
  281. fi
  282.  
  283. chmod 644 timestamp.c
  284.  
  285. exit 0
  286.