1 diff --git a/utilities/srlua.c b/utilities/srlua.c
2 index 9203c41..51b2229 100644
3 --- a/utilities/srlua.c
4 +++ b/utilities/srlua.c
10 +#include <sys/stat.h>
11 +#include <sys/types.h>
16 @@ -27,6 +31,81 @@ typedef struct
24 +/* int checkifexecutable(const char *filename)
26 + * Return non-zero if the name is an executable file, and
27 + * zero if it is not executable, or if it does not exist.
30 +int checkifexecutable(const char *filename)
33 + struct stat statinfo;
35 + result = stat(filename, &statinfo);
36 + if (result < 0) return 0;
37 + if (!S_ISREG(statinfo.st_mode)) return 0;
39 + if (statinfo.st_uid == geteuid()) return statinfo.st_mode & S_IXUSR;
40 + if (statinfo.st_gid == getegid()) return statinfo.st_mode & S_IXGRP;
41 + return statinfo.st_mode & S_IXOTH;
45 +/* int findpathof(char *pth, const char *exe)
47 + * Find executable by searching the PATH environment variable.
49 + * const char *exe - executable name to search for.
50 + * char *pth - the path found is stored here, space
51 + * needs to be available.
53 + * If a path is found, returns non-zero, and the path is stored
54 + * in pth. If exe is not found returns 0, with pth undefined.
57 +int findpathof(char *pth, const char *exe)
64 + if (strchr(exe, '/') != NULL) {
65 + if (realpath(exe, pth) == NULL) return 0;
66 + return checkifexecutable(pth);
69 + searchpath = getenv("PATH");
70 + if (searchpath == NULL) return 0;
71 + if (strlen(searchpath) <= 0) return 0;
74 + stop = 0; found = 0;
76 + end = strchr(beg, ':');
79 + strncpy(pth, beg, PATH_MAX);
82 + strncpy(pth, beg, end - beg);
83 + pth[end - beg] = '\0';
86 + if (pth[len - 1] != '/') strncat(pth, "/", 1);
87 + strncat(pth, exe, PATH_MAX - len);
88 + found = checkifexecutable(pth);
89 + if (!stop) beg = end + 1;
90 + } while (!stop && !found);
95 static const char *myget(lua_State *L, void *data, size_t *size)
98 @@ -60,11 +139,16 @@ static void load(lua_State *L, const char *name)
99 static int pmain(lua_State *L)
101 char **argv=lua_touserdata(L,1);
102 + char path[PATH_MAX+1];
104 lua_gc(L,LUA_GCSTOP,0);
106 lua_gc(L,LUA_GCRESTART,0);
108 + if(!findpathof(path, argv[0])){
109 + fprintf(stderr, "%s not found in path", argv[0]);
113 for (i=1; argv[i]; i++) ; /* count */
114 lua_createtable(L,i-1,1);
115 for (i=0; argv[i]; i++)