From nobody Tue May 24 08:50:41 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 96A1B1B42E13; Tue, 24 May 2022 08:50:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4L6nwK3s1Nz4v1G; Tue, 24 May 2022 08:50:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653382241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3PAYKLo6SdR4f4WbRNiwh223oBV1kRicj8o7g6a06vM=; b=Zrgw/bOf6AFy9I0OHQXhi12n3Ko6G0PzVPKJvqom75DG7qwY6kVZYiMxgMbWrOstD8sc0I vOJoLeBrIm7i5JWjG1YG59useGRNUNA4YX7pQ6bsTStb9MvzpSkV04v+NR7Iev5sxaVf8k EbRSFxUtenEJje7cFgeczpV14AQGtrku/e9Ha78auwaYoGpATsniLo1/OKN2lNdPYjPmGw wR4z5deTRMJGD48dmJIweMZkK67Ejn0hZQ59MZ7nXXs8hag/NsydH8Tj34L3+5yRuEwJPB +jLuu/HlLNZARwraPaGvpnKsbZQeo8ijteJRAeaM8uXnGKUQcU92kTPoQjC8dA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 6573F2142D; Tue, 24 May 2022 08:50:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 24O8ofwX056038; Tue, 24 May 2022 08:50:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 24O8ofXL056037; Tue, 24 May 2022 08:50:41 GMT (envelope-from git) Date: Tue, 24 May 2022 08:50:41 GMT Message-Id: <202205240850.24O8ofXL056037@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Stefan E=C3=9Fer?= Subject: git: be038c3afcae - main - bin/sleep: add support for multiple delay values List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: se X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: be038c3afcaec78d6e92c33f03ec6d2523b3c719 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653382241; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3PAYKLo6SdR4f4WbRNiwh223oBV1kRicj8o7g6a06vM=; b=CLGHI6vmjpKTk/IR/787RO4wnmeg8rjETnSDiqU0fLahrPURxKLhwgcy/G1AOKPYOoBTqH YcHbrPdNzGX4pzd7qPg5F4FUzXrmS0gU02bURYz8xCum6GycPgRTI9pLkhdZ43KcRqcQyN Qm3ymNTBw0CwYg/fVo2mjG2H7cuifdnlyCWlNIeqvbRNli6BF9jNogo27m97ZJick7D4vk Tiz6CKRVl70SLaVzHo7FdOTTC97zXH6uMXS/vKn+adevL7U+fcjJVzhEN+Q3lKzmfk4uoC NyQWw90dVALB6NzTs28qseyePb88U3THOQITDy8Ry+HZh0z9oPY7Xnst54MSRw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1653382241; a=rsa-sha256; cv=none; b=dUS0lqyNJQyuhJIWfPQAZo4xxnPTyjLH0qLhp01c92cS8XHGDjA/BskbgC6+NLCdbVdnSZ iabdvAooRZ383q1y+CDzsx3f4Uu9Kf9UquiMyzcmHizy0J/5Cy80cO50/ptp3u8J8e5q3o 3ZLhWye2aITye6dT6qkgeasqxPuQLDiQ5qF/ljdU4wxqyZlTZSUwiP+L0MH/FNMEI/qB6r E9O1JfF71DYIlKCLnJC4YHrFJZU83Yodl2Ck95aSZ9d0DWjrVwhjJxdnvrf7RLepYXLfF6 XUmK+yaxXfS/wx9fjmZUuNDuP5VVJL8apBOPB1chdfr5EZFUnRWjUbv6xrCKlQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by se: URL: https://cgit.FreeBSD.org/src/commit/?id=be038c3afcaec78d6e92c33f03ec6d2523b3c719 commit be038c3afcaec78d6e92c33f03ec6d2523b3c719 Author: Stefan Eßer AuthorDate: 2022-05-24 08:47:40 +0000 Commit: Stefan Eßer CommitDate: 2022-05-24 08:47:40 +0000 bin/sleep: add support for multiple delay values The sleep time is calculated as the sum of all arguments passed. This makes the FreeBSD version of sleep functionally compatible with the version in GNU coreutils. MFC after: 1 week --- bin/sleep/sleep.1 | 16 ++++++++-------- bin/sleep/sleep.c | 50 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/bin/sleep/sleep.1 b/bin/sleep/sleep.1 index 3ce82936a0e7..85086acec5ad 100644 --- a/bin/sleep/sleep.1 +++ b/bin/sleep/sleep.1 @@ -32,7 +32,7 @@ .\" @(#)sleep.1 8.3 (Berkeley) 4/18/94 .\" $FreeBSD$ .\" -.Dd December 31, 2020 +.Dd May 24, 2022 .Dt SLEEP 1 .Os .Sh NAME @@ -40,21 +40,21 @@ .Nd suspend execution for an interval of time .Sh SYNOPSIS .Nm -.Ar number[unit] +.Ar number[unit] ... .Sh DESCRIPTION The .Nm -command -suspends execution for a minimum of +command suspends execution for a minimum of .Ar number seconds (the default, or unit .Ar s ) , -or minutes (unit +minutes (unit .Ar m ) , hours (unit .Ar h ) , or days (unit .Ar d ) . +If multiple arguments are passed, the delay will be the sum of all values. .Pp If the .Nm @@ -72,9 +72,9 @@ The .Nm command allows and honors a non-integer number of seconds to sleep in any form acceptable by -.Xr strtod 3 . -This is a non-portable extension, but is also implemented in GNU sh-utils -since version 2.0a (released in 2002). +.Xr strtod 3 and it accepts more than one delay value. +These are non-portable extensions, but they have also been implemented +in GNU sh-utils since version 2.0a (released in 2002). .Sh EXIT STATUS .Ex -std .Sh EXAMPLES diff --git a/bin/sleep/sleep.c b/bin/sleep/sleep.c index 8f383e43c479..55e0aba9871a 100644 --- a/bin/sleep/sleep.c +++ b/bin/sleep/sleep.c @@ -64,34 +64,48 @@ int main(int argc, char *argv[]) { struct timespec time_to_sleep; - double d; + double d, seconds; time_t original; char unit; char buf[2]; + int i, matches; if (caph_limit_stdio() < 0 || caph_enter() < 0) err(1, "capsicum"); - if (argc != 2) + if (argc < 2) usage(); - if (sscanf(argv[1], "%lf%c%1s", &d, &unit, buf) == 2) - switch(unit) { - case 'd': d *= 24; - case 'h': d *= 60; - case 'm': d *= 60; - case 's': break; - default: usage(); - } - else - if (sscanf(argv[1], "%lf%1s", &d, buf) != 1) - usage(); - if (d > INT_MAX) + seconds = 0; + for (i = 1; i < argc; i++) { + matches = sscanf(argv[i], "%lf%c%1s", &d, &unit, buf); + if (matches == 2) + switch(unit) { + case 'd': + d *= 24; + /* FALLTHROUGH */ + case 'h': + d *= 60; + /* FALLTHROUGH */ + case 'm': + d *= 60; + /* FALLTHROUGH */ + case 's': + break; + default: + usage(); + } + else + if (matches != 1) + usage(); + seconds += d; + } + if (seconds > INT_MAX) usage(); - if (d <= 0) + if (seconds <= 0) return (0); - original = time_to_sleep.tv_sec = (time_t)d; - time_to_sleep.tv_nsec = 1e9 * (d - time_to_sleep.tv_sec); + original = time_to_sleep.tv_sec = (time_t)seconds; + time_to_sleep.tv_nsec = 1e9 * (seconds - time_to_sleep.tv_sec); signal(SIGINFO, report_request); @@ -116,7 +130,7 @@ static void usage(void) { - fprintf(stderr, "usage: sleep number[unit]\n"); + fprintf(stderr, "usage: sleep number[unit] ...\n"); fprintf(stderr, "Unit can be 's' (seconds, the default), " "m (minutes), h (hours), or d (days).\n"); exit(1);