logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 2ad2f5fd5e92dc1fe38025a3be0865ac4bdb60d5
parent 950e3a9c99ff0d965077b3013783a3612d7d7b38
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 10 May 2022 18:59:34 +0200

bin/unlink: New util

Diffstat:

Abin/unlink.125+++++++++++++++++++++++++
Abin/unlink.c19+++++++++++++++++++
Mcoreutils.txt2+-
Mtest-bin/Kyuafile1+
Atest-bin/unlink21+++++++++++++++++++++
5 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/bin/unlink.1 b/bin/unlink.1 @@ -0,0 +1,25 @@ +.\" Collection of Unix tools, comparable to coreutils +.\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +.\" SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only +.Dd 2022-05-10 +.Dt UNLINK 1 +.Os +.Sh NAME +.Nm unlink +.Nd unlink filenames and possibly the referred files +.Sh SYNOPSIS +.Nm +.Op Ar files ... +.Sh DESCRIPTION +.Nm +deletes a filename from the filesystem. +If that filename was the last link to a file and no processes have the file open, the file is deleted. +.Sh EXIT STATUS +.Ex -std +.Sh STANDARDS +.Nm +is compliant with the +.St -p1003.1-2008 +specification. +.Sh AUTHORS +.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me diff --git a/bin/unlink.c b/bin/unlink.c @@ -0,0 +1,19 @@ +#include <errno.h> // errno +#include <stdio.h> // fprintf +#include <string.h> // strerror +#include <unistd.h> // unlink + +int +main(int argc, char *argv[]) +{ + for(int i = 1; i < argc; i++) + { + if(unlink(argv[i]) != 0) + { + fprintf(stderr, "unlink: Cannot unlink ‘%s’: %s\n", argv[i], strerror(errno)); + return 1; + } + } + + return 0; +} diff --git a/coreutils.txt b/coreutils.txt @@ -95,7 +95,7 @@ tty: Done uname: Use sname unexpand: Use sed uniq: Todo -unlink: Todo +unlink: Done users: No, use who vdir: No, use ls wc: Todo diff --git a/test-bin/Kyuafile b/test-bin/Kyuafile @@ -29,3 +29,4 @@ atf_test_program{name="tee", required_files=basedir.."/bin/tee", timeout=1} atf_test_program{name="true", required_files=basedir.."/bin/true", timeout=1} atf_test_program{name="tty", required_files=basedir.."/bin/tty", timeout=1} atf_test_program{name="xcd", required_files=basedir.."/bin/xcd", timeout=1} +atf_test_program{name="unlink", required_files=basedir.."/bin/unlink", timeout=1} diff --git a/test-bin/unlink b/test-bin/unlink @@ -0,0 +1,21 @@ +#!/usr/bin/env atf-sh +atf_test_case basic cleanup +basic_body() { + touch inputs/unlink-this || atf_fail "touch inputs/unlink-this" + atf_check ../bin/unlink inputs/unlink-this +} +basic_cleanup() { + rm -f inputs/unlink-this || atf_fail "rm -f inputs/unlink-this" +} + +atf_test_case enoent +enoent_body() { + # shellcheck disable=SC1112 + atf_check -s exit:1 -e 'inline:unlink: Cannot unlink ‘/var/empty/e/no/ent’: No such file or directory\n' ../bin/unlink /var/empty/e/no/ent +} + +atf_init_test_cases() { + cd "$(atf_get_srcdir)" || exit 1 + atf_add_test_case basic + atf_add_test_case enoent +}