Skip to content

Commit 084fc1b

Browse files
committed
First version, tested on NetBSD and OS X 10.11.1.
0 parents  commit 084fc1b

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn
2+
*~
3+
*.tar.gz

Diff for: LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2015 Thomas Klausner
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions
6+
are met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
2. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
18+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
POSSIBILITY OF SUCH DAMAGE.
25+

Diff for: Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn: fn.c
2+
$(CC) $(CFLAGS) -o fn fn.c
3+
clean:
4+
rm -f fn
5+
tar:
6+
rm -f fn-0.1
7+
ln -fs . fn-0.1
8+
tar -cvzf fn-0.1.tar.gz fn-0.1/fn.c fn-0.1/Makefile fn-0.1/LICENSE fn-0.1/README.md
9+
rm fn-0.1
10+
11+
.PHONY: clean tar

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# findnewest
2+
3+
Recursively find newest file in a hierarchy and print its timestamp.
4+
5+
-- Thomas Klausner <[email protected]>

Diff for: fn.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)