execvep.c 769 B

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