File:  [Local Repository] / solidite / utils / uname.cpp
Revision 1.1: download - view: text, annotated - select for diffs
Tue Sep 8 00:37:30 2026 UTC (11 days, 2 hours ago) by user
CVS tags: MAIN, HEAD
Initial revision

/* 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 <iostream>
#include <locale>
#include <string>

#include <cstddef>
#include <cstdint>
#include <cstring>

#include <sys/utsname.h>

int main(int argc, char* argv[]) {
  // locale work
  std::locale::global(std::locale(""));
  std::cerr.imbue(std::locale()); 
  
  std::bitset<6> options = 0b000000;
  /* options[0]=print system hardware type (-m)
   * options[1]=write node (-n)
   * options[2]=write OS release level (-r)
   * options[3]=write the name of the operating system (-s)
   * options[4]=write OS version level of the OS release (-v)
   * options[5]=error occurred, return code 1*/
  
  for (int i = 1; i < argc; ++i) {
    if (argv[i][0] != '-')
      continue;
    for (std::size_t j = 1; j < strlen(argv[i]); ++j) {
      switch (argv[i][j]) {
        case 'a':
          for (int i = 0; i < 5; ++i)
            options.set(i);
          break;
        case 'm':
          options.set(0);
          break;
        case 'n':
          options.set(1);
          break;
        case 'r':
          options.set(2);
          break;
        case 's':
          options.set(3);
          break;
        case 'v':
          options.set(4);
          break;
        default:
          std::cerr << "bad option\n";
	  options.set(5);
          break;
      }
    }
  }

  if (options == 0)
    options.set(3);

  struct utsname uts_name;
  if (uname(&uts_name) != 0) {
    std::cerr << "getting uname failed\n";
    return 1;
  }

  std::string output_buffer {};
  if (options.test(0) == 1) {
    output_buffer+=uts_name.machine;
    output_buffer+=' ';
  }
  if (options.test(1) == 1) {
    output_buffer+=uts_name.nodename;
    output_buffer+=' ';
  }
  if (options.test(2) == 1) {
    output_buffer+=uts_name.release;
    output_buffer+=' ';
  }
  if (options.test(3) == 1) {
    output_buffer+=uts_name.sysname;
    output_buffer+=' ';
  }
  if (options.test(4) == 1) {
    output_buffer+=uts_name.version;
    output_buffer+=' ';
  }
  output_buffer.erase(output_buffer.length() - 1, 1);
  std::cout << output_buffer + '\n';
  return options.test(5);
}

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