summaryrefslogtreecommitdiff
path: root/src/os.cpp
blob: af37a0e55129c6234bb903ab5aaa6ebbeda1df26 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
// Display images inside a terminal
// Copyright (C) 2023  JustKidding
//
// This program 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.
//
// This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.

#include "os.hpp"
#include "util/ptr.hpp"

#include <array>
#include <cstdlib>
#include <system_error>

#include <cerrno>
#include <cstdio>
#include <poll.h>
#include <unistd.h>

auto os::exec(const std::string &cmd) -> std::string
{
    const int bufsize = 128;
    std::array<char, bufsize> buffer{};
    std::string result;
    const c_unique_ptr<FILE, &pclose> pipe{popen(cmd.c_str(), "r")};
    if (!pipe) {
        throw std::system_error(errno, std::generic_category());
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result.append(buffer.data());
    }
    if (!result.empty()) {
        result.erase(result.length() - 1);
    }
    return result;
}

auto os::read_data_from_fd(int filde, char sep) -> std::string
{
    using std::errc;
    std::string response;
    char readch = 0;

    while (true) {
        const auto status = read(filde, &readch, 1);
        if (status == -1) {
            throw std::system_error(errno, std::system_category());
        }
        if (status == 0 || readch == sep) {
            if (response.empty()) {
                throw std::system_error(EIO, std::system_category());
            }
            break;
        }
        response.push_back(readch);
    }
    return response;
}

auto os::read_data_from_stdin(char sep) -> std::string
{
    return read_data_from_fd(STDIN_FILENO, sep);
}

auto os::wait_for_data_on_fd(int filde, int waitms) -> bool
{
    struct pollfd fds;
    fds.fd = filde;
    fds.events = POLLIN;

    poll(&fds, 1, waitms);

    if ((fds.revents & (POLLERR | POLLNVAL | POLLHUP)) != 0) {
        throw std::system_error(EIO, std::generic_category());
    }

    return (fds.revents & POLLIN) != 0;
}

auto os::wait_for_data_on_stdin(int waitms) -> bool
{
    return wait_for_data_on_fd(STDIN_FILENO, waitms);
}

auto os::getenv(const std::string &var) -> std::optional<std::string>
{
    const char *env_p = std::getenv(var.c_str()); // NOLINT
    if (env_p == nullptr) {
        return {};
    }
    return env_p;
}

auto os::get_pid() -> int
{
    return getpid();
}

auto os::get_ppid() -> int
{
    return getppid();
}

void os::daemonize()
{
    const int pid = fork();
    if (pid < 0) {
        std::exit(EXIT_FAILURE); // NOLINT
    }
    if (pid > 0) {
        std::exit(EXIT_SUCCESS); // NOLINT
    }
    const int status = setsid();
    if (status < 0) {
        std::exit(EXIT_FAILURE); // NOLINT
    }
}