/* Copyright (c) 2026 The Solidite Developers
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero 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 Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <bitset>
#include <filesystem>
#include <iostream>
#include <locale>
#include <sstream>
#include <string>
#include <vector>
#include <sys/stat.h>
int main(int argc, char* argv[]) {
// locale work
std::locale::global(std::locale(""));
std::cerr.imbue(std::locale());
std::vector<std::string> directories;
std::bitset<4> options = 0b00;
/* bitset[0]=do not process arguments further (-- encountered)
* bitset[1]=-p flag, create intermediate directories
* bitset[2]=ignore -m XXX for internal use to not process the mode argument
* as a directory to create
* bitset[3]=-m flag is chosen to change mode*/
uint_fast16_t mode = 777;
for (int i = 1; i < argc; ++i) {
if (options.test(2) == 1) {
options.reset(2);
continue;
// ignore the next XXX argument if -m XXX
}
const std::string &ARG = argv[i];
if (options.test(0) == 0) {
if (ARG == "--") {
options.set(0);
continue;
}
if (ARG[0] == '-') {
if (ARG.find('p') != std::string::npos)
options.set(1);
if (ARG.find('m') != std::string::npos) {
if (i + 1 > argc - 1) {
std::cerr << "octal integer required after -m\n";
return 1;
}
std::istringstream(ARG) >> std::oct >> mode;
options.set(2); // set ignore XXX argument if -m XXX
options.set(3);
}
} else
directories.push_back(ARG);
}
}
for (std::string directory : directories) {
if (options.test(1) == 0)
std::filesystem::create_directory(directory);
else
std::filesystem::create_directories(directory);
if (options.test(3) == 1) {
try {
chmod(directory.c_str(), mode);
} catch (...) {
std::cerr << "failed to change permissions: " + directory + '\n';
return 1;
}
}
}
return 0;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>