uname.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*******************************************************************************
  2. * Example source code for using the argtable3 library to implement:
  3. *
  4. * uname [-asnrvmpio] [--help] [--version]
  5. *
  6. * This file is part of the argtable3 library.
  7. *
  8. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  9. * <sheitmann@users.sourceforge.net>
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  27. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. ******************************************************************************/
  34. #include "argtable3.h"
  35. /* Here we simulate the uname functionality */
  36. int mymain(int kname, int nname, int krel, int kver, int mach, int proc, int hard, int opsys)
  37. {
  38. if (kname) printf("Linux ");
  39. if (nname) printf("localhost.localdomain ");
  40. if (krel) printf("2.4.19-16 ");
  41. if (kver) printf("#1 Fri Sep 20 18:15:05 CEST 2002 ");
  42. if (mach) printf("i686 ");
  43. if (proc) printf("Intel ");
  44. if (hard) printf("unknown ");
  45. if (opsys) printf("GNU/Linux ");
  46. printf("\n");
  47. return 0;
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. const char* progname = "uname";
  52. struct arg_lit *all = arg_lit0("a", "all", "print all information, in the following order:");
  53. struct arg_lit *kname = arg_lit0("s", "kernel-name", "print the kernel name");
  54. struct arg_lit *nname = arg_lit0("n", "nodename", "print the node name");
  55. struct arg_lit *krel = arg_lit0("r", "kernel-release", "print the kernel release");
  56. struct arg_lit *kver = arg_lit0("v", "kernel-version", "print the kernel version");
  57. struct arg_lit *mach = arg_lit0("m", "machine", "print the machine hardware name");
  58. struct arg_lit *proc = arg_lit0("p", "processor", "print the processor type");
  59. struct arg_lit *hard = arg_lit0("i", "hardware-platform","print the hardware platform");
  60. struct arg_lit *opsys = arg_lit0("o", "operating-system", "print the operating system");
  61. struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit");
  62. struct arg_lit *vers = arg_lit0(NULL,"version", "print version information and exit");
  63. struct arg_end *end = arg_end(20);
  64. void* argtable[] = {all,kname,nname,krel,kver,mach,proc,hard,opsys,help,vers,end};
  65. int nerrors;
  66. int exitcode=0;
  67. /* verify the argtable[] entries were allocated sucessfully */
  68. if (arg_nullcheck(argtable) != 0)
  69. {
  70. /* NULL entries were detected, some allocations must have failed */
  71. printf("%s: insufficient memory\n",progname);
  72. exitcode=1;
  73. goto exit;
  74. }
  75. /* Parse the command line as defined by argtable[] */
  76. nerrors = arg_parse(argc,argv,argtable);
  77. /* special case: '--help' takes precedence over error reporting */
  78. if (help->count > 0)
  79. {
  80. printf("Usage: %s", progname);
  81. arg_print_syntax(stdout,argtable,"\n");
  82. printf("Print certain system information. With no options, same as -s.\n\n");
  83. arg_print_glossary(stdout,argtable," %-25s %s\n");
  84. printf("\nReport bugs to <foo@bar>.\n");
  85. exitcode=0;
  86. goto exit;
  87. }
  88. /* special case: '--version' takes precedence error reporting */
  89. if (vers->count > 0)
  90. {
  91. printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname);
  92. printf("September 2003, Stewart Heitmann\n");
  93. exitcode=0;
  94. goto exit;
  95. }
  96. /* If the parser returned any errors then display them and exit */
  97. if (nerrors > 0)
  98. {
  99. /* Display the error details contained in the arg_end struct.*/
  100. arg_print_errors(stdout,end,progname);
  101. printf("Try '%s --help' for more information.\n",progname);
  102. exitcode=1;
  103. goto exit;
  104. }
  105. /* special case: uname with no command line options is equivalent to "uname -s" */
  106. if (argc==1)
  107. {
  108. exitcode = mymain(0,1,0,0,0,0,0,0);
  109. goto exit;
  110. }
  111. /* special case: "uname -a" is equivalent to "uname -snrvmpi" */
  112. if (all->count>0)
  113. {
  114. exitcode = mymain(1,1,1,1,1,1,1,1);
  115. goto exit;
  116. }
  117. /* normal case: take the command line options at face value */
  118. exitcode = mymain(kname->count, nname->count, krel->count, kver->count, mach->count, proc->count, hard->count, opsys->count);
  119. exit:
  120. /* deallocate each non-null entry in argtable[] */
  121. arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
  122. return exitcode;
  123. }