Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions src/uu/printenv/src/printenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@

use clap::{Arg, ArgAction, Command};
use std::env;
#[cfg(unix)]
use std::io::{self, Write};
use uucore::translate;
use uucore::{error::UResult, format_usage};

#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;

static OPT_NULL: &str = "null";

static ARG_VARIABLES: &str = "variables";
Expand All @@ -21,15 +26,34 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default();

let separator = if matches.get_flag(OPT_NULL) {
"\x00"
let separator: &[u8] = if matches.get_flag(OPT_NULL) {
b"\x00"
} else {
"\n"
b"\n"
};

#[cfg(unix)]
let mut stdout = io::stdout().lock();

if variables.is_empty() {
for (env_var, value) in env::vars() {
print!("{env_var}={value}{separator}");
for (env_var, value) in env::vars_os() {
#[cfg(unix)]
{
stdout.write_all(env_var.as_bytes())?;
stdout.write_all(b"=")?;
stdout.write_all(value.as_bytes())?;
stdout.write_all(separator)?;
}
#[cfg(not(unix))]
{
// On non-Unix, use lossy conversion as OsStrExt is not available
print!(
"{}={}{}",
env_var.to_string_lossy(),
value.to_string_lossy(),
String::from_utf8_lossy(separator)
);
}
}
return Ok(());
}
Expand All @@ -41,8 +65,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
error_found = true;
continue;
}
if let Ok(var) = env::var(env_var) {
print!("{var}{separator}");
if let Some(var) = env::var_os(&env_var) {
#[cfg(unix)]
{
stdout.write_all(var.as_bytes())?;
stdout.write_all(separator)?;
}
#[cfg(not(unix))]
{
print!(
"{}{}",
var.to_string_lossy(),
String::from_utf8_lossy(separator)
);
}
} else {
error_found = true;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_printenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
#[cfg(unix)]
use std::ffi::OsString;
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;
Comment on lines +5 to +8
Copy link
Contributor

@cakebaker cakebaker Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the imports to the test function, otherwise there are "unused import" warnings on Android and the BSDs.

use uutests::new_ucmd;

#[test]
Expand Down Expand Up @@ -90,3 +94,28 @@ fn test_null_separator() {
.stdout_is("FOO\x00VALUE\x00");
}
}

#[test]
#[cfg(unix)]
#[cfg(not(any(target_os = "freebsd", target_os = "android", target_os = "openbsd")))]
fn test_non_utf8_value() {
// Environment variable values can contain non-UTF-8 bytes on Unix.
// printenv should output them correctly, matching GNU behavior.
// Reproduces: LD_PRELOAD=$'/tmp/lib.so\xff' printenv LD_PRELOAD
let value_with_invalid_utf8 = OsString::from_vec(b"/tmp/lib.so\xff".to_vec());

let result = new_ucmd!()
.env("LD_PRELOAD", &value_with_invalid_utf8)
.arg("LD_PRELOAD")
.run();

// Use byte-based assertions to avoid UTF-8 conversion issues
// when the test framework tries to format error messages
assert!(
result.succeeded(),
"Command failed with exit code: {:?}, stderr: {:?}",
result.code(),
String::from_utf8_lossy(result.stderr())
);
result.stdout_is_bytes(b"/tmp/lib.so\xff\n");
}
Loading