|
| 1 | +extern crate libc; |
| 2 | +use libc::*; |
| 3 | +extern crate libcc2rs; |
| 4 | +use libcc2rs::*; |
| 5 | +use std::collections::BTreeMap; |
| 6 | +use std::io::{Read, Seek, Write}; |
| 7 | +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; |
| 8 | +use std::rc::Rc; |
| 9 | +pub unsafe fn test_stat_0() { |
| 10 | + let mut path: *const u8 = (b"/tmp/cpp2rust_stat_test.tmp\0".as_ptr().cast_mut()).cast_const(); |
| 11 | + let mut fp: *mut ::std::fs::File = |
| 12 | + match std::ffi::CStr::from_ptr((b"wb\0".as_ptr().cast_mut()).cast_const() as *const i8) |
| 13 | + .to_str() |
| 14 | + .expect("invalid c-string") |
| 15 | + { |
| 16 | + v if v == "rb" => std::fs::OpenOptions::new() |
| 17 | + .read(true) |
| 18 | + .open( |
| 19 | + std::ffi::CStr::from_ptr(path as *const i8) |
| 20 | + .to_str() |
| 21 | + .expect("invalid c-string"), |
| 22 | + ) |
| 23 | + .ok() |
| 24 | + .map_or(std::ptr::null_mut(), |f| Box::into_raw(Box::new(f))), |
| 25 | + v if v == "wb" => std::fs::OpenOptions::new() |
| 26 | + .write(true) |
| 27 | + .create(true) |
| 28 | + .truncate(true) |
| 29 | + .open( |
| 30 | + std::ffi::CStr::from_ptr(path as *const i8) |
| 31 | + .to_str() |
| 32 | + .expect("invalid c-string"), |
| 33 | + ) |
| 34 | + .ok() |
| 35 | + .map_or(std::ptr::null_mut(), |f| Box::into_raw(Box::new(f))), |
| 36 | + _ => panic!("unsupported mode"), |
| 37 | + }; |
| 38 | + assert!((((!((fp).is_null())) as i32) != 0)); |
| 39 | + { |
| 40 | + let bytes = |
| 41 | + std::ffi::CStr::from_ptr((b"hello\0".as_ptr().cast_mut()).cast_const() as *const i8) |
| 42 | + .to_bytes(); |
| 43 | + match (*fp).write_all(bytes) { |
| 44 | + Ok(()) => 0, |
| 45 | + Err(_) => -1, |
| 46 | + } |
| 47 | + }; |
| 48 | + assert!( |
| 49 | + (((({ |
| 50 | + Box::from_raw(fp); |
| 51 | + 0 |
| 52 | + }) == (0)) as i32) |
| 53 | + != 0) |
| 54 | + ); |
| 55 | + let mut st: stat = std::mem::zeroed::<stat>(); |
| 56 | + assert!(((((libc::stat(path as *const i8, (&mut st as *mut stat))) == (0)) as i32) != 0)); |
| 57 | + assert!(((((st.st_size) == (5_i64)) as i32) != 0)); |
| 58 | + libc::unlink(path as *const i8); |
| 59 | +} |
| 60 | +pub unsafe fn test_fstat_1() { |
| 61 | + let mut path: *const u8 = (b"/tmp/cpp2rust_fstat_test.tmp\0".as_ptr().cast_mut()).cast_const(); |
| 62 | + let mut fp: *mut ::std::fs::File = |
| 63 | + match std::ffi::CStr::from_ptr((b"wb\0".as_ptr().cast_mut()).cast_const() as *const i8) |
| 64 | + .to_str() |
| 65 | + .expect("invalid c-string") |
| 66 | + { |
| 67 | + v if v == "rb" => std::fs::OpenOptions::new() |
| 68 | + .read(true) |
| 69 | + .open( |
| 70 | + std::ffi::CStr::from_ptr(path as *const i8) |
| 71 | + .to_str() |
| 72 | + .expect("invalid c-string"), |
| 73 | + ) |
| 74 | + .ok() |
| 75 | + .map_or(std::ptr::null_mut(), |f| Box::into_raw(Box::new(f))), |
| 76 | + v if v == "wb" => std::fs::OpenOptions::new() |
| 77 | + .write(true) |
| 78 | + .create(true) |
| 79 | + .truncate(true) |
| 80 | + .open( |
| 81 | + std::ffi::CStr::from_ptr(path as *const i8) |
| 82 | + .to_str() |
| 83 | + .expect("invalid c-string"), |
| 84 | + ) |
| 85 | + .ok() |
| 86 | + .map_or(std::ptr::null_mut(), |f| Box::into_raw(Box::new(f))), |
| 87 | + _ => panic!("unsupported mode"), |
| 88 | + }; |
| 89 | + assert!((((!((fp).is_null())) as i32) != 0)); |
| 90 | + { |
| 91 | + let bytes = std::ffi::CStr::from_ptr( |
| 92 | + (b"hello world\0".as_ptr().cast_mut()).cast_const() as *const i8 |
| 93 | + ) |
| 94 | + .to_bytes(); |
| 95 | + match (*fp).write_all(bytes) { |
| 96 | + Ok(()) => 0, |
| 97 | + Err(_) => -1, |
| 98 | + } |
| 99 | + }; |
| 100 | + if !(fp).is_null() { |
| 101 | + match (*fp).sync_all() { |
| 102 | + Ok(_) => 0, |
| 103 | + Err(_) => -1, |
| 104 | + } |
| 105 | + } else { |
| 106 | + ::std::io::stdout().flush().unwrap(); |
| 107 | + ::std::io::stderr().flush().unwrap(); |
| 108 | + 0 |
| 109 | + }; |
| 110 | + let mut fd: i32 = if fp == libcc2rs::cin_unsafe() { |
| 111 | + 0 |
| 112 | + } else if fp == libcc2rs::cout_unsafe() { |
| 113 | + 1 |
| 114 | + } else if fp == libcc2rs::cerr_unsafe() { |
| 115 | + 2 |
| 116 | + } else { |
| 117 | + ::std::os::fd::AsRawFd::as_raw_fd(&*fp) |
| 118 | + }; |
| 119 | + let mut st: stat = std::mem::zeroed::<stat>(); |
| 120 | + assert!(((((libc::fstat(fd, (&mut st as *mut stat))) == (0)) as i32) != 0)); |
| 121 | + assert!(((((st.st_size) == (11_i64)) as i32) != 0)); |
| 122 | + assert!( |
| 123 | + (((({ |
| 124 | + Box::from_raw(fp); |
| 125 | + 0 |
| 126 | + }) == (0)) as i32) |
| 127 | + != 0) |
| 128 | + ); |
| 129 | + libc::unlink(path as *const i8); |
| 130 | +} |
| 131 | +pub fn main() { |
| 132 | + unsafe { |
| 133 | + std::process::exit(main_0() as i32); |
| 134 | + } |
| 135 | +} |
| 136 | +unsafe fn main_0() -> i32 { |
| 137 | + (unsafe { test_stat_0() }); |
| 138 | + (unsafe { test_fstat_1() }); |
| 139 | + return 0; |
| 140 | +} |
0 commit comments