uname.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <sys/utsname.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #define REQ_PRINT_USAGE /* Require print_usage() from ../common/common.h */
  11. #define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
  12. #define DESCRIPTION "Return system name."
  13. #define OPERANDS "[-amnrsv]"
  14. #include "../common/common.h"
  15. int main(int argc, char *const argv[]) {
  16. int argument;
  17. struct utsname name;
  18. char param[256];
  19. for (int i = 0; i < 256; i++) param[i] = 0;
  20. char *argv0 = strdup(argv[0]);
  21. while ((argument = getopt(argc, argv, "amnrsv")) != -1) {
  22. if (argument == '?') {
  23. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  24. return 1;
  25. }
  26. param[argument] = argument;
  27. if (argument == 'a') {
  28. param['s'] = 's';
  29. param['n'] = 'n';
  30. param['r'] = 'r';
  31. param['v'] = 'v';
  32. param['m'] = 'm';
  33. }
  34. } argc -= optind; argv += optind;
  35. uname(&name);
  36. if (errno) return errprint(argv0, NULL, errno);
  37. if (param['s'] || !(param['m'] && param['v'] && param['r'] && param['n']))
  38. printf("%s ", name.sysname);
  39. if (param['n']) printf("%s ", name.nodename);
  40. if (param['r']) printf("%s ", name.release);
  41. if (param['v']) printf("%s ", name.version);
  42. if (param['m']) printf("%s", name.machine);
  43. printf("\n");
  44. return errprint(argv0, NULL, errno);
  45. }