commit:     6b0db7d9abfded8bdf8c7d061b261f053eec886d
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Mon Sep 28 20:00:17 2015 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Mon Sep 28 20:00:17 2015 +0000
URL:        https://gitweb.gentoo.org/proj/sandbox.git/commit/?id=6b0db7d9

tests: add basic parsing of timespec fields

Signed-off-by: Mike Frysinger <vapier <AT> gentoo.org>

 tests/test-skel-0.c | 23 ++++++++++++++++++++++-
 tests/tests.h       |  3 +++
 tests/utimensat-0.c |  2 +-
 3 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/tests/test-skel-0.c b/tests/test-skel-0.c
index dbe60db..96e42ae 100644
--- a/tests/test-skel-0.c
+++ b/tests/test-skel-0.c
@@ -9,7 +9,6 @@ const char *color_red    = "\033[31;01m";
 # define CONFIG 1
 #endif
 
-#define V_TIMESPEC "NULL"
 #define V_STRMODE "<r|w|a>[+bcemx] (see `man 3 fopen`)"
 
 static bool _strtoul(const char *sul, unsigned long *ul)
@@ -132,6 +131,28 @@ int at_get_fd(const char *str_dirfd)
        return open(str_path, f_get_flags(str_flags), sscanf_mode_t(str_mode));
 }
 
+#define V_TIMESPEC "NULL | NOW | #[,#]"
+struct timespec *parse_timespec(const char *s)
+{
+       struct timespec *times;
+
+       if (!strcmp(s, "NULL"))
+               return NULL;
+
+       times = xzalloc(sizeof(*times));
+
+       if (!strcmp(s, "NOW")) {
+               times->tv_sec = time(0);
+       } else {
+               long sec = 0, nsec = 0;
+               sscanf(s, "%li,%li", &sec, &nsec);
+               times->tv_sec = sec;
+               times->tv_nsec = nsec;
+       }
+
+       return times;
+}
+
 #define V_ACCESS_MODE "r | w | x | f"
 int access_mode(const char *s)
 {

diff --git a/tests/tests.h b/tests/tests.h
index 51dc68a..22733ca 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -10,6 +10,9 @@
 #define err(fmt, args...) ({ _stderr_msg(fmt, ##args); exit(1); })
 #define errp(fmt, args...) ({ _stderr_pmsg(fmt, ##args); exit(1); })
 
+#define xmalloc(size) ({ void *ret = malloc(size); assert(ret); ret; })
+#define xzalloc(size) ({ void *ret = xmalloc(size); memset(ret, 0, size); ret; 
})
+
 typedef struct {
        const char *name;
        int val;

diff --git a/tests/utimensat-0.c b/tests/utimensat-0.c
index 431d179..99c3fa4 100644
--- a/tests/utimensat-0.c
+++ b/tests/utimensat-0.c
@@ -14,7 +14,7 @@
        const char *file = f_get_file(s); \
        \
        s = argv[i++]; \
-       const struct timespec *times = NULL; \
+       const struct timespec *times = parse_timespec(s); \
        \
        s = argv[i++]; \
        int flags = at_get_flags(s);

Reply via email to