execvep.c 667 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. int execvep(const char *path, char *__const argv[], char *__const envp[])
  6. {
  7. if (!strchr(path, '/')) {
  8. char partial[128];
  9. char *p = getenv("PATH");
  10. char *p2;
  11. if (!p)
  12. p = "/bin:/usr/bin";
  13. for (; p && *p;) {
  14. strcpy(partial, p);
  15. p2 = strchr(partial, ':');
  16. if (p2)
  17. *p2 = '\0';
  18. if (strlen(partial))
  19. strcat(partial, "/");
  20. strcat(partial, path);
  21. execve(partial, argv, envp);
  22. if (errno != ENOENT)
  23. return -1;
  24. p2 = strchr(p, ':');
  25. if (p2)
  26. p = p2 + 1;
  27. else
  28. p = 0;
  29. }
  30. return -1;
  31. } else
  32. return execve(path, argv, envp);
  33. }