getargv.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2012 Qualcomm Atheros Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software
  6. * for any purpose with or without fee is hereby granted, provided
  7. * that the above copyright notice and this permission notice appear
  8. * in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  13. * THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  14. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  16. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  17. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. *
  19. *--------------------------------------------------------------------*/
  20. /*====================================================================*
  21. *
  22. * signed getargv (signed argc, char const * argv [])
  23. *
  24. * symbol.h
  25. *
  26. * read one line from stdin; fill argv [] with fields from that
  27. * line; return the number of fields found; ignore blank lines
  28. * and script style comment lines; this implementation inserts
  29. * a program name at argv [0] to emulate a true argv [];
  30. *
  31. *--------------------------------------------------------------------*/
  32. #ifndef GETARGV_SOURCE
  33. #define GETARGV_SOURCE
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #include <memory.h>
  37. #include "../tools/symbol.h"
  38. #include "../tools/chars.h"
  39. signed getargv (signed argc, char const * argv [])
  40. {
  41. extern char const * program_name;
  42. static char string [1024];
  43. char const ** argp = argv;
  44. char * sp = string;
  45. signed c = getc (stdin);
  46. memset (string, 0, sizeof (string));
  47. memset ((char **)(argv), 0, argc * sizeof (char const *));
  48. while (nobreak (c))
  49. {
  50. if (isspace (c))
  51. {
  52. do
  53. {
  54. c = getc (stdin);
  55. }
  56. while (isspace (c));
  57. }
  58. if (c == '#')
  59. {
  60. do
  61. {
  62. c = getc (stdin);
  63. }
  64. while (nobreak (c));
  65. c = getc (stdin);
  66. continue;
  67. }
  68. *argp++ = program_name;
  69. *argp++ = sp = string;
  70. while (nobreak (c))
  71. {
  72. if (c == '#')
  73. {
  74. do
  75. {
  76. c = getc (stdin);
  77. }
  78. while (nobreak (c));
  79. break;
  80. }
  81. if (isblank (c))
  82. {
  83. c = (char)(0);
  84. *argp = sp + 1;
  85. }
  86. else if (sp == *argp)
  87. {
  88. if ((signed)(argp - argv) < argc)
  89. {
  90. argp++;
  91. }
  92. }
  93. *sp++ = (char)(c);
  94. c = getc (stdin);
  95. }
  96. *argp = (char const *)(0);
  97. *sp = (char)(0);
  98. }
  99. return ((unsigned)(argp - argv));
  100. }
  101. #endif