logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 87274b96510774cf6a6ae3737f57bea4f1d7114b
parent bb481deac770083524b18577724cfa21b2061d78
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 19 Nov 2022 20:27:46 +0100

cmd/sleep: New utility

Diffstat:

MMakefile3+++
Acmd/sleep.130++++++++++++++++++++++++++++++
Acmd/sleep.c72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mconfigure6++++--
Mcoreutils.txt2+-
5 files changed, 110 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile @@ -67,3 +67,6 @@ cmd/strings: cmd/strings.c Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) $(LIBBSD_CFLAGS) -o $@ $< $(LIBBSD_LIBS) $(LDFLAGS) +cmd/sleep: cmd/sleep.c Makefile + rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} + $(CC) -std=c99 $(CFLAGS) $(LIBBSD_CFLAGS) -o $@ $< $(LIBBSD_LIBS) $(LDFLAGS) diff --git a/cmd/sleep.1 b/cmd/sleep.1 @@ -0,0 +1,30 @@ +.\" 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-11-19 +.Dt SLEEP 1 +.Os +.Sh NAME +.Nm sleep +.Nd delay for a specified amount of time +.Sh SYNOPSIS +.Nm +.Ar duration ... +.Sh DESCRIPTION +The +.nm +utily shell suspends execution for the total of each +.Ar duration +argument. +.Pp +.Ar duration +is a non-negative decimal integer optionally followed by a suffix: s for seconds (default), m for minutes, h for hours. +Longer durations are taken as out of scope. +.Sh SEE ALSO +.Xr sleep 3 , +.Xr crontab 1 , +.Xr at 1 +.Sh EXIT STATUS +.Ex -std +.Sh AUTHORS +.An Haelwenn (lanodan) Monnier Aq Mt contact@hacktivis.me diff --git a/cmd/sleep.c b/cmd/sleep.c @@ -0,0 +1,72 @@ +// Collection of Unix tools, comparable to coreutils +// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only + +#define _DEFAULT_SOURCE +// strtou +#define _NETBSD_SOURCE +// On non-BSDs use libbsd +#include <errno.h> // ENOTSUP +#include <inttypes.h> // strtou +#include <limits.h> // UINT_MAX, _POSIX_ARG_MAX +#include <stdio.h> // fprintf +#include <stdlib.h> // exit +#include <string.h> // strnlen, strerror +#include <unistd.h> // sleep + +uintmax_t +argtonum(char *arg) +{ + char *end; + int err = 0; + uintmax_t dur = strtou(arg, &end, 10, 1, UINT_MAX, &err); + if(err != 0 && err != ENOTSUP) + { + fprintf(stderr, "sleep: Error parsing '%s' as a number: %s\n", arg, strerror(err)); + exit(1); + } + if(end[0] != 0) + { + size_t end_len = strnlen(end, _POSIX_ARG_MAX); + if(end_len != 1) + { + fprintf(stderr, "sleep: suffix '%s' is too long, should be only one character\n", end); + exit(1); + } + switch(end[0]) + { + case 's': // seconds + break; + case 'm': // minutes + dur *= 60; + break; + case 'h': // hours + dur *= 60 * 60; + break; + default: + fprintf(stderr, "sleep: Unknown suffix %c\n", end[0]); + exit(1); + } + } + + return dur; +} + +int +main(int argc, char *argv[]) +{ + unsigned int dur = 0; + for(int i = 1; i < argc; i++) + { + dur += argtonum(argv[i]); + } + if(dur == 0) + { + fprintf(stderr, "sleep: Got a duration of 0\n"); + return 1; + } + + sleep(dur); + + return 0; +} diff --git a/configure b/configure @@ -176,17 +176,19 @@ do pkg_config_check --exists "$dep" || exit 1 done -# TODO: Check for humanize_number and strtonum existance +# TODO: Check for strtonum and strtou existance if pkg_config_check libbsd-overlay then LIBBSD_CFLAGS="$("${PKGCONFIG}" --cflags libbsd-overlay) -DHAVE_LIBBSD" LIBBSD_LIBS="$("${PKGCONFIG}" --libs libbsd-overlay)" else cat >> target_filter <<EOF +# strtou +sleep.c +sleep.1 # strtonum strings.c strings.1 -# humanize_number humanize.c humanize.1 EOF diff --git a/coreutils.txt b/coreutils.txt @@ -75,7 +75,7 @@ sha384sum: No sha512sum: No shred: ? shuf: Todo -sleep: Todo +sleep: Done sort: ? split: No. Considered obsolete stat: Todo