Add %m to printf & fix CI
%m is a format specifier that prints an error string for errno. The specifier is technically only for syslog, but musl and glibc implement it for printf itself. Parsing for a single specifier in a single function is error prone, especially when syslog itself is variadic.
This commit is contained in:
@@ -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;
|
||||
pub const O_NDELAY: c_int = O_NONBLOCK;
|
||||
|
||||
@@ -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;
|
||||
pub const O_NDELAY: c_int = O_NONBLOCK;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> // free()
|
||||
#include <math.h> // 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user