|
|
1.1 root 1:
2:
3: execve() COHERENT System Call execve()
4:
5:
6:
7:
8: Execute a load module
9:
10: eexxeeccvvee(_f_i_l_e, _a_r_g_v, _e_n_v)
11: cchhaarr *_f_i_l_e, *_a_r_g_v[], *_e_n_v[];
12:
13: The COHERENT system includes six functions that allow a process
14: to execute another executable file (or load module, as described
15: in the header l.out.h). execve specifies arguments as a single,
16: NULL-terminated array of parameters, called argv. The argument
17: env is the address of an array of pointers to strings that define
18: file's environment. This allows execve to pass a new environment
19: to the program being executed. For more information on program
20: execution, see execl.
21:
22: ***** Example *****
23:
24: The following example demonstrates execve, as well as tmpnam,
25: getenv, and path. It finds all lines with more than LIMIT
26: characters and call MicroEMACS to edit them.
27:
28:
29: #include <stdio.h>
30: #include <path.h>
31: #include <sys/stat.h>
32:
33:
34:
35: #define LIMIT 70
36:
37:
38:
39: extern char *getenv(), **environ, *tempnam();
40:
41:
42:
43: main(argc, argv)
44: char *argv[];
45: {
46: /* me -e tmp file */
47: char *cmda[5] = { NULL, "-e", NULL, NULL, NULL };
48: FILE *ifp, *tmp;
49: char line[256];
50: int ct, len;
51:
52:
53:
54: if ((NULL == (cmda[3] = argv[1])) ||
55: (NULL == (ifp = fopen(argv[1], "r")))) {
56: fprintf(stderr, "Cannot open %s\n", argv[1]);
57: exit(1);
58: }
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: execve() COHERENT System Call execve()
70:
71:
72:
73:
74: if (cmda[0] = path(getenv("PATH"), "me", AEXEC) == NULL) {
75: fprintf(stderr, "Cannot locate me\n");
76: exit(1);
77: }
78:
79:
80:
81: if (NULL == (tmp = fopen((cmda[2] = tempnam(NULL, "lng")), "w"))) {
82: fprintf(stderr, "Cannot open tmpfile\n");
83: exit(1);
84: }
85:
86:
87:
88: for (ct = 1; NULL != fgets(line, sizeof(line), ifp); ct++)
89: if (((len = strlen(line)) > LIMIT) ||
90: ('\n' != line[len -1]))
91: fprintf(tmp, "%d: %d characters long\n", ct, len);
92:
93:
94:
95: fclose(tmp);
96: fclose(ifp);
97:
98:
99:
100: if (execve(cmda[0], cmda, environ) < 0) {
101: fprintf(stderr, "cannot execute me\n");
102: exit(1);
103: }
104: /* We never reach here ! */
105: }
106:
107:
108: ***** See Also *****
109:
110: COHERENT system calls, environ, execution
111:
112: ***** Diagnostics *****
113:
114: execve does not return if successful. It returns -1 for errors,
115: such as file being nonexistent, not accessible with execute per-
116: mission, having a bad format, or too large to fit in memory.
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.