env.c 1015 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "test_helpers.h"
  5. int main(void) {
  6. //puts(getenv("SHELL"));
  7. //puts(getenv("CC"));
  8. char* owned = malloc(26); // "TEST=Updates accordingly." + NUL
  9. strcpy(owned, "TEST=It's working!!");
  10. putenv(owned);
  11. puts(getenv("TEST"));
  12. strcpy(owned, "TEST=Updates accordingly.");
  13. puts(getenv("TEST"));
  14. // Allocation is reused
  15. setenv("TEST", "in place", 1);
  16. puts(getenv("TEST"));
  17. puts(owned);
  18. // Allocation is not reused
  19. setenv("TEST", "Value overwritten and not in place because it's really long", 1);
  20. puts(getenv("TEST"));
  21. puts(owned);
  22. // Value is not overwritten
  23. setenv("TEST", "Value not overwritten", 0);
  24. puts(getenv("TEST"));
  25. unsetenv("TEST");
  26. char* env = getenv("TEST");
  27. if (env) {
  28. puts("This should be null, but isn't");
  29. puts(env);
  30. exit(EXIT_FAILURE);
  31. } else {
  32. puts("Value deleted successfully!");
  33. }
  34. free(owned);
  35. }