Annotation of solidite/utils/ln.cpp, revision 1.1.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 <locale>
19: #include <memory>
20: #include <string>
21: #include <system_error>
22: #include <vector>
23:
24: #include <cstddef>
25: #include <cstring>
26:
27: #include "include/io.hpp"
28:
29: int first_synopsis(const std::unique_ptr<std::vector<std::string>> &TARGETS,
30: const std::bitset<6> &OPTIONS) {
31: std::error_code ec;
32:
33: if (OPTIONS.test(2) == 1)
34: std::filesystem::create_symlink(TARGETS -> at(0), TARGETS -> at(1));
35: else {
36: std::filesystem::create_hard_link(TARGETS -> at(0), TARGETS -> at(1));
37: }
38:
39: if (ec.value() != 0)
40: return 1;
41: return 0;
42: }
43:
44: int second_synopsis(const std::unique_ptr<std::vector<std::string>> &TARGETS,
45: const std::bitset<6> &OPTIONS) {
46: }
47:
48: int main(int argc, char *argv[]) {
49:
50: std::locale::global(std::locale(""));
51: std::bitset<6> options = 0b0;
52: /* [0]: -- encountered?
53: * [1]: -f
54: * [2]: -s
55: * [3]: -L
56: * [4]: -P
57: * [5]: prog return code*/
58:
59: std::unique_ptr<std::vector<std::string>> targets =
60: std::make_unique<std::vector<std::string>>();
61: // 2 elements implies source file, target file
62: // 3+ elements implies source file..., target dir
63:
64: for (int i = 1; i < argc; ++i) {
65: if (std::strlen(argv[i]) == 0 || argv[i][0] != '-' ||
66: options.test(0) == true) {
67: targets -> push_back(argv[i]);
68: } else if (std::strcmp("--", argv[i]) == 0) {
69: options.set(0);
70: } else {
71: for (std::size_t j = 1; j < std::strlen(argv[i]); ++j) {
72: switch (argv[i][j]) {
73: case 'f':
74: options.set(1);
75: break;
76: case 's':
77: options.set(2);
78: break;
79: case 'L':
80: options.reset(4);
81: options.set(3);
82: break;
83: case 'P':
84: options.reset(4);
85: options.set(4);
86: break;
87: default:
88: print_error("invalid option");
89: break;
90: }
91: }
92: }
93: }
94:
95: if (targets -> size() == 2 && !std::filesystem::is_directory(targets ->
96: at(1))) {
97: if (first_synopsis(targets, options) == 1)
98: options.set(5);
99: } else if (targets -> size() > 2) {
100: if (second_synopsis(targets, options) == 1)
101: options.set(5);
102: } else {
103: print_error("not enough operands\n");
104: options.set(5);
105: }
106:
107: return options.test(5);
108: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>