File:  [Local Repository] / solidite / utils / ln.cpp
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Sep 8 00:37:30 2026 UTC (11 days, 1 hour ago) by user
Branches: none, MAIN
CVS tags: notag, HEAD
Migrate host

/* 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 <locale>
#include <memory>
#include <string>
#include <system_error>
#include <vector>

#include <cstddef>
#include <cstring>

#include "include/io.hpp"

int first_synopsis(const std::unique_ptr<std::vector<std::string>> &TARGETS,
    const std::bitset<6> &OPTIONS) {
  std::error_code ec;

  if (OPTIONS.test(2) == 1)
    std::filesystem::create_symlink(TARGETS -> at(0), TARGETS -> at(1));
  else {
    std::filesystem::create_hard_link(TARGETS -> at(0), TARGETS -> at(1));
  }

  if (ec.value() != 0)
    return 1;
  return 0;
}

int second_synopsis(const std::unique_ptr<std::vector<std::string>> &TARGETS,
    const std::bitset<6> &OPTIONS) {
}

int main(int argc, char *argv[]) {

  std::locale::global(std::locale(""));
  std::bitset<6> options = 0b0;
  /* [0]: -- encountered?
   * [1]: -f
   * [2]: -s
   * [3]: -L
   * [4]: -P
   * [5]: prog return code*/

  std::unique_ptr<std::vector<std::string>> targets =
    std::make_unique<std::vector<std::string>>();
  // 2 elements implies source file, target file
  // 3+ elements implies source file..., target dir

  for (int i = 1; i < argc; ++i) {
    if (std::strlen(argv[i]) == 0 || argv[i][0] != '-' ||
        options.test(0) == true) {
      targets -> push_back(argv[i]);
    } else if (std::strcmp("--", argv[i]) == 0) {
      options.set(0);
    } else {
      for (std::size_t j = 1; j < std::strlen(argv[i]); ++j) {
        switch (argv[i][j]) {
          case 'f':
            options.set(1);
            break;
          case 's':
            options.set(2);
            break;
          case 'L':
            options.reset(4);
            options.set(3);
            break;
          case 'P':
            options.reset(4);
            options.set(4);
            break;
          default:
            print_error("invalid option");
            break;
        }
      }
    }
  }

  if (targets -> size() == 2 && !std::filesystem::is_directory(targets ->
        at(1))) {
    if (first_synopsis(targets, options) == 1)
      options.set(5);
  } else if (targets -> size() > 2) {
    if (second_synopsis(targets, options) == 1)
      options.set(5);
  } else {
    print_error("not enough operands\n");
    options.set(5);
  }

  return options.test(5);
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>