1 /*
2 * get-mtime.c: get a file's modification time
3 *
4 * Copyright (C) 2022 Colin Watson.
5 *
6 * This file is part of man-db.
7 *
8 * man-db is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * man-db is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif /* HAVE_CONFIG_H */
26
27 #include <errno.h>
28 #include <stdlib.h>
29
30 #include "argp.h"
31 #include "progname.h"
32 #include "stat-time.h"
33
34 #include "manconfig.h"
35
36 #include "fatal.h"
37
38 char *path;
39
40 static const char args_doc[] = "PATH";
41
42 static error_t parse_opt (int key, char *arg, struct argp_state *state)
43 {
44 switch (key) {
45 case ARGP_KEY_ARG:
46 if (path)
47 argp_usage (state);
48 path = arg;
49 return 0;
50 case ARGP_KEY_NO_ARGS:
51 argp_usage (state);
52 break;
53 }
54 return ARGP_ERR_UNKNOWN;
55 }
56
57 static struct argp argp = { NULL, parse_opt, args_doc };
58
59 int main (int argc, char **argv)
60 {
61 struct stat st;
62 struct timespec ts;
63
64 set_program_name (argv[0]);
65
66 if (argp_parse (&argp, argc, argv, 0, 0, 0))
67 exit (FAIL);
68
69 if (lstat (path, &st) < 0)
70 fatal (errno, "can't lstat %s", path);
71 ts = get_stat_mtime (&st);
72 printf ("%ld.%09ld\n", (long) ts.tv_sec, ts.tv_nsec);
73
74 exit (OK);
75 }