updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / gazelle-git / 1.patch
blobe672aa9eed798763a03a17e20355274d1948f6b5
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
5 @@ -14,6 +14,10 @@
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 +#include <limits.h>
10 +#include <sys/stat.h>
11 +#include <sys/types.h>
12 +#include <unistd.h>
14 #include "glue.h"
15 #include "lua.h"
16 @@ -27,6 +31,81 @@ typedef struct
17 char buff[512];
18 } State;
20 +#ifndef PATH_MAX
21 +#define PATH_MAX 512
22 +#endif
24 +/* int checkifexecutable(const char *filename)
25 + *
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.
28 + */
30 +int checkifexecutable(const char *filename)
32 + int result;
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)
46 + *
47 + * Find executable by searching the PATH environment variable.
48 + *
49 + * const char *exe - executable name to search for.
50 + * char *pth - the path found is stored here, space
51 + * needs to be available.
52 + *
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.
55 + */
57 +int findpathof(char *pth, const char *exe)
59 + char *searchpath;
60 + char *beg, *end;
61 + int stop, found;
62 + int len;
64 + if (strchr(exe, '/') != NULL) {
65 + if (realpath(exe, pth) == NULL) return 0;
66 + return checkifexecutable(pth);
67 + }
69 + searchpath = getenv("PATH");
70 + if (searchpath == NULL) return 0;
71 + if (strlen(searchpath) <= 0) return 0;
73 + beg = searchpath;
74 + stop = 0; found = 0;
75 + do {
76 + end = strchr(beg, ':');
77 + if (end == NULL) {
78 + stop = 1;
79 + strncpy(pth, beg, PATH_MAX);
80 + len = strlen(pth);
81 + } else {
82 + strncpy(pth, beg, end - beg);
83 + pth[end - beg] = '\0';
84 + len = end - beg;
85 + }
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);
92 + return found;
95 static const char *myget(lua_State *L, void *data, size_t *size)
97 State* s=data;
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];
103 int i;
104 lua_gc(L,LUA_GCSTOP,0);
105 luaL_openlibs(L);
106 lua_gc(L,LUA_GCRESTART,0);
107 - load(L,argv[0]);
108 + if(!findpathof(path, argv[0])){
109 + fprintf(stderr, "%s not found in path", argv[0]);
110 + return 1;
112 + load(L,path);
113 for (i=1; argv[i]; i++) ; /* count */
114 lua_createtable(L,i-1,1);
115 for (i=0; argv[i]; i++)