execle.c 812 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 execle(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. const char *const *envp;
  13. i = 1;
  14. va_start(args, arg);
  15. do {
  16. c = va_arg(args, const char *);
  17. i++;
  18. } while (c);
  19. va_end(args);
  20. if (i <= 16)
  21. argv = shortargv;
  22. else {
  23. argv = (const char **) alloca(sizeof(char *) * i);
  24. }
  25. argv[0] = arg;
  26. i = 1;
  27. va_start(args, arg);
  28. do {
  29. argv[i] = va_arg(args, const char *);
  30. } while (argv[i++]);
  31. envp = va_arg (args, const char *const *);
  32. va_end(args);
  33. i = execvep(file, (char *const *) argv, (char *const *) envp);
  34. if (argv != shortargv)
  35. free(argv);
  36. return i;
  37. }