|
| 1 | +/* |
| 2 | + Copyright (c) 2015 Thomas Klausner |
| 3 | + All rights reserved. |
| 4 | +
|
| 5 | + Redistribution and use in source and binary forms, with or without |
| 6 | + modification, are permitted provided that the following conditions |
| 7 | + are met: |
| 8 | +
|
| 9 | + 1. Redistributions of source code must retain the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer. |
| 11 | + 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in the |
| 13 | + documentation and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 17 | + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
| 19 | + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*/ |
| 27 | +#include <sys/types.h> |
| 28 | +#include <sys/stat.h> |
| 29 | +#include <fts.h> |
| 30 | +#include <time.h> |
| 31 | +#include <stdio.h> |
| 32 | +#include <stdlib.h> |
| 33 | + |
| 34 | +time_t |
| 35 | +get_newest(char * const *path) { |
| 36 | + FTS *dh; |
| 37 | + FTSENT *entry; |
| 38 | + time_t newest = 0; |
| 39 | + dh = fts_open(path, FTS_PHYSICAL, NULL); |
| 40 | + if (dh == NULL) { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + while ((entry=fts_read(dh)) != NULL) { |
| 44 | + if ((entry->fts_info != FTS_F) || (entry->fts_statp == NULL)) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + if (entry->fts_statp->st_mtime > newest) { |
| 48 | + newest = entry->fts_statp->st_mtime; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (fts_close(dh) != 0) { |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + return newest; |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +int |
| 61 | +main(int argc, char * const *argv) |
| 62 | +{ |
| 63 | + time_t newest = 0; |
| 64 | + |
| 65 | + if (argc < 2 || (newest=get_newest(argv+1)) == 0) { |
| 66 | + fprintf(stderr, "no files found\n"); |
| 67 | + exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + printf("%lld\n", (long long)newest); |
| 71 | + exit(0); |
| 72 | +} |
0 commit comments