Skip to content

Commit

Permalink
file: Use compile-time macro env!
Browse files Browse the repository at this point in the history
  • Loading branch information
fox0 committed Oct 26, 2024
1 parent 48a2c01 commit 1a6247c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 71 deletions.
21 changes: 11 additions & 10 deletions file/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
// - Questionable behavior: if write_all() produces Err, the program will
// continue to the next file, rather than stopping.

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::io::{self, Read, Write};
use std::path::PathBuf;

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::io::input_stream;
use plib::BUFSZ;

#[derive(Parser)]
#[command(version, about = gettext("cat - concatenate and print files"))]
struct Args {
Expand All @@ -34,8 +36,8 @@ struct Args {
}

fn cat_file(pathname: &PathBuf) -> io::Result<()> {
let mut file = plib::io::input_stream(pathname, true)?;
let mut buffer = [0; plib::BUFSZ];
let mut file = input_stream(pathname, true)?;
let mut buffer = [0; BUFSZ];

loop {
let n_read = file.read(&mut buffer[..])?;
Expand All @@ -50,12 +52,11 @@ fn cat_file(pathname: &PathBuf) -> io::Result<()> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// parse command line arguments
let mut args = Args::parse();

setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;

let mut args = Args::parse();

// if no file args, read from stdin
if args.files.is_empty() {
Expand Down
20 changes: 10 additions & 10 deletions file/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
// SPDX-License-Identifier: MIT
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::io::{self, ErrorKind, Read};
use std::path::PathBuf;
use std::process::ExitCode;

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::io::input_reader;

#[derive(Parser)]
#[command(version, about = gettext("cmp - compare two files"))]
struct Args {
Expand Down Expand Up @@ -76,8 +77,8 @@ fn cmp_main(args: &Args) -> io::Result<u8> {
return Ok(0);
}

let mut reader1 = plib::io::input_reader(&args.file1, true)?;
let mut reader2 = plib::io::input_reader(&args.file2, true)?;
let mut reader1 = input_reader(&args.file1, true)?;
let mut reader2 = input_reader(&args.file2, true)?;

let mut lines: u64 = 1;
let mut bytes: u64 = 0;
Expand Down Expand Up @@ -135,12 +136,11 @@ fn cmp_main(args: &Args) -> io::Result<u8> {
}

fn main() -> ExitCode {
let args = Args::parse();

// Initialize translation system
setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME).unwrap();
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();
textdomain(env!("PROJECT_NAME")).unwrap();
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8").unwrap();

let mut args = Args::parse();

match cmp_main(&args) {
Ok(x) => ExitCode::from(x),
Expand Down
8 changes: 4 additions & 4 deletions file/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// SPDX-License-Identifier: MIT
//

use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, Read, Write};

use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};

const DEF_BLOCK_SIZE: usize = 512;

const CONV_ASCII_IBM: [u8; 256] = [
Expand Down Expand Up @@ -383,8 +383,8 @@ fn parse_cmdline(args: &[String]) -> Result<Config, Box<dyn std::error::Error>>

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

let args: Vec<String> = std::env::args().skip(1).collect();
let config = parse_cmdline(&args)?;
Expand Down
23 changes: 10 additions & 13 deletions file/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@

mod magic;

use crate::magic::{get_type_from_magic_file_dbs, DEFAULT_MAGIC_FILE};
use std::fs::read_link;
use std::os::unix::fs::FileTypeExt;
use std::path::PathBuf;
use std::{fs, io};

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::{
fs::{self, read_link},
io,
os::unix::fs::FileTypeExt,
path::PathBuf,
};

use crate::magic::{get_type_from_magic_file_dbs, DEFAULT_MAGIC_FILE};

#[derive(Parser)]
#[command(
Expand Down Expand Up @@ -192,12 +190,11 @@ fn analyze_file(mut path: String, args: &Args, magic_files: &Vec<PathBuf>) {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();

// Initialize translation system
setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME).unwrap();
bind_textdomain_codeset(PROJECT_NAME, "UTF-8").unwrap();
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;

let args = Args::parse();

let magic_files = get_magic_files(&args);

Expand Down
11 changes: 6 additions & 5 deletions file/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
// SPDX-License-Identifier: MIT
//

use gettextrs::{bind_textdomain_codeset, textdomain};
use plib::PROJECT_NAME;
use regex::Regex;
use std::collections::HashSet;
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
use std::path::PathBuf;
use std::{env, fs};

use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use regex::Regex;
use walkdir::{DirEntry, WalkDir};

#[derive(Clone)]
Expand Down Expand Up @@ -484,8 +484,9 @@ fn find(args: Vec<String>) -> Result<(), String> {
}

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

let args: Vec<String> = env::args().collect();

Expand Down
16 changes: 9 additions & 7 deletions file/od.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//
//
use crate::io::ErrorKind;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;

use std::fs::File;
use std::io::{self, BufReader, Error, Read, Seek, SeekFrom};
use std::num::ParseIntError;
use std::path::PathBuf;
use std::slice::Chunks;
use std::str::FromStr;

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};

use crate::io::ErrorKind;

#[derive(Parser)]
#[command(version, about = gettext("od - dump files in octal and other formats"))]
struct Args {
Expand Down Expand Up @@ -1138,10 +1139,11 @@ fn od(args: &Args) -> Result<(), Box<dyn std::error::Error>> {

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

let mut args = Args::parse();

args.validate_args()?;
let mut exit_code = 0;

Expand Down
23 changes: 12 additions & 11 deletions file/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
// SPDX-License-Identifier: MIT
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::PROJECT_NAME;
use std::cmp;
use std::fs::{File, OpenOptions};
use std::io::{self, BufRead, Error, ErrorKind, Read, Write};
use std::path::PathBuf;

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::io::{input_reader, input_stream};
use plib::BUFSZ;

#[derive(Parser)]
#[command(version, about = gettext("split - split a file into pieces"))]
struct Args {
Expand Down Expand Up @@ -213,8 +215,8 @@ fn split_by_bytes(args: &Args, bytesplit: String) -> io::Result<()> {
};

// open file, or stdin
let mut file = plib::io::input_stream(&args.file, false)?;
let mut raw_buffer = [0; plib::BUFSZ];
let mut file = input_stream(&args.file, false)?;
let mut raw_buffer = [0; BUFSZ];
let mut state = OutputState::new(&args.prefix, boundary, args.suffix_len);

loop {
Expand All @@ -237,7 +239,7 @@ fn split_by_lines(args: &Args, linesplit: u64) -> io::Result<()> {
assert!(linesplit > 0);

// open file, or stdin
let mut reader = plib::io::input_reader(&args.file, false)?;
let mut reader = input_reader(&args.file, false)?;
let mut state = OutputState::new(&args.prefix, linesplit, args.suffix_len);

loop {
Expand All @@ -258,12 +260,11 @@ fn split_by_lines(args: &Args, linesplit: u64) -> io::Result<()> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// parse command line arguments
let mut args = Args::parse();

setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;

let mut args = Args::parse();

if args.lines.is_none() && args.bytes.is_none() {
args.lines = Some(1000);
Expand Down
21 changes: 10 additions & 11 deletions file/tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
// SPDX-License-Identifier: MIT
//

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use libc::{signal, SIGINT, SIG_IGN};
use plib::PROJECT_NAME;
use std::fs::{File, OpenOptions};
use std::io::{self, Read, Write};

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, setlocale, textdomain, LocaleCategory};
use plib::BUFSZ;

#[derive(Parser)]
#[command(version, about = gettext("tee - duplicate standard input"))]
struct Args {
Expand Down Expand Up @@ -65,7 +65,7 @@ fn open_outputs(args: &Args, info: &mut TeeInfo) -> io::Result<()> {
}

fn tee_stdin(info: &mut TeeInfo) -> io::Result<()> {
let mut buffer = [0; plib::BUFSZ];
let mut buffer = [0; BUFSZ];

loop {
let n_read_res = io::stdin().read(&mut buffer[..]);
Expand Down Expand Up @@ -93,16 +93,15 @@ fn tee_stdin(info: &mut TeeInfo) -> io::Result<()> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// parse command line arguments
let args = Args::parse();

setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
textdomain(env!("PROJECT_NAME"))?;
bind_textdomain_codeset(env!("PROJECT_NAME"), "UTF-8")?;

let args = Args::parse();

if args.ignore {
unsafe {
signal(SIGINT, SIG_IGN);
libc::signal(libc::SIGINT, libc::SIG_IGN);
}
}

Expand Down

0 comments on commit 1a6247c

Please sign in to comment.