1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 05:57:46 +00:00

fix whoami

This commit is contained in:
kwantam 2015-04-27 17:31:53 -04:00
parent 9d31fe83e6
commit 09937b66b9
4 changed files with 77 additions and 46 deletions

View 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;

View 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()
}

View 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()
}

View file

@ -1,5 +1,5 @@
#![crate_name = "whoami"]
#![feature(collections, core, old_io, rustc_private, std_misc)]
#![feature(rustc_private)]
/*
* This file is part of the uutils coreutils package.
@ -12,62 +12,22 @@
/* last synced with: whoami (GNU coreutils) 8.21 */
#![allow(non_camel_case_types)]
extern crate getopts;
extern crate libc;
use std::old_io::print;
use std::io::Write;
#[path = "../common/util.rs"] #[macro_use] mod util;
#[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()
}
}
mod platform;
static NAME: &'static str = "whoami";
pub fn uumain(args: Vec<String>) -> i32 {
let program = args[0].as_slice();
let opts = [
getopts::optflag("h", "help", "display this help 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,
Err(f) => crash!(1, "{}", f),
};
@ -75,9 +35,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
println!("whoami 1.0.0");
println!("");
println!("Usage:");
println!(" {}", program);
println!(" {}", args[0]);
println!("");
print(getopts::usage("print effective userid", &opts).as_slice());
println!("{}", getopts::usage("print effective userid", &opts));
return 0;
}
if matches.opt_present("version") {