diff --git a/include/bits/stdio.h b/include/bits/stdio.h index 0af8e6acdb47f04d8d629341d32d4409336c70af..428cded594f09c14353ee36e8db6b222879e91a2 100644 --- a/include/bits/stdio.h +++ b/include/bits/stdio.h @@ -9,6 +9,7 @@ typedef struct FILE FILE; extern "C" { #endif +int asprintf(char **strp, const char * fmt, ...); int fprintf(FILE * stream, const char * fmt, ...); int printf(const char * fmt, ...); int snprintf(char *s, size_t n, const char * fmt, ...); diff --git a/include/math.h b/include/math.h index 82d66d596628df06b0de6e4df08849add2ec21f9..a0cd1d106fa3ac1e428d61f30866b3f6b8b3e688 100644 --- a/include/math.h +++ b/include/math.h @@ -1 +1,5 @@ #include <openlibm_math.h> + +// Included to fix mesa issues +#define M_PI_2 (M_PI/2.0) +#define M_PI_4 (M_PI/4.0) diff --git a/src/c/stdio.c b/src/c/stdio.c index a3afa7b2e52e6f7c388da7e4e8f5a6c9b19b4542..fe4ab46b4b97703f5091bf82bebf10dfc62c9235 100644 --- a/src/c/stdio.c +++ b/src/c/stdio.c @@ -3,6 +3,17 @@ typedef struct FILE FILE; +int vasprintf(char ** strp, const char * fmt, va_list ap); + +int asprintf(char ** strp, const char * fmt, ...) { + int ret; + va_list ap; + va_start(ap, fmt); + ret = vasprintf(strp, fmt, ap); + va_end(ap); + return ret; +} + int vfprintf(FILE * stream, const char * fmt, va_list ap); int fprintf(FILE * stream, const char * fmt, ...) { diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 8ed46f147bb1e7aeba5f85ab412561a9561e9beb..e313849e052fadc2097e30f1315373a2b7c6335f 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -920,6 +920,14 @@ pub unsafe extern "C" fn vprintf(format: *const c_char, ap: va_list) -> c_int { vfprintf(&mut *stdout, format, ap) } +#[no_mangle] +pub unsafe extern "C" fn vasprintf(strp: *mut *mut c_char, format: *const c_char, ap: va_list) -> c_int { + let mut alloc_writer = platform::AllocStringWriter(ptr::null_mut(), 0); + let ret = printf::printf(&mut alloc_writer, format, ap); + *strp = alloc_writer.0 as *mut c_char; + ret +} + #[no_mangle] pub unsafe extern "C" fn vsnprintf( s: *mut c_char, diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 238e385007bc98a04f0a8834a3f20495e722d397..d94515a7da2e1e6425ca22d36bf790c19e0598fc 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -99,6 +99,47 @@ impl Read for FileReader { } } +pub struct AllocStringWriter(pub *mut u8, pub usize); +impl Write for AllocStringWriter { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let ptr = unsafe { + realloc(self.0 as *mut c_void, self.1 + buf.len() + 1) as *mut u8 + }; + if ptr.is_null() { + return Err(io::Error::new( + io::ErrorKind::Other, + "AllocStringWriter::write failed to allocate" + )); + } + self.0 = ptr; + + unsafe { + ptr::copy_nonoverlapping(buf.as_ptr(), self.0.add(self.1), buf.len()); + self.1 += buf.len(); + *self.0.add(self.1) = 0; + } + + Ok(buf.len()) + } + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} +impl fmt::Write for AllocStringWriter { + fn write_str(&mut self, s: &str) -> fmt::Result { + // can't fail + self.write(s.as_bytes()).unwrap(); + Ok(()) + } +} +impl WriteByte for AllocStringWriter { + fn write_u8(&mut self, byte: u8) -> fmt::Result { + // can't fail + self.write(&[byte]).unwrap(); + Ok(()) + } +} + pub struct StringWriter(pub *mut u8, pub usize); impl Write for StringWriter { fn write(&mut self, buf: &[u8]) -> io::Result<usize> {