env.c 958 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2018 K. Lange
  5. *
  6. * env - Print or set environment
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. extern int _environ_size;
  14. int main(int argc, char ** argv) {
  15. int start = 1;
  16. if (start < argc && !strcmp(argv[start],"-i")) {
  17. for (int i = 0; i < _environ_size; ++i) {
  18. environ[i] = NULL;
  19. }
  20. start++;
  21. }
  22. for (; start < argc; ++start) {
  23. if (!strchr(argv[start],'=')) {
  24. break;
  25. } else {
  26. putenv(argv[start]);
  27. }
  28. }
  29. if (start < argc) {
  30. /* Execute command */
  31. if (execvp(argv[start], &argv[start])) {
  32. fprintf(stderr, "%s: %s: %s\n", argv[0], argv[start], strerror(errno));
  33. }
  34. } else {
  35. char ** env = environ;
  36. while (*env) {
  37. printf("%s\n", *env);
  38. env++;
  39. }
  40. }
  41. return 0;
  42. }