mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
Merge pull request #459 from Arcterus/deprecation
Fix most deprecation warnings and remove hacky nohup code
This commit is contained in:
commit
60fc8e9dcc
14 changed files with 86 additions and 105 deletions
|
@ -106,7 +106,7 @@ fn decode(input: &mut Reader, ignore_garbage: bool) {
|
|||
to_decode.as_slice()
|
||||
.trim_chars(|c: char| {
|
||||
let num = match c.to_ascii_opt() {
|
||||
Some(ascii) => ascii.to_byte(),
|
||||
Some(ascii) => ascii.as_byte(),
|
||||
None => return false
|
||||
};
|
||||
!(num >= 'a' as u8 && num <= 'z' as u8 ||
|
||||
|
|
|
@ -92,8 +92,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
|||
set_context(&newroot, &opts);
|
||||
|
||||
unsafe {
|
||||
let executable = command[0].as_slice().to_c_str().unwrap();
|
||||
let mut command_parts: Vec<*const i8> = command.iter().map(|x| x.to_c_str().unwrap()).collect();
|
||||
let executable = command[0].as_slice().to_c_str().into_inner();
|
||||
let mut command_parts: Vec<*const i8> = command.iter().map(|x| x.to_c_str().into_inner()).collect();
|
||||
command_parts.push(std::ptr::null());
|
||||
execvp(executable as *const libc::c_char, command_parts.as_ptr() as *mut *const libc::c_char) as int
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ fn enter_chroot(root: &Path) {
|
|||
let root_str = root.display();
|
||||
std::os::change_dir(root).unwrap();
|
||||
let err = unsafe {
|
||||
chroot(".".to_c_str().unwrap() as *const libc::c_char)
|
||||
chroot(".".to_c_str().into_inner() as *const libc::c_char)
|
||||
};
|
||||
if err != 0 {
|
||||
crash!(1, "cannot chroot to {}: {}", root_str, strerror(err).as_slice())
|
||||
|
@ -193,7 +193,7 @@ fn set_user(user: &str) {
|
|||
fn strerror(errno: i32) -> String {
|
||||
unsafe {
|
||||
let err = libc::funcs::c95::string::strerror(errno);
|
||||
std::string::raw::from_buf(err as *const u8)
|
||||
String::from_raw_buf(err as *const u8)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@ use std::vec::Vec;
|
|||
|
||||
use std::os;
|
||||
use std::ptr::{null_mut, read};
|
||||
use std::string::raw::from_buf;
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
|
||||
#[repr(C)]
|
||||
|
@ -124,7 +123,7 @@ pub fn get_pw_from_args(free: &Vec<String>) -> Option<c_passwd> {
|
|||
// Passed the username as a string
|
||||
} else {
|
||||
let pw_pointer = unsafe {
|
||||
getpwnam(username.as_slice().to_c_str().unwrap() as *const libc::c_char)
|
||||
getpwnam(username.as_slice().to_c_str().into_inner() as *const libc::c_char)
|
||||
};
|
||||
if pw_pointer.is_not_null() {
|
||||
Some(unsafe { read(pw_pointer) })
|
||||
|
@ -141,7 +140,7 @@ pub fn get_group(groupname: &str) -> Option<c_group> {
|
|||
let group = if groupname.chars().all(|c| c.is_digit(10)) {
|
||||
unsafe { getgrgid(from_str::<gid_t>(groupname).unwrap()) }
|
||||
} else {
|
||||
unsafe { getgrnam(groupname.to_c_str().unwrap() as *const c_char) }
|
||||
unsafe { getgrnam(groupname.to_c_str().into_inner() as *const c_char) }
|
||||
};
|
||||
|
||||
if group.is_not_null() {
|
||||
|
@ -217,7 +216,7 @@ pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
|||
let group = unsafe { getgrgid(g) };
|
||||
if group.is_not_null() {
|
||||
let name = unsafe {
|
||||
from_buf(read(group).gr_name as *const u8)
|
||||
String::from_raw_buf(read(group).gr_name as *const u8)
|
||||
};
|
||||
print!("{} ", name);
|
||||
}
|
||||
|
|
|
@ -94,7 +94,7 @@ pub fn break_lines(para: &Paragraph, opts: &FmtOptions, ostream: &mut Box<Writer
|
|||
|
||||
// break_simple implements a "greedy" breaking algorithm: print words until
|
||||
// maxlength would be exceeded, then print a linebreak and indent and continue.
|
||||
fn break_simple<'a, T: Iterator<&'a WordInfo<'a>>>(mut iter: T, args: &mut BreakArgs<'a>) {
|
||||
fn break_simple<'a, T: Iterator<&'a WordInfo<'a>>>(iter: T, args: &mut BreakArgs<'a>) {
|
||||
iter.fold((args.init_len, false), |l, winfo| accum_words_simple(args, l, winfo));
|
||||
silent_unwrap!(args.ostream.write_char('\n'));
|
||||
}
|
||||
|
|
|
@ -212,10 +212,10 @@ fn hashsum(algoname: &str, mut digest: Box<Digest>, files: Vec<String>, binary:
|
|||
let mut buffer = file;
|
||||
for (i, line) in buffer.lines().enumerate() {
|
||||
let line = safe_unwrap!(line);
|
||||
let (ck_filename, sum, binary_check) = match gnu_re.captures(line.as_slice()) {
|
||||
Some(caps) => (caps.name("fileName"), caps.name("digest").to_ascii().to_lowercase(), caps.name("binary") == "*"),
|
||||
let (ck_filename, sum, binary_check): (_, Vec<Ascii>, _) = match gnu_re.captures(line.as_slice()) {
|
||||
Some(caps) => (caps.name("fileName"), caps.name("digest").to_ascii().iter().map(|ch| ch.to_lowercase()).collect(), caps.name("binary") == "*"),
|
||||
None => match bsd_re.captures(line.as_slice()) {
|
||||
Some(caps) => (caps.name("fileName"), caps.name("digest").to_ascii().to_lowercase(), true),
|
||||
Some(caps) => (caps.name("fileName"), caps.name("digest").to_ascii().iter().map(|ch| ch.to_lowercase()).collect(), true),
|
||||
None => {
|
||||
bad_format += 1;
|
||||
if strict {
|
||||
|
@ -228,8 +228,8 @@ fn hashsum(algoname: &str, mut digest: Box<Digest>, files: Vec<String>, binary:
|
|||
}
|
||||
}
|
||||
};
|
||||
let real_sum = safe_unwrap!(digest_reader(&mut digest, &mut safe_unwrap!(File::open(&Path::new(ck_filename))), binary_check))
|
||||
.as_slice().to_ascii().to_lowercase();
|
||||
let real_sum: Vec<Ascii> = safe_unwrap!(digest_reader(&mut digest, &mut safe_unwrap!(File::open(&Path::new(ck_filename))), binary_check))
|
||||
.as_slice().to_ascii().iter().map(|ch| ch.to_lowercase()).collect();
|
||||
if sum.as_slice() == real_sum.as_slice() {
|
||||
if !quiet {
|
||||
pipe_println!("{}: OK", ck_filename);
|
||||
|
|
49
src/id/id.rs
49
src/id/id.rs
|
@ -25,7 +25,6 @@ use libc::{
|
|||
getuid
|
||||
};
|
||||
use libc::funcs::posix88::unistd::{getegid, geteuid, getlogin};
|
||||
use std::string::raw::from_buf;
|
||||
use getopts::{getopts, optflag, usage};
|
||||
use c_types::{
|
||||
c_passwd,
|
||||
|
@ -139,7 +138,7 @@ pub fn uumain(args: Vec<String>) -> int {
|
|||
let gr = unsafe { getgrgid(id) };
|
||||
|
||||
if nflag && gr.is_not_null() {
|
||||
let gr_name = unsafe { from_buf(read(gr).gr_name as *const u8) };
|
||||
let gr_name = unsafe { String::from_raw_buf(read(gr).gr_name as *const u8) };
|
||||
println!("{}", gr_name);
|
||||
} else {
|
||||
println!("{}", id);
|
||||
|
@ -159,7 +158,7 @@ pub fn uumain(args: Vec<String>) -> int {
|
|||
let pw = unsafe { getpwuid(id) };
|
||||
if nflag && pw.is_not_null() {
|
||||
let pw_name = unsafe {
|
||||
from_buf(read(pw).pw_name as *const u8)
|
||||
String::from_raw_buf(read(pw).pw_name as *const u8)
|
||||
};
|
||||
println!("{}", pw_name);
|
||||
} else {
|
||||
|
@ -197,16 +196,16 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
|||
if possible_pw.is_some() {
|
||||
let pw = possible_pw.unwrap();
|
||||
|
||||
let pw_name = unsafe { from_buf(pw.pw_name as *const u8) };
|
||||
let pw_name = unsafe { String::from_raw_buf(pw.pw_name as *const u8) };
|
||||
print!("uid\t{}\ngroups\t", pw_name);
|
||||
group(possible_pw, true);
|
||||
} else {
|
||||
let login = unsafe { from_buf(getlogin() as *const u8) };
|
||||
let login = unsafe { String::from_raw_buf(getlogin() as *const u8) };
|
||||
let rid = unsafe { getuid() };
|
||||
let pw = unsafe { getpwuid(rid) };
|
||||
|
||||
let is_same_user = unsafe {
|
||||
from_buf(read(pw).pw_name as *const u8) == login
|
||||
String::from_raw_buf(read(pw).pw_name as *const u8) == login
|
||||
};
|
||||
|
||||
if pw.is_null() || is_same_user {
|
||||
|
@ -216,7 +215,7 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
|||
if pw.is_not_null() {
|
||||
println!(
|
||||
"uid\t{}",
|
||||
unsafe { from_buf(read(pw).pw_name as *const u8) })
|
||||
unsafe { String::from_raw_buf(read(pw).pw_name as *const u8) })
|
||||
} else {
|
||||
println!("uid\t{}\n", rid);
|
||||
}
|
||||
|
@ -227,7 +226,7 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
|||
if pw.is_not_null() {
|
||||
println!(
|
||||
"euid\t{}",
|
||||
unsafe { from_buf(read(pw).pw_name as *const u8) });
|
||||
unsafe { String::from_raw_buf(read(pw).pw_name as *const u8) });
|
||||
} else {
|
||||
println!("euid\t{}", eid);
|
||||
}
|
||||
|
@ -240,7 +239,7 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
|||
if gr.is_not_null() {
|
||||
println!(
|
||||
"rgid\t{}",
|
||||
unsafe { from_buf(read(gr).gr_name as *const u8) });
|
||||
unsafe { String::from_raw_buf(read(gr).gr_name as *const u8) });
|
||||
} else {
|
||||
println!("rgid\t{}", rid);
|
||||
}
|
||||
|
@ -259,12 +258,12 @@ fn pline(possible_pw: Option<c_passwd>) {
|
|||
possible_pw.unwrap()
|
||||
};
|
||||
|
||||
let pw_name = unsafe { from_buf(pw.pw_name as *const u8) };
|
||||
let pw_passwd = unsafe { from_buf(pw.pw_passwd as *const u8) };
|
||||
let pw_class = unsafe { from_buf(pw.pw_class as *const u8) };
|
||||
let pw_gecos = unsafe { from_buf(pw.pw_gecos as *const u8) };
|
||||
let pw_dir = unsafe { from_buf(pw.pw_dir as *const u8) };
|
||||
let pw_shell = unsafe { from_buf(pw.pw_shell as *const u8) };
|
||||
let pw_name = unsafe { String::from_raw_buf(pw.pw_name as *const u8) };
|
||||
let pw_passwd = unsafe { String::from_raw_buf(pw.pw_passwd as *const u8) };
|
||||
let pw_class = unsafe { String::from_raw_buf(pw.pw_class as *const u8) };
|
||||
let pw_gecos = unsafe { String::from_raw_buf(pw.pw_gecos as *const u8) };
|
||||
let pw_dir = unsafe { String::from_raw_buf(pw.pw_dir as *const u8) };
|
||||
let pw_shell = unsafe { String::from_raw_buf(pw.pw_shell as *const u8) };
|
||||
|
||||
println!(
|
||||
"{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
|
||||
|
@ -288,11 +287,11 @@ fn pline(possible_pw: Option<c_passwd>) {
|
|||
possible_pw.unwrap()
|
||||
};
|
||||
|
||||
let pw_name = unsafe { from_buf(pw.pw_name as *const u8)};
|
||||
let pw_passwd = unsafe { from_buf(pw.pw_passwd as *const u8)};
|
||||
let pw_gecos = unsafe { from_buf(pw.pw_gecos as *const u8)};
|
||||
let pw_dir = unsafe { from_buf(pw.pw_dir as *const u8)};
|
||||
let pw_shell = unsafe { from_buf(pw.pw_shell as *const u8)};
|
||||
let pw_name = unsafe { String::from_raw_buf(pw.pw_name as *const u8)};
|
||||
let pw_passwd = unsafe { String::from_raw_buf(pw.pw_passwd as *const u8)};
|
||||
let pw_gecos = unsafe { String::from_raw_buf(pw.pw_gecos as *const u8)};
|
||||
let pw_dir = unsafe { String::from_raw_buf(pw.pw_dir as *const u8)};
|
||||
let pw_shell = unsafe { String::from_raw_buf(pw.pw_shell as *const u8)};
|
||||
|
||||
println!(
|
||||
"{}:{}:{}:{}:{}:{}:{}",
|
||||
|
@ -352,7 +351,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
|||
print!(
|
||||
"uid={}({})",
|
||||
uid,
|
||||
unsafe { from_buf(possible_pw.unwrap().pw_name as *const u8) });
|
||||
unsafe { String::from_raw_buf(possible_pw.unwrap().pw_name as *const u8) });
|
||||
} else {
|
||||
print!("uid={}", unsafe { getuid() });
|
||||
}
|
||||
|
@ -362,7 +361,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
|||
if gr.is_not_null() {
|
||||
print!(
|
||||
"({})",
|
||||
unsafe { from_buf(read(gr).gr_name as *const u8) });
|
||||
unsafe { String::from_raw_buf(read(gr).gr_name as *const u8) });
|
||||
}
|
||||
|
||||
let euid = unsafe { geteuid() };
|
||||
|
@ -372,7 +371,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
|||
if pw.is_not_null() {
|
||||
print!(
|
||||
"({})",
|
||||
unsafe { from_buf(read(pw).pw_name as *const u8) });
|
||||
unsafe { String::from_raw_buf(read(pw).pw_name as *const u8) });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -382,7 +381,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
|||
unsafe {
|
||||
let grp = getgrgid(egid);
|
||||
if grp.is_not_null() {
|
||||
print!("({})", from_buf(read(grp).gr_name as *const u8));
|
||||
print!("({})", String::from_raw_buf(read(grp).gr_name as *const u8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -397,7 +396,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
|||
let group = unsafe { getgrgid(gr) };
|
||||
if group.is_not_null() {
|
||||
let name = unsafe {
|
||||
from_buf(read(group).gr_name as *const u8)
|
||||
String::from_raw_buf(read(group).gr_name as *const u8)
|
||||
};
|
||||
print!("({})", name);
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@ extern crate getopts;
|
|||
extern crate libc;
|
||||
|
||||
use std::io::print;
|
||||
use std::string;
|
||||
use libc::c_char;
|
||||
|
||||
#[path = "../common/util.rs"] mod util;
|
||||
|
@ -32,7 +31,7 @@ extern {
|
|||
unsafe fn get_userlogin() -> String {
|
||||
let login: *const libc::c_char = getlogin();
|
||||
|
||||
string::raw::from_buf(login as *const u8)
|
||||
String::from_raw_buf(login as *const u8)
|
||||
}
|
||||
|
||||
static NAME: &'static str = "logname";
|
||||
|
|
|
@ -9,13 +9,14 @@
|
|||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
#![feature(macro_rules)]
|
||||
#![feature(globs, macro_rules)]
|
||||
extern crate getopts;
|
||||
extern crate libc;
|
||||
|
||||
use getopts::{optflag, getopts, usage};
|
||||
use std::io::stdio::{stdin_raw, stdout_raw, stderr_raw};
|
||||
use std::io::{File, Open, Read, Append, Write};
|
||||
use std::os::unix::prelude::*;
|
||||
use libc::funcs::posix88::unistd::{dup2, execvp};
|
||||
use libc::consts::os::posix88::SIGHUP;
|
||||
use libc::funcs::posix01::signal::signal;
|
||||
|
@ -32,35 +33,11 @@ extern {
|
|||
fn _vprocmgr_detach_from_console(flags: u32) -> *const libc::c_int;
|
||||
}
|
||||
|
||||
// BEGIN CODE TO DELETE AFTER https://github.com/rust-lang/rust/issues/18897 is fixed
|
||||
struct HackyFile {
|
||||
pub fd: FileDesc,
|
||||
path: Path,
|
||||
last_nread: int
|
||||
}
|
||||
|
||||
struct FileDesc {
|
||||
fd: libc::c_int,
|
||||
close_on_drop: bool
|
||||
}
|
||||
|
||||
trait AsFileDesc {
|
||||
fn as_fd(&self) -> FileDesc;
|
||||
}
|
||||
|
||||
impl AsFileDesc for File {
|
||||
fn as_fd(&self) -> FileDesc {
|
||||
let hack: HackyFile = unsafe { std::mem::transmute_copy(self) };
|
||||
hack.fd
|
||||
}
|
||||
}
|
||||
// END CODE TO DELETE
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn rewind_stdout(s: &mut FileDesc) {
|
||||
fn rewind_stdout(s: Fd) {
|
||||
match s.seek(0, io::SeekEnd) {
|
||||
Ok(_) => {}
|
||||
Err(f) => crash!(1, "{}", f.detail.unwrap())
|
||||
Err(f) => crash!(1, "{}", f.detail.into_inner())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,7 +45,7 @@ fn rewind_stdout(s: &mut FileDesc) {
|
|||
unsafe fn _vprocmgr_detach_from_console(_: u32) -> *const libc::c_int { std::ptr::null() }
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
|
||||
fn rewind_stdout(_: &mut FileDesc) {}
|
||||
fn rewind_stdout(_: Fd) {}
|
||||
|
||||
pub fn uumain(args: Vec<String>) -> int {
|
||||
let program = &args[0];
|
||||
|
@ -103,8 +80,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
|||
|
||||
unsafe {
|
||||
// we ignore the memory leak here because it doesn't matter anymore
|
||||
let executable = opts.free[0].as_slice().to_c_str().unwrap();
|
||||
let mut args: Vec<*const i8> = opts.free.iter().map(|x| x.to_c_str().unwrap()).collect();
|
||||
let executable = opts.free[0].as_slice().to_c_str().into_inner();
|
||||
let mut args: Vec<*const i8> = opts.free.iter().map(|x| x.to_c_str().into_inner()).collect();
|
||||
args.push(std::ptr::null());
|
||||
execvp(executable as *const libc::c_char, args.as_ptr() as *mut *const libc::c_char) as int
|
||||
}
|
||||
|
@ -122,18 +99,18 @@ fn replace_fds() {
|
|||
crash!(2, "Cannot replace STDIN: {}", e)
|
||||
}
|
||||
};
|
||||
if unsafe { dup2(new_stdin.as_fd().fd, 0) } != 0 {
|
||||
if unsafe { dup2(new_stdin.as_raw_fd(), 0) } != 0 {
|
||||
crash!(2, "Cannot replace STDIN: {}", std::io::IoError::last_error())
|
||||
}
|
||||
}
|
||||
|
||||
if replace_stdout {
|
||||
let new_stdout = find_stdout();
|
||||
let mut fd = new_stdout.as_fd();
|
||||
let fd = new_stdout.as_raw_fd();
|
||||
|
||||
rewind_stdout(&mut fd);
|
||||
rewind_stdout(fd);
|
||||
|
||||
if unsafe { dup2(fd.fd, 1) } != 1 {
|
||||
if unsafe { dup2(fd, 1) } != 1 {
|
||||
crash!(2, "Cannot replace STDOUT: {}", std::io::IoError::last_error())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -191,8 +191,8 @@ fn parse_size(size: &str) -> (u64, TruncateMode) {
|
|||
}
|
||||
};
|
||||
if size.char_at(size.len() - 1).is_alphabetic() {
|
||||
number *= match size.char_at(size.len() - 1).to_ascii().to_uppercase().to_char() {
|
||||
'B' => match size.char_at(size.len() - 2).to_ascii().to_uppercase().to_char() {
|
||||
number *= match size.char_at(size.len() - 1).to_ascii().to_uppercase().as_char() {
|
||||
'B' => match size.char_at(size.len() - 2).to_ascii().to_uppercase().as_char() {
|
||||
'K' => 1000,
|
||||
'M' => 1000 * 1000,
|
||||
'G' => 1000 * 1000 * 1000,
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
extern crate getopts;
|
||||
extern crate libc;
|
||||
|
||||
use std::string;
|
||||
use std::io::println;
|
||||
use std::io::stdio::stderr;
|
||||
use getopts::{optflag,getopts};
|
||||
|
@ -50,7 +49,7 @@ pub fn uumain(args: Vec<String>) -> int {
|
|||
}
|
||||
};
|
||||
|
||||
let tty = unsafe { string::raw::from_buf(ttyname(libc::STDIN_FILENO) as *const u8) };
|
||||
let tty = unsafe { String::from_raw_buf(ttyname(libc::STDIN_FILENO) as *const u8) };
|
||||
|
||||
if !silent {
|
||||
if !tty.as_slice().is_whitespace() {
|
||||
|
|
|
@ -19,7 +19,6 @@ extern crate libc;
|
|||
|
||||
use std::mem::uninitialized;
|
||||
use std::io::print;
|
||||
use std::string::raw::from_buf;
|
||||
use c_types::utsname;
|
||||
|
||||
#[path = "../common/util.rs"] mod util;
|
||||
|
@ -41,11 +40,11 @@ unsafe fn getuname() -> utsrust {
|
|||
let mut uts: utsname = uninitialized();
|
||||
uname(&mut uts);
|
||||
utsrust {
|
||||
sysname: from_buf(uts.sysname.as_ptr() as *const u8),
|
||||
nodename: from_buf(uts.nodename.as_ptr() as *const u8),
|
||||
release: from_buf(uts.release.as_ptr() as *const u8),
|
||||
version: from_buf(uts.version.as_ptr() as *const u8),
|
||||
machine: from_buf(uts.machine.as_ptr() as *const u8)
|
||||
sysname: String::from_raw_buf(uts.sysname.as_ptr() as *const u8),
|
||||
nodename: String::from_raw_buf(uts.nodename.as_ptr() as *const u8),
|
||||
release: String::from_raw_buf(uts.release.as_ptr() as *const u8),
|
||||
version: String::from_raw_buf(uts.version.as_ptr() as *const u8),
|
||||
machine: String::from_raw_buf(uts.machine.as_ptr() as *const u8)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,13 +16,13 @@
|
|||
|
||||
extern crate getopts;
|
||||
extern crate libc;
|
||||
extern crate "time" as rtime;
|
||||
|
||||
use std::mem::transmute;
|
||||
use std::io::{print, File};
|
||||
use std::ptr::{null_mut, null};
|
||||
use std::ptr::null;
|
||||
use std::str::from_str;
|
||||
use libc::{time_t, c_double, c_int, c_char};
|
||||
use c_types::c_tm;
|
||||
use utmpx::*;
|
||||
|
||||
#[path = "../common/util.rs"] mod util;
|
||||
|
@ -33,10 +33,8 @@ use utmpx::*;
|
|||
|
||||
static NAME: &'static str = "uptime";
|
||||
|
||||
#[cfg(unix)]
|
||||
extern {
|
||||
fn time(timep: *mut time_t) -> time_t;
|
||||
fn localtime(timep: *const time_t) -> *const c_tm;
|
||||
|
||||
fn getloadavg(loadavg: *mut c_double, nelem: c_int) -> c_int;
|
||||
|
||||
fn getutxent() -> *const c_utmp;
|
||||
|
@ -47,6 +45,11 @@ extern {
|
|||
fn utmpxname(file: *const c_char) -> c_int;
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
extern {
|
||||
fn GetTickCount() -> libc::uint32_t;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "freebsd")]
|
||||
unsafe extern fn utmpxname(_file: *const c_char) -> c_int {
|
||||
0
|
||||
|
@ -102,6 +105,7 @@ fn print_loadavg() {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn process_utmpx() -> (Option<time_t>, uint) {
|
||||
DEFAULT_FILE.with_c_str(|filename| {
|
||||
unsafe {
|
||||
|
@ -140,6 +144,11 @@ fn process_utmpx() -> (Option<time_t>, uint) {
|
|||
(boot_time, nusers)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn process_utmpx() -> (Option<time_t>, uint) {
|
||||
(None, 0) // TODO: change 0 to number of users
|
||||
}
|
||||
|
||||
fn print_nusers(nusers: uint) {
|
||||
if nusers == 1 {
|
||||
print!("1 user, ");
|
||||
|
@ -149,15 +158,13 @@ fn print_nusers(nusers: uint) {
|
|||
}
|
||||
|
||||
fn print_time() {
|
||||
let local_time = unsafe { *localtime(&time(null_mut())) };
|
||||
let local_time = rtime::now();
|
||||
|
||||
if local_time.tm_hour >= 0 && local_time.tm_min >= 0 &&
|
||||
local_time.tm_sec >= 0 {
|
||||
print!(" {:02}:{:02}:{:02} ", local_time.tm_hour,
|
||||
local_time.tm_min, local_time.tm_sec);
|
||||
}
|
||||
print!(" {:02}:{:02}:{:02} ", local_time.tm_hour,
|
||||
local_time.tm_min, local_time.tm_sec);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
||||
let proc_uptime = File::open(&Path::new("/proc/uptime"))
|
||||
.read_to_string();
|
||||
|
@ -166,7 +173,7 @@ fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
|||
Ok(s) => s,
|
||||
_ => return match boot_time {
|
||||
Some(t) => {
|
||||
let now = unsafe { time(null_mut()) };
|
||||
let now = rtime::get_time().sec;
|
||||
((now - t) * 100) as i64 // Return in ms
|
||||
},
|
||||
_ => -1
|
||||
|
@ -174,7 +181,7 @@ fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
|||
};
|
||||
|
||||
match uptime_text.as_slice().words().next() {
|
||||
Some(s) => match from_str(s.replace(".","").as_slice()) {
|
||||
Some(s) => match from_str(s.replace(".", "").as_slice()) {
|
||||
Some(n) => n,
|
||||
None => -1
|
||||
},
|
||||
|
@ -182,6 +189,11 @@ fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
||||
unsafe { GetTickCount() as i64 }
|
||||
}
|
||||
|
||||
fn print_uptime(upsecs: i64) {
|
||||
let updays = upsecs / 86400;
|
||||
let uphours = (upsecs - (updays * 86400)) / 3600;
|
||||
|
|
|
@ -22,7 +22,6 @@ extern crate libc;
|
|||
use std::io::print;
|
||||
use std::mem;
|
||||
use std::ptr;
|
||||
use std::string;
|
||||
use utmpx::*;
|
||||
|
||||
#[path = "../common/util.rs"]
|
||||
|
@ -109,7 +108,7 @@ fn exec(filename: &str) {
|
|||
}
|
||||
|
||||
if (*line).ut_type == USER_PROCESS {
|
||||
let user = string::raw::from_buf(mem::transmute(&(*line).ut_user));
|
||||
let user = String::from_raw_buf(mem::transmute(&(*line).ut_user));
|
||||
users.push(user);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ use std::io::print;
|
|||
#[cfg(unix)]
|
||||
mod platform {
|
||||
use super::libc;
|
||||
use std::string;
|
||||
use self::c_types::{c_passwd, getpwuid};
|
||||
|
||||
#[path = "../../common/c_types.rs"] mod c_types;
|
||||
|
@ -38,7 +37,7 @@ mod platform {
|
|||
let passwd: *const c_passwd = getpwuid(geteuid());
|
||||
|
||||
let pw_name: *const libc::c_char = (*passwd).pw_name;
|
||||
let name = string::raw::from_buf(pw_name as *const u8);
|
||||
let name = String::from_raw_buf(pw_name as *const u8);
|
||||
|
||||
name
|
||||
}
|
||||
|
@ -48,7 +47,6 @@ mod platform {
|
|||
mod platform {
|
||||
pub use super::libc;
|
||||
use std::mem;
|
||||
use std::string;
|
||||
|
||||
extern "system" {
|
||||
pub fn GetUserNameA(out: *mut libc::c_char, len: *mut libc::uint32_t) -> libc::uint8_t;
|
||||
|
@ -60,7 +58,7 @@ mod platform {
|
|||
if !GetUserNameA(buffer.as_mut_ptr(), &mut (buffer.len() as libc::uint32_t)) == 0 {
|
||||
crash!(1, "username is too long");
|
||||
}
|
||||
string::raw::from_buf(buffer.as_ptr() as *const u8)
|
||||
String::from_raw_buf(buffer.as_ptr() as *const u8)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue