File:  [Local Repository] / solidite / utils / head.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, 2 hours 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 <fstream>
#include <iostream>
#include <locale>
#include <string>
#include <vector>

#include <cstring>
#include <cstdint>

int main(int argc, char* argv[]) {
  // locale work
  std::locale::global(std::locale(""));
  std::cerr.imbue(std::locale()); 

  size_t number = 10;
  std::vector<std::string> files;
  std::bitset<3> options = 0b000;
  /* options[0]=stop process options (-- stop processing options if set to 1)
   * options[1]=skip next argv - in case of -c 10, dont process 10 as a file
   * options[2]=type (either -c or -n, default=0 -n 10) */

  for (int i = 1; i < argc; ++i) {
    if (options.test(1) == 1) {
      options.reset(1);
      continue;
    } else if (options.test(0) == 0) {
      if (std::strcmp(argv[i], "--") == 0) {
        options.set(0);
        continue;
      } else if (std::strcmp(argv[i], "-c") == 0) {
        if (i + 1 > argc - 1) {
          std::cerr << "byte required after -c\n";
          return 1;
        } else {
          number = std::atoi(argv[i + 1]);
          options.set(2);
          options.set(1);
          continue;
        }
      } else if (std::strcmp(argv[i], "-n") == 0) {
        if (i + 1 > argc - 1) {
          std::cerr << "line number required after -n\n";
          return 1;
        } else {
          number = std::atoi(argv[i + 1]);
          options.reset(2);
          options.set(1);
          continue;
        }
      }
    }
    files.push_back(argv[i]);
  }

  if (files.empty())
    files.push_back("-");
  // induce an stdin into the files if empty
  for (std::string file : files) {
    if (file == "-") {
      std::string line;
      if (options.test(2) == 0) {
        for (size_t i = 0; i < number; ++i) {
          std::getline(std::cin, line);
          std::cout << line + '\n';
        }
      } else if (options.test(2) == 1) {
        size_t chars = 0;
        while (chars < number) {
          std::getline(std::cin, line);
          if (chars + line.length() < number) {
            line += '\n';
            chars += line.length();
          } else {
            line = line.substr(0, number - chars);
            std::cout << line;
            break;
          }
          std::cout << line;
        }
      }
      continue;
    }

    std::ifstream read(file);
    if (!read.is_open()) {
      std::cerr << "failed to read: " + file + '\n';
      continue;
    }
    
    std::string line;
    if (options.test(2) == 0) {
      for (size_t i = 0; i < number; ++i) {
        std::getline(read, line);
        std::cout << line + '\n';
      }
    } else if (options.test(2) == 1) {
      size_t chars = 0;
      while (chars < number) {
        std::getline(read, line);
        if (chars + line.length() < number) {
          line += '\n';
          chars += line.length();
        } else {
          line = line.substr(0, number - chars);
          std::cout << line;
          break;
        }
        std::cout << line;
      }
    }
    read.close();
  }
  return 0;
}

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