diff options
author | Leah Neukirchen <leah@vuxu.org> | 2021-11-05 18:52:11 +0100 |
---|---|---|
committer | Leah Neukirchen <leah@vuxu.org> | 2021-11-05 18:52:46 +0100 |
commit | 75ea20675d012e692996bc6beae9149b0deb08b0 (patch) | |
tree | ab1569403e1c5f8e76f5f93f969a3335275c54a5 /example.c | |
download | libste-75ea20675d012e692996bc6beae9149b0deb08b0.tar.gz libste-75ea20675d012e692996bc6beae9149b0deb08b0.tar.xz libste-75ea20675d012e692996bc6beae9149b0deb08b0.zip |
initial commit
Diffstat (limited to 'example.c')
-rw-r--r-- | example.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/example.c b/example.c new file mode 100644 index 0000000..e9f57d5 --- /dev/null +++ b/example.c @@ -0,0 +1,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[]) +{ + char *path = getenv("PATH"); + if (!path) + path = ""; + + char *program = argc > 1 ? argv[1] : "xyzzy"; + + 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; + } +} |