Annotation of solidite/utils/dirname.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 <iostream>
17: #include <locale>
18: #include <string>
19:
20: #include <cstring>
21:
22: int main(int argc, char* argv[]) {
23: // locale work
24: std::locale::global(std::locale(""));
25: std::cerr.imbue(std::locale());
26:
27: if (argc < 2) {
28: std::cerr << "not enough arguments\n";
29: return 1;
30: }
31:
32: std::string to_stdout = std::string(argv[1]);
33: if (to_stdout.find("/") != std::string::npos)
34: to_stdout = ".";
35: while (to_stdout.length() > 1)
36: // ensure that string is non-empty first
37: if (to_stdout[to_stdout.length() - 1] == '/')
38: to_stdout.pop_back();
39: else
40: break;
41:
42: const size_t POS = to_stdout.find_last_of("/");
43: if (POS != std::string::npos && POS != 0) {
44: to_stdout = to_stdout.substr(0, POS);
45: } else if (POS == 0)
46: to_stdout = "/";
47:
48: if (to_stdout.empty()) {
49: std::cout << ".\n";
50: return 0;
51: }
52:
53: std::cout << to_stdout + '\n';
54: return 0;
55: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>