meli/ui/src/lib.rs

181 lines
4.2 KiB
Rust
Raw Normal View History

/*
2018-08-07 15:01:15 +03:00
* meli - ui crate.
*
2018-08-07 15:01:15 +03:00
* Copyright 2017-2018 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
2018-08-06 22:20:34 +03:00
/*!
* This library exports the public types and methods of its modules
*/
2019-05-01 19:20:33 +03:00
#[macro_use]
2018-07-18 10:42:52 +03:00
extern crate melib;
extern crate mime_apps;
extern crate notify_rust;
2019-07-22 15:14:39 +03:00
extern crate text_processing;
2018-07-21 11:20:13 +03:00
#[macro_use]
2018-08-19 13:12:48 +03:00
extern crate serde_derive;
extern crate linkify;
extern crate uuid;
2018-08-06 14:58:54 +03:00
extern crate fnv;
2018-07-18 10:42:52 +03:00
extern crate termion;
#[macro_use]
extern crate nom;
2018-07-11 17:07:51 +03:00
2019-03-04 12:03:39 +02:00
extern crate serde_json;
2018-08-06 16:53:23 +03:00
use melib::*;
use std::collections::VecDeque;
2019-07-22 15:14:39 +03:00
use text_processing::*;
2018-07-11 17:07:51 +03:00
2018-08-06 16:53:23 +03:00
#[macro_use]
mod types;
2019-06-18 21:13:58 +03:00
pub use crate::types::*;
2018-07-18 10:42:52 +03:00
2019-03-12 20:35:23 +02:00
#[macro_use]
mod terminal;
2019-06-18 21:13:58 +03:00
pub use crate::terminal::*;
2019-03-12 20:35:23 +02:00
2018-08-06 16:53:23 +03:00
#[macro_use]
mod execute;
2019-06-18 21:13:58 +03:00
use crate::execute::*;
2018-07-21 11:20:13 +03:00
2018-08-06 16:53:23 +03:00
pub mod state;
2019-06-18 21:13:58 +03:00
pub use crate::state::*;
2018-07-21 11:20:13 +03:00
2018-08-06 16:53:23 +03:00
pub mod components;
2019-06-18 21:13:58 +03:00
pub use crate::components::*;
2018-08-19 13:12:48 +03:00
pub mod conf;
2019-06-18 21:13:58 +03:00
pub use crate::conf::*;
2018-10-14 19:49:16 +03:00
pub mod workers;
2019-06-18 21:13:58 +03:00
pub use crate::workers::*;
2019-07-11 11:44:27 +03:00
2019-10-07 16:42:44 +03:00
#[cfg(feature = "sqlite3")]
pub mod sqlite3;
pub mod search;
2019-07-11 11:44:27 +03:00
pub use crate::username::*;
pub mod username {
use libc;
use std::ptr::null_mut;
/* taken from whoami-0.1.1 */
fn getpwuid() -> libc::passwd {
let mut pwentp = null_mut();
2019-09-29 14:49:23 +03:00
#[cfg(target_arch = "aarch64")]
let mut buffer = [0u8; 16384]; // from the man page
#[cfg(not(target_arch = "aarch64"))]
2019-07-11 11:44:27 +03:00
let mut buffer = [0i8; 16384]; // from the man page
2019-07-30 17:56:50 +03:00
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
{
let mut pwent = libc::passwd {
pw_name: null_mut(),
pw_passwd: null_mut(),
pw_uid: 0,
pw_gid: 0,
pw_change: 0,
pw_class: null_mut(),
pw_gecos: null_mut(),
pw_dir: null_mut(),
pw_shell: null_mut(),
pw_expire: 0,
};
unsafe {
libc::getpwuid_r(
libc::geteuid(),
&mut pwent,
&mut buffer[0],
16384,
&mut pwentp,
);
}
pwent
}
#[cfg(target_os = "linux")]
{
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(),
};
unsafe {
libc::getpwuid_r(
libc::geteuid(),
&mut pwent,
&mut buffer[0],
16384,
&mut pwentp,
);
}
pwent
2019-07-11 11:44:27 +03:00
}
}
2019-09-29 14:49:23 +03:00
#[cfg(target_arch = "aarch64")]
fn ptr_to_string(name: *mut u8) -> String {
let uname = name as *const 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
}
#[cfg(not(target_arch = "aarch64"))]
2019-07-11 11:44:27 +03:00
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
}
pub fn username() -> String {
let pwent = getpwuid();
ptr_to_string(pwent.pw_name)
}
}