From 82a941eee38b484967833ab11c401fd8eb2bf295 Mon Sep 17 00:00:00 2001 From: DTB Date: Thu, 21 Mar 2024 20:41:05 -0600 Subject: [PATCH] sleep(1): initial implementation --- src/sleep.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/sleep.rs diff --git a/src/sleep.rs b/src/sleep.rs new file mode 100644 index 0000000..f5af47d --- /dev/null +++ b/src/sleep.rs @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024 DTB + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +use std::{ + env::args, + process::ExitCode, + thread::sleep, + time::Duration +}; + +extern crate sysexits; +use sysexits::{EX_OK, EX_USAGE}; + +fn usage(s: &str) -> ExitCode { + eprintln!("Usage: {} [seconds]", s); + ExitCode::from(EX_USAGE as u8) +} + +fn main() -> ExitCode { + let argv = args().collect::>(); + + if argv.len() == 2 { + if let Ok(s) = argv[1].parse::() { + sleep(Duration::from_secs(s)); + ExitCode::from(EX_OK as u8) + } else { + usage(&argv[0]) + } + } else { + usage(&argv[0]) + } +}