61-array.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. char *env[] = { "foo", "bar", "baz", 0 };
  25. #if 0 //!SYSTEM_LIBC
  26. #define getenv xgetenv
  27. char *
  28. getenv (char const *s)
  29. {
  30. eputs ("getenv\n");
  31. char **p = environ;
  32. int length = strlen (s);
  33. eputs ("getenv length=");
  34. eputs (itoa (length));
  35. eputs ("\n");
  36. while (*p)
  37. {
  38. eputs ("*p=");
  39. eputs (*p);
  40. eputs ("\n");;
  41. eputs (" p=");
  42. eputs (itoa ((long) p));
  43. eputs ("\n");
  44. if (!strncmp (s, *p, length) && *(*p + length) == '=')
  45. return (*p + length + 1);
  46. p++;
  47. }
  48. return 0;
  49. }
  50. #endif
  51. int
  52. test (char **e)
  53. {
  54. int i = 0;
  55. oputs ("\n");
  56. oputs ("a[i] = i-1\n");
  57. int a[3];
  58. for (int i = 0; i < 3; i++)
  59. a[i] = i - 1;
  60. for (int i = 0; i < 3; i++)
  61. if (a[i] != i - 1)
  62. return 1;
  63. oputs ("env [");
  64. oputs (itoa ((long) env));
  65. oputs ("]\n");
  66. oputs ("e [");
  67. oputs (itoa ((int) e));
  68. oputs ("]\n");
  69. oputs ("env [0] == \"foo\"\n");
  70. if (strcmp (env[0], "foo"))
  71. return 2;
  72. oputs ("env [1] == \"bar\"\n");
  73. if (strcmp (env[1], "bar"))
  74. return 3;
  75. oputs ("t: **p in *env[]\n");
  76. char **pp = env;
  77. while (*pp)
  78. {
  79. oputs ("pp [");
  80. oputs (itoa ((int) pp));
  81. oputs ("]: ");
  82. if (*pp)
  83. oputs (*pp);
  84. oputs ("\n");
  85. pp++;
  86. i++;
  87. }
  88. if (i != 3)
  89. return i;
  90. pp = env;
  91. oputs ("t: *pp++ == \"foo\"\n");
  92. if (strcmp (*pp++, "foo"))
  93. return 4;
  94. oputs ("t: *pp++ == \"bar\"\n");
  95. if (strcmp (*pp++, "bar"))
  96. return 5;
  97. char *buf = "hello";
  98. oputs ("t: buf[0]\n");
  99. if (buf[0] != 'h')
  100. return 6;
  101. oputs ("t: buf + 1\n");
  102. if (*(buf + 1) != 'e')
  103. return 7;
  104. char **p = &buf;
  105. oputs ("t: **p\n");
  106. if (**p != 'h')
  107. return 8;
  108. oputs ("t: *(p + 1)\n");
  109. if (*(*p + 1) != 'e')
  110. return 9;
  111. oputs ("t: getenv ()\n");
  112. if (!getenv ("PATH"))
  113. return 10;
  114. oputs ("t: setenv ()\n");
  115. if (setenv ("61-array", "yes", 1))
  116. return 11;
  117. oputs ("t: getenv2 ()\n");
  118. if (strcmp (getenv ("61-array"), "yes"))
  119. return 12;
  120. return 0;
  121. }
  122. int
  123. main (int argc, char *argv[])
  124. {
  125. return test (env);
  126. }