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;
|
||||||
use serialize::base64::{FromBase64, ToBase64};
|
use serialize::base64::{FromBase64, ToBase64};
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "base64";
|
static NAME: &'static str = "base64";
|
||||||
|
|
|
@ -18,7 +18,7 @@ use std::os;
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::str::StrSlice;
|
use std::str::StrSlice;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "basename";
|
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 time::Timespec;
|
||||||
use sync::{Arc, Future};
|
use sync::{Arc, Future};
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "du";
|
static NAME: &'static str = "du";
|
||||||
|
|
|
@ -17,7 +17,7 @@ use std::os;
|
||||||
use std::io::{print, println};
|
use std::io::{print, println};
|
||||||
use std::uint;
|
use std::uint;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "echo";
|
static NAME: &'static str = "echo";
|
||||||
|
|
108
id/id.rs
108
id/id.rs
|
@ -14,32 +14,29 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[allow(non_camel_case_types)];
|
#[allow(non_camel_case_types)];
|
||||||
|
#[feature(macro_rules)];
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
|
|
||||||
use std::{libc, os, vec};
|
use std::{libc, os, vec};
|
||||||
use std::ptr::read;
|
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 std::str::raw::from_c_str;
|
||||||
use getopts::{getopts, optflag, usage};
|
use getopts::{getopts, optflag, usage};
|
||||||
|
use c_types::{
|
||||||
|
c_passwd,
|
||||||
|
c_group
|
||||||
|
};
|
||||||
|
|
||||||
// These could be extracted into their own file
|
#[path = "../common/util.rs"] mod util;
|
||||||
struct c_passwd {
|
#[path = "../common/c_types.rs"] mod c_types;
|
||||||
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 */
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
mod audit {
|
mod audit {
|
||||||
|
@ -87,7 +84,38 @@ extern {
|
||||||
ngroups: *mut c_int) -> c_int;
|
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 () {
|
fn main () {
|
||||||
let args = os::args();
|
let args = os::args();
|
||||||
|
@ -108,13 +136,13 @@ fn main () {
|
||||||
let matches = match getopts(args_t, options) {
|
let matches = match getopts(args_t, options) {
|
||||||
Ok(m) => { m },
|
Ok(m) => { m },
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
println!("{:s}", usage(PROGRAM, options));
|
println!("{:s}", usage(NAME, options));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("h") {
|
if matches.opt_present("h") {
|
||||||
println!("{:s}", usage(PROGRAM, options));
|
println!("{:s}", usage(NAME, options));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,37 +152,7 @@ fn main () {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let possible_pw = if matches.free.len() == 1 {
|
let possible_pw = get_pw_from_args(&matches);
|
||||||
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 nflag = matches.opt_present("n");
|
let nflag = matches.opt_present("n");
|
||||||
let uflag = matches.opt_present("u");
|
let uflag = matches.opt_present("u");
|
||||||
|
@ -312,10 +310,6 @@ fn pline(possible_pw: Option<c_passwd>) {
|
||||||
pw_shell);
|
pw_shell);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn no_such_user(username: ~str) {
|
|
||||||
println!("{:s}: {:s}: no such user", PROGRAM, username.as_slice());
|
|
||||||
}
|
|
||||||
|
|
||||||
static NGROUPS: i32 = 20;
|
static NGROUPS: i32 = 20;
|
||||||
|
|
||||||
fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
||||||
|
|
|
@ -18,7 +18,7 @@ use std::os;
|
||||||
use std::io::fs;
|
use std::io::fs;
|
||||||
use std::num::strconv;
|
use std::num::strconv;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "mkdir";
|
static NAME: &'static str = "mkdir";
|
||||||
|
|
|
@ -19,7 +19,7 @@ extern crate getopts;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::print;
|
use std::io::print;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "printenv";
|
static NAME: &'static str = "printenv";
|
||||||
|
|
|
@ -17,7 +17,7 @@ extern crate getopts;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::print;
|
use std::io::print;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "pwd";
|
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::os;
|
||||||
use std::io::{print, stdin, stdio, fs, BufferedReader};
|
use std::io::{print, stdin, stdio, fs, BufferedReader};
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
#[deriving(Eq)]
|
#[deriving(Eq)]
|
||||||
|
|
|
@ -17,7 +17,7 @@ extern crate getopts;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{print, fs};
|
use std::io::{print, fs};
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "rmdir";
|
static NAME: &'static str = "rmdir";
|
||||||
|
|
|
@ -11,7 +11,7 @@ extern crate getopts;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::cmp::max;
|
use std::cmp::max;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "seq";
|
static NAME: &'static str = "seq";
|
||||||
|
|
|
@ -19,7 +19,7 @@ use std::cast;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{print, timer};
|
use std::io::{print, timer};
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "sleep";
|
static NAME: &'static str = "sleep";
|
||||||
|
|
|
@ -18,7 +18,7 @@ use std::io::{File, Open, ReadWrite, fs};
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::u64;
|
use std::u64;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
macro_rules! get_file_size(
|
macro_rules! get_file_size(
|
||||||
|
|
|
@ -24,7 +24,7 @@ use std::io::println;
|
||||||
use std::io::stdio::stderr;
|
use std::io::stdio::stderr;
|
||||||
use getopts::{optflag,getopts};
|
use getopts::{optflag,getopts};
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
extern {
|
extern {
|
||||||
|
|
|
@ -27,7 +27,7 @@ use std::ptr;
|
||||||
use std::str;
|
use std::str;
|
||||||
use utmpx::*;
|
use utmpx::*;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[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 std::io::{print, stdin, File, BufferedReader};
|
||||||
use getopts::Matches;
|
use getopts::Matches;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
struct Result {
|
struct Result {
|
||||||
|
|
|
@ -22,14 +22,10 @@ use std::io::print;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::libc;
|
use std::libc;
|
||||||
|
use c_types::c_passwd;
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"] mod util;
|
||||||
mod util;
|
#[path = "../common/c_types.rs"] mod c_types;
|
||||||
|
|
||||||
struct c_passwd {
|
|
||||||
pw_name: *libc::c_char,
|
|
||||||
// Maybe I should put here others struct members, but...Well, maybe.
|
|
||||||
}
|
|
||||||
|
|
||||||
extern {
|
extern {
|
||||||
pub fn geteuid() -> libc::c_int;
|
pub fn geteuid() -> libc::c_int;
|
||||||
|
|
|
@ -19,7 +19,7 @@ extern crate getopts;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{print, println};
|
use std::io::{print, println};
|
||||||
|
|
||||||
#[path = "../util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
static NAME: &'static str = "yes";
|
static NAME: &'static str = "yes";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue