diff --git a/src/header/stdio/constants.rs b/src/header/stdio/constants.rs index 0d4346d94074ad1fa03b4e1b51a705c00900bf32..4c3e32ae758ddb11cc135817e4dc4c615238a045 100644 --- a/src/header/stdio/constants.rs +++ b/src/header/stdio/constants.rs @@ -24,6 +24,8 @@ pub const _IOFBF: c_int = 0; pub const _IOLBF: c_int = 1; pub const _IONBF: c_int = 2; +// /dev/tty + nul +pub const L_ctermid: usize = 9; // form of name is /XXXXXX, so 7 pub const L_tmpnam: c_int = 7; // 36^6 (26 letters + 10 digits) is larger than i32::MAX, so just set to that diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 5416101ec13c216f2ebaddfb9dc9e5a24d96bdd5..878bc58bba88131e307abbaf947b5ad7333e7fd0 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -278,9 +278,15 @@ pub unsafe extern "C" fn clearerr(stream: *mut FILE) { stream.flags &= !(F_EOF | F_ERR); } -// #[no_mangle] -pub extern "C" fn ctermid(_s: *mut c_char) -> *mut c_char { - unimplemented!(); +#[no_mangle] +pub unsafe extern "C" fn ctermid(s: *mut c_char) -> *mut c_char { + static mut TERMID: [u8; L_ctermid] = *b"/dev/tty\0"; + + if s.is_null() { + return TERMID.as_mut_ptr() as *mut c_char; + } + + strncpy(s, TERMID.as_mut_ptr() as *mut c_char, L_ctermid) } // #[no_mangle] diff --git a/tests/Makefile b/tests/Makefile index 74cc43bc3c2ef6d19c877d5fd1026660d6c20e5d..d98fee8e43e5a6ae45de3a475f60c13718e0d264 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -161,6 +161,7 @@ NAMES=\ pwd \ sa_restart \ sigchld \ + stdio/ctermid \ stdio/tempnam \ stdio/tmpnam \ stdlib/bsearch \ diff --git a/tests/stdio/ctermid.c b/tests/stdio/ctermid.c new file mode 100644 index 0000000000000000000000000000000000000000..5a291cf607046a15d165c0c6204f9001964d79a6 --- /dev/null +++ b/tests/stdio/ctermid.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <string.h> + +#define DEVTTY "/dev/tty" + +int main(void) +{ + char *name = ctermid(NULL); + if(strcmp(name, DEVTTY) != 0) { + printf("ctermid name differs: expected %s, got: %s\n", DEVTTY, name); + return 1; + } + + char name2[L_ctermid]; + ctermid(name2); + if(strcmp(name, DEVTTY) != 0) { + printf("ctermid name2 differs: expected %s, got: %s\n", DEVTTY, name2); + return 1; + } + + return 0; +} \ No newline at end of file