idmapped-mounts: add missing newline to print_r()
[xfstests-dev.git] / src / t_get_file_time.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2009 FUJITSU LIMITED
4  * Author: Li Zefan <lizf@cn.fujitsu.com>
5  */
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 /*
14  * Usage: t_get_file_time <filename> <atime|mtime|ctime> <sec|nsec>
15  */
16 int main(int argc, char *argv[])
17 {
18        time_t t;
19        struct stat st;
20
21        if (argc != 4) {
22                fprintf(stderr, "Wrong argument num!\n");
23                return 1;
24        }
25
26        if (stat(argv[1], &st) != 0) {
27                perror("stat failed");
28                return 1;
29        }
30
31        if (strcmp(argv[3], "sec") == 0) {
32                if (strcmp(argv[2], "atime") == 0)
33                        t = st.st_atime;
34                else if (strcmp(argv[2], "mtime") == 0)
35                        t = st.st_mtime;
36                else
37                        t = st.st_ctime;
38        } else if (strcmp(argv[3], "nsec") == 0) {
39                if (strcmp(argv[2], "atime") == 0)
40                        t = st.st_atim.tv_nsec;
41                else if (strcmp(argv[2], "mtime") == 0)
42                        t = st.st_mtim.tv_nsec;
43                else
44                        t = st.st_ctim.tv_nsec;
45        } else {
46                fprintf(stderr, "Wrong argument: %s\n", argv[3]);
47                return 1;
48        }
49
50        printf("%lu\n", t);
51
52        return 0;
53 }