-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from rustcoreutils/pwd
add util: pwd
- Loading branch information
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// 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 | ||
// | ||
// TODO: | ||
// - compliance: for -L mode, Rust performs unwanted normalization for "." | ||
// | ||
|
||
extern crate clap; | ||
extern crate plib; | ||
|
||
use clap::Parser; | ||
use gettextrs::{bind_textdomain_codeset, textdomain}; | ||
use plib::PROJECT_NAME; | ||
use std::ffi::OsStr; | ||
use std::path::{Component, Path}; | ||
|
||
const PWD_ENV: &'static str = "PWD"; | ||
|
||
/// pwd - return working directory name | ||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about)] | ||
struct Args { | ||
/// Count number of bytes in each file | ||
#[arg(short = 'L', long)] | ||
env: bool, | ||
|
||
/// Count number of bytes in each file | ||
#[arg(short = 'P', long)] | ||
process: bool, | ||
} | ||
|
||
fn dirname_valid(name: &OsStr) -> bool { | ||
let path = Path::new(name); | ||
|
||
let mut first = true; | ||
for component in path.components() { | ||
if first { | ||
first = false; | ||
|
||
if component != Component::RootDir { | ||
return false; | ||
} | ||
} else { | ||
if component == Component::CurDir || component == Component::ParentDir { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
true | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// parse command line arguments | ||
let args = Args::parse(); | ||
|
||
textdomain(PROJECT_NAME)?; | ||
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; | ||
|
||
let mut cwd = std::env::current_dir()?.into_os_string(); | ||
if args.env { | ||
if let Some(dir) = std::env::var_os(PWD_ENV) { | ||
if dirname_valid(&dir) { | ||
cwd = dir.clone(); | ||
} | ||
} | ||
} | ||
|
||
println!("{}", cwd.to_string_lossy()); | ||
|
||
Ok(()) | ||
} |