execlp.c 700 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdarg.h>
  4. extern int execvep(const char *path, char *__const argv[], char *__const envp[]);
  5. int execlp(__const char *file, __const char *arg, ...)
  6. {
  7. const char *shortargv[16];
  8. const char **argv;
  9. const char *c;
  10. int i;
  11. va_list args;
  12. i = 1;
  13. va_start(args, arg);
  14. do {
  15. c = va_arg(args, const char *);
  16. i++;
  17. } while (c);
  18. va_end(args);
  19. if (i <= 16)
  20. argv = shortargv;
  21. else {
  22. argv = (const char **) alloca(sizeof(char *) * i);
  23. }
  24. argv[0] = arg;
  25. i = 1;
  26. va_start(args, arg);
  27. do {
  28. argv[i] = va_arg(args, const char *);
  29. } while (argv[i++]);
  30. va_end(args);
  31. i = execvep(file, (char *const *) argv, __environ);
  32. return i;
  33. }