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
64b2970c
Commit
64b2970c
authored
7 years ago
by
Jeremy Soller
Browse files
Options
Downloads
Patches
Plain Diff
make it possible to printf to any fmt::Write implementer
parent
5520526b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
stdio/src/lib.rs
+7
-97
7 additions, 97 deletions
stdio/src/lib.rs
stdio/src/printf.rs
+101
-0
101 additions, 0 deletions
stdio/src/printf.rs
tests/alloc.c
+2
-4
2 additions, 4 deletions
tests/alloc.c
with
110 additions
and
101 deletions
stdio/src/lib.rs
+
7
−
97
View file @
64b2970c
...
...
@@ -8,113 +8,23 @@ extern crate va_list as vl;
use
platform
::
types
::
*
;
use
vl
::
VaList
as
va_list
;
mod
printf
;
pub
const
BUFSIZ
:
c_int
=
4096
;
pub
const
FILENAME_MAX
:
c_int
=
4096
;
pub
struct
FILE
;
#[no_mangle]
pub
static
mut
stdout
:
*
mut
FILE
=
1
as
*
mut
FILE
;
pub
static
mut
stderr
:
*
mut
FILE
=
2
as
*
mut
FILE
;
#[no_mangle]
pub
unsafe
extern
"C"
fn
vfprintf
(
file
:
*
mut
FILE
,
format
:
*
const
c_char
,
mut
ap
:
va_list
)
->
c_int
{
use
core
::
fmt
::
Write
;
use
core
::
slice
;
use
core
::
str
;
extern
"C"
{
fn
strlen
(
s
:
*
const
c_char
)
->
size_t
;
}
let
mut
w
=
platform
::
FileWriter
(
file
as
c_int
);
let
format
=
slice
::
from_raw_parts
(
format
as
*
const
u8
,
strlen
(
format
));
let
mut
i
=
0
;
let
mut
found_percent
=
false
;
while
i
<
format
.len
()
{
let
b
=
format
[
i
];
if
found_percent
{
match
b
as
char
{
'%'
=>
{
w
.write_char
(
'%'
);
found_percent
=
false
;
},
'c'
=>
{
let
a
=
ap
.get
::
<
u32
>
();
w
.write_char
(
a
as
u8
as
char
);
found_percent
=
false
;
},
'd'
|
'i'
=>
{
let
a
=
ap
.get
::
<
c_int
>
();
w
.write_fmt
(
format_args!
(
"{}"
,
a
));
found_percent
=
false
;
},
'n'
=>
{
let
_a
=
ap
.get
::
<
c_int
>
();
found_percent
=
false
;
},
'p'
=>
{
let
a
=
ap
.get
::
<
usize
>
();
w
.write_fmt
(
format_args!
(
"0x{:x}"
,
a
));
found_percent
=
false
;
},
's'
=>
{
let
a
=
ap
.get
::
<
usize
>
();
w
.write_str
(
str
::
from_utf8_unchecked
(
slice
::
from_raw_parts
(
a
as
*
const
u8
,
strlen
(
a
as
*
const
c_char
))
));
found_percent
=
false
;
},
'u'
=>
{
let
a
=
ap
.get
::
<
c_uint
>
();
w
.write_fmt
(
format_args!
(
"{}"
,
a
));
found_percent
=
false
;
},
'x'
=>
{
let
a
=
ap
.get
::
<
c_uint
>
();
w
.write_fmt
(
format_args!
(
"{:x}"
,
a
));
found_percent
=
false
;
},
'X'
=>
{
let
a
=
ap
.get
::
<
c_uint
>
();
w
.write_fmt
(
format_args!
(
"{:X}"
,
a
));
found_percent
=
false
;
},
'-'
=>
{},
'+'
=>
{},
' '
=>
{},
'#'
=>
{},
'0'
...
'9'
=>
{},
_
=>
{}
}
}
else
if
b
==
b'%'
{
found_percent
=
true
;
}
else
{
w
.write_char
(
b
as
char
);
}
i
+=
1
;
}
pub
static
mut
stderr
:
*
mut
FILE
=
2
as
*
mut
FILE
;
0
#[no_mangle]
pub
unsafe
extern
"C"
fn
vfprintf
(
file
:
*
mut
FILE
,
format
:
*
const
c_char
,
ap
:
va_list
)
->
c_int
{
printf
::
printf
(
platform
::
FileWriter
(
file
as
c_int
),
format
,
ap
)
}
#[no_mangle]
...
...
This diff is collapsed.
Click to expand it.
stdio/src/printf.rs
0 → 100644
+
101
−
0
View file @
64b2970c
use
core
::
fmt
;
use
platform
::
types
::
*
;
use
vl
::
VaList
;
pub
unsafe
fn
printf
<
W
:
fmt
::
Write
>
(
mut
w
:
W
,
format
:
*
const
c_char
,
mut
ap
:
VaList
)
->
c_int
{
use
core
::
fmt
::
Write
;
use
core
::
slice
;
use
core
::
str
;
extern
"C"
{
fn
strlen
(
s
:
*
const
c_char
)
->
size_t
;
}
let
format
=
slice
::
from_raw_parts
(
format
as
*
const
u8
,
strlen
(
format
));
let
mut
i
=
0
;
let
mut
found_percent
=
false
;
while
i
<
format
.len
()
{
let
b
=
format
[
i
];
if
found_percent
{
match
b
as
char
{
'%'
=>
{
w
.write_char
(
'%'
);
found_percent
=
false
;
},
'c'
=>
{
let
a
=
ap
.get
::
<
u32
>
();
w
.write_char
(
a
as
u8
as
char
);
found_percent
=
false
;
},
'd'
|
'i'
=>
{
let
a
=
ap
.get
::
<
c_int
>
();
w
.write_fmt
(
format_args!
(
"{}"
,
a
));
found_percent
=
false
;
},
'n'
=>
{
let
_a
=
ap
.get
::
<
c_int
>
();
found_percent
=
false
;
},
'p'
=>
{
let
a
=
ap
.get
::
<
usize
>
();
w
.write_fmt
(
format_args!
(
"0x{:x}"
,
a
));
found_percent
=
false
;
},
's'
=>
{
let
a
=
ap
.get
::
<
usize
>
();
w
.write_str
(
str
::
from_utf8_unchecked
(
slice
::
from_raw_parts
(
a
as
*
const
u8
,
strlen
(
a
as
*
const
c_char
))
));
found_percent
=
false
;
},
'u'
=>
{
let
a
=
ap
.get
::
<
c_uint
>
();
w
.write_fmt
(
format_args!
(
"{}"
,
a
));
found_percent
=
false
;
},
'x'
=>
{
let
a
=
ap
.get
::
<
c_uint
>
();
w
.write_fmt
(
format_args!
(
"{:x}"
,
a
));
found_percent
=
false
;
},
'X'
=>
{
let
a
=
ap
.get
::
<
c_uint
>
();
w
.write_fmt
(
format_args!
(
"{:X}"
,
a
));
found_percent
=
false
;
},
'-'
=>
{},
'+'
=>
{},
' '
=>
{},
'#'
=>
{},
'0'
...
'9'
=>
{},
_
=>
{}
}
}
else
if
b
==
b'%'
{
found_percent
=
true
;
}
else
{
w
.write_char
(
b
as
char
);
}
i
+=
1
;
}
0
}
This diff is collapsed.
Click to expand it.
tests/alloc.c
+
2
−
4
View file @
64b2970c
#include
<stdio.h>
#include
<stdlib.h>
#include
<unistd.h>
int
main
(
int
argc
,
char
**
argv
)
{
write
(
STDERR_FILENO
,
"malloc
\n
"
,
7
);
char
*
ptr
=
(
char
*
)
malloc
(
256
);
w
ri
te
(
STDERR_FILENO
,
"set
\n
"
,
4
);
p
ri
ntf
(
"malloc %p
\n
"
,
ptr
);
int
i
;
for
(
i
=
0
;
i
<
256
;
i
++
)
{
ptr
[
i
]
=
(
char
)
i
;
}
write
(
STDERR_FILENO
,
"free
\n
"
,
5
);
free
(
ptr
);
}
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