exec.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "lib.h"
  2. extern char **environ; /* environment pointer */
  3. #define PTRSIZE sizeof(char *)
  4. PUBLIC int execl(name, arg0)
  5. char *name;
  6. char *arg0;
  7. {
  8. return execve(name, &arg0, environ);
  9. }
  10. PUBLIC int execle(name, argv)
  11. char *name, *argv;
  12. {
  13. char **p;
  14. p = (char **) &argv;
  15. while (*p++) /* null statement */ ;
  16. return execve(name, &argv, *p);
  17. }
  18. PUBLIC int execv(name, argv)
  19. char *name, *argv[];
  20. {
  21. return execve(name, argv, environ);
  22. }
  23. PUBLIC int execve(name, argv, envp)
  24. char *name; /* pointer to name of file to be executed */
  25. char *argv[]; /* pointer to argument array */
  26. char *envp[]; /* pointer to environment */
  27. {
  28. char stack[MAX_ISTACK_BYTES];
  29. char **argorg, **envorg, *hp, **ap, *p;
  30. int i, nargs, nenvps, stackbytes, offset;
  31. extern errno;
  32. /* Count the argument pointers and environment pointers. */
  33. nargs = 0;
  34. nenvps = 0;
  35. argorg = argv;
  36. envorg = envp;
  37. while (*argorg++ != NIL_PTR) nargs++;
  38. while (*envorg++ != NIL_PTR) nenvps++;
  39. /* Prepare to set up the initial stack. */
  40. hp = &stack[(nargs + nenvps + 3) * PTRSIZE];
  41. if (hp + nargs + nenvps >= &stack[MAX_ISTACK_BYTES]) {
  42. errno = E2BIG;
  43. return(-1);
  44. }
  45. ap = (char **) stack;
  46. *ap++ = (char *) nargs;
  47. /* Prepare the argument pointers and strings. */
  48. for (i = 0; i < nargs; i++) {
  49. offset = hp - stack;
  50. *ap++ = (char *) offset;
  51. p = *argv++;
  52. while (*p) {
  53. *hp++ = *p++;
  54. if (hp >= &stack[MAX_ISTACK_BYTES]) {
  55. errno = E2BIG;
  56. return(-1);
  57. }
  58. }
  59. *hp++ = (char) 0;
  60. }
  61. *ap++ = NIL_PTR;
  62. /* Prepare the environment pointers and strings. */
  63. for (i = 0; i < nenvps; i++) {
  64. offset = hp - stack;
  65. *ap++ = (char *) offset;
  66. p = *envp++;
  67. while (*p) {
  68. *hp++ = *p++;
  69. if (hp >= &stack[MAX_ISTACK_BYTES]) {
  70. errno = E2BIG;
  71. return(-1);
  72. }
  73. }
  74. *hp++ = (char) 0;
  75. }
  76. *ap++ = NIL_PTR;
  77. stackbytes = ( ( (int)(hp - stack) + PTRSIZE - 1)/PTRSIZE) * PTRSIZE;
  78. return callm1(MM_PROC_NR, EXEC, len(name), stackbytes, 0,name, stack,NIL_PTR);
  79. }
  80. PUBLIC execn(name)
  81. char *name; /* pointer to file to be exec'd */
  82. {
  83. /* Special version used when there are no args and no environment. This call
  84. * is principally used by INIT, to avoid having to allocate MAX_ISTACK_BYTES.
  85. */
  86. static char stack[3 * PTRSIZE];
  87. return callm1(MM_PROC_NR, EXEC, len(name), sizeof(stack), 0, name, stack, NIL_PTR);
  88. }