From e34e4fad3f67abc5810f374589881e6a50fc4467 Mon Sep 17 00:00:00 2001 From: DTB Date: Tue, 23 Jul 2024 21:14:42 -0600 Subject: [PATCH] pschdir(1): rewrite in Rust --- Makefile | 4 ++-- src/pschdir.c | 48 --------------------------------------- src/pschdir.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 50 deletions(-) delete mode 100644 src/pschdir.c create mode 100644 src/pschdir.rs diff --git a/Makefile b/Makefile index 482c321..2e4a95e 100644 --- a/Makefile +++ b/Makefile @@ -124,8 +124,8 @@ build/bin/npc: src/npc.c build .PHONY: pschdir pschdir: build/bin/pschdir -build/bin/pschdir: src/pschdir.c build - $(CC) $(CFLAGS) -o $@ src/pschdir.c +build/bin/pschdir: src/pschdir.rs build + $(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/pschdir.rs .PHONY: rpn rpn: build/bin/rpn diff --git a/src/pschdir.c b/src/pschdir.c deleted file mode 100644 index 44ea573..0000000 --- a/src/pschdir.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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/. - */ - -#include /* EACCESS, ELOOP, ENAMETOOLONG, ENOENT, ENOTDIR, errno */ -#include /* fprintf(3), stderr */ -#include /* strerror(3) */ -#include /* EX_OSERR, EX_NOPERM, EX_NOINPUT, EX_UNAVAILABLE, - * EX_USAGE */ -#include /* chdir(2) */ - -char *program_name = "pschdir"; - -int main(int argc, char *argv[]){ - if (argc < 3) { - fprintf(stderr, "Usage: %s directory command [argument...]\n", - argv[0] == NULL ? program_name : argv[0]); - return EX_USAGE; - } - - if(chdir(argv[1]) != 0){ - fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], strerror(errno)); - - switch (errno) { - case ENAMETOOLONG: return EX_OSERR; - case EACCES: return EX_NOPERM; - case ENOENT: - case ENOTDIR: return EX_NOINPUT; - default: return EX_UNAVAILABLE; - } - } - - execvp(argv[2], &argv[2]); -} diff --git a/src/pschdir.rs b/src/pschdir.rs new file mode 100644 index 0000000..eaa7e63 --- /dev/null +++ b/src/pschdir.rs @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023_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, io::Error, process::{ Command, ExitCode } }; + +extern crate strerror; +use strerror::StrError; + +extern crate sysexits; +use sysexits::{ EX_UNAVAILABLE, EX_USAGE }; + +fn error(p: &str, n: &str, e: Error) -> ExitCode { + eprintln!("{}: {}: {}", p, n, e.strerror()); + ExitCode::from(EX_UNAVAILABLE as u8) +} + +fn main() -> ExitCode { + let argv = args().collect::>(); + + if argv.len() < 3 { + eprintln!("Usage: {} directory command [argument]", argv[0]); + return ExitCode::from(EX_USAGE as u8); + } + + let cmdargs = argv.iter().clone().skip(3).collect::>(); + + match Command::new(argv[2].clone()) + .current_dir(argv[1].clone()) + .args(cmdargs) + .spawn() { + Ok(mut child) => match child.wait() { + Ok(status) => { + match status.code() { + Some(code) => ExitCode::from(code as u8), + None => { + eprintln!("{}: {}: process terminated by signal", + argv[0], argv[2]); + ExitCode::FAILURE + } + } + }, + Err(e) => error(&argv[0], &argv[2], e) + }, + Err(e) => error(&argv[0], &argv[2], e) + } +}