bug-envz1.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Test for bug BZ #2703. */
  2. #include <stdio.h>
  3. #include <envz.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. static const struct
  7. {
  8. const char *s;
  9. int in_result;
  10. } strs[] =
  11. {
  12. { "a=1", 1 },
  13. { "b=2", 1 },
  14. { "(*)", 0 },
  15. { "(*)", 0 },
  16. { "e=5", 1 },
  17. { "f=", 1 },
  18. { "(*)", 0 },
  19. { "h=8", 1 },
  20. { "i=9", 1 },
  21. { "j", 0 }
  22. };
  23. #define nstrs (sizeof (strs) / sizeof (strs[0]))
  24. int
  25. do_test (void)
  26. {
  27. size_t size = 0;
  28. char *str = malloc (100);
  29. if (str == NULL)
  30. {
  31. puts ("out of memory");
  32. return 1;
  33. }
  34. char **argz = &str;
  35. for (int i = 0; i < nstrs; ++i)
  36. argz_add_sep (argz, &size, strs[i].s, '\0');
  37. printf ("calling envz_strip with size=%zu\n", size);
  38. envz_strip (argz, &size);
  39. int result = 0;
  40. printf ("new size=%zu\n", size);
  41. for (int i = 0; i < nstrs; ++i)
  42. if (strs[i].in_result)
  43. {
  44. char name[2];
  45. name[0] = strs[i].s[0];
  46. name[1] = '\0';
  47. char *e = envz_entry (*argz, size, name);
  48. if (e == NULL)
  49. {
  50. printf ("entry '%s' not found\n", name);
  51. result = 1;
  52. }
  53. else if (strcmp (e, strs[i].s) != 0)
  54. {
  55. printf ("entry '%s' does not match: is '%s', expected '%s'\n",
  56. name, e, strs[i].s);
  57. result = 1;
  58. }
  59. }
  60. free (*argz);
  61. return result;
  62. }
  63. #include <support/test-driver.c>