mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 21:17:46 +00:00
c_str_to_bytes -> CStr::from_ptr
This commit is contained in:
parent
eb6594cc91
commit
a5547507af
8 changed files with 39 additions and 39 deletions
|
@ -16,7 +16,7 @@ extern crate libc;
|
||||||
use getopts::{optflag, optopt, getopts, usage};
|
use getopts::{optflag, optopt, getopts, usage};
|
||||||
use c_types::{get_pw_from_args, get_group};
|
use c_types::{get_pw_from_args, get_group};
|
||||||
use libc::funcs::posix88::unistd::{execvp, setuid, setgid};
|
use libc::funcs::posix88::unistd::{execvp, setuid, setgid};
|
||||||
use std::ffi::{c_str_to_bytes, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use std::old_io::fs::PathExtensions;
|
use std::old_io::fs::PathExtensions;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
|
|
||||||
|
@ -196,7 +196,7 @@ fn set_user(user: &str) {
|
||||||
fn strerror(errno: i32) -> String {
|
fn strerror(errno: i32) -> String {
|
||||||
unsafe {
|
unsafe {
|
||||||
let err = libc::funcs::c95::string::strerror(errno) as *const libc::c_char;
|
let err = libc::funcs::c95::string::strerror(errno) as *const libc::c_char;
|
||||||
let bytes= c_str_to_bytes(&err);
|
let bytes= CStr::from_ptr(err).to_bytes();
|
||||||
String::from_utf8_lossy(bytes).to_string()
|
String::from_utf8_lossy(bytes).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ use self::libc::int32_t;
|
||||||
|
|
||||||
use self::libc::funcs::posix88::unistd::getgroups;
|
use self::libc::funcs::posix88::unistd::getgroups;
|
||||||
|
|
||||||
use std::ffi::{c_str_to_bytes, CString};
|
use std::ffi::{CStr, CString};
|
||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
|
|
||||||
|
@ -231,7 +231,7 @@ pub fn group(possible_pw: Option<c_passwd>, nflag: bool) {
|
||||||
if !group.is_null() {
|
if !group.is_null() {
|
||||||
let name = unsafe {
|
let name = unsafe {
|
||||||
let gname = read(group).gr_name;
|
let gname = read(group).gr_name;
|
||||||
let bytes= c_str_to_bytes(&gname);
|
let bytes= CStr::from_ptr(gname).to_bytes();
|
||||||
String::from_utf8_lossy(bytes).to_string()
|
String::from_utf8_lossy(bytes).to_string()
|
||||||
};
|
};
|
||||||
print!("{} ", name);
|
print!("{} ", name);
|
||||||
|
|
50
src/id/id.rs
50
src/id/id.rs
|
@ -18,7 +18,7 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::ffi::c_str_to_bytes;
|
use std::ffi::CStr;
|
||||||
use std::ptr::read;
|
use std::ptr::read;
|
||||||
use libc::{
|
use libc::{
|
||||||
uid_t,
|
uid_t,
|
||||||
|
@ -139,7 +139,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let gr = unsafe { getgrgid(id) };
|
let gr = unsafe { getgrgid(id) };
|
||||||
|
|
||||||
if nflag && !gr.is_null() {
|
if nflag && !gr.is_null() {
|
||||||
let gr_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(gr).gr_name)).to_string() };
|
let gr_name = unsafe { String::from_utf8_lossy(CStr::from_ptr(read(gr).gr_name).to_bytes()).to_string() };
|
||||||
println!("{}", gr_name);
|
println!("{}", gr_name);
|
||||||
} else {
|
} else {
|
||||||
println!("{}", id);
|
println!("{}", id);
|
||||||
|
@ -159,7 +159,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let pw = unsafe { getpwuid(id) };
|
let pw = unsafe { getpwuid(id) };
|
||||||
if nflag && !pw.is_null() {
|
if nflag && !pw.is_null() {
|
||||||
let pw_name = unsafe {
|
let pw_name = unsafe {
|
||||||
String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string()
|
String::from_utf8_lossy(CStr::from_ptr(read(pw).pw_name).to_bytes()).to_string()
|
||||||
};
|
};
|
||||||
println!("{}", pw_name);
|
println!("{}", pw_name);
|
||||||
} else {
|
} else {
|
||||||
|
@ -197,16 +197,16 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
||||||
if possible_pw.is_some() {
|
if possible_pw.is_some() {
|
||||||
let pw = possible_pw.unwrap();
|
let pw = possible_pw.unwrap();
|
||||||
|
|
||||||
let pw_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_name)).to_string() };
|
let pw_name = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_name).to_bytes()).to_string() };
|
||||||
print!("uid\t{}\ngroups\t", pw_name);
|
print!("uid\t{}\ngroups\t", pw_name);
|
||||||
group(possible_pw, true);
|
group(possible_pw, true);
|
||||||
} else {
|
} else {
|
||||||
let login = unsafe { String::from_utf8_lossy(c_str_to_bytes(&(getlogin() as *const i8))).to_string() };
|
let login = unsafe { String::from_utf8_lossy(CStr::from_ptr((getlogin() as *const i8)).to_bytes()).to_string() };
|
||||||
let rid = unsafe { getuid() };
|
let rid = unsafe { getuid() };
|
||||||
let pw = unsafe { getpwuid(rid) };
|
let pw = unsafe { getpwuid(rid) };
|
||||||
|
|
||||||
let is_same_user = unsafe {
|
let is_same_user = unsafe {
|
||||||
String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() == login
|
String::from_utf8_lossy(CStr::from_ptr(read(pw).pw_name).to_bytes()).to_string() == login
|
||||||
};
|
};
|
||||||
|
|
||||||
if pw.is_null() || is_same_user {
|
if pw.is_null() || is_same_user {
|
||||||
|
@ -216,7 +216,7 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
||||||
if !pw.is_null() {
|
if !pw.is_null() {
|
||||||
println!(
|
println!(
|
||||||
"uid\t{}",
|
"uid\t{}",
|
||||||
unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() })
|
unsafe { String::from_utf8_lossy(CStr::from_ptr(read(pw).pw_name).to_bytes()).to_string() })
|
||||||
} else {
|
} else {
|
||||||
println!("uid\t{}\n", rid);
|
println!("uid\t{}\n", rid);
|
||||||
}
|
}
|
||||||
|
@ -227,7 +227,7 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
||||||
if !pw.is_null() {
|
if !pw.is_null() {
|
||||||
println!(
|
println!(
|
||||||
"euid\t{}",
|
"euid\t{}",
|
||||||
unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() });
|
unsafe { String::from_utf8_lossy(CStr::from_ptr(read(pw).pw_name).to_bytes()).to_string() });
|
||||||
} else {
|
} else {
|
||||||
println!("euid\t{}", eid);
|
println!("euid\t{}", eid);
|
||||||
}
|
}
|
||||||
|
@ -240,7 +240,7 @@ fn pretty(possible_pw: Option<c_passwd>) {
|
||||||
if !gr.is_null() {
|
if !gr.is_null() {
|
||||||
println!(
|
println!(
|
||||||
"rgid\t{}",
|
"rgid\t{}",
|
||||||
unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(gr).gr_name)).to_string() });
|
unsafe { String::from_utf8_lossy(CStr::from_ptr(read(gr).gr_name).to_bytes()).to_string() });
|
||||||
} else {
|
} else {
|
||||||
println!("rgid\t{}", rid);
|
println!("rgid\t{}", rid);
|
||||||
}
|
}
|
||||||
|
@ -259,12 +259,12 @@ fn pline(possible_pw: Option<c_passwd>) {
|
||||||
possible_pw.unwrap()
|
possible_pw.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
let pw_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_name )).to_string()};
|
let pw_name = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_name ).to_bytes()).to_string()};
|
||||||
let pw_passwd = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_passwd)).to_string()};
|
let pw_passwd = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_passwd).to_bytes()).to_string()};
|
||||||
let pw_class = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_class )).to_string()};
|
let pw_class = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_class ).to_bytes()).to_string()};
|
||||||
let pw_gecos = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_gecos )).to_string()};
|
let pw_gecos = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_gecos ).to_bytes()).to_string()};
|
||||||
let pw_dir = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_dir )).to_string()};
|
let pw_dir = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_dir ).to_bytes()).to_string()};
|
||||||
let pw_shell = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_shell )).to_string()};
|
let pw_shell = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_shell ).to_bytes()).to_string()};
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
|
"{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
|
||||||
|
@ -288,11 +288,11 @@ fn pline(possible_pw: Option<c_passwd>) {
|
||||||
possible_pw.unwrap()
|
possible_pw.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
let pw_name = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_name )).to_string()};
|
let pw_name = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_name ).to_bytes()).to_string()};
|
||||||
let pw_passwd = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_passwd)).to_string()};
|
let pw_passwd = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_passwd).to_bytes()).to_string()};
|
||||||
let pw_gecos = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_gecos )).to_string()};
|
let pw_gecos = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_gecos ).to_bytes()).to_string()};
|
||||||
let pw_dir = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_dir )).to_string()};
|
let pw_dir = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_dir ).to_bytes()).to_string()};
|
||||||
let pw_shell = unsafe { String::from_utf8_lossy(c_str_to_bytes(&pw.pw_shell )).to_string()};
|
let pw_shell = unsafe { String::from_utf8_lossy(CStr::from_ptr(pw.pw_shell ).to_bytes()).to_string()};
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{}:{}:{}:{}:{}:{}:{}",
|
"{}:{}:{}:{}:{}:{}:{}",
|
||||||
|
@ -352,7 +352,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
||||||
print!(
|
print!(
|
||||||
"uid={}({})",
|
"uid={}({})",
|
||||||
uid,
|
uid,
|
||||||
unsafe { String::from_utf8_lossy(c_str_to_bytes(&possible_pw.unwrap().pw_name)).to_string() });
|
unsafe { String::from_utf8_lossy(CStr::from_ptr(possible_pw.unwrap().pw_name).to_bytes()).to_string() });
|
||||||
} else {
|
} else {
|
||||||
print!("uid={}", unsafe { getuid() });
|
print!("uid={}", unsafe { getuid() });
|
||||||
}
|
}
|
||||||
|
@ -362,7 +362,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
||||||
if !gr.is_null() {
|
if !gr.is_null() {
|
||||||
print!(
|
print!(
|
||||||
"({})",
|
"({})",
|
||||||
unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(gr).gr_name)).to_string() });
|
unsafe { String::from_utf8_lossy(CStr::from_ptr(read(gr).gr_name).to_bytes()).to_string() });
|
||||||
}
|
}
|
||||||
|
|
||||||
let euid = unsafe { geteuid() };
|
let euid = unsafe { geteuid() };
|
||||||
|
@ -372,7 +372,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
||||||
if !pw.is_null() {
|
if !pw.is_null() {
|
||||||
print!(
|
print!(
|
||||||
"({})",
|
"({})",
|
||||||
unsafe { String::from_utf8_lossy(c_str_to_bytes(&read(pw).pw_name)).to_string() });
|
unsafe { String::from_utf8_lossy(CStr::from_ptr(read(pw).pw_name).to_bytes()).to_string() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -382,7 +382,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
||||||
unsafe {
|
unsafe {
|
||||||
let grp = getgrgid(egid);
|
let grp = getgrgid(egid);
|
||||||
if !grp.is_null() {
|
if !grp.is_null() {
|
||||||
print!("({})", String::from_utf8_lossy(c_str_to_bytes(&read(grp).gr_name)).to_string());
|
print!("({})", String::from_utf8_lossy(CStr::from_ptr(read(grp).gr_name).to_bytes()).to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -397,7 +397,7 @@ fn id_print(possible_pw: Option<c_passwd>,
|
||||||
let group = unsafe { getgrgid(gr) };
|
let group = unsafe { getgrgid(gr) };
|
||||||
if !group.is_null() {
|
if !group.is_null() {
|
||||||
let name = unsafe {
|
let name = unsafe {
|
||||||
String::from_utf8_lossy(c_str_to_bytes(&read(group).gr_name)).to_string()
|
String::from_utf8_lossy(CStr::from_ptr(read(group).gr_name).to_bytes()).to_string()
|
||||||
};
|
};
|
||||||
print!("({})", name);
|
print!("({})", name);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::ffi::c_str_to_bytes;
|
use std::ffi::CStr;
|
||||||
use std::old_io::print;
|
use std::old_io::print;
|
||||||
use libc::c_char;
|
use libc::c_char;
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ fn get_userlogin() -> Option<String> {
|
||||||
if login.is_null() {
|
if login.is_null() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(String::from_utf8_lossy(c_str_to_bytes(&login)).to_string())
|
Some(String::from_utf8_lossy(CStr::from_ptr(login).to_bytes()).to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::ffi::c_str_to_bytes;
|
use std::ffi::CStr;
|
||||||
use std::old_io::println;
|
use std::old_io::println;
|
||||||
use std::old_io::stdio::stderr;
|
use std::old_io::stdio::stderr;
|
||||||
use getopts::{optflag,getopts};
|
use getopts::{optflag,getopts};
|
||||||
|
@ -52,7 +52,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let tty = unsafe {
|
let tty = unsafe {
|
||||||
let ptr = ttyname(libc::STDIN_FILENO);
|
let ptr = ttyname(libc::STDIN_FILENO);
|
||||||
if !ptr.is_null() {
|
if !ptr.is_null() {
|
||||||
String::from_utf8_lossy(c_str_to_bytes(&ptr)).to_string()
|
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string()
|
||||||
} else {
|
} else {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::ffi::c_str_to_bytes;
|
use std::ffi::CStr;
|
||||||
use std::mem::uninitialized;
|
use std::mem::uninitialized;
|
||||||
use std::old_io::print;
|
use std::old_io::print;
|
||||||
use c_types::utsname;
|
use c_types::utsname;
|
||||||
|
@ -38,7 +38,7 @@ extern {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn string_from_c_str(ptr: *const i8) -> String {
|
unsafe fn string_from_c_str(ptr: *const i8) -> String {
|
||||||
String::from_utf8_lossy(c_str_to_bytes(&ptr)).to_string()
|
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn getuname() -> utsrust {
|
unsafe fn getuname() -> utsrust {
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::ffi::{CString, c_str_to_bytes};
|
use std::ffi::{CStr, CString};
|
||||||
use std::old_io::print;
|
use std::old_io::print;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
@ -107,7 +107,7 @@ fn exec(filename: &str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*line).ut_type == USER_PROCESS {
|
if (*line).ut_type == USER_PROCESS {
|
||||||
let user = String::from_utf8_lossy(c_str_to_bytes(mem::transmute(&(*line).ut_user))).to_string();
|
let user = String::from_utf8_lossy(CStr::from_ptr(mem::transmute(&(*line).ut_user)).to_bytes()).to_string();
|
||||||
users.push(user);
|
users.push(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ mod platform {
|
||||||
let passwd: *const c_passwd = getpwuid(geteuid());
|
let passwd: *const c_passwd = getpwuid(geteuid());
|
||||||
|
|
||||||
let pw_name: *const libc::c_char = (*passwd).pw_name;
|
let pw_name: *const libc::c_char = (*passwd).pw_name;
|
||||||
String::from_utf8_lossy(::std::ffi::c_str_to_bytes(&pw_name)).to_string()
|
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(pw_name).to_bytes()).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ mod platform {
|
||||||
if !GetUserNameA(buffer.as_mut_ptr(), &mut (buffer.len() as libc::uint32_t)) == 0 {
|
if !GetUserNameA(buffer.as_mut_ptr(), &mut (buffer.len() as libc::uint32_t)) == 0 {
|
||||||
crash!(1, "username is too long");
|
crash!(1, "username is too long");
|
||||||
}
|
}
|
||||||
String::from_utf8_lossy(::std::ffi::c_str_to_bytes(&buffer.as_ptr())).to_string()
|
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(buffer.as_ptr()).to_bytes()).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue