-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkfifo.cpp
More file actions
50 lines (43 loc) · 1.35 KB
/
mkfifo.cpp
File metadata and controls
50 lines (43 loc) · 1.35 KB
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
#include <cstdio>
#include <cstring>
#include <string>
#include <sys/stat.h>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "mkfifo",
.version = CFBOX_VERSION_STRING,
.one_line = "make FIFOs (named pipes)",
.usage = "mkfifo [NAME]...",
.options = " -m MODE set permission mode (octal, default 0644)",
.extra = "",
};
} // namespace
auto mkfifo_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'m', true, "mode"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
auto mode_str = parsed.get_any('m', "mode");
mode_t mode = 0644;
if (mode_str) {
mode = static_cast<mode_t>(std::stoul(std::string{*mode_str}, nullptr, 8));
}
const auto& pos = parsed.positional();
if (pos.empty()) {
CFBOX_ERR("mkfifo", "missing operand");
return 1;
}
int rc = 0;
for (auto p : pos) {
std::string path{p};
if (mkfifo(path.c_str(), mode) != 0) {
CFBOX_ERR("mkfifo", "cannot create fifo '%s': %s", path.c_str(), std::strerror(errno));
rc = 1;
}
}
return rc;
}