]>
Commit | Line | Data |
---|---|---|
8211d1b5 DD |
1 | /* |
2 | * Copyright (c) 2016 Deepa Dinamani | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it would be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | #include "command.h" | |
20 | #include "input.h" | |
21 | #include "init.h" | |
22 | #include "io.h" | |
23 | ||
24 | static cmdinfo_t utimes_cmd; | |
25 | ||
26 | static void | |
27 | utimes_help(void) | |
28 | { | |
29 | printf(_( | |
30 | "\n" | |
31 | " Update file atime and mtime of the current file with nansecond precision.\n" | |
32 | "\n" | |
33 | " Usage: utimes atime_sec atime_nsec mtime_sec mtime_nsec.\n" | |
34 | " *_sec: Seconds elapsed since 1970-01-01 00:00:00 UTC.\n" | |
35 | " *_nsec: Nanoseconds since the corresponding *_sec.\n" | |
36 | "\n")); | |
37 | } | |
38 | ||
39 | static int | |
40 | utimes_f( | |
41 | int argc, | |
42 | char **argv) | |
43 | { | |
44 | struct timespec t[2]; | |
45 | int result; | |
46 | ||
47 | /* Get the timestamps */ | |
48 | result = timespec_from_string(argv[1], argv[2], &t[0]); | |
49 | if (result) { | |
50 | fprintf(stderr, "Bad value for atime\n"); | |
51 | return 0; | |
52 | } | |
53 | result = timespec_from_string(argv[3], argv[4], &t[1]); | |
54 | if (result) { | |
55 | fprintf(stderr, "Bad value for mtime\n"); | |
56 | return 0; | |
57 | } | |
58 | ||
59 | /* Call futimens to update time. */ | |
60 | if (futimens(file->fd, t)) { | |
61 | perror("futimens"); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
68 | void | |
69 | utimes_init(void) | |
70 | { | |
71 | utimes_cmd.name = "utimes"; | |
72 | utimes_cmd.cfunc = utimes_f; | |
73 | utimes_cmd.argmin = 4; | |
74 | utimes_cmd.argmax = 4; | |
75 | utimes_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK; | |
76 | utimes_cmd.args = _("atime_sec atime_nsec mtime_sec mtime_nsec"); | |
77 | utimes_cmd.oneline = _("Update file times of the current file"); | |
78 | utimes_cmd.help = utimes_help; | |
79 | ||
80 | add_command(&utimes_cmd); | |
81 | } |