Skip to content

Commit dbb271d

Browse files
committed
Cleanup src/dln.c
1 parent f637536 commit dbb271d

1 file changed

Lines changed: 42 additions & 76 deletions

File tree

src/dln.c

Lines changed: 42 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,47 @@
1-
2-
#include "mruby.h"
3-
#define dln_warning mrb_warn
4-
#define dln_warning_arg
5-
6-
//#include "dln.h"
1+
/* MIT License
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
721

822
#include <stdlib.h>
923
#include <strings.h>
10-
#include <stdio.h>
1124
#include <string.h>
1225
#include <unistd.h>
13-
14-
#if defined(_WIN32)
15-
//#include "missing/file.h"
16-
#endif
17-
1826
#include <sys/types.h>
1927
#include <sys/stat.h>
2028

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
2929
#ifndef PATH_ENV
30-
#define PATH_ENV "path"
30+
# define PATH_ENV "path"
3131
#endif
3232
#ifndef PATH_SEP
33-
#define PATH_SEP ";"
34-
#endif
35-
36-
#if !defined(_WIN32) && !HAVE_DECL_GETENV
37-
char *getenv();
33+
# define PATH_SEP ";"
3834
#endif
3935

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-
36+
static char *dln_find_1(const char *fname, const char *path, char *buf, size_t size, int exe_flag);
37+
char* dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size);
38+
char* dln_find_file_r(const char *fname, const char *path, char *buf, size_t size);
4539

4640
char*
47-
dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size /*DLN_FIND_EXTRA_ARG_DECL*/)
41+
dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size)
4842
{
4943
char *envpath = 0;
44+
5045
if (!path) {
5146
path = getenv(PATH_ENV);
5247
if (path)
@@ -61,32 +56,31 @@ dln_find_exe_r(const char *fname, const char *path, char *buf, size_t size /*DLN
6156
"/bin" PATH_SEP
6257
".";
6358
}
64-
buf = dln_find_1(fname, path, buf, size, 1 /*DLN_FIND_EXTRA_ARG*/);
59+
60+
buf = dln_find_1(fname, path, buf, size, 1);
61+
6562
if (envpath)
6663
free(envpath);
64+
6765
return buf;
6866
}
6967

7068
char *
71-
dln_find_file_r(const char *fname, const char *path, char *buf, size_t size
72-
/*DLN_FIND_EXTRA_ARG_DECL*/)
69+
dln_find_file_r(const char *fname, const char *path, char *buf, size_t size)
7370
{
7471
if (!path) path = ".";
75-
return dln_find_1(fname, path, buf, size, 0 /*DLN_FIND_EXTRA_ARG*/);
72+
return dln_find_1(fname, path, buf, size, 0);
7673
}
7774

7875
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*/)
76+
dln_find_1(const char *fname, const char *path, char *fbuf, size_t size, int exe_flag)
8277
{
8378
register const char *dp;
8479
register const char *ep;
8580
register char *bp;
8681
struct stat st;
8782
size_t i, fnlen, fspace;
8883

89-
9084
#ifdef DOSISH
9185
static const char extension[][5] = {
9286
EXECUTABLE_EXTS,
@@ -96,35 +90,18 @@ dln_find_1(const char *fname, const char *path, char *fbuf, size_t size,
9690
const char *ext = 0;
9791
#endif
9892

99-
100-
10193
const char *p = fname;
10294

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-
11195
#define RETURN_IF(expr) if (expr) return (char *)fname;
11296

11397
RETURN_IF(!fname);
98+
11499
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-
}
122100

101+
if (fnlen >= size)
102+
return NULL;
123103

124104
#ifdef DOSISH
125-
# ifndef CharNext
126-
# define CharNext(p) ((p)+1)
127-
# endif
128105
# ifdef DOSISH_DRIVE_LETTER
129106
if (((p[0] | 0x20) - 'a') < 26 && p[1] == ':') {
130107
p += 2;
@@ -161,29 +138,23 @@ dln_find_1(const char *fname, const char *path, char *fbuf, size_t size,
161138
}
162139
}
163140
ep = bp = 0;
164-
printf("returned at %s", "something?");
141+
165142
if (!exe_flag) {
166-
printf("returned at %s", "exe_flag?");
167143
RETURN_IF(is_abs);
168-
printf("nope\n");
169144
}
170145
else if (has_path) {
171-
printf("returned at %s", "if ext?");
172146
RETURN_IF(ext);
173-
printf("nope\n");
174147
i = p - fname;
175148
if (i + 1 > size) goto toolong;
176149
fspace = size - i - 1;
177150
bp = fbuf;
178151
ep = p;
179-
printf("ep is here %s\n", ep);
180152
memcpy(fbuf, fname, i + 1);
181153
goto needs_extension;
182154
}
183155
p = fname;
184156
#endif
185157

186-
187158
if (*p == '.' && *++p == '.') ++p;
188159
RETURN_IF(*p == '/');
189160
RETURN_IF(exe_flag && strchr(fname, '/'));
@@ -211,7 +182,7 @@ dln_find_1(const char *fname, const char *path, char *fbuf, size_t size,
211182
** take the path literally.
212183
*/
213184
if (*dp == '~' && (l == 1 ||
214-
#if defined(DOSISH)
185+
#ifdef DOSISH
215186
dp[1] == '\\' ||
216187
#endif
217188
dp[1] == '/')) {
@@ -247,13 +218,12 @@ dln_find_1(const char *fname, const char *path, char *fbuf, size_t size,
247218
i = fnlen;
248219
if (fspace < i) {
249220
toolong:
250-
printf("Pathname is too long \n" );//PATHNAME_TOO_LONG();
251221
goto next;
252222
}
253223
fspace -= i;
254224
memcpy(bp, fname, i + 1);
255225

256-
#if defined(DOSISH)
226+
#ifdef DOSISH
257227
if (exe_flag && !ext) {
258228
needs_extension:
259229
for (j = 0; j < sizeof(extension) / sizeof(extension[0]); j++) {
@@ -270,9 +240,6 @@ dln_find_1(const char *fname, const char *path, char *fbuf, size_t size,
270240
}
271241
#endif
272242

273-
#ifndef S_ISREG
274-
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
275-
#endif
276243
if (stat(fbuf, &st) == 0 && S_ISREG(st.st_mode)) {
277244
if (exe_flag == 0){
278245
return fbuf;
@@ -281,11 +248,10 @@ dln_find_1(const char *fname, const char *path, char *fbuf, size_t size,
281248
if (access(fbuf, X_OK) == 0) return fbuf;
282249
}
283250
next:
284-
/* if not, and no other alternatives, life is bleak */
251+
/* if not, and no other alternatives, life is bleak */
285252
if (*ep == '\0') {
286253
return NULL;
287254
}
288-
289-
/* otherwise try the next component in the search path */
255+
/* otherwise try the next component in the search path */
290256
}
291257
}

0 commit comments

Comments
 (0)