[go: up one dir, main page]

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// lib.rs
// whoami
// Copyright 2017 (c) Aldaron's Tech
// Copyright 2017 (c) Jeron Lau
// Licensed under the MIT LICENSE

//! Crate for getting the user's username and realname.

extern crate libc;

use std::ptr::{ null_mut };
use std::io::BufReader;
use std::io::Read;
use std::process::Command;
use std::process::Stdio;

pub enum DesktopEnv {
	Gnome,
	Windows,
	Unknown,
}

impl ::std::fmt::Display for DesktopEnv {
	fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
		use self::DesktopEnv::*;

		write!(f, "{}", match *self {
			Gnome => "gnome",
			Windows => "windows",
			Unknown => "unknown",
		})
	}
}

fn getpwuid() -> libc::passwd {
	let mut pwent = libc::passwd {
		pw_name: null_mut(),
		pw_passwd: null_mut(),
		pw_uid: 0,
		pw_gid: 0,
		pw_gecos: null_mut(),
		pw_dir: null_mut(),
		pw_shell: null_mut(),
	};
	let mut pwentp = null_mut();
	let mut buffer = [0i8;16384]; // from the man page

	unsafe {
		libc::getpwuid_r(libc::geteuid(), &mut pwent, &mut buffer[0],
			16384, &mut pwentp);
	}

	pwent
}

fn ptr_to_string(name: *mut i8) -> String {
	let uname = name as *mut _ as *mut u8;

	let s;
	let string;

	unsafe {
		s = ::std::slice::from_raw_parts(uname, libc::strlen(name));
		string = String::from_utf8_lossy(s).to_string();
	}

	string
}

/// Get the user's username.
pub fn username() -> String {
	let pwent = getpwuid();

	ptr_to_string(pwent.pw_name)
}

/// Get the user's full name.  Format: `FIRST_NAME [MIDDLE_NAME] [LAST_NAME]`
pub fn realname() -> String {
	let pwent = getpwuid();

	ptr_to_string(pwent.pw_gecos)
}

/// Get the computer's pretty name.
pub fn computer() -> String {
	let mut computer = String::new();

	let mut program = Command::new("hostnamectl")
		.arg("--pretty")
		.stdout(Stdio::piped())
		.spawn()
		.expect(&format!("Couldn't Find `hostnamectl`"));
	let mut pretty = BufReader::new(program.stdout.as_mut().unwrap());

	pretty.read_to_string(&mut computer).unwrap();

	computer.pop();

	computer
}

/// Get the computer's hostname.
pub fn hostname() -> String {
	let mut string = [0 as libc::c_char; 255];

	unsafe {
		libc::gethostname(&mut string[0], 255);
	}

	ptr_to_string(&mut string[0])
}

/// Get the OS.  Example: "Windows 10" or "Fedora 26 (Workstation Edition)"
pub fn os() -> String {
	if cfg!(target_os = "linux") {
		let mut distro = String::new();

		let mut program = Command::new("cat")
			.arg("/etc/os-release")
			.stdout(Stdio::piped())
			.spawn()
			.expect(&format!("Couldn't Find `cat`"));
		let mut pretty = BufReader::new(program.stdout.as_mut().unwrap());

		pretty.read_to_string(&mut distro).unwrap();

		for i in distro.split('\n') {
			let mut j = i.split('=');

			match j.next().unwrap() {
				"PRETTY_NAME" => return j.next().unwrap()
					.trim_matches('"').to_string(),
				_ => {},
			}
		}

		return "uknown".to_string();
	} else if cfg!(target_os = "windows") {
		"Windows 10".to_string() // TODO: Not all Windows is Windows 10
	} else {
		"unknown".to_string()
	}
}

/// Get the Desktop Environment.  Example: "gnome" or "windows"
#[inline(always)]
pub fn env() -> DesktopEnv {
	if cfg!(target_os = "linux") {
		match ::std::env::var_os("DESKTOP_SESSION") {
			Some(val) => match val.to_str().unwrap() {
				"gnome" => DesktopEnv::Gnome,
				_ => DesktopEnv::Unknown	
			},
			// TODO: Other Linux Desktop Environments
			None => DesktopEnv::Unknown
		}
	} else if cfg!(target_os = "windows") {
		DesktopEnv::Windows
		// TODO: Other Environments
	} else {
		DesktopEnv::Unknown
	}
}