|
| 1 | + |
| 2 | +#include "mruby.h" |
| 3 | +#define dln_warning mrb_warn |
| 4 | +#define dln_warning_arg |
| 5 | + |
| 6 | +//#include "dln.h" |
| 7 | + |
| 8 | +#include <stdlib.h> |
| 9 | +#include <strings.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <string.h> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | +#if defined(_WIN32) |
| 15 | +//#include "missing/file.h" |
| 16 | +#endif |
| 17 | + |
| 18 | +#include <sys/types.h> |
| 19 | +#include <sys/stat.h> |
| 20 | + |
| 21 | +#ifndef S_ISDIR |
| 22 | +# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 23 | +#endif |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +// TODO makros weg, wrong usage. blergh |
| 29 | +#ifndef PATH_ENV |
| 30 | +#define PATH_ENV "path" |
| 31 | +#endif |
| 32 | +#ifndef PATH_SEP |
| 33 | +#define PATH_SEP ";" |
| 34 | +#endif |
| 35 | + |
| 36 | +#if !defined(_WIN32) && !HAVE_DECL_GETENV |
| 37 | +char *getenv(); |
| 38 | +#endif |
| 39 | + |
| 40 | +static char *dln_find_1(const char *fname, const char *path, char *buf, size_t size, int exe_flag /*DLN_FIND_EXTRA_ARG_DECL*/); |
| 41 | +char* dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size /*DLN_FIND_EXTRA_ARG_DECL*/); |
| 42 | +char* dln_find_file_r(const char *fname, const char *path, char *buf, size_t size /* DLN_FIND_EXTRA_ARG_DECL*/); |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +char* |
| 47 | +dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size /*DLN_FIND_EXTRA_ARG_DECL*/) |
| 48 | +{ |
| 49 | + char *envpath = 0; |
| 50 | + if (!path) { |
| 51 | + path = getenv(PATH_ENV); |
| 52 | + if (path) |
| 53 | + path = envpath = strdup(path); |
| 54 | + } |
| 55 | + |
| 56 | + if (!path) { |
| 57 | + path = |
| 58 | + "/usr/local/bin" PATH_SEP |
| 59 | + "/usr/ucb" PATH_SEP |
| 60 | + "/usr/bin" PATH_SEP |
| 61 | + "/bin" PATH_SEP |
| 62 | + "."; |
| 63 | + } |
| 64 | + buf = dln_find_1(fname, path, buf, size, 1 /*DLN_FIND_EXTRA_ARG*/); |
| 65 | + if (envpath) |
| 66 | + free(envpath); |
| 67 | + return buf; |
| 68 | +} |
| 69 | + |
| 70 | +char * |
| 71 | +dln_find_file_r(const char *fname, const char *path, char *buf, size_t size |
| 72 | +/*DLN_FIND_EXTRA_ARG_DECL*/) |
| 73 | +{ |
| 74 | + if (!path) path = "."; |
| 75 | + return dln_find_1(fname, path, buf, size, 0 /*DLN_FIND_EXTRA_ARG*/); |
| 76 | +} |
| 77 | + |
| 78 | +static char * |
| 79 | +dln_find_1(const char *fname, const char *path, char *fbuf, size_t size, |
| 80 | + int exe_flag /* non 0 if looking for executable. */ |
| 81 | + /*DLN_FIND_EXTRA_ARG_DECL*/) |
| 82 | +{ |
| 83 | + register const char *dp; |
| 84 | + register const char *ep; |
| 85 | + register char *bp; |
| 86 | + struct stat st; |
| 87 | + size_t i, fnlen, fspace; |
| 88 | + |
| 89 | + |
| 90 | +#ifdef DOSISH |
| 91 | + static const char extension[][5] = { |
| 92 | + EXECUTABLE_EXTS, |
| 93 | + }; |
| 94 | + size_t j; |
| 95 | + int is_abs = 0, has_path = 0; |
| 96 | + const char *ext = 0; |
| 97 | +#endif |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + const char *p = fname; |
| 102 | + |
| 103 | + static const char pathname_too_long[] = "openpath: pathname too long (ignored)\n\ |
| 104 | +\tDirectory \"%.*s\"%s\n\tFile \"%.*s\"%s\n"; |
| 105 | +/*#define PATHNAME_TOO_LONG() dln_warning(dln_warning_arg pathname_too_long, \ |
| 106 | + ((bp - fbuf) > 100 ? 100 : (int)(bp - fbuf)), fbuf, \ |
| 107 | + ((bp - fbuf) > 100 ? "..." : ""), \ |
| 108 | + (fnlen > 100 ? 100 : (int)fnlen), fname, \ |
| 109 | + (fnlen > 100 ? "..." : ""))*/ |
| 110 | + |
| 111 | +#define RETURN_IF(expr) if (expr) return (char *)fname; |
| 112 | + |
| 113 | + RETURN_IF(!fname); |
| 114 | + fnlen = strlen(fname); |
| 115 | + if (fnlen >= size) { |
| 116 | + /*dln_warning(dln_warning_arg |
| 117 | + "openpath: pathname too long (ignored)\n\tFile \"%.*s\"%s\n", |
| 118 | + (fnlen > 100 ? 100 : (int)fnlen), fname, |
| 119 | + (fnlen > 100 ? "..." : ""));*/ |
| 120 | + return NULL; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | +#ifdef DOSISH |
| 125 | +# ifndef CharNext |
| 126 | +# define CharNext(p) ((p)+1) |
| 127 | +# endif |
| 128 | +# ifdef DOSISH_DRIVE_LETTER |
| 129 | + if (((p[0] | 0x20) - 'a') < 26 && p[1] == ':') { |
| 130 | + p += 2; |
| 131 | + is_abs = 1; |
| 132 | + } |
| 133 | +# endif |
| 134 | + switch (*p) { |
| 135 | + case '/': case '\\': |
| 136 | + is_abs = 1; |
| 137 | + p++; |
| 138 | + } |
| 139 | + has_path = is_abs; |
| 140 | + while (*p) { |
| 141 | + switch (*p) { |
| 142 | + case '/': case '\\': |
| 143 | + has_path = 1; |
| 144 | + ext = 0; |
| 145 | + p++; |
| 146 | + break; |
| 147 | + case '.': |
| 148 | + ext = p; |
| 149 | + p++; |
| 150 | + break; |
| 151 | + default: |
| 152 | + p = CharNext(p); |
| 153 | + } |
| 154 | + } |
| 155 | + if (ext) { |
| 156 | + for (j = 0; STRCASECMP(ext, extension[j]); ) { |
| 157 | + if (++j == sizeof(extension) / sizeof(extension[0])) { |
| 158 | + ext = 0; |
| 159 | + break; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + ep = bp = 0; |
| 164 | + printf("returned at %s", "something?"); |
| 165 | + if (!exe_flag) { |
| 166 | + printf("returned at %s", "exe_flag?"); |
| 167 | + RETURN_IF(is_abs); |
| 168 | + printf("nope\n"); |
| 169 | + } |
| 170 | + else if (has_path) { |
| 171 | + printf("returned at %s", "if ext?"); |
| 172 | + RETURN_IF(ext); |
| 173 | + printf("nope\n"); |
| 174 | + i = p - fname; |
| 175 | + if (i + 1 > size) goto toolong; |
| 176 | + fspace = size - i - 1; |
| 177 | + bp = fbuf; |
| 178 | + ep = p; |
| 179 | + printf("ep is here %s\n", ep); |
| 180 | + memcpy(fbuf, fname, i + 1); |
| 181 | + goto needs_extension; |
| 182 | + } |
| 183 | + p = fname; |
| 184 | +#endif |
| 185 | + |
| 186 | + |
| 187 | + if (*p == '.' && *++p == '.') ++p; |
| 188 | + RETURN_IF(*p == '/'); |
| 189 | + RETURN_IF(exe_flag && strchr(fname, '/')); |
| 190 | + |
| 191 | +#undef RETURN_IF |
| 192 | + |
| 193 | + for (dp = path;; dp = ++ep) { |
| 194 | + register size_t l; |
| 195 | + |
| 196 | + /* extract a component */ |
| 197 | + ep = strchr(dp, PATH_SEP[0]); |
| 198 | + if (ep == NULL){ |
| 199 | + ep = dp+strlen(dp); |
| 200 | + } |
| 201 | + /* find the length of that component */ |
| 202 | + l = ep - dp; |
| 203 | + bp = fbuf; |
| 204 | + fspace = size - 2; |
| 205 | + if (l > 0) { |
| 206 | + /* |
| 207 | + ** If the length of the component is zero length, |
| 208 | + ** start from the current directory. If the |
| 209 | + ** component begins with "~", start from the |
| 210 | + ** user's $HOME environment variable. Otherwise |
| 211 | + ** take the path literally. |
| 212 | + */ |
| 213 | + if (*dp == '~' && (l == 1 || |
| 214 | +#if defined(DOSISH) |
| 215 | + dp[1] == '\\' || |
| 216 | +#endif |
| 217 | + dp[1] == '/')) { |
| 218 | + |
| 219 | + char *home; |
| 220 | + |
| 221 | + home = getenv("HOME"); |
| 222 | + if (home != NULL) { |
| 223 | + i = strlen(home); |
| 224 | + if (fspace < i) |
| 225 | + goto toolong; |
| 226 | + fspace -= i; |
| 227 | + memcpy(bp, home, i); |
| 228 | + bp += i; |
| 229 | + } |
| 230 | + dp++; |
| 231 | + l--; |
| 232 | + } |
| 233 | + if (l > 0) { |
| 234 | + if (fspace < l) |
| 235 | + goto toolong; |
| 236 | + fspace -= l; |
| 237 | + memcpy(bp, dp, l); |
| 238 | + bp += l; |
| 239 | + } |
| 240 | + |
| 241 | + /* add a "/" between directory and filename */ |
| 242 | + if (ep[-1] != '/') |
| 243 | + *bp++ = '/'; |
| 244 | + } |
| 245 | + |
| 246 | + /* now append the file name */ |
| 247 | + i = fnlen; |
| 248 | + if (fspace < i) { |
| 249 | + toolong: |
| 250 | + printf("Pathname is too long \n" );//PATHNAME_TOO_LONG(); |
| 251 | + goto next; |
| 252 | + } |
| 253 | + fspace -= i; |
| 254 | + memcpy(bp, fname, i + 1); |
| 255 | + |
| 256 | +#if defined(DOSISH) |
| 257 | + if (exe_flag && !ext) { |
| 258 | + needs_extension: |
| 259 | + for (j = 0; j < sizeof(extension) / sizeof(extension[0]); j++) { |
| 260 | + if (fspace < strlen(extension[j])) { |
| 261 | + PATHNAME_TOO_LONG(); |
| 262 | + continue; |
| 263 | + } |
| 264 | + strlcpy(bp + i, extension[j], fspace); |
| 265 | + if (stat(fbuf, &st) == 0){ |
| 266 | + return fbuf; |
| 267 | + } |
| 268 | + } |
| 269 | + goto next; |
| 270 | + } |
| 271 | +#endif |
| 272 | + |
| 273 | +#ifndef S_ISREG |
| 274 | +# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
| 275 | +#endif |
| 276 | + if (stat(fbuf, &st) == 0 && S_ISREG(st.st_mode)) { |
| 277 | + if (exe_flag == 0){ |
| 278 | + return fbuf; |
| 279 | + } |
| 280 | + /* looking for executable */ |
| 281 | + if (access(fbuf, X_OK) == 0) return fbuf; |
| 282 | + } |
| 283 | + next: |
| 284 | + /* if not, and no other alternatives, life is bleak */ |
| 285 | + if (*ep == '\0') { |
| 286 | + return NULL; |
| 287 | + } |
| 288 | + |
| 289 | + /* otherwise try the next component in the search path */ |
| 290 | + } |
| 291 | +} |
0 commit comments