tests: move print_time function to libtests

Rename print_time function to print_time_t and move it to libtests.

* tests/print_time.c: New file.
* tests/Makefile.am (libtests_a_SOURCES): Add it.
* tests/tests.h (print_time_t): New prototype.
* tests/print_time.c (print_time): Remove.
(print_stat): Replace print_time with print_time_t.
This commit is contained in:
Дмитрий Левин 2017-02-26 20:35:37 +00:00
parent b8a045fbf0
commit af10e6e751
4 changed files with 57 additions and 22 deletions

View File

@ -54,6 +54,7 @@ libtests_a_SOURCES = \
overflowuid.c \
pipe_maxfd.c \
print_quoted_string.c \
print_time.c \
printflags.c \
printxval.c \
signal2name.c \

52
tests/print_time.c Normal file
View File

@ -0,0 +1,52 @@
/*
* Print time_t in symbolic format.
*
* Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "tests.h"
#include <stdio.h>
#include <time.h>
void
print_time_t(const time_t t)
{
if (!t) {
putchar('0');
return;
}
const struct tm *const p = localtime(&t);
if (p) {
char buf[256];
strftime(buf, sizeof(buf), "%FT%T%z", p);
fputs(buf, stdout);
} else {
printf("%llu", zero_extend_signed_to_ull(t));
}
}

View File

@ -103,6 +103,9 @@ void print_quoted_string(const char *);
/* Print memory in a quoted form. */
void print_quoted_memory(const char *, size_t);
/* Print time_t in symbolic format. */
void print_time_t(time_t);
/* Read an int from the file. */
int read_int_from_file(const char *, int *);

View File

@ -47,27 +47,6 @@
# include <unistd.h>
# include <sys/sysmacros.h>
static void
print_time(const time_t t)
{
if (!t) {
printf("0");
return;
}
struct tm *p = localtime(&t);
if (p) {
char buf[256];
strftime(buf, sizeof(buf), "%FT%T%z", p);
printf("%s", buf);
} else {
printf("%llu", zero_extend_signed_to_ull(t));
}
}
# ifndef STRUCT_STAT
# define STRUCT_STAT struct stat
# define STRUCT_STAT_STR "struct stat"
@ -192,7 +171,7 @@ print_stat(const STRUCT_STAT *st)
# define PRINT_ST_TIME(field) \
printf(", st_" #field "="); \
print_time(sign_extend_unsigned_to_ll(st->st_ ## field)); \
print_time_t(sign_extend_unsigned_to_ll(st->st_ ## field)); \
PRINT_TIME_NSEC(st->st_ ## field ## _nsec)
PRINT_ST_TIME(atime);