Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
relibc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Peter Limkilde Svendsen
relibc
Commits
4c65f14f
Verified
Commit
4c65f14f
authored
6 years ago
by
Tom Almeida
Browse files
Options
Downloads
Patches
Plain Diff
Fixed some issues with temporary files and moved some raw pointers to Option<&T>s
parent
71fa4026
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/stdio/src/helpers.rs
+17
-11
17 additions, 11 deletions
src/stdio/src/helpers.rs
src/stdio/src/lib.rs
+22
-19
22 additions, 19 deletions
src/stdio/src/lib.rs
with
39 additions
and
30 deletions
src/stdio/src/helpers.rs
+
17
−
11
View file @
4c65f14f
...
...
@@ -36,8 +36,10 @@ pub unsafe fn parse_mode_flags(mode_str: *const c_char) -> i32 {
}
/// Open a file with the file descriptor `fd` in the mode `mode`
pub
unsafe
fn
_fdopen
(
fd
:
c_int
,
mode
:
*
const
c_char
)
->
Option
<
FILE
>
{
pub
unsafe
fn
_fdopen
(
fd
:
c_int
,
mode
:
*
const
c_char
)
->
Option
<
*
mut
FILE
>
{
use
string
::
strchr
;
use
stdlib
::
malloc
;
use
core
::
mem
::
size_of
;
if
*
mode
!=
b'r'
as
i8
&&
*
mode
!=
b'w'
as
i8
&&
*
mode
!=
b'a'
as
i8
{
platform
::
errno
=
errno
::
EINVAL
;
return
None
;
...
...
@@ -61,16 +63,20 @@ pub unsafe fn _fdopen(fd: c_int, mode: *const c_char) -> Option<FILE> {
}
// Allocate the file
Some
(
FILE
{
flags
:
flags
,
read
:
None
,
write
:
None
,
fd
:
fd
,
buf
:
vec!
[
0u8
;
BUFSIZ
+
UNGET
],
buf_char
:
-
1
,
unget
:
UNGET
,
lock
:
AtomicBool
::
new
(
false
),
})
let
f
=
malloc
(
size_of
::
<
FILE
>
())
as
*
mut
FILE
;
if
f
.is_null
()
{
None
}
else
{
(
*
f
)
.flags
=
flags
;
(
*
f
)
.read
=
None
;
(
*
f
)
.write
=
None
;
(
*
f
)
.fd
=
fd
;
(
*
f
)
.buf
=
vec!
[
0u8
;
BUFSIZ
+
UNGET
];
(
*
f
)
.buf_char
=
-
1
;
(
*
f
)
.unget
=
UNGET
;
(
*
f
)
.lock
=
AtomicBool
::
new
(
false
);
Some
(
f
)
}
}
/// Write buffer `buf` of length `l` into `stream`
...
...
This diff is collapsed.
Click to expand it.
src/stdio/src/lib.rs
+
22
−
19
View file @
4c65f14f
...
...
@@ -44,14 +44,14 @@ mod internal;
/// This struct gets exposed to the C API.
///
pub
struct
FILE
{
flags
:
i32
,
read
:
Option
<
(
usize
,
usize
)
>
,
write
:
Option
<
(
usize
,
usize
,
usize
)
>
,
fd
:
c_int
,
buf
:
Vec
<
u8
>
,
flags
:
i32
,
read
:
Option
<
(
usize
,
usize
)
>
,
write
:
Option
<
(
usize
,
usize
,
usize
)
>
,
fd
:
c_int
,
buf
:
Vec
<
u8
>
,
buf_char
:
i8
,
lock
:
AtomicBool
,
unget
:
usize
,
lock
:
AtomicBool
,
unget
:
usize
,
}
impl
FILE
{
...
...
@@ -221,6 +221,8 @@ pub extern "C" fn fclose(stream: &mut FILE) -> c_int {
unsafe
{
free
(
stream
as
*
mut
_
as
*
mut
_
);
}
}
else
{
funlockfile
(
stream
);
}
r
}
...
...
@@ -229,8 +231,8 @@ pub extern "C" fn fclose(stream: &mut FILE) -> c_int {
#[no_mangle]
pub
extern
"C"
fn
fdopen
(
fildes
:
c_int
,
mode
:
*
const
c_char
)
->
*
mut
FILE
{
use
core
::
ptr
;
if
let
Some
(
mut
f
)
=
unsafe
{
helpers
::
_fdopen
(
fildes
,
mode
)
}
{
&
mut
f
if
let
Some
(
f
)
=
unsafe
{
helpers
::
_fdopen
(
fildes
,
mode
)
}
{
f
}
else
{
ptr
::
null_mut
()
}
...
...
@@ -278,15 +280,17 @@ pub extern "C" fn fgetc(stream: &mut FILE) -> c_int {
/// Get the position of the stream and store it in pos
#[no_mangle]
pub
extern
"C"
fn
fgetpos
(
stream
:
&
mut
FILE
,
pos
:
*
mut
fpos_t
)
->
c_int
{
pub
extern
"C"
fn
fgetpos
(
stream
:
&
mut
FILE
,
pos
:
Option
<&
mut
fpos_t
>
)
->
c_int
{
let
off
=
internal
::
ftello
(
stream
);
if
off
<
0
{
return
-
1
;
}
unsafe
{
(
*
pos
)
=
off
;
if
let
Some
(
pos
)
=
pos
{
*
pos
=
off
;
0
}
else
{
-
1
}
0
}
/// Get a string from the stream
...
...
@@ -372,12 +376,11 @@ pub extern "C" fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FI
fcntl
::
sys_fcntl
(
fd
,
fcntl
::
F_SETFD
,
fcntl
::
FD_CLOEXEC
);
}
let
f
=
unsafe
{
helpers
::
_fdopen
(
fd
,
mode
)
};
if
let
Some
(
mut
fi
)
=
f
{
&
mut
fi
if
let
Some
(
f
)
=
unsafe
{
helpers
::
_fdopen
(
fd
,
mode
)
}
{
f
}
else
{
platform
::
close
(
fd
);
return
ptr
::
null_mut
()
;
ptr
::
null_mut
()
}
}
...
...
@@ -540,8 +543,8 @@ pub extern "C" fn fseeko(stream: &mut FILE, offset: off_t, whence: c_int) -> c_i
/// Seek to a position `pos` in the file from the beginning of the file
#[no_mangle]
pub
unsafe
extern
"C"
fn
fsetpos
(
stream
:
&
mut
FILE
,
pos
:
*
const
fpos_t
)
->
c_int
{
fseek
(
stream
,
*
pos
as
off_t
,
SEEK_SET
)
pub
unsafe
extern
"C"
fn
fsetpos
(
stream
:
&
mut
FILE
,
pos
:
Option
<&
fpos_t
>
)
->
c_int
{
fseek
(
stream
,
*
pos
.expect
(
"You must specify a valid position"
)
,
SEEK_SET
)
}
/// Get the current position of the cursor in the file
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment