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
545e9e7b
Commit
545e9e7b
authored
8 months ago
by
Agoston Szepessy
Browse files
Options
Downloads
Patches
Plain Diff
Implement getpass using File API
Returns a Result for better error handling.
parent
a83d4cbc
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/header/unistd/getpass.rs
+81
-0
81 additions, 0 deletions
src/header/unistd/getpass.rs
src/header/unistd/mod.rs
+4
-51
4 additions, 51 deletions
src/header/unistd/mod.rs
with
85 additions
and
51 deletions
src/header/unistd/getpass.rs
0 → 100644
+
81
−
0
View file @
545e9e7b
use
core
::
ptr
;
use
alloc
::
vec
::
Vec
;
use
crate
::{
fs
::
File
,
header
::{
fcntl
::{
O_CLOEXEC
,
O_RDWR
},
limits
::
PASS_MAX
,
stdio
,
termios
,
},
io
::{
self
,
Read
},
};
use
crate
::
platform
::
types
::
*
;
#[derive(Debug)]
enum
Error
{
Io
(
io
::
Error
),
CannotConvertFd
,
}
impl
From
<
io
::
Error
>
for
Error
{
fn
from
(
value
:
io
::
Error
)
->
Self
{
Error
::
Io
(
value
)
}
}
unsafe
fn
getpass_rs
(
prompt
:
*
const
c_char
)
->
Result
<*
mut
c_char
,
Error
>
{
let
mut
f
=
File
::
open
(
c_str!
(
"/dev/tty"
),
O_RDWR
|
O_CLOEXEC
)
?
;
let
mut
term
=
termios
::
termios
::
default
();
termios
::
tcgetattr
(
f
.fd
,
&
mut
term
as
*
mut
termios
::
termios
);
let
old_term
=
term
.clone
();
term
.c_iflag
&=
!
(
termios
::
IGNCR
|
termios
::
INLCR
)
as
u32
;
term
.c_iflag
|
=
termios
::
ICRNL
as
u32
;
term
.c_lflag
&=
!
(
termios
::
ECHO
|
termios
::
ISIG
)
as
u32
;
term
.c_lflag
|
=
termios
::
ICANON
as
u32
;
let
cfile
=
stdio
::
fdopen
(
f
.fd
,
c_str!
(
"w+e"
)
.as_ptr
());
if
cfile
.is_null
()
{
return
Err
(
Error
::
CannotConvertFd
);
}
termios
::
tcsetattr
(
f
.fd
,
termios
::
TCSAFLUSH
,
&
term
as
*
const
termios
::
termios
);
stdio
::
fputs
(
prompt
,
cfile
);
stdio
::
fflush
(
cfile
);
let
mut
buff
=
Vec
::
new
();
let
mut
len
=
f
.read
(
&
mut
buff
)
?
;
static
mut
PASSBUFF
:
[
c_char
;
PASS_MAX
]
=
[
0
;
PASS_MAX
];
for
(
dst
,
src
)
in
PASSBUFF
.iter_mut
()
.zip
(
&
buff
)
{
*
dst
=
*
src
as
c_char
;
}
if
len
>
0
{
if
PASSBUFF
[
len
-
1
]
==
b'\n'
as
c_char
||
PASSBUFF
.len
()
==
len
{
len
-=
1
;
}
}
PASSBUFF
[
len
]
=
0
;
termios
::
tcsetattr
(
f
.fd
,
termios
::
TCSAFLUSH
,
&
old_term
as
*
const
termios
::
termios
,
);
stdio
::
fputs
(
c_str!
(
"
\n
"
)
.as_ptr
(),
cfile
);
Ok
(
PASSBUFF
.as_mut_ptr
())
}
#[no_mangle]
pub
extern
"C"
fn
getpass
(
prompt
:
*
const
c_char
)
->
*
mut
c_char
{
unsafe
{
getpass_rs
(
prompt
)
.unwrap_or
(
ptr
::
null_mut
())
}
}
This diff is collapsed.
Click to expand it.
src/header/unistd/mod.rs
+
4
−
51
View file @
545e9e7b
...
@@ -11,9 +11,7 @@ use crate::{
...
@@ -11,9 +11,7 @@ use crate::{
c_str
::
CStr
,
c_str
::
CStr
,
header
::{
header
::{
crypt
::{
crypt_data
,
crypt_r
},
crypt
::{
crypt_data
,
crypt_r
},
errno
,
fcntl
,
errno
,
fcntl
,
limits
,
limits
::{
self
,
PASS_MAX
},
stdio
,
stdlib
::
getenv
,
stdlib
::
getenv
,
sys_ioctl
,
sys_resource
,
sys_time
,
sys_utsname
,
termios
,
sys_ioctl
,
sys_resource
,
sys_time
,
sys_utsname
,
termios
,
time
::
timespec
,
time
::
timespec
,
...
@@ -21,14 +19,16 @@ use crate::{
...
@@ -21,14 +19,16 @@ use crate::{
platform
::{
self
,
types
::
*
,
Pal
,
Sys
},
platform
::{
self
,
types
::
*
,
Pal
,
Sys
},
pthread
::
ResultExt
,
pthread
::
ResultExt
,
};
};
use
alloc
::
collections
::
LinkedList
;
use
alloc
::
collections
::
LinkedList
;
pub
use
self
::{
brk
::
*
,
getopt
::
*
,
pathconf
::
*
,
sysconf
::
*
};
pub
use
self
::{
brk
::
*
,
getopt
::
*
,
getpass
::
getpass
,
pathconf
::
*
,
sysconf
::
*
};
use
super
::
errno
::{
E2BIG
,
ENOMEM
};
use
super
::
errno
::{
E2BIG
,
ENOMEM
};
mod
brk
;
mod
brk
;
mod
getopt
;
mod
getopt
;
mod
getpass
;
mod
pathconf
;
mod
pathconf
;
mod
sysconf
;
mod
sysconf
;
...
@@ -452,53 +452,6 @@ pub extern "C" fn getpagesize() -> c_int {
...
@@ -452,53 +452,6 @@ pub extern "C" fn getpagesize() -> c_int {
.expect
(
"page size not representable as type `int`"
)
.expect
(
"page size not representable as type `int`"
)
}
}
#[no_mangle]
pub
unsafe
extern
"C"
fn
getpass
(
prompt
:
*
const
c_char
)
->
*
mut
c_char
{
let
tty
=
stdio
::
fopen
(
c_str!
(
"/dev/tty"
)
.as_ptr
(),
c_str!
(
"w+e"
)
.as_ptr
());
if
tty
.is_null
()
{
return
ptr
::
null_mut
();
}
let
fd
=
stdio
::
fileno
(
tty
);
let
mut
term
=
termios
::
termios
::
default
();
termios
::
tcgetattr
(
fd
,
&
mut
term
as
*
mut
termios
::
termios
);
let
old_temr
=
term
.clone
();
term
.c_iflag
&=
!
(
termios
::
IGNCR
|
termios
::
INLCR
)
as
u32
;
term
.c_iflag
|
=
termios
::
ICRNL
as
u32
;
term
.c_lflag
&=
!
(
termios
::
ECHO
|
termios
::
ISIG
)
as
u32
;
term
.c_lflag
|
=
termios
::
ICANON
as
u32
;
termios
::
tcsetattr
(
fd
,
termios
::
TCSAFLUSH
,
&
term
as
*
const
termios
::
termios
);
stdio
::
fputs
(
prompt
,
tty
);
stdio
::
fflush
(
tty
);
static
mut
PASSBUFF
:
[
c_char
;
PASS_MAX
]
=
[
0
;
PASS_MAX
];
let
len
=
read
(
fd
,
PASSBUFF
.as_mut_ptr
()
as
*
const
c_void
,
PASSBUFF
.len
());
if
len
>=
0
{
let
mut
l
=
len
as
usize
;
if
PASSBUFF
[
l
-
1
]
==
b'\n'
as
c_char
||
PASSBUFF
.len
()
==
l
{
l
-=
1
;
}
PASSBUFF
[
l
]
=
0
;
}
termios
::
tcsetattr
(
fd
,
termios
::
TCSAFLUSH
,
&
old_temr
as
*
const
termios
::
termios
);
stdio
::
fputs
(
c_str!
(
"
\n
"
)
.as_ptr
(),
tty
);
stdio
::
fclose
(
tty
);
if
len
<
0
{
return
ptr
::
null_mut
();
}
PASSBUFF
.as_mut_ptr
()
}
#[no_mangle]
#[no_mangle]
pub
extern
"C"
fn
getpgid
(
pid
:
pid_t
)
->
pid_t
{
pub
extern
"C"
fn
getpgid
(
pid
:
pid_t
)
->
pid_t
{
Sys
::
getpgid
(
pid
)
Sys
::
getpgid
(
pid
)
...
...
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