1 /******************************************************************************/
2 /* */
3 /* Copyright (c) 2009 FUJITSU LIMITED */
4 /* */
5 /* This program is free software; you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation; either version 2 of the License, or */
8 /* (at your option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
13 /* the GNU General Public License for more details. */
14 /* */
15 /* You should have received a copy of the GNU General Public License */
16 /* along with this program; if not, write to the Free Software */
17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
18 /* */
19 /* Author: Li Zefan <lizf@cn.fujitsu.com> */
20 /* */
21 /******************************************************************************/
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 /*
30 * Usage: file_time <filename> <atime|mtime|ctime> <sec|nsec>
31 */
main(int argc,char * argv[])32 int main(int argc, char *argv[])
33 {
34 time_t t;
35 struct stat st;
36
37 if (argc != 4) {
38 fprintf(stderr, "Wrong argument num!\n");
39 return 1;
40 }
41
42 if (stat(argv[1], &st) != 0) {
43 perror("stat failed");
44 return 1;
45 }
46
47 if (strcmp(argv[3], "sec") == 0) {
48 if (strcmp(argv[2], "atime") == 0)
49 t = st.st_atime;
50 else if (strcmp(argv[2], "mtime") == 0)
51 t = st.st_mtime;
52 else
53 t = st.st_ctime;
54 } else if (strcmp(argv[3], "nsec") == 0) {
55 if (strcmp(argv[2], "atime") == 0)
56 t = st.st_atim.tv_nsec;
57 else if (strcmp(argv[2], "mtime") == 0)
58 t = st.st_mtim.tv_nsec;
59 else
60 t = st.st_ctim.tv_nsec;
61 } else {
62 fprintf(stderr, "Wrong argument: %s\n", argv[3]);
63 return 1;
64 }
65
66 printf("%lu\n", t);
67
68 return 0;
69 }
70