diff --git a/src/header/fcntl/linux.rs b/src/header/fcntl/linux.rs index b1e3d42aff..39c9233293 100644 --- a/src/header/fcntl/linux.rs +++ b/src/header/fcntl/linux.rs @@ -17,4 +17,4 @@ pub const O_PATH: c_int = 0x20_0000; pub const FD_CLOEXEC: c_int = 0x8_0000; // Defined for compatibility -pub const O_NDELAY: c_int = O_NONBLOCK; \ No newline at end of file +pub const O_NDELAY: c_int = O_NONBLOCK; diff --git a/src/header/fcntl/redox.rs b/src/header/fcntl/redox.rs index ba429af995..ac15f21005 100644 --- a/src/header/fcntl/redox.rs +++ b/src/header/fcntl/redox.rs @@ -26,4 +26,4 @@ pub const FD_CLOEXEC: c_int = 0x0100_0000; pub const O_NOCTTY: c_int = 0x00000200; // Defined for compatibility -pub const O_NDELAY: c_int = O_NONBLOCK; \ No newline at end of file +pub const O_NDELAY: c_int = O_NONBLOCK; diff --git a/src/header/stdio/printf.rs b/src/header/stdio/printf.rs index 6faf477507..d4224fc9d5 100644 --- a/src/header/stdio/printf.rs +++ b/src/header/stdio/printf.rs @@ -7,7 +7,7 @@ use alloc::{ use core::{cmp, ffi::VaList, fmt, num::FpCategory, ops::Range, slice}; use crate::{ - header::errno::EILSEQ, + header::errno::{self, EILSEQ}, platform::{self, types::*}, }; @@ -588,6 +588,18 @@ impl Iterator for PrintfIter { b'c' => FmtKind::Char, b'p' => FmtKind::Pointer, b'n' => FmtKind::GetWritten, + b'm' => { + // %m is technically for syslog only, but musl and glibc implement it for + // printf because it is difficult and error prone to implement a format + // specifier for just *one* function. + self.format = self.format.add(1); + return Some(Ok(PrintfFmt::Plain( + errno::STR_ERROR + .get(platform::ERRNO.get() as usize) + .map(|e| e.as_bytes()) + .unwrap_or(b"unknown error"), + ))); + } _ => return Some(Err(())), }; self.format = self.format.add(1); diff --git a/tests/expected/bins_dynamic/stdio/printf.stdout b/tests/expected/bins_dynamic/stdio/printf.stdout index 8e28c396b1..aaa9ee635e 100644 --- a/tests/expected/bins_dynamic/stdio/printf.stdout +++ b/tests/expected/bins_dynamic/stdio/printf.stdout @@ -67,6 +67,8 @@ printed: test string, value: 11 printed: test string 2, value: 13 printed: test string 2, value: 13 +%m: Owner died + C23: Binary %b: 100 Binary %b alternate: 0b100 diff --git a/tests/expected/bins_static/stdio/printf.stdout b/tests/expected/bins_static/stdio/printf.stdout index 8e28c396b1..aaa9ee635e 100644 --- a/tests/expected/bins_static/stdio/printf.stdout +++ b/tests/expected/bins_static/stdio/printf.stdout @@ -67,6 +67,8 @@ printed: test string, value: 11 printed: test string 2, value: 13 printed: test string 2, value: 13 +%m: Owner died + C23: Binary %b: 100 Binary %b alternate: 0b100 diff --git a/tests/stdio/printf.c b/tests/stdio/printf.c index fb0c1478b7..197ad254b9 100644 --- a/tests/stdio/printf.c +++ b/tests/stdio/printf.c @@ -1,3 +1,4 @@ +#include #include #include // free() #include // INFINITY, NAN constants @@ -91,9 +92,14 @@ int main(void) { printf("printed: %s, value: %d\n", s, res); free(s); + errno = EOWNERDEAD; + printf("\n%%m: %m\n"); + puts("\nC23:"); printf("Binary %%b: %b\n", 4); printf("Binary %%b alternate: %#b\n", 4); printf("Binary %%B: %B\n", 4); printf("Binary %%B alternate: %#B\n", 4); + + return EXIT_SUCCESS; }