Annotation of solidite/utils/chmod.cpp, revision 1.1
1.1 ! user 1: /* Copyright (c) 2026 The Solidite Developers
! 2: *
! 3: * This program is free software: you can redistribute it and/or modify it under
! 4: * the terms of the GNU Affero General Public License as published by the Free
! 5: * Software Foundation, either version 3 of the License, or (at your option)
! 6: * any later version.
! 7: *
! 8: * This program is distributed in the hope that it will be useful, but WITHOUT
! 9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
! 10: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
! 11: * details.
! 12: *
! 13: * You should have received a copy of the GNU Affero General Public License
! 14: * along with this program. If not, see <https://www.gnu.org/licenses/>. */
! 15:
! 16: #include <bitset>
! 17: #include <filesystem>
! 18: #include <iostream>
! 19: #include <locale>
! 20: #include <string>
! 21: #include <sstream>
! 22: #include <vector>
! 23:
! 24: #include <cstdint>
! 25: #include <cstring>
! 26:
! 27: #include <sys/stat.h>
! 28:
! 29: int main(int argc, char* argv[]) {
! 30: // locale work
! 31: std::locale::global(std::locale(""));
! 32: std::cout.imbue(std::locale());
! 33: std::wcout.imbue(std::locale());
! 34: std::cerr.imbue(std::locale());
! 35: std::wcerr.imbue(std::locale());
! 36:
! 37: std::bitset<3> options = 0b001;
! 38: /* options[0]=process args? (-- not encountered), set to 0 if --
! 39: * options[1]=recursive (if -R then true)
! 40: * options[2]=mode found (if not found then error)
! 41: * reminder std::bitset initializes RTL so 001 sets [0] to true*/
! 42:
! 43: std::vector<char*> files;
! 44: uint_fast16_t mode = 511;
! 45: // mode will get converted to octal
! 46:
! 47: for (int i = 1; i < argc; ++i) {
! 48: if (options.test(0) == 0)
! 49: files.push_back(argv[i]);
! 50: else if (std::strcmp(argv[i], "--") == 0)
! 51: options.reset(0);
! 52: else if (options.test(1) == 0 && std::strcmp(argv[i], "-R") == 0)
! 53: options.set(1);
! 54: else if (options.test(2) == 0) {
! 55: try {
! 56: std::istringstream(argv[i]) >> std::oct >> mode;
! 57: } catch (...) {
! 58: std::cerr << "not an octal integer: " + std::string(argv[i]) + '\n';
! 59: return 1;
! 60: }
! 61: options.set(2);
! 62: } else
! 63: files.push_back(argv[i]);
! 64: }
! 65:
! 66: for (char* file : files) {
! 67: try {
! 68: if (options.test(1) == 0) {
! 69: chmod(file, mode);
! 70: } else {
! 71: // loop through the files/folders recursively and chmod them
! 72: chmod(file, mode); // chmod the original directory
! 73: for (const std::filesystem::directory_entry& ENTRY :
! 74: std::filesystem::recursive_directory_iterator(file)) {
! 75: std::string what;
! 76: chmod(ENTRY.path().string().c_str(), mode); // chmod the subdirectories
! 77: }
! 78: }
! 79: } catch (...) {
! 80: std::cerr << "failed to change permissions: " + std::string(file) + '\n';
! 81: return 1;
! 82: }
! 83: }
! 84: return 0;
! 85: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>