about summary refs log tree commit diff
path: root/example.c
blob: 042966328cb26ec006a6ca16c304798f1c01a2eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ste.h"

/* example of libste usage: iterate over $PATH and append argv[1],
   print the entries that fit into PATH_MAX. */

int
main(int argc, char *argv[])
{
	const char *path = getenv("PATH");
	if (!path)
	    path = "";

	const char *program = argc > 1 ? argv[1] : "xyzzy";

	const char *pathend = path + strlen(path);

	char buf[PATH_MAX];
	char *bufend = buf + sizeof buf;

	while (1) {
		char *pos = buf;

		char *colon = stechr(path, pathend, ':');
		if (path == colon)  /* empty entry */
			pos = stecpy(buf, bufend, ".");
		else
			pos = stecpe(buf, bufend, path, colon);

		pos = steprn(pos, bufend, "/%s", program);

		if (pos < bufend) {  /* no trunaction */
			printf("%s\n", buf);
		}

		if (colon == pathend)
			break;
		path = colon + 1;
	}
}