argv.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Set up arguments for main() and prepare environment variables
  3. *
  4. * This code is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Library General Public License version 2.
  6. */
  7. #include "libcflat.h"
  8. #include "argv.h"
  9. #include "auxinfo.h"
  10. int __argc;
  11. const char *__args;
  12. char *__argv[100];
  13. char *__environ[200];
  14. char **environ = __environ;
  15. static char args_copy[1000];
  16. static char *copy_ptr = args_copy;
  17. #define isblank(c) ((c) == ' ' || (c) == '\t')
  18. #define isalpha(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z') || (c) == '_')
  19. #define isalnum(c) (isalpha(c) || ((c) >= '0' && (c) <= '9'))
  20. static const char *skip_blanks(const char *p)
  21. {
  22. while (isblank(*p))
  23. ++p;
  24. return p;
  25. }
  26. void __setup_args(void)
  27. {
  28. const char *args = __args;
  29. char **argv = __argv + __argc;
  30. while (*(args = skip_blanks(args)) != '\0') {
  31. *argv++ = copy_ptr;
  32. while (*args != '\0' && !isblank(*args))
  33. *copy_ptr++ = *args++;
  34. *copy_ptr++ = '\0';
  35. }
  36. __argc = argv - __argv;
  37. }
  38. static void setup_args(const char *args)
  39. {
  40. if (!args)
  41. return;
  42. __args = args;
  43. __setup_args();
  44. }
  45. void setup_args_progname(const char *args)
  46. {
  47. __argv[0] = copy_ptr;
  48. strcpy(__argv[0], auxinfo.progname);
  49. copy_ptr += strlen(auxinfo.progname) + 1;
  50. ++__argc;
  51. setup_args(args);
  52. }
  53. static char *env_eol(char *env)
  54. {
  55. while (*env && *env != '\n')
  56. ++env;
  57. return env;
  58. }
  59. static char *env_invalid_eol(char *env)
  60. {
  61. char *eol = env_eol(env);
  62. char eol_old = *eol;
  63. *eol = '\0';
  64. printf("Invalid environment variable: %s\n", env);
  65. *eol = eol_old;
  66. return eol;
  67. }
  68. static char *env_next(char *env)
  69. {
  70. char *p;
  71. if (!*env)
  72. return env;
  73. if (isalpha(*env)) {
  74. bool invalid = false;
  75. p = env + 1;
  76. while (*p && *p != '=' && *p != '\n') {
  77. if (!isalnum(*p))
  78. invalid = true;
  79. ++p;
  80. }
  81. if (*p != '=')
  82. invalid = true;
  83. if (invalid) {
  84. env = env_invalid_eol(env);
  85. return *env ? env_next(env + 1) : env;
  86. }
  87. return env;
  88. }
  89. p = env;
  90. while (isblank(*p))
  91. ++p;
  92. if (*p == '\n')
  93. return env_next(p + 1);
  94. if (*p == '#')
  95. env = env_eol(env);
  96. else
  97. env = env_invalid_eol(env);
  98. return *env ? env_next(env + 1) : env;
  99. }
  100. void setup_env(char *env, int size)
  101. {
  102. char *eof = env + size, *p = env;
  103. bool newline = false;
  104. int i = 0;
  105. while (*p)
  106. ++p;
  107. if (p == eof)
  108. newline = true;
  109. while (env < eof) {
  110. if (newline)
  111. env = env_next(env);
  112. if (!*env || env >= eof)
  113. break;
  114. __environ[i++] = env;
  115. while (env < eof && *env && !(newline && *env == '\n'))
  116. ++env;
  117. *env++ = '\0';
  118. }
  119. }