Skip to content

add util: write #149

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Because it is a FAQ, the major differences between this project and uutils are:
- [x] touch
- [x] tty
- [x] unlink
- [x] write

## Stage 1 - Rough draft

Expand Down Expand Up @@ -216,7 +217,6 @@ Because it is a FAQ, the major differences between this project and uutils are:
- [ ] talk
- [ ] time
- [ ] timeout
- [ ] write

## Testing

Expand Down
54 changes: 54 additions & 0 deletions plib/src/curuser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright (c) 2024 Jeff Garzik
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//

extern crate libc;
use std::ffi::CStr;

pub fn login_name() -> String {
let username = unsafe {
// Call getlogin to get the username as a *mut c_char
let c_str = libc::getlogin();

// Check if the pointer is not null
if c_str.is_null() {
panic!("Failed to get login name");
}

// Convert the *mut c_char to a &CStr
let c_str = CStr::from_ptr(c_str);

// Convert the &CStr to a Rust String
match c_str.to_str() {
Ok(s) => s.to_owned(), // Successfully converted CStr to Rust String
Err(e) => panic!("Failed to convert login name to a Rust String: {}", e),
}
};

username
}

pub fn tty() -> String {
// Try to get the tty name from STDIN, STDOUT, STDERR in that order
for fd in [libc::STDIN_FILENO, libc::STDOUT_FILENO, libc::STDERR_FILENO].iter() {
unsafe {
let c_str = libc::ttyname(*fd);

if !c_str.is_null() {
let c_str = CStr::from_ptr(c_str);

return match c_str.to_str() {
Ok(s) => s.to_owned(),
Err(e) => panic!("Failed to convert tty name to a Rust String: {}", e),
};
}
}
}

panic!("Failed to get tty name from any file descriptor");
}
1 change: 1 addition & 0 deletions plib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// SPDX-License-Identifier: MIT
//

pub mod curuser;
pub mod group;
pub mod io;
pub mod lzw;
Expand Down
7 changes: 6 additions & 1 deletion users/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ plib = { path = "../plib" }
clap.workspace = true
gettext-rs.workspace = true
libc.workspace = true
syslog = "6.1"
atty.workspace = true
chrono.workspace = true
syslog = "6.1"

[[bin]]
name = "id"
Expand All @@ -38,3 +39,7 @@ path = "src/pwd.rs"
name = "tty"
path = "src/tty.rs"

[[bin]]
name = "write"
path = "src/write.rs"

22 changes: 2 additions & 20 deletions users/src/logname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,10 @@
// SPDX-License-Identifier: MIT
//

extern crate libc;
use std::ffi::CStr;
extern crate plib;

fn main() {
let username = unsafe {
// Call getlogin to get the username as a *mut c_char
let c_str = libc::getlogin();

// Check if the pointer is not null
if c_str.is_null() {
panic!("Failed to get login name");
}

// Convert the *mut c_char to a &CStr
let c_str = CStr::from_ptr(c_str);

// Convert the &CStr to a Rust String
match c_str.to_str() {
Ok(s) => s.to_owned(), // Successfully converted CStr to Rust String
Err(e) => panic!("Failed to convert login name to a Rust String: {}", e),
}
};
let username = plib::curuser::login_name();

println!("{}", username);
}
22 changes: 2 additions & 20 deletions users/src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
//

extern crate atty;
extern crate libc;
use std::ffi::CStr;
extern crate plib;

fn main() {
let is_tty = atty::is(atty::Stream::Stdin);
Expand All @@ -18,24 +17,7 @@ fn main() {
std::process::exit(1);
}

let ttyname = unsafe {
// Call getlogin to get the username as a *mut c_char
let c_str = libc::ttyname(libc::STDIN_FILENO);

// Check if the pointer is not null
if c_str.is_null() {
panic!("Failed to get tty name");
}

// Convert the *mut c_char to a &CStr
let c_str = CStr::from_ptr(c_str);

// Convert the &CStr to a Rust String
match c_str.to_str() {
Ok(s) => s.to_owned(), // Successfully converted CStr to Rust String
Err(e) => panic!("Failed to convert tty name to a Rust String: {}", e),
}
};
let ttyname = plib::curuser::tty();

println!("{}", ttyname);
}
Loading