logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 19f4662eeb0e6db7c60772cb438624cadd56fd49
parent d63f75d6def87d5b2d5a602bf04759eb2168b1cd
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed,  8 Dec 2021 13:06:44 +0100

bin/pwd: Simplify, return correctly

Diffstat:

Mbin/pwd.c29+++++++++++++++++------------
Mtest-bin/Kyuafile4+++-
Atest-bin/pwd10++++++++++
3 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/bin/pwd.c b/bin/pwd.c @@ -3,34 +3,39 @@ // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only #define _POSIX_C_SOURCE 200809L -#include <errno.h> /* errno */ #include <stdio.h> /* puts, perror, printf */ -#include <stdlib.h> /* exit */ -#include <string.h> /* strerror */ #include <unistd.h> /* getcwd() */ -char buf[BUFSIZ]; - void usage(char *a0) { (void)printf("usage: %s\n", a0); - exit(EXIT_SUCCESS); } int main(int argc, char *argv[]) { - if(argc != 1) usage(argv[0]); + char pwd[BUFSIZ]; + + if(argc != 1) + { + usage(argv[0]); + } - if(getcwd(buf, sizeof(buf)) == NULL) + if(getcwd(pwd, sizeof(pwd)) == NULL) { - perror(strerror(errno)); - exit(EXIT_FAILURE); + perror("getcwd"); + return 1; } else { - const char *pwd = buf; - return puts(pwd); + if(puts(pwd) < 0) + { + return 1; + } + else + { + return 0; + } } } diff --git a/test-bin/Kyuafile b/test-bin/Kyuafile @@ -2,7 +2,8 @@ syntax(2) test_suite("utils") --- /BEGIN/,$ | LC_ALL=C.UTF-8 sort +-- /BEGIN/,$ +-- |LC_ALL=C.UTF-8 sort atf_test_program{name="args"} atf_test_program{name="basename"} atf_test_program{name="cat"} @@ -10,6 +11,7 @@ atf_test_program{name="dirname"} atf_test_program{name="echo"} atf_test_program{name="false"} atf_test_program{name="humanize"} +atf_test_program{name="pwd"} atf_test_program{name="sname"} atf_test_program{name="tee"} atf_test_program{name="true"} diff --git a/test-bin/pwd b/test-bin/pwd @@ -0,0 +1,10 @@ +#!/usr/bin/env atf-sh +atf_test_case simple +simple_body() { + atf_check -o "inline:${PWD}\n" ../bin/pwd +} + +atf_init_test_cases() { + cd "$(atf_get_srcdir)" + atf_add_test_case simple +}