whoami-0.8.1/.gitignore010066400017500001750000000000521357721230600132220ustar0000000000000000**/target/ **/*.rs.bk Cargo.lock /ignore/ whoami-0.8.1/.rustfmt.toml010066400017500001750000000000171352363353400137130ustar0000000000000000max_width = 80 whoami-0.8.1/.travis.yml010066400017500001750000000006071362430271600133470ustar0000000000000000language: rust cache: cargo directories: - $HOME/.cargo - $HOME/target before-cache: - rm -rf $HOME/.cargo/registry rust: - beta - nightly matrix: allow_failures: - rust: nightly fast_finish: true include: - os: linux rust: stable compiler: gcc - os: windows rust: stable - os: osx rust: stable whoami-0.8.1/CODEOFCONDUCT.md010066400017500001750000000125601357721603500135450ustar0000000000000000# Code of Conduct ## 1. Purpose A primary goal of this project is to be inclusive to the largest number of contributors, with the most varied and diverse backgrounds possible. As such, we are committed to providing a friendly, safe and welcoming environment for all, regardless of gender, sexual orientation, ability, ethnicity, socioeconomic status, and religion (or lack thereof). This code of conduct outlines our expectations for all those who participate in our community, as well as the consequences for unacceptable behavior. We invite all those who participate in this project to help us create safe and positive experiences for everyone. ## 2. Open Source Citizenship A supplemental goal of this Code of Conduct is to increase open source citizenship by encouraging participants to recognize and strengthen the relationships between our actions and their effects on our community. Communities mirror the societies in which they exist and positive action is essential to counteract the many forms of inequality and abuses of power that exist in society. If you see someone who is making an extra effort to ensure our community is welcoming, friendly, and encourages all participants to contribute to the fullest extent, we want to know. ## 3. Expected Behavior The following behaviors are expected and requested of all community members: * Participate in an authentic and active way. In doing so, you contribute to the health and longevity of this community. * Exercise consideration and respect in your speech and actions. * Attempt collaboration before conflict. * Refrain from demeaning, discriminatory, or harassing behavior and speech. * Be mindful of your surroundings and of your fellow participants. Alert community leaders if you notice a dangerous situation, someone in distress, or violations of this Code of Conduct, even if they seem inconsequential. * Remember that community event venues may be shared with members of the public; please be respectful to all patrons of these locations. ## 4. Unacceptable Behavior The following behaviors are considered harassment and are unacceptable within our community: * Violence, threats of violence or violent language directed against another person. * Sexist, racist, homophobic, transphobic, ableist or otherwise discriminatory jokes and language. * Posting or displaying sexually explicit or violent material. * Posting or threatening to post other people’s personally identifying information ("doxing"). * Personal insults, particularly those related to gender, sexual orientation, race, religion, or disability. * Inappropriate photography or recording. * Inappropriate physical contact. You should have someone’s consent before touching them. * Unwelcome sexual attention. This includes, sexualized comments or jokes; inappropriate touching, groping, and unwelcomed sexual advances. * Deliberate intimidation, stalking or following (online or in person). * Advocating for, or encouraging, any of the above behavior. * Sustained disruption of community events, including talks and presentations. ## 5. Consequences of Unacceptable Behavior Unacceptable behavior from any community member, including sponsors and those with decision-making authority, will not be tolerated. Anyone asked to stop unacceptable behavior is expected to comply immediately. If a community member engages in unacceptable behavior, the community organizers may take any action they deem appropriate, up to and including a temporary ban or permanent expulsion from the community without warning (and without refund in the case of a paid event). ## 6. Reporting Guidelines If you are subject to or witness unacceptable behavior, or have any other concerns, please notify a community organizer as soon as possible. jeronaldaron@gmail.com. Additionally, community organizers are available to help community members engage with local law enforcement or to otherwise help those experiencing unacceptable behavior feel safe. In the context of in-person events, organizers will also provide escorts as desired by the person experiencing distress. ## 7. Addressing Grievances If you feel you have been falsely or unfairly accused of violating this Code of Conduct, you should notify Plop Grizzly with a concise description of your grievance. Your grievance will be handled in accordance with our existing governing policies. ## 8. Scope We expect all community participants (contributors, paid or otherwise; sponsors; and other guests) to abide by this Code of Conduct in all community venues–online and in-person–as well as in all one-on-one communications pertaining to community business. This code of conduct and its related procedures also applies to unacceptable behavior occurring outside the scope of community activities when such behavior has the potential to adversely affect the safety and well-being of community members. ## 9. Contact info jeronlau@plopgrizzly.com ## 10. License and attribution This Code of Conduct is distributed under a [Creative Commons Attribution-ShareAlike license](http://creativecommons.org/licenses/by-sa/3.0/). Portions of text derived from the [Django Code of Conduct](https://www.djangoproject.com/conduct/) and the [Geek Feminism Anti-Harassment Policy](http://geekfeminism.wikia.com/wiki/Conference_anti-harassment/Policy). Retrieved on November 22, 2016 from [http://citizencodeofconduct.org/](http://citizencodeofconduct.org/) whoami-0.8.1/Cargo.toml.orig010066400017500001750000000014341362430222700141210ustar0000000000000000# WhoAmI # # Copyright © 2017-2020 Jeron Aldaron Lau. # Dual-licensed under either the MIT License or the Boost Software License, # Version 1.0. (See accompanying file LICENSE_BSL.txt or copy at # https://www.boost.org/LICENSE_1_0.txt, and accompanying file LICENSE_MIT.txt # or copy at https://opensource.org/licenses/MIT) [package] name = "whoami" version = "0.8.1" authors = [ "Jeron Aldaron Lau ", ] edition = "2018" license = "MIT OR BSL-1.0" documentation = "https://docs.rs/whoami" homepage = "https://libcala.github.io/whoami" repository = "https://github.com/libcala/whoami" readme = "README.md" description = "Retrieve the current user and environment." keywords = ["user", "username", "whoami", "platform", "detect"] categories = ["os"] [dependencies] whoami-0.8.1/Cargo.toml0000644000000017341362430303100104220ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "whoami" version = "0.8.1" authors = ["Jeron Aldaron Lau "] description = "Retrieve the current user and environment." homepage = "https://libcala.github.io/whoami" documentation = "https://docs.rs/whoami" readme = "README.md" keywords = ["user", "username", "whoami", "platform", "detect"] categories = ["os"] license = "MIT OR BSL-1.0" repository = "https://github.com/libcala/whoami" [dependencies] whoami-0.8.1/LICENSE_BSL.txt010066400017500001750000000025451362362716200135700ustar0000000000000000Boost Software License - Version 1.0 - August 17th, 2003 Copyright (c) 2017-2020 Jeron Aldaron Lau Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. whoami-0.8.1/LICENSE_MIT.txt010066400017500001750000000020671362362716200136000ustar0000000000000000MIT License Copyright (c) 2017-2020 Jeron Aldaron Lau Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. whoami-0.8.1/README.md010066400017500001750000000064431362362716200125250ustar0000000000000000![whoami](https://libcala.github.io/whoami/icon.svg) [![docs.rs](https://docs.rs/whoami/badge.svg)](https://docs.rs/whoami) [![build status](https://api.travis-ci.com/libcala/whoami.svg?branch=master)](https://travis-ci.com/libcala/whoami) [![crates.io](https://img.shields.io/crates/v/whoami.svg)](https://crates.io/crates/whoami) [![discord](https://img.shields.io/badge/discord-Cala%20Project-green.svg)](https://discord.gg/nXwF59K) [About](https://libcala.github.io/whoami) | [Source](https://github.com/libcala/whoami) | [Changelog](https://libcala.github.io/whoami/changelog) # WhoAmI Retrieve the current user and environment. ## Getting Started Using the whoami crate is super easy! All of the exported items are simple functions with no parameters that return either a `String` or enum. The following example shows how to use all of the functions: ```rust fn main() { print!( "user's full name whoami::user(): {}\n\ username whoami::username(): {}\n\ host's fancy name whoami::host(): {}\n\ hostname whoami::hostname(): {}\n\ platform whoami::platform(): {}\n\ operating system whoami::os(): {}\n\ desktop environment whoami::env(): {}\n\ ", whoami::user(), whoami::username(), whoami::host(), whoami::hostname(), whoami::platform(), whoami::os(), whoami::env(), ); } ``` ## Features * Get the user's full name * Get the user's username * Get the computer's hostname * Get the computer's fancy name * Get the computer's desktop environment * Get the computer's OS name and version * Get the computer's platform name * Works on Linux, Windows, Mac OS, and Web Assembly ## Binary [whome](https://crates.io/crates/whome): replacement of the `whoami` command that depends on this crate. ## TODO * Support iOS / Android / Nintendo Switch (and other consoles) / other OS's. # Contributing Contributors are always welcome! Whether it is a bug report, bug fix, feature request, feature implementation or whatever. Don't be shy about getting involved. I always make time to fix bugs, so usually a patched version of the library will be out soon after a report. Features take me longer, though. I'll also always listen to any design critiques you have. If you have any questions you can email me at jeronlau@plopgrizzly.com. Otherwise, here's a link to the [issues on GitHub](https://github.com/libcala/whoami/issues). And, as always, make sure to always follow the [code of conduct](https://github.com/libcala/whoami/blob/master/CODEOFCONDUCT.md). Happy coding! # License This repository is licensed under either of the following: - MIT License (MIT) - See accompanying file [LICENSE_MIT.txt](https://github.com/libcala/whoami/blob/master/LICENSE_MIT.txt) or copy at https://opensource.org/licenses/MIT - Boost Software License (BSL-1.0) - See accompanying file [LICENSE_BSL.txt](https://github.com/libcala/whoami/blob/master/LICENSE_BSL.txt) or copy at https://www.boost.org/LICENSE_1_0.txt at your option. ## Contribution Licensing Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above without any additional terms or conditions. whoami-0.8.1/_config.yml010066400017500001750000000000311352363353400133570ustar0000000000000000theme: jekyll-theme-slatewhoami-0.8.1/changelog.md010066400017500001750000000055501362430235100135040ustar0000000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://code.plopgrizzly.com/semver/). ## [0.8.1] - 2020-02-22 ### Fixed - Remove unnecessary use of `to_mut()` on `Cow`s returned from `String::from_utf8_lossy()`. ## [0.8.0] - 2020-02-21 ### Added - Detection for KDE desktop environment. ### Changed - Unknown desktop environment may now contain lowercase characters. ### Fixed - No longer unwraps in any place where bad data from the OS could cause a panic. ## [0.7.0] - 2019-12-21 ### Removed - `stdweb` dependency when targetting web assembly. ### Changed - All public enums now have the attribute `#[non_exhaustive]` and derive `Debug`. ### Fixed - Some out-of-date documentation ## [0.6.0] - 2019-10-25 ### Added - Web Assembly support. ### Removed - `Platform::Web` variant of enum, use `env()` if you need to. ### Changed - `platform()` is no longer a const fn (needed for wasm platform detection). ## [0.5.3] - 2019-07-18 ### Changed - Now uses a more modern Rust coding style (replace `::std::` with `std::`). - Now uses a more modern Rust coding style with `mem::MaybeUninit`. - `impl Display` for desktop environment now uses proper capitalization. - Don't depend on `libc` anymore. ### Fixed - Fancy Names not working on Windows - `user()` now uses Windows Display Name on Windows rather than the username. - `host()` now uses Windows Name DNS Fully Qualified rather than the hostname. ## [0.5.2] - 2019-05-12 ### Fixed - Fixed more broken links! ## [0.5.1] - 2019-05-12 ### Fixed - Clippy lint warning: change `expect(&format!("…"))` to `expect("…")` for optimization in 2 cases. - Fixed broken links ## [0.5.0] - 2019-03-17 ### Added - `Platform` enum with corresponding `platform()` function. - `Dive`, `Fuchsia`, and `Redox` to `DesktopEnv` enum. ### Changed - Started using `const fn` for some functions. ## [0.4.1] - 2019-01-12 ### Fixed - Fixed README errors. ## [0.4.0] - 2019-01-12 ### Added - MacOS support. ### Changed - `env` on Ubuntu now returns DesktopEnv::Ubuntu instead of DesktopEnv::Other("UBUNTU") - Split off the binary into `whome` (who me?) crate ## [0.3.0] - 2019-01-04 ### Added - Added more fallbacks. ### Changed - Rename realname -> user - Rename computer -> host ### Fixed - Fix typo for uknown -> unknown. ## [0.2.4] - 2018-12-04 ### Fixed - Works now on platforms that use u8 instead of i8 for chars (like ARM). ## [0.2.3] - 2018-11-26 ### Fixed - Trailing newline on Windows. ## [0.2.2] - 2018-06-02 ### Fixed - Typo in markdown. ## [0.2.1] - 2018-06-02 ### Fixed - Undefined behavior on Linux. ## [0.2.0] - 2017-12-28 ### Added - Windows support. ## [0.1.1] - 2017-08-04 ### Fixed - Something in the markdown. ## [0.1.0] - 2017-08-04 ### Added - Published to crates.io. whoami-0.8.1/examples/whoami-demo.rs010066400017500001750000000012571362362716200156360ustar0000000000000000fn main() { print!( "whoami {}\n\n\ User's Full Name whoami::user() {}\n\ Username whoami::username() {}\n\ Host's Fancy Name whoami::host() {}\n\ Hostname whoami::hostname() {}\n\ Platform whoami::platform() {}\n\ Operating System whoami::os() {}\n\ Desktop Environment whoami::env() {}\n\ ", env!("CARGO_PKG_VERSION"), whoami::user(), whoami::username(), whoami::host(), whoami::hostname(), whoami::platform(), whoami::os(), whoami::env(), ); } whoami-0.8.1/icon.svg010066400017500001750000000111331352363353400127060ustar0000000000000000 image/svg+xml whoami-0.8.1/src/lib.rs010066400017500001750000000115711362402537100131420ustar0000000000000000//! Crate for getting the user's username, realname and environment. //! //! ## Getting Started //! Using the whoami crate is super easy! All of the public items are simple //! functions with no parameters that return `String`s (with the exception of //! [`env()`](fn.env.html), and [`platform()`](fn.platform.html) which return //! enums). The following example shows how to use all of the functions: //! //! ```rust //! fn main() { //! print!( //! "user's full name whoami::user(): {}\n\ //! username whoami::username(): {}\n\ //! host's fancy name whoami::host(): {}\n\ //! hostname whoami::hostname(): {}\n\ //! platform whoami::platform(): {}\n\ //! operating system whoami::os(): {}\n\ //! desktop environment whoami::env(): {}\n\ //! ", //! whoami::user(), //! whoami::username(), //! whoami::host(), //! whoami::hostname(), //! whoami::platform(), //! whoami::os(), //! whoami::env(), //! ); //! } //! ``` #![warn(missing_docs)] #![doc( html_logo_url = "https://libcala.github.io/whoami/icon.svg", html_favicon_url = "https://libcala.github.io/whoami/icon.svg" )] /// Which Desktop Environment #[allow(missing_docs)] #[derive(Debug)] #[non_exhaustive] pub enum DesktopEnv { Gnome, Windows, Lxde, Openbox, Mate, Xfce, Kde, Cinnamon, I3, Mac, Ios, Android, Wasm, Console, Ubuntu, Dive, Fuchsia, Redox, Unknown(String), } impl std::fmt::Display for DesktopEnv { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use self::DesktopEnv::*; if let Unknown(_) = self { write!(f, "Unknown: ")?; } write!( f, "{}", match self { Gnome => "Gnome", Windows => "Windows", Lxde => "LXDE", Openbox => "Openbox", Mate => "Mate", Xfce => "XFCE", Kde => "KDE", Cinnamon => "Cinnamon", I3 => "I3", Mac => "Mac OS", Ios => "IOS", Android => "Android", Wasm => "Wasm", Console => "Console", Ubuntu => "Ubuntu", Dive => "Dive", Fuchsia => "Fuchsia", Redox => "Redox", Unknown(a) => &a, } ) } } /// Which Platform #[allow(missing_docs)] #[derive(Debug)] #[non_exhaustive] pub enum Platform { Linux, FreeBsd, Windows, MacOS, Ios, Android, Nintendo, Xbox, PlayStation, Dive, Fuchsia, Redox, Unknown(String), } impl std::fmt::Display for Platform { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use self::Platform::*; if let Unknown(_) = self { write!(f, "Unknown: ")?; } write!( f, "{}", match self { Linux => "Linux", FreeBsd => "Free BSD", Windows => "Windows", MacOS => "Mac OS", Ios => "iOS", Android => "Android", Nintendo => "Nintendo", Xbox => "XBox", PlayStation => "PlayStation", Dive => "Dive", Fuchsia => "Fuchsia", Redox => "Redox", Unknown(a) => a, } ) } } #[cfg(all(target_os = "windows", not(target_arch = "wasm32")))] mod windows; #[cfg(all(target_os = "windows", not(target_arch = "wasm32")))] use self::windows as native; #[cfg(target_arch = "wasm32")] mod wasm; #[cfg(target_arch = "wasm32")] use self::wasm as native; #[cfg(not(any(target_os = "windows", target_arch = "wasm32")))] mod unix; #[cfg(not(any(target_os = "windows", target_arch = "wasm32")))] use self::unix as native; /// Get the user's username. #[inline(always)] pub fn username() -> String { native::username() } /// Get the user's full name. #[inline(always)] pub fn user() -> String { native::realname() } /// Get the host device's (pretty) name. #[inline(always)] pub fn host() -> String { native::computer() } /// Get the host device's hostname. #[inline(always)] pub fn hostname() -> String { native::hostname() } /// Get the the operating system name and version. /// /// Example: "Windows 10" or "Fedora 26 (Workstation Edition)" #[inline(always)] pub fn os() -> String { native::os().unwrap_or_else(|| "Unknown".to_string()) } /// Get the desktop environment. /// /// Example: "gnome" or "windows" #[inline(always)] pub fn env() -> DesktopEnv { native::env() } /// Get the platform. #[inline(always)] pub fn platform() -> Platform { native::platform() } whoami-0.8.1/src/unix.rs010066400017500001750000000154211362430173700133600ustar0000000000000000use crate::{DesktopEnv, Platform}; use std::ffi::c_void; use std::mem; use std::process::Command; use std::process::Stdio; #[repr(C)] struct PassWd { pw_name: *const c_void, pw_passwd: *const c_void, pw_uid: u32, pw_gid: u32, #[cfg(target_os = "macos")] pw_change: isize, #[cfg(target_os = "macos")] pw_class: *const c_void, pw_gecos: *const c_void, pw_dir: *const c_void, pw_shell: *const c_void, #[cfg(target_os = "macos")] pw_expire: isize, #[cfg(target_os = "macos")] pw_fields: i32, } extern "system" { fn getpwuid_r( uid: u32, pwd: *mut PassWd, buf: *mut c_void, buflen: usize, result: *mut *mut PassWd, ) -> i32; fn geteuid() -> u32; fn strlen(cs: *const c_void) -> usize; fn gethostname(name: *mut c_void, len: usize) -> i32; } fn string_from_cstring(string: *const c_void) -> String { if string.is_null() { return "".to_string(); } // Get a byte slice of the c string. let slice = unsafe { let length = strlen(string); std::slice::from_raw_parts(string as *const u8, length) }; // Turn byte slice into Rust String. String::from_utf8_lossy(slice).to_string() } // This function must return `String`s, because a slice or Cow would still // reference `passwd` which is dropped when this function returns. #[inline(always)] fn getpwuid() -> (String, String) { const BUF_SIZE: usize = 16_384; // size from the man page let mut buffer = mem::MaybeUninit::<[u8; BUF_SIZE]>::uninit(); let mut passwd = mem::MaybeUninit::::uninit(); let mut _passwd = mem::MaybeUninit::<*mut PassWd>::uninit(); // Get PassWd `struct`. let passwd = unsafe { getpwuid_r( geteuid(), passwd.as_mut_ptr(), buffer.as_mut_ptr() as *mut c_void, BUF_SIZE, _passwd.as_mut_ptr(), ); passwd.assume_init() }; // Extract names. let a = string_from_cstring(passwd.pw_name); let b = string_from_cstring(passwd.pw_gecos); (a, b) } pub fn username() -> String { let pwent = getpwuid(); pwent.0 } fn fancy_fallback(mut computer: String, fallback_fn: fn() -> String) -> String { let mut cap = true; if computer.is_empty() { let fallback = fallback_fn(); for c in fallback.chars() { match c { '.' | '-' | '_' => { computer.push(' '); cap = true; } a => { if cap { cap = false; for i in a.to_uppercase() { computer.push(i); } } else { computer.push(a); } } } } } computer } pub fn realname() -> String { let pwent = getpwuid(); let realname = pwent.1; // If no real name is provided, guess based on username. fancy_fallback(realname, username) } pub fn computer() -> String { let mut computer = String::new(); let program = if cfg!(not(target_os = "macos")) { Command::new("hostnamectl") .arg("--pretty") .stdout(Stdio::piped()) .output() .expect("Couldn't Find `hostnamectl`") } else { Command::new("scutil") .arg("--get") .arg("ComputerName") .output() .expect("Couldn't find `scutil`") }; computer.push_str(&String::from_utf8_lossy(&program.stdout)); computer.pop(); fancy_fallback(computer, hostname) } pub fn hostname() -> String { // Maximum hostname length = 255, plus a NULL byte. let mut string = mem::MaybeUninit::<[u8; 256]>::uninit(); let string = unsafe { gethostname(string.as_mut_ptr() as *mut c_void, 255); &string.assume_init()[..strlen(string.as_ptr() as *const c_void)] }; String::from_utf8_lossy(string).to_string() } #[cfg(target_os = "macos")] pub fn os() -> Option { let mut distro = String::new(); let name = Command::new("sw_vers") .arg("-productName") .output() .expect("Couldn't find `sw_vers`"); let version = Command::new("sw_vers") .arg("-productVersion") .output() .expect("Couldn't find `sw_vers`"); let build = Command::new("sw_vers") .arg("-buildVersion") .output() .expect("Couldn't find `sw_vers`"); distro.push_str(&String::from_utf8_lossy(&name.stdout)); distro.pop(); distro.push(' '); distro.push_str(&String::from_utf8_lossy(&version.stdout)); distro.pop(); distro.push(' '); distro.push_str(&String::from_utf8_lossy(&build.stdout)); distro.pop(); Some(distro) } #[cfg(not(target_os = "macos"))] pub fn os() -> Option { let mut distro = String::new(); let program = std::fs::read_to_string("/etc/os-release") .expect("Couldn't read file /etc/os-release") .into_bytes(); distro.push_str(&String::from_utf8_lossy(&program)); let mut fallback = None; for i in distro.split('\n') { let mut j = i.split('='); match j.next()? { "PRETTY_NAME" => { return Some(j.next()?.trim_matches('"').to_string()) } "NAME" => { fallback = Some(j.next()?.trim_matches('"').to_string()) } _ => {} } } if let Some(x) = fallback { Some(x) } else { None } } #[cfg(target_os = "macos")] #[inline(always)] pub const fn env() -> DesktopEnv { DesktopEnv::Mac } #[cfg(not(target_os = "macos"))] #[inline(always)] pub fn env() -> DesktopEnv { match std::env::var_os("DESKTOP_SESSION") .map(|env| env.to_string_lossy().to_string()) { Some(env_orig) => { let env = env_orig.to_uppercase(); if env.contains("GNOME") { DesktopEnv::Gnome } else if env.contains("LXDE") { DesktopEnv::Lxde } else if env.contains("OPENBOX") { DesktopEnv::Openbox } else if env.contains("I3") { DesktopEnv::I3 } else if env.contains("UBUNTU") { DesktopEnv::Ubuntu } else if env.contains("PLASMA5") { DesktopEnv::Kde } else { DesktopEnv::Unknown(env_orig) } } // TODO: Other Linux Desktop Environments None => DesktopEnv::Unknown("Unknown".to_string()), } } #[cfg(target_os = "macos")] #[inline(always)] pub const fn platform() -> Platform { Platform::MacOS } #[cfg(not(target_os = "macos"))] #[inline(always)] pub const fn platform() -> Platform { Platform::Linux } whoami-0.8.1/src/wasm.rs010066400017500001750000000106601362402527100133400ustar0000000000000000use crate::{DesktopEnv, Platform}; // navigator.userAgent extern { /// Get the length of the string in utf-16 codepoints. fn navigator_userAgent_Len() -> usize; /// Fill in a utf-16 buffer with the string data. fn navigator_userAgent_Ptr(ptr: *mut u16); } fn user_agent() -> String { let ret = unsafe { let len = navigator_userAgent_Len(); let mut vec = Vec::with_capacity(len); navigator_userAgent_Ptr(vec.as_mut_ptr()); vec.set_len(len); String::from_utf16_lossy(&vec) }; ret } #[inline(always)] pub fn username() -> String { "anonymous".to_string() } #[inline(always)] pub fn realname() -> String { "Anonymous".to_string() } pub fn computer() -> String { let orig_string = user_agent(); let end = if let Some(e) = orig_string.rfind("/") { e } else { return "Unknown Browser".to_string(); }; let start = if let Some(s) = orig_string.rfind(" ") { s } else { return "Unknown Browser".to_string(); }; let string = orig_string.get(start + 1..end).unwrap().to_string(); if string == "Safari" { if orig_string.contains("Chrome") { "Chrome".to_string() } else { "Safari".to_string() } } else { string } } #[inline(always)] pub fn hostname() -> String { "localhost".to_string() } pub fn os() -> Option { let string = user_agent(); let begin = if let Some(b) = string.find('(') { b } else { return None; }; let end = if let Some(e) = string.find(')') { e } else { return None; }; let string = &string[begin + 1..end]; if string.contains("Win32") || string.contains("Win64") { let begin = if let Some(b) = string.find("NT") { b } else { return Some("Windows".to_string()); }; let end = if let Some(e) = string.find(".") { e } else { return Some("Windows".to_string()); }; let string = &string[begin + 3..end]; Some(format!("Windows {}", string)) } else if string.contains("Linux") { let string = if string.contains("X11") || string.contains("Wayland") { let begin = if let Some(b) = string.find(";") { b } else { return Some("Unknown Linux".to_string()); }; let string = &string[begin + 2..]; string } else { string }; if string.starts_with("Linux") { Some("Unknown Linux".to_string()) } else { let end = if let Some(e) = string.find(";") { e } else { return Some("Unknown Linux".to_string()); }; Some(string[..end].to_string()) } } else if string.contains("Mac OS X") { let begin = string.find("Mac OS X").unwrap(); Some(if let Some(end) = string[begin..].find(";") { string[begin..begin + end].to_string() } else { string[begin..].to_string().replace("_", ".") }) } else { // TODO: // Platform::FreeBsd, // Platform::Ios, // Platform::Android, // Platform::Nintendo, // Platform::Xbox, // Platform::PlayStation, // Platform::Dive, // Platform::Fuchsia, // Platform::Redox Some(string.to_string()) } } pub const fn env() -> DesktopEnv { DesktopEnv::Wasm } pub fn platform() -> Platform { let string = user_agent(); let begin = if let Some(b) = string.find('(') { b } else { return Platform::Unknown("Unknown".to_string()); }; let end = if let Some(e) = string.find(')') { e } else { return Platform::Unknown("Unknown".to_string()); }; let string = &string[begin + 1..end]; if string.contains("Win32") || string.contains("Win64") { Platform::Windows } else if string.contains("Linux") { Platform::Linux } else if string.contains("Mac OS X") { Platform::MacOS } else { // TODO: // Platform::FreeBsd, // Platform::Ios, // Platform::Android, // Platform::Nintendo, // Platform::Xbox, // Platform::PlayStation, // Platform::Dive, // Platform::Fuchsia, // Platform::Redox, Platform::Unknown(string.to_string()) } } whoami-0.8.1/src/windows.rs010066400017500001750000000077141362402505300140670ustar0000000000000000use crate::{DesktopEnv, Platform}; use std::mem; #[allow(unused)] #[repr(C)] enum ExtendedNameFormat { NameUnknown, // Nothing NameFullyQualifiedDN, // Nothing NameSamCompatible, // Hostname Followed By Username NameDisplay, // Full Name NameUniqueId, // Nothing NameCanonical, // Nothing NameUserPrincipal, // Nothing NameCanonicalEx, // Nothing NameServicePrincipal, // Nothing NameDnsDomain, // Nothing NameGivenName, // Nothing NameSurname, // Nothing } #[allow(unused)] #[repr(C)] enum ComputerNameFormat { ComputerNameNetBIOS, // Same as GetComputerNameW ComputerNameDnsHostname, // Fancy Name ComputerNameDnsDomain, // Nothing ComputerNameDnsFullyQualified, // Fancy Name with, for example, .com ComputerNamePhysicalNetBIOS, // Same as GetComputerNameW ComputerNamePhysicalDnsHostname, // Same as GetComputerNameW ComputerNamePhysicalDnsDomain, // Nothing ComputerNamePhysicalDnsFullyQualified, // Fancy Name with, for example, .com ComputerNameMax, } #[link(name = "Secur32")] extern "system" { fn GetUserNameExW(a: ExtendedNameFormat, b: *mut u16, c: *mut usize) -> u8; fn GetUserNameW(a: *mut u16, b: *mut usize) -> i32; fn GetComputerNameW(a: *mut u16, b: *mut usize) -> i32; fn GetComputerNameExW( a: ComputerNameFormat, b: *mut u16, c: *mut usize, ) -> i32; } pub fn username() -> String { let mut name = mem::MaybeUninit::<[u16; 256]>::uninit(); let mut size = [256]; let name = unsafe { GetUserNameW(name.as_mut_ptr() as *mut _, size.as_mut_ptr()); name.assume_init() }; String::from_utf16_lossy(if size[0] == 0 { &[] } else { &name[..size[0] - 1] }) } #[inline(always)] pub fn realname() -> String { let mut name = mem::MaybeUninit::<[u16; 256]>::uninit(); let mut size = [256]; let name = unsafe { GetUserNameExW( ExtendedNameFormat::NameDisplay, name.as_mut_ptr() as *mut _, size.as_mut_ptr(), ); name.assume_init() }; if size[0] == 0 { username() } else { String::from_utf16_lossy(&name[..size[0]]) } } #[inline(always)] pub fn computer() -> String { let mut name = mem::MaybeUninit::<[u16; 256]>::uninit(); let mut size = [256]; let name = unsafe { GetComputerNameExW( ComputerNameFormat::ComputerNameDnsFullyQualified, name.as_mut_ptr() as *mut _, size.as_mut_ptr(), ); name.assume_init() }; String::from_utf16_lossy(&name[..size[0]]) } pub fn hostname() -> String { let mut name = mem::MaybeUninit::<[u16; 256]>::uninit(); let mut size = [256]; let name = unsafe { GetComputerNameW(name.as_mut_ptr() as *mut _, size.as_mut_ptr()); name.assume_init() }; String::from_utf16_lossy(&name[..size[0]]) } pub fn os() -> Option { extern "system" { fn GetVersion() -> usize; } let bits = unsafe { GetVersion() } as u32; let mut out = "Windows ".to_string(); let major: u8 = ((bits & 0b00000000_00000000_00000000_11111111) >> 0) as u8; let minor: u8 = ((bits & 0b00000000_00000000_11111111_00000000) >> 8) as u8; let build: u16 = ((bits & 0b11111111_11111111_00000000_00000000) >> 16) as u16; match major { 5 => out.push_str("XP"), 6 => match minor { 0 => out.push_str("Vista"), 1 => out.push_str("7"), 2 => match build { 9200 => out.push_str("10"), _ => out.push_str("8"), }, _ => out.push_str("8"), }, _ => out.push_str("Unknown"), } Some(out) } #[inline(always)] pub const fn env() -> DesktopEnv { DesktopEnv::Windows } #[inline(always)] pub const fn platform() -> Platform { Platform::Windows } whoami-0.8.1/.cargo_vcs_info.json0000644000000001121362430303100124110ustar00{ "git": { "sha1": "6774893615059578b09d671ced99af3f847096a9" } } whoami-0.8.1/Cargo.lock0000644000000002121362430303100103650ustar00# This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "whoami" version = "0.8.1"