logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 812e249e965ec9ea871ec9b00558e43dac0615ea
parent e1aa97f70e6005d675b741c59063a602141378ee
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  1 Mar 2022 18:39:44 +0100

bin/date: Correctly handle "+" as sole argument

Diffstat:

Mbin/date.c4+++-
Mtest-bin/date12+++++++++++-
2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/bin/date.c b/bin/date.c @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only #define _POSIX_C_SOURCE 200809L +#include <errno.h> /* errno */ #include <locale.h> /* setlocale() */ #include <stdio.h> /* BUFSIZ, perror(), puts() */ #include <stdlib.h> /* exit() */ @@ -64,7 +65,8 @@ main(int argc, char *argv[]) if(*argv && **argv == '+') format = *argv + 1; - if(strftime(outstr, sizeof(outstr), format, tm) == 0) + errno = 0; + if(strftime(outstr, sizeof(outstr), format, tm) == 0 && errno != 0) { perror("strftime"); exit(EXIT_FAILURE); diff --git a/test-bin/date b/test-bin/date @@ -14,6 +14,15 @@ rfc3339_body() { atf_check -o "match:^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{4}$" ../bin/date '+%FT%T%z' } +atf_test_case empty +empty_body() { + atf_check -o 'inline:\n' ../bin/date '+' +} + +atf_test_case echolike +echolike_body() { + atf_check -o 'inline:hello world\n' ../bin/date '+hello world' +} atf_test_case devfull devfull_body() { @@ -37,7 +46,8 @@ atf_init_test_cases() { atf_add_test_case noargs atf_add_test_case epoch atf_add_test_case rfc3339 + atf_add_test_case empty atf_add_test_case utc - + atf_add_test_case echolike atf_add_test_case devfull }