Skip to main content

yash_env/system/
sysconf.rs

1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2025 WATANABE Yuki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17//! Items for obtaining system configuration information
18
19use super::Result;
20use crate::str::UnixString;
21use std::ffi::CString;
22use std::rc::Rc;
23
24/// Trait for getting system configuration information
25pub trait Sysconf {
26    /// Returns the standard `$PATH` value where all standard utilities are
27    /// expected to be found.
28    ///
29    /// This is a thin wrapper around `confstr(_CS_PATH, …)`.
30    fn confstr_path(&self) -> Result<UnixString>;
31}
32
33/// Delegates the `Sysconf` trait to the contained instance of `S`
34impl<S: Sysconf> Sysconf for Rc<S> {
35    #[inline]
36    fn confstr_path(&self) -> Result<UnixString> {
37        (self as &S).confstr_path()
38    }
39}
40
41/// Trait for getting the path to the shell executable
42pub trait ShellPath {
43    // TODO: Should return `Cow<CStr>` instead
44    /// Returns the path to the shell executable.
45    ///
46    /// If possible, this function should return the path to the current shell
47    /// executable. Otherwise, it should return the path to the default POSIX
48    /// shell.
49    fn shell_path(&self) -> CString;
50}
51
52/// Delegates the `ShellPath` trait to the contained instance of `S`
53impl<S: ShellPath> ShellPath for Rc<S> {
54    #[inline]
55    fn shell_path(&self) -> CString {
56        (self as &S).shell_path()
57    }
58}