summaryrefslogtreecommitdiffstats
path: root/src/ui.rs
blob: 3aff635346bde6c55509378bad1dcde8ba309e5b (plain)
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
/*
 * bb
 *
 * Copyright 2019 Manos Pitsidianakis
 *
 * This file is part of bb.
 *
 * bb 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.
 *
 * bb 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 bb. If not, see <http://www.gnu.org/licenses/>.
 */

/*!
 * This library exports the public types and methods of its modules
 */

#[allow(dead_code)]
mod text_processing;
pub use crate::ui::text_processing::*;
#[macro_use]
#[allow(dead_code)]
mod types;
pub use crate::ui::types::*;

#[macro_use]
#[allow(dead_code)]
mod terminal;
pub use crate::ui::terminal::*;

pub mod state;
pub use crate::ui::state::*;

#[allow(dead_code)]
pub mod components;
pub use crate::ui::components::*;
pub use crate::ui::username::*;
pub mod username {
    use libc;
    use std::ptr::null_mut;
    /* taken from whoami-0.1.1 */
    fn getpwuid(pw_uid: u32, buffer: &mut [i8; 16384]) -> libc::passwd {
        let mut pwentp = null_mut();
        #[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,
                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(pw_uid, &mut pwent, buffer, 16384, &mut pwentp);
            }

            pwent
        }
        #[cfg(target_os = "linux")]
        {
            let mut pwent = libc::passwd {
                pw_name: null_mut(),
                pw_passwd: null_mut(),
                pw_uid,
                pw_gid: 0,
                pw_gecos: null_mut(),
                pw_dir: null_mut(),
                pw_shell: null_mut(),
            };

            unsafe {
                libc::getpwuid_r(pw_uid, &mut pwent, buffer.as_mut_ptr(), 16384, &mut pwentp);
            }

            pwent
        }
    }

    pub fn username(uid: u32) -> String {
        let mut buffer = [0i8; 16384]; // from the man page
        let pwent = getpwuid(uid, &mut buffer);

        let string;
        unsafe {
            string = ::std::ffi::CStr::from_ptr(pwent.pw_name)
                .to_str()
                .unwrap_or_else(|_| "")
                .to_string();
        }

        string
    }
}