From 3dfc9082e680340cefb7f1a3b1fcf489ad01e522 Mon Sep 17 00:00:00 2001 From: Victor Song Date: Wed, 8 Mar 2023 01:30:48 -0500 Subject: [PATCH] Use `std::io::Error::last_os_error()` for `errno` --- fish-rust/src/builtins/realpath.rs | 34 +++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/fish-rust/src/builtins/realpath.rs b/fish-rust/src/builtins/realpath.rs index 401dd49fe..802377635 100644 --- a/fish-rust/src/builtins/realpath.rs +++ b/fish-rust/src/builtins/realpath.rs @@ -1,6 +1,9 @@ //! Implementation of the realpath builtin. +use std::io::Error; + use libc::c_int; +use nix::errno::errno; use crate::{ ffi::parser_t, @@ -95,12 +98,22 @@ pub fn realpath( if let Some(real_path) = wrealpath(arg) { streams.out.append(real_path); } else { - // TODO: get error from errno - // Report the error and make it clear this is an error - // from our builtin, not the system's realpath. - streams - .err - .append(wgettext_fmt!("builtin %ls: %ls\n", cmd, arg)); + if errno() != 0 { + // realpath() just couldn't do it. Report the error and make it clear + // this is an error from our builtin, not the system's realpath. + streams.err.append(wgettext_fmt!( + "builtin %ls: %ls: %s\n", + cmd, + arg, + Error::last_os_error().to_string() + )); + } else { + // Who knows. Probably a bug in our wrealpath() implementation. + streams + .err + .append(wgettext_fmt!("builtin %ls: Invalid arg: %ls\n", cmd, arg)); + } + return STATUS_CMD_ERROR; } } else { @@ -115,10 +128,11 @@ pub fn realpath( }; streams.out.append(normalize_path(&absolute_arg, false)); } else { - // TODO: get error from errno - streams - .err - .append(wgettext_fmt!("builtin %ls: realpath failed\n", cmd)); + streams.err.append(wgettext_fmt!( + "builtin %ls: realpath failed: %s\n", + cmd, + std::io::Error::last_os_error().to_string() + )); return STATUS_CMD_ERROR; } }