Skip to content

Commit 152b265

Browse files
committed
users: Use compile-time macro env!
1 parent 029be79 commit 152b265

File tree

8 files changed

+34
-48
lines changed

8 files changed

+34
-48
lines changed

users/id.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use clap::Parser;
1414
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
15-
use plib::PROJECT_NAME;
15+
use plib::group;
1616
use std::collections::HashMap;
1717
use std::io::Error;
1818

@@ -122,7 +122,7 @@ fn get_user_info(args: &Args) -> Result<UserInfo, Box<dyn std::error::Error>> {
122122
}
123123

124124
fn get_group_info(userinfo: &mut UserInfo) -> Result<(), Box<dyn std::error::Error>> {
125-
let groups = plib::group::load();
125+
let groups = group::load();
126126

127127
for group in &groups {
128128
// skip groups that the user is not a member of
@@ -193,12 +193,11 @@ fn display_user_info(args: &Args, userinfo: &UserInfo) {
193193
}
194194

195195
fn main() -> Result<(), Box<dyn std::error::Error>> {
196-
// parse command line arguments
197-
let args = Args::parse();
198-
199196
setlocale(LocaleCategory::LcAll, "");
200-
textdomain(PROJECT_NAME)?;
201-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
197+
textdomain(env!("PROJECT_NAME"))?;
198+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
199+
200+
let args = Args::parse();
202201

203202
let mut userinfo = get_user_info(&args)?;
204203
get_group_info(&mut userinfo)?;

users/logger.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
//
99

1010
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
11-
use plib::PROJECT_NAME;
1211
use syslog::{Facility, Formatter3164};
1312

1413
fn main() -> Result<(), Box<dyn std::error::Error>> {
1514
setlocale(LocaleCategory::LcAll, "");
16-
textdomain(PROJECT_NAME)?;
17-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
15+
textdomain(env!("PROJECT_NAME"))?;
16+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
1817

1918
let mut args: Vec<String> = std::env::args().collect();
2019
args.remove(0);

users/logname.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10+
use plib::curuser::login_name;
11+
1012
fn main() {
11-
let username = plib::curuser::login_name();
13+
let username = login_name();
1214

1315
println!("{}", username);
1416
}

users/mesg.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use clap::Parser;
1515
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
16-
use plib::PROJECT_NAME;
1716
use std::io::{self, Error, ErrorKind, IsTerminal};
1817
use std::mem;
1918

@@ -121,12 +120,11 @@ fn set_mesg(fd: i32, st: libc::stat, setting: &str) -> io::Result<()> {
121120
}
122121

123122
fn main() -> Result<(), Box<dyn std::error::Error>> {
124-
// parse command line arguments
125-
let args = Args::parse();
126-
127123
setlocale(LocaleCategory::LcAll, "");
128-
textdomain(PROJECT_NAME)?;
129-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
124+
textdomain(env!("PROJECT_NAME"))?;
125+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
126+
127+
let args = Args::parse();
130128

131129
let (fd, stat) = stat_tty()?;
132130

users/pwd.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use clap::Parser;
1414
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
15-
use plib::PROJECT_NAME;
1615
use std::ffi::OsStr;
1716
use std::path::{Component, Path};
1817

@@ -53,12 +52,11 @@ fn dirname_valid(name: &OsStr) -> bool {
5352
}
5453

5554
fn main() -> Result<(), Box<dyn std::error::Error>> {
56-
// parse command line arguments
57-
let args = Args::parse();
58-
5955
setlocale(LocaleCategory::LcAll, "");
60-
textdomain(PROJECT_NAME)?;
61-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
56+
textdomain(env!("PROJECT_NAME"))?;
57+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
58+
59+
let args = Args::parse();
6260

6361
let mut cwd = std::env::current_dir()?.into_os_string();
6462
if args.env {

users/talk.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use clap::{error::ErrorKind, Parser};
1010
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
11-
use plib::PROJECT_NAME;
1211
use thiserror::Error;
1312

1413
use binrw::{binrw, BinReaderExt, BinWrite, Endian};
@@ -1721,6 +1720,10 @@ fn get_terminal_size() -> (u16, u16) {
17211720
}
17221721

17231722
fn main() -> Result<(), Box<dyn std::error::Error>> {
1723+
setlocale(LocaleCategory::LcAll, "");
1724+
textdomain(env!("PROJECT_NAME"))?;
1725+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
1726+
17241727
let args = Args::try_parse().unwrap_or_else(|err| {
17251728
if err.kind() == ErrorKind::DisplayHelp || err.kind() == ErrorKind::DisplayVersion {
17261729
// Print help or version message
@@ -1733,9 +1736,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
17331736
// Exit with a non-zero status code
17341737
std::process::exit(1);
17351738
});
1736-
setlocale(LocaleCategory::LcAll, "");
1737-
textdomain(PROJECT_NAME)?;
1738-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
17391739

17401740
let mut exit_code = 0;
17411741

users/tty.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10+
use plib::curuser::tty;
1011
use std::io::{self, IsTerminal};
1112

1213
fn main() {
@@ -16,7 +17,7 @@ fn main() {
1617
std::process::exit(1);
1718
}
1819

19-
let ttyname = plib::curuser::tty();
20+
let ttyname = tty();
2021

2122
println!("{}", ttyname);
2223
}

users/write.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
use chrono::Local;
1111
use clap::Parser;
1212
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
13-
use plib::{platform, PROJECT_NAME};
14-
use std::fs;
15-
use std::fs::OpenOptions;
13+
use plib::{curuser, platform, utmpx};
14+
use std::fs::{self, OpenOptions};
1615
use std::io::{self, BufRead, Write};
1716
use std::os::unix::fs::MetadataExt;
1817
use std::os::unix::fs::PermissionsExt;
@@ -38,7 +37,7 @@ struct Args {
3837
// Select terminal in an implementation-defined manner and return terminal
3938
// Print an informational message about the chosen terminal
4039
fn select_terminal(user_name: &str) -> String {
41-
let entries = plib::utmpx::load();
40+
let entries = utmpx::load();
4241

4342
// Filter the entries to find terminals for the specified user
4443
let user_entries: Vec<_> = entries
@@ -106,15 +105,6 @@ fn check_write_permission(terminal: &str) -> bool {
106105
}
107106
}
108107

109-
// Retrieve the sender's login ID
110-
fn get_login_id() -> String {
111-
plib::curuser::login_name()
112-
}
113-
114-
fn get_terminal() -> String {
115-
plib::curuser::tty()
116-
}
117-
118108
fn get_current_date() -> String {
119109
// Retrieve the current date and time in a human-readable format
120110
let now = Local::now();
@@ -213,12 +203,11 @@ fn process_non_printable(line: &str) -> String {
213203
}
214204

215205
fn main() -> Result<(), Box<dyn std::error::Error>> {
216-
// parse command line arguments
217-
let args = Args::parse();
218-
219206
setlocale(LocaleCategory::LcAll, "");
220-
textdomain(PROJECT_NAME)?;
221-
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
207+
textdomain(env!("PROJECT_NAME"))?;
208+
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;
209+
210+
let args = Args::parse();
222211

223212
let user_name = args.username;
224213
let terminal = match args.terminal {
@@ -237,8 +226,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
237226
exit(1);
238227
}
239228

240-
let sender_login_id = get_login_id();
241-
let sending_terminal = get_terminal();
229+
let sender_login_id = curuser::login_name();
230+
let sending_terminal = curuser::tty();
242231
let date = get_current_date();
243232

244233
let message = format!(

0 commit comments

Comments
 (0)