Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #define EXPANDPATH_MAX 256
- char *expandpath(const char *cmd)
- {
- struct stat st;
- char *pathele, *clpt; /* path element, colon pointer */
- static char apath[EXPANDPATH_MAX];
- pathele = getenv("PATH");
- assert(pathele);
- for (clpt = strchr(pathele, ':'); clpt; pathele = clpt + 1, clpt = strchr(pathele, ':')) {
- snprintf(apath, EXPANDPATH_MAX, "%.*s/%s", (int)(clpt - pathele), pathele, cmd);
- if (!stat(apath, &st))
- return apath;
- }
- snprintf(apath, EXPANDPATH_MAX, "%s/%s", pathele, cmd);
- if (!stat(apath, &st))
- return apath;
- return NULL;
- }
- int main(int argc, char **argv)
- {
- char *ptr;
- assert(argv[1]);
- ptr = expandpath(argv[1]);
- printf("%s\n", (ptr ? ptr : "Not found"));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement