env.c 978 B

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