wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

pathsearch.c (764B)


      1 /* Copyright ©2008-2010 Kris Maglione <maglione.k at Gmail>
      2  * See LICENSE file for license details.
      3  */
      4 #include <string.h>
      5 #include <unistd.h>
      6 #include <fmt.h>
      7 #include "util.h"
      8 
      9 char*
     10 pathsearch(const char *path, const char *file, bool slashok) {
     11 	char *orig, *p, *s;
     12 
     13 	if(!slashok && strchr(file, '/') > file)
     14 		file = sxprint("%s/%s", getcwd(buffer, sizeof buffer), file);
     15 	else if(!strncmp(file, "./",  2))
     16 		file = sxprint("%s/%s", getcwd(buffer, sizeof buffer), file+2);
     17 	if(file[0] == '/') {
     18 		if(access(file, X_OK))
     19 			return strdup(file);
     20 		return nil;
     21 	}
     22 
     23 	orig = estrdup(path ? path : getenv("PATH"));
     24 	for(p=orig; (s=strtok(p, ":")); p=nil) {
     25 		s = smprint("%s/%s", s, file);
     26 		if(!access(s, X_OK))
     27 			break;
     28 		free(s);
     29 	}
     30 	free(orig);
     31 	return s;
     32 }