@@ -8,124 +8,38 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88use std:: rc:: Rc ;
99pub unsafe fn test_stat_0 ( ) {
1010 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- } ;
11+ let mut fp: * mut :: libc:: FILE = libc:: fopen (
12+ path as * const i8 ,
13+ ( b"wb\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( ) as * const i8 ,
14+ ) ;
3815 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 )
16+ libc:: fputs (
17+ ( b"hello\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( ) as * const i8 ,
18+ fp,
5419 ) ;
20+ assert ! ( ( ( ( ( libc:: fclose( fp) ) == ( 0 ) ) as i32 ) != 0 ) ) ;
5521 let mut st: stat = std:: mem:: zeroed :: < stat > ( ) ;
5622 assert ! ( ( ( ( ( libc:: stat( path as * const i8 , ( & mut st as * mut stat) ) ) == ( 0 ) ) as i32 ) != 0 ) ) ;
5723 assert ! ( ( ( ( ( st. st_size) == ( 5_i64 ) ) as i32 ) != 0 ) ) ;
5824 libc:: unlink ( path as * const i8 ) ;
5925}
6026pub unsafe fn test_fstat_1 ( ) {
6127 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- } ;
28+ let mut fp: * mut :: libc:: FILE = libc:: fopen (
29+ path as * const i8 ,
30+ ( b"wb\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( ) as * const i8 ,
31+ ) ;
8932 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- } ;
33+ libc:: fputs (
34+ ( b"hello world\0 " . as_ptr ( ) . cast_mut ( ) ) . cast_const ( ) as * const i8 ,
35+ fp,
36+ ) ;
37+ libc:: fflush ( fp) ;
38+ let mut fd: i32 = libc:: fileno ( fp) ;
11939 let mut st: stat = std:: mem:: zeroed :: < stat > ( ) ;
12040 assert ! ( ( ( ( ( libc:: fstat( fd, ( & mut st as * mut stat) ) ) == ( 0 ) ) as i32 ) != 0 ) ) ;
12141 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- ) ;
42+ assert ! ( ( ( ( ( libc:: fclose( fp) ) == ( 0 ) ) as i32 ) != 0 ) ) ;
12943 libc:: unlink ( path as * const i8 ) ;
13044}
13145pub fn main ( ) {
0 commit comments