mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #107 from alan-andrade/common
/common for code reusability
This commit is contained in:
commit
faf01182b6
20 changed files with 94 additions and 80 deletions
|
@ -28,7 +28,7 @@ use getopts::{
|
|||
use serialize::base64;
|
||||
use serialize::base64::{FromBase64, ToBase64};
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "base64";
|
||||
|
|
|
@ -18,7 +18,7 @@ use std::os;
|
|||
use std::str;
|
||||
use std::str::StrSlice;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "basename";
|
||||
|
|
24
common/c_types.rs
Normal file
24
common/c_types.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
#[allow(dead_code)];
|
||||
|
||||
use std::libc::{
|
||||
c_char,
|
||||
c_int,
|
||||
time_t
|
||||
};
|
||||
|
||||
pub struct c_passwd {
|
||||
pw_name: *c_char, /* user name */
|
||||
pw_passwd: *c_char, /* user name */
|
||||
pw_uid: c_int, /* user uid */
|
||||
pw_gid: c_int, /* user gid */
|
||||
pw_change: time_t,
|
||||
pw_class: *c_char,
|
||||
pw_gecos: *c_char,
|
||||
pw_dir: *c_char,
|
||||
pw_shell: *c_char,
|
||||
pw_expire: time_t
|
||||
}
|
||||
|
||||
pub struct c_group {
|
||||
gr_name: *c_char /* group name */
|
||||
}
|
2
du/du.rs
2
du/du.rs
|
@ -23,7 +23,7 @@ use std::path::Path;
|
|||
use time::Timespec;
|
||||
use sync::{Arc, Future};
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "du";
|
||||
|
|
|
@ -17,7 +17,7 @@ use std::os;
|
|||
use std::io::{print, println};
|
||||
use std::uint;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "echo";
|
||||
|
|
108
id/id.rs
108
id/id.rs
|
@ -14,32 +14,29 @@
|
|||
*/
|
||||
|
||||
#[allow(non_camel_case_types)];
|
||||
|
||||
#[feature(macro_rules)];
|
||||
extern crate getopts;
|
||||
|
||||
use std::{libc, os, vec};
|
||||
use std::ptr::read;
|
||||
use std::libc::{c_char, c_int, time_t, uid_t, getgid, getegid, getuid, getlogin};
|
||||
use std::libc::{
|
||||
c_char,
|
||||
c_int,
|
||||
uid_t,
|
||||
getgid,
|
||||
getegid,
|
||||
getuid,
|
||||
getlogin
|
||||
};
|
||||
use std::str::raw::from_c_str;
|
||||
use getopts::{getopts, optflag, usage};
|
||||
use c_types::{
|
||||
c_passwd,
|
||||
c_group
|
||||
};
|
||||
|
||||
// These could be extracted into their own file
|
||||
struct c_passwd {
|
||||
pw_name: *c_char, /* user name */
|
||||
pw_passwd: *c_char, /* user name */
|
||||
pw_uid: c_int, /* user uid */
|
||||
pw_gid: c_int, /* user gid */
|
||||
pw_change: time_t,
|
||||
pw_class: *c_char,
|
||||
pw_gecos: *c_char,
|
||||
pw_dir: *c_char,
|
||||
pw_shell: *c_char,
|
||||
pw_expire: time_t
|
||||
}
|
||||
|
||||
struct c_group {
|
||||
gr_name: *c_char /* group name */
|
||||
}
|
||||
#[path = "../common/util.rs"] mod util;
|
||||
#[path = "../common/c_types.rs"] mod c_types;
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
mod audit {
|
||||
|
@ -87,7 +84,38 @@ extern {
|
|||
ngroups: *mut c_int) -> c_int;
|
||||
}
|
||||
|
||||
static PROGRAM: &'static str = "id";
|
||||
static NAME: &'static str = "id";
|
||||
|
||||
fn get_pw_from_args(matches: &getopts::Matches) -> Option<c_passwd> {
|
||||
if matches.free.len() == 1 {
|
||||
let username = matches.free[0].clone();
|
||||
|
||||
// Passed user as id
|
||||
if username.chars().all(|c| c.is_digit()) {
|
||||
let id = from_str::<u32>(username).unwrap();
|
||||
let pw_pointer = unsafe { getpwuid(id) };
|
||||
|
||||
if pw_pointer.is_not_null() {
|
||||
Some(unsafe { read(pw_pointer) })
|
||||
} else {
|
||||
crash!(1, "{:s}: no such user", username);
|
||||
}
|
||||
|
||||
// Passed the username as a string
|
||||
} else {
|
||||
let pw_pointer = unsafe {
|
||||
getpwnam(username.as_slice().as_ptr() as *i8)
|
||||
};
|
||||
if pw_pointer.is_not_null() {
|
||||
Some(unsafe { read(pw_pointer) })
|
||||
} else {
|
||||
crash!(1, "{:s}: no such user", username);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn main () {
|
||||
let args = os::args();
|
||||
|
@ -108,13 +136,13 @@ fn main () {
|
|||
let matches = match getopts(args_t, options) {
|
||||
Ok(m) => { m },
|
||||
Err(_) => {
|
||||
println!("{:s}", usage(PROGRAM, options));
|
||||
println!("{:s}", usage(NAME, options));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if matches.opt_present("h") {
|
||||
println!("{:s}", usage(PROGRAM, options));
|
||||
println!("{:s}", usage(NAME, options));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -124,37 +152,7 @@ fn main () {
|
|||
}
|
||||
|
||||
|
||||
let possible_pw = if matches.free.len() == 1 {
|
||||
let username = matches.free[0].clone();
|
||||
|
||||
// Passed user by id
|
||||
if username.chars().all(|c| c.is_digit()) {
|
||||
let id = from_str::<u32>(username).unwrap();
|
||||
let pw_pointer = unsafe { getpwuid(id) };
|
||||
|
||||
if pw_pointer.is_not_null() {
|
||||
Some(unsafe { read(pw_pointer) })
|
||||
} else {
|
||||
no_such_user(username);
|
||||
return;
|
||||
}
|
||||
|
||||
// Passed the username as a string
|
||||
} else {
|
||||
let pw_pointer = unsafe {
|
||||
getpwnam(username.as_slice().as_ptr() as *i8)
|
||||
};
|
||||
if pw_pointer.is_not_null() {
|
||||
Some(unsafe { read(pw_pointer) })
|
||||
} else {
|
||||
no_such_user(username);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let possible_pw = get_pw_from_args(&matches);
|
||||
|
||||
let nflag = matches.opt_present("n");
|
||||
let uflag = matches.opt_present("u");
|
||||
|
@ -312,10 +310,6 @@ fn pline(possible_pw: Option<c_passwd>) {
|
|||
pw_shell);
|
||||
}
|
||||
|
||||
fn no_such_user(username: ~str) {
|
||||
println!("{:s}: {:s}: no such user", PROGRAM, username.as_slice());
|
||||
}
|
||||
|
||||
static NGROUPS: i32 = 20;
|
||||
|
||||
fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
||||
|
|
|
@ -18,7 +18,7 @@ use std::os;
|
|||
use std::io::fs;
|
||||
use std::num::strconv;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "mkdir";
|
||||
|
|
|
@ -19,7 +19,7 @@ extern crate getopts;
|
|||
use std::os;
|
||||
use std::io::print;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "printenv";
|
||||
|
|
|
@ -17,7 +17,7 @@ extern crate getopts;
|
|||
use std::os;
|
||||
use std::io::print;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "pwd";
|
||||
|
|
2
rm/rm.rs
2
rm/rm.rs
|
@ -17,7 +17,7 @@ extern crate getopts;
|
|||
use std::os;
|
||||
use std::io::{print, stdin, stdio, fs, BufferedReader};
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
#[deriving(Eq)]
|
||||
|
|
|
@ -17,7 +17,7 @@ extern crate getopts;
|
|||
use std::os;
|
||||
use std::io::{print, fs};
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "rmdir";
|
||||
|
|
|
@ -11,7 +11,7 @@ extern crate getopts;
|
|||
use std::os;
|
||||
use std::cmp::max;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "seq";
|
||||
|
|
|
@ -19,7 +19,7 @@ use std::cast;
|
|||
use std::os;
|
||||
use std::io::{print, timer};
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "sleep";
|
||||
|
|
|
@ -18,7 +18,7 @@ use std::io::{File, Open, ReadWrite, fs};
|
|||
use std::os;
|
||||
use std::u64;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
macro_rules! get_file_size(
|
||||
|
|
|
@ -24,7 +24,7 @@ use std::io::println;
|
|||
use std::io::stdio::stderr;
|
||||
use getopts::{optflag,getopts};
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
extern {
|
||||
|
|
|
@ -27,7 +27,7 @@ use std::ptr;
|
|||
use std::str;
|
||||
use utmpx::*;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
|
|
2
wc/wc.rs
2
wc/wc.rs
|
@ -19,7 +19,7 @@ use std::str::from_utf8;
|
|||
use std::io::{print, stdin, File, BufferedReader};
|
||||
use getopts::Matches;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
struct Result {
|
||||
|
|
|
@ -22,14 +22,10 @@ use std::io::print;
|
|||
use std::os;
|
||||
use std::str;
|
||||
use std::libc;
|
||||
use c_types::c_passwd;
|
||||
|
||||
#[path = "../util.rs"]
|
||||
mod util;
|
||||
|
||||
struct c_passwd {
|
||||
pw_name: *libc::c_char,
|
||||
// Maybe I should put here others struct members, but...Well, maybe.
|
||||
}
|
||||
#[path = "../common/util.rs"] mod util;
|
||||
#[path = "../common/c_types.rs"] mod c_types;
|
||||
|
||||
extern {
|
||||
pub fn geteuid() -> libc::c_int;
|
||||
|
|
|
@ -19,7 +19,7 @@ extern crate getopts;
|
|||
use std::os;
|
||||
use std::io::{print, println};
|
||||
|
||||
#[path = "../util.rs"]
|
||||
#[path = "../common/util.rs"]
|
||||
mod util;
|
||||
|
||||
static NAME: &'static str = "yes";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue