uname.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*++
  2. Copyright (c) 2013 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. uname.c
  5. Abstract:
  6. This module implements support for the uname utility.
  7. Author:
  8. Evan Green 17-Jul-2013
  9. Environment:
  10. POSIX
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/lib/types.h>
  16. #include <assert.h>
  17. #include <getopt.h>
  18. #include <string.h>
  19. #include "swlib.h"
  20. //
  21. // ---------------------------------------------------------------- Definitions
  22. //
  23. #define UNAME_VERSION_MAJOR 1
  24. #define UNAME_VERSION_MINOR 0
  25. #define UNAME_USAGE \
  26. "usage: uname [-asnrvm]\n" \
  27. "The uname utility prints out the system name and version number." \
  28. "Options are:\n" \
  29. " -a, --all -- Turns on all options and prints them out separated by " \
  30. "spaces.\n" \
  31. " -s, --kernel-name -- Print the system name.\n" \
  32. " -n, --nodename -- Print out the name of this system on the network.\n" \
  33. " -r, --kernel-release -- Print out the system release number string.\n" \
  34. " -v, --kernel-version -- Print out the version string within this " \
  35. "release.\n" \
  36. " -m, --machine -- Print out the machine type.\n" \
  37. " --help -- Display this help text and exit.\n" \
  38. " --version -- Display the application version and exit.\n" \
  39. #define UNAME_OPTIONS_STRING "asnrvm"
  40. //
  41. // ------------------------------------------------------ Data Type Definitions
  42. //
  43. //
  44. // ----------------------------------------------- Internal Function Prototypes
  45. //
  46. //
  47. // -------------------------------------------------------------------- Globals
  48. //
  49. struct option UnameLongOptions[] = {
  50. {"all", no_argument, 0, 'a'},
  51. {"kernel-name", no_argument, 0, 's'},
  52. {"nodename", no_argument, 0, 'n'},
  53. {"kernel-release", no_argument, 0, 'r'},
  54. {"kernel-version", no_argument, 0, 'v'},
  55. {"machine", no_argument, 0, 'm'},
  56. {"help", no_argument, 0, 'h'},
  57. {"version", no_argument, 0, 'V'},
  58. {NULL, 0, 0, 0}
  59. };
  60. //
  61. // ------------------------------------------------------------------ Functions
  62. //
  63. INT
  64. UnameMain (
  65. INT ArgumentCount,
  66. CHAR **Arguments
  67. )
  68. /*++
  69. Routine Description:
  70. This routine is the main entry point for the uname utility.
  71. Arguments:
  72. ArgumentCount - Supplies the number of command line arguments the program
  73. was invoked with.
  74. Arguments - Supplies a tokenized array of command line arguments.
  75. Return Value:
  76. Returns an integer exit code. 0 for success, nonzero otherwise.
  77. --*/
  78. {
  79. SYSTEM_NAME Name;
  80. INT Option;
  81. BOOL PrintedSomething;
  82. BOOL PrintMachine;
  83. BOOL PrintNodeName;
  84. BOOL PrintRelease;
  85. BOOL PrintSystemName;
  86. BOOL PrintVersion;
  87. int Status;
  88. PrintSystemName = FALSE;
  89. PrintNodeName = FALSE;
  90. PrintRelease = FALSE;
  91. PrintVersion = FALSE;
  92. PrintMachine = FALSE;
  93. //
  94. // Process the control arguments.
  95. //
  96. while (TRUE) {
  97. Option = getopt_long(ArgumentCount,
  98. Arguments,
  99. UNAME_OPTIONS_STRING,
  100. UnameLongOptions,
  101. NULL);
  102. if (Option == -1) {
  103. break;
  104. }
  105. if ((Option == '?') || (Option == ':')) {
  106. Status = 1;
  107. return Status;
  108. }
  109. switch (Option) {
  110. case 'a':
  111. PrintSystemName = TRUE;
  112. PrintNodeName = TRUE;
  113. PrintRelease = TRUE;
  114. PrintVersion = TRUE;
  115. PrintMachine = TRUE;
  116. break;
  117. case 's':
  118. PrintSystemName = TRUE;
  119. break;
  120. case 'n':
  121. PrintNodeName = TRUE;
  122. break;
  123. case 'r':
  124. PrintRelease = TRUE;
  125. break;
  126. case 'v':
  127. PrintVersion = TRUE;
  128. break;
  129. case 'm':
  130. PrintMachine = TRUE;
  131. break;
  132. case 'V':
  133. SwPrintVersion(UNAME_VERSION_MAJOR, UNAME_VERSION_MINOR);
  134. return 1;
  135. case 'h':
  136. printf(UNAME_USAGE);
  137. return 1;
  138. default:
  139. assert(FALSE);
  140. Status = 1;
  141. return Status;
  142. }
  143. }
  144. //
  145. // If everything is off, just print the system name.
  146. //
  147. if ((PrintSystemName == FALSE) && (PrintNodeName == FALSE) &&
  148. (PrintRelease == FALSE) && (PrintVersion == FALSE) &&
  149. (PrintMachine == FALSE)) {
  150. PrintSystemName = TRUE;
  151. }
  152. Status = SwGetSystemName(&Name);
  153. if (Status != 0) {
  154. goto MainEnd;
  155. }
  156. PrintedSomething = FALSE;
  157. if (PrintSystemName != FALSE) {
  158. printf("%s", Name.SystemName);
  159. PrintedSomething = TRUE;
  160. }
  161. if (PrintNodeName != FALSE) {
  162. if (PrintedSomething != FALSE) {
  163. fputc(' ', stdout);
  164. }
  165. printf("%s", Name.NodeName);
  166. PrintedSomething = TRUE;
  167. }
  168. if (PrintRelease != FALSE) {
  169. if (PrintedSomething != FALSE) {
  170. fputc(' ', stdout);
  171. }
  172. printf("%s", Name.Release);
  173. PrintedSomething = TRUE;
  174. }
  175. if (PrintVersion != FALSE) {
  176. if (PrintedSomething != FALSE) {
  177. fputc(' ', stdout);
  178. }
  179. printf("%s", Name.Version);
  180. PrintedSomething = TRUE;
  181. }
  182. if (PrintMachine != FALSE) {
  183. if (PrintedSomething != FALSE) {
  184. fputc(' ', stdout);
  185. }
  186. printf("%s", Name.Machine);
  187. PrintedSomething = TRUE;
  188. }
  189. if (PrintedSomething != FALSE) {
  190. fputc('\n', stdout);
  191. }
  192. Status = 0;
  193. MainEnd:
  194. return Status;
  195. }
  196. //
  197. // --------------------------------------------------------- Internal Functions
  198. //