mirror of
https://github.com/uutils/coreutils.git
synced 2025-12-23 08:47:37 +00:00
Fix printenv non-UTF8 (#9728)
* printenv: Handle invalid UTF-8 encoding in variables * test(printenv): Add test for non-UTF8 content in variable
This commit is contained in:
parent
b9b965555c
commit
0bfbbc00c7
2 changed files with 46 additions and 12 deletions
|
|
@ -3,10 +3,14 @@
|
|||
// For the full copyright and license information, please view the LICENSE
|
||||
// file that was distributed with this source code.
|
||||
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
use std::env;
|
||||
use uucore::translate;
|
||||
use uucore::{error::UResult, format_usage};
|
||||
use std::io::Write;
|
||||
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
|
||||
use uucore::error::UResult;
|
||||
use uucore::line_ending::LineEnding;
|
||||
use uucore::{format_usage, os_str_as_bytes, translate};
|
||||
|
||||
static OPT_NULL: &str = "null";
|
||||
|
||||
|
|
@ -21,15 +25,16 @@ 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"
|
||||
} else {
|
||||
"\n"
|
||||
};
|
||||
let separator = LineEnding::from_zero_flag(matches.get_flag(OPT_NULL));
|
||||
|
||||
if variables.is_empty() {
|
||||
for (env_var, value) in env::vars() {
|
||||
print!("{env_var}={value}{separator}");
|
||||
for (env_var, value) in env::vars_os() {
|
||||
let env_bytes = os_str_as_bytes(&env_var)?;
|
||||
let val_bytes = os_str_as_bytes(&value)?;
|
||||
std::io::stdout().lock().write_all(env_bytes)?;
|
||||
print!("=");
|
||||
std::io::stdout().lock().write_all(val_bytes)?;
|
||||
print!("{separator}");
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
|
@ -41,8 +46,10 @@ 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) {
|
||||
let val_bytes = os_str_as_bytes(&var)?;
|
||||
std::io::stdout().lock().write_all(val_bytes)?;
|
||||
print!("{separator}");
|
||||
} else {
|
||||
error_found = true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue