|
|
1.1 root 1: /*
2: * execve(), tmpnam(), getenv(), path() example for the COHERENT.
3: * find all lines with more than LIMIT characters and call 'me'
4: * to edit them.
5: */
6: #include <stdio.h>
7: #include <path.h>
8: #include <sys/stat.h>
9:
10: #define LIMIT 70
11:
12: extern char *getenv(), **environ, *tempnam();
13:
14: main(argc, argv)
15: char *argv[];
16: {
17: /* me -e tmp file */
18: char *cmda[5] = { NULL, "-e", NULL, NULL, NULL };
19: FILE *ifp, *tmp;
20: char line[256];
21: int ct, len;
22:
23: if ((NULL == (cmda[3] = argv[1])) ||
24: (NULL == (ifp = fopen(argv[1], "r")))) {
25: fprintf(stderr, "Cannot open %s\n", argv[1]);
26: exit(1);
27: }
28:
29: if (NULL == (cmda[0] = path(getenv("PATH"), "me", AEXEC))) {
30: fprintf(stderr, "Cannot locate me\n");
31: exit(1);
32: }
33:
34: if (NULL == (tmp = fopen((cmda[2] = tempnam(NULL, "lng")), "w"))) {
35: fprintf(stderr, "Cannot open tmpfile\n");
36: exit(1);
37: }
38:
39: for (ct = 1; NULL != fgets(line, sizeof(line), ifp); ct++)
40: if (((len = strlen(line)) > LIMIT) ||
41: ('\n' != line[len -1]))
42: fprintf(tmp, "%d: %d characters long\n", ct, len);
43:
44: fclose(tmp);
45: fclose(ifp);
46:
47: if (execve(cmda[0], cmda, environ) < 0) {
48: fprintf(stderr, "cannot execute me\n");
49: exit(1);
50: }
51: /* We never reach here ! */
52: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.