winpath.cpp 860 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2024 Stefan Weil
  2. //
  3. // SPDX-License-Identifier: Apache-2.0
  4. //
  5. // winpath - run a Windows program with extended PATH
  6. //
  7. // Usage:
  8. //
  9. // winpath [CMD [ARGUMENT ...]]
  10. //
  11. // Example:
  12. //
  13. // winpath cmd
  14. //
  15. // This will start a Windows command line with PATH extended by
  16. // the location of the winpath executable.
  17. #include <process.h> // _spawnvp
  18. #include <stdlib.h> // _putenv_s
  19. #include <string.h> // strcpy, strcat
  20. static char path[4096];
  21. int main(int argc, char *argv[]) {
  22. if (argc > 1) {
  23. char *dir = argv[0];
  24. char *last = strrchr(dir, '\\');
  25. if (last != nullptr) {
  26. *last = '\0';
  27. }
  28. strcpy(path, dir);
  29. strcat(path, ";");
  30. strcat(path, getenv("PATH"));
  31. _putenv_s("PATH", path);
  32. _spawnvp(_P_WAIT, argv[1], argv + 1);
  33. //~ _spawnvp(_P_OVERLAY, argv[1], argv + 1);
  34. }
  35. return 0;
  36. }