From 3f0aef146af8fcd489b4ec99facc0e10afcc9247 Mon Sep 17 00:00:00 2001 From: wangmengc Date: Wed, 12 Jun 2024 09:36:22 +0800 Subject: [PATCH] add open_buffered_stream free_buffered_stream function --- utshell-0.5/r_input/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/utshell-0.5/r_input/src/lib.rs b/utshell-0.5/r_input/src/lib.rs index 4141d750..e24a7cb0 100644 --- a/utshell-0.5/r_input/src/lib.rs +++ b/utshell-0.5/r_input/src/lib.rs @@ -640,4 +640,36 @@ pub unsafe extern "C" fn fd_to_buffered_stream(mut fd: libc::c_int) -> *mut BUFF buffer = libc::malloc(size as usize) as *mut libc::c_char; return make_buffered_stream(fd, buffer, size); +} + +/* Return a buffered stream corresponding to FILE, a file name. */ +#[no_mangle] +pub unsafe extern "C" fn open_buffered_stream(mut file: *mut libc::c_char) -> *mut BUFFERED_STREAM { + let mut fd: libc::c_int = 0; + + fd = open(file, O_RDONLY); + return if fd >= 0 as libc::c_int { + fd_to_buffered_stream(fd) + } else { + 0 as *mut libc::c_void as *mut BUFFERED_STREAM + }; +} + +/* Deallocate a buffered stream and free up its resources. Make sure we +zero out the slot in BUFFERS that points to BP. */ +#[no_mangle] +pub unsafe extern "C" fn free_buffered_stream(mut bp: *mut BUFFERED_STREAM) { + let mut n: libc::c_int = 0; + + if bp.is_null() { + return; + } + + n = (*bp).b_fd; + if !((*bp).b_buffer).is_null() { + libc::free((*bp).b_buffer as *mut libc::c_void); + } + libc::free(bp as *mut libc::c_void); + let ref mut fresh7 = *buffers.offset(n as isize); + *fresh7 = 0 as *mut libc::c_void as *mut BUFFERED_STREAM; } \ No newline at end of file -- Gitee