mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +00:00
fix whoami
This commit is contained in:
parent
9d31fe83e6
commit
09937b66b9
4 changed files with 77 additions and 46 deletions
20
src/whoami/platform/mod.rs
Normal file
20
src/whoami/platform/mod.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
* This file is part of the uutils coreutils package.
|
||||||
|
*
|
||||||
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
pub use self::unix::getusername;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub use self::windows::getusername;
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
mod unix;
|
||||||
|
|
||||||
|
#[cfg(windows)]
|
||||||
|
mod windows;
|
24
src/whoami/platform/unix.rs
Normal file
24
src/whoami/platform/unix.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
/*
|
||||||
|
* This file is part of the uutils coreutils package.
|
||||||
|
*
|
||||||
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use ::libc;
|
||||||
|
use self::c_types::{c_passwd, getpwuid};
|
||||||
|
|
||||||
|
#[path = "../../common/c_types.rs"] mod c_types;
|
||||||
|
|
||||||
|
extern {
|
||||||
|
pub fn geteuid() -> libc::uid_t;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn getusername() -> String {
|
||||||
|
let passwd: *const c_passwd = getpwuid(geteuid());
|
||||||
|
|
||||||
|
let pw_name: *const libc::c_char = (*passwd).pw_name;
|
||||||
|
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(pw_name).to_bytes()).to_string()
|
||||||
|
}
|
27
src/whoami/platform/windows.rs
Normal file
27
src/whoami/platform/windows.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* This file is part of the uutils coreutils package.
|
||||||
|
*
|
||||||
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use ::libc;
|
||||||
|
use std::mem;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
extern "system" {
|
||||||
|
pub fn GetUserNameA(out: *mut libc::c_char, len: *mut libc::uint32_t) -> libc::uint8_t;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_unsafe)]
|
||||||
|
pub unsafe fn getusername() -> String {
|
||||||
|
// XXX: it may be possible that this isn't long enough. I don't know
|
||||||
|
let mut buffer: [libc::c_char; 2048] = mem::uninitialized();
|
||||||
|
|
||||||
|
if !GetUserNameA(buffer.as_mut_ptr(), &mut (buffer.len() as libc::uint32_t)) == 0 {
|
||||||
|
crash!(1, "username is too long");
|
||||||
|
}
|
||||||
|
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(buffer.as_ptr()).to_bytes()).to_string()
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "whoami"]
|
#![crate_name = "whoami"]
|
||||||
#![feature(collections, core, old_io, rustc_private, std_misc)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -12,62 +12,22 @@
|
||||||
|
|
||||||
/* last synced with: whoami (GNU coreutils) 8.21 */
|
/* last synced with: whoami (GNU coreutils) 8.21 */
|
||||||
|
|
||||||
#![allow(non_camel_case_types)]
|
|
||||||
|
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::old_io::print;
|
use std::io::Write;
|
||||||
|
|
||||||
#[path = "../common/util.rs"] #[macro_use] mod util;
|
#[path = "../common/util.rs"] #[macro_use] mod util;
|
||||||
|
mod platform;
|
||||||
#[cfg(unix)]
|
|
||||||
mod platform {
|
|
||||||
use super::libc;
|
|
||||||
use self::c_types::{c_passwd, getpwuid};
|
|
||||||
|
|
||||||
#[path = "../../common/c_types.rs"] mod c_types;
|
|
||||||
|
|
||||||
extern {
|
|
||||||
pub fn geteuid() -> libc::uid_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub unsafe fn getusername() -> String {
|
|
||||||
let passwd: *const c_passwd = getpwuid(geteuid());
|
|
||||||
|
|
||||||
let pw_name: *const libc::c_char = (*passwd).pw_name;
|
|
||||||
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(pw_name).to_bytes()).to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
mod platform {
|
|
||||||
pub use super::libc;
|
|
||||||
use std::mem;
|
|
||||||
|
|
||||||
extern "system" {
|
|
||||||
pub fn GetUserNameA(out: *mut libc::c_char, len: *mut libc::uint32_t) -> libc::uint8_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused_unsafe)]
|
|
||||||
pub unsafe fn getusername() -> String {
|
|
||||||
let mut buffer: [libc::c_char; 2048] = mem::uninitialized(); // XXX: it may be possible that this isn't long enough. I don't know
|
|
||||||
if !GetUserNameA(buffer.as_mut_ptr(), &mut (buffer.len() as libc::uint32_t)) == 0 {
|
|
||||||
crash!(1, "username is too long");
|
|
||||||
}
|
|
||||||
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(buffer.as_ptr()).to_bytes()).to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static NAME: &'static str = "whoami";
|
static NAME: &'static str = "whoami";
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let program = args[0].as_slice();
|
|
||||||
let opts = [
|
let opts = [
|
||||||
getopts::optflag("h", "help", "display this help and exit"),
|
getopts::optflag("h", "help", "display this help and exit"),
|
||||||
getopts::optflag("V", "version", "output version information and exit"),
|
getopts::optflag("V", "version", "output version information and exit"),
|
||||||
];
|
];
|
||||||
let matches = match getopts::getopts(args.tail(), &opts) {
|
let matches = match getopts::getopts(&args[1..], &opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => crash!(1, "{}", f),
|
Err(f) => crash!(1, "{}", f),
|
||||||
};
|
};
|
||||||
|
@ -75,9 +35,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
println!("whoami 1.0.0");
|
println!("whoami 1.0.0");
|
||||||
println!("");
|
println!("");
|
||||||
println!("Usage:");
|
println!("Usage:");
|
||||||
println!(" {}", program);
|
println!(" {}", args[0]);
|
||||||
println!("");
|
println!("");
|
||||||
print(getopts::usage("print effective userid", &opts).as_slice());
|
println!("{}", getopts::usage("print effective userid", &opts));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if matches.opt_present("version") {
|
if matches.opt_present("version") {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue