1 |
commit: 6b0db7d9abfded8bdf8c7d061b261f053eec886d |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Sep 28 20:00:17 2015 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Sep 28 20:00:17 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=6b0db7d9 |
7 |
|
8 |
tests: add basic parsing of timespec fields |
9 |
|
10 |
Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org> |
11 |
|
12 |
tests/test-skel-0.c | 23 ++++++++++++++++++++++- |
13 |
tests/tests.h | 3 +++ |
14 |
tests/utimensat-0.c | 2 +- |
15 |
3 files changed, 26 insertions(+), 2 deletions(-) |
16 |
|
17 |
diff --git a/tests/test-skel-0.c b/tests/test-skel-0.c |
18 |
index dbe60db..96e42ae 100644 |
19 |
--- a/tests/test-skel-0.c |
20 |
+++ b/tests/test-skel-0.c |
21 |
@@ -9,7 +9,6 @@ const char *color_red = "\033[31;01m"; |
22 |
# define CONFIG 1 |
23 |
#endif |
24 |
|
25 |
-#define V_TIMESPEC "NULL" |
26 |
#define V_STRMODE "<r|w|a>[+bcemx] (see `man 3 fopen`)" |
27 |
|
28 |
static bool _strtoul(const char *sul, unsigned long *ul) |
29 |
@@ -132,6 +131,28 @@ int at_get_fd(const char *str_dirfd) |
30 |
return open(str_path, f_get_flags(str_flags), sscanf_mode_t(str_mode)); |
31 |
} |
32 |
|
33 |
+#define V_TIMESPEC "NULL | NOW | #[,#]" |
34 |
+struct timespec *parse_timespec(const char *s) |
35 |
+{ |
36 |
+ struct timespec *times; |
37 |
+ |
38 |
+ if (!strcmp(s, "NULL")) |
39 |
+ return NULL; |
40 |
+ |
41 |
+ times = xzalloc(sizeof(*times)); |
42 |
+ |
43 |
+ if (!strcmp(s, "NOW")) { |
44 |
+ times->tv_sec = time(0); |
45 |
+ } else { |
46 |
+ long sec = 0, nsec = 0; |
47 |
+ sscanf(s, "%li,%li", &sec, &nsec); |
48 |
+ times->tv_sec = sec; |
49 |
+ times->tv_nsec = nsec; |
50 |
+ } |
51 |
+ |
52 |
+ return times; |
53 |
+} |
54 |
+ |
55 |
#define V_ACCESS_MODE "r | w | x | f" |
56 |
int access_mode(const char *s) |
57 |
{ |
58 |
|
59 |
diff --git a/tests/tests.h b/tests/tests.h |
60 |
index 51dc68a..22733ca 100644 |
61 |
--- a/tests/tests.h |
62 |
+++ b/tests/tests.h |
63 |
@@ -10,6 +10,9 @@ |
64 |
#define err(fmt, args...) ({ _stderr_msg(fmt, ##args); exit(1); }) |
65 |
#define errp(fmt, args...) ({ _stderr_pmsg(fmt, ##args); exit(1); }) |
66 |
|
67 |
+#define xmalloc(size) ({ void *ret = malloc(size); assert(ret); ret; }) |
68 |
+#define xzalloc(size) ({ void *ret = xmalloc(size); memset(ret, 0, size); ret; }) |
69 |
+ |
70 |
typedef struct { |
71 |
const char *name; |
72 |
int val; |
73 |
|
74 |
diff --git a/tests/utimensat-0.c b/tests/utimensat-0.c |
75 |
index 431d179..99c3fa4 100644 |
76 |
--- a/tests/utimensat-0.c |
77 |
+++ b/tests/utimensat-0.c |
78 |
@@ -14,7 +14,7 @@ |
79 |
const char *file = f_get_file(s); \ |
80 |
\ |
81 |
s = argv[i++]; \ |
82 |
- const struct timespec *times = NULL; \ |
83 |
+ const struct timespec *times = parse_timespec(s); \ |
84 |
\ |
85 |
s = argv[i++]; \ |
86 |
int flags = at_get_flags(s); |