headius (owner)

Revisions

gist: 223633 Download_button fork
public
Public Clone URL: git://gist.github.com/223633.git
Embed All Files: show embed
C #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
static char *
dln_find_1(fname, path, exe_flag)
    const char *fname;
    const char *path;
    int exe_flag; /* non 0 if looking for executable. */
{
    register const char *dp;
    register const char *ep;
    register char *bp;
    struct stat st;
#ifdef __MACOS__
    const char* mac_fullpath;
#endif
 
    if (!fname) return (char *)fname;
    if (fname[0] == '/') return (char *)fname;
    if (strncmp("./", fname, 2) == 0 || strncmp("../", fname, 3) == 0)
      return (char *)fname;
    if (exe_flag && strchr(fname, '/')) return (char *)fname;
#ifdef DOSISH
    if (fname[0] == '\\') return (char *)fname;
# ifdef DOSISH_DRIVE_LETTER
    if (strlen(fname) > 2 && fname[1] == ':') return (char *)fname;
# endif
    if (strncmp(".\\", fname, 2) == 0 || strncmp("..\\", fname, 3) == 0)
      return (char *)fname;
    if (exe_flag && strchr(fname, '\\')) return (char *)fname;
#endif
 
    for (dp = path;; dp = ++ep) {
register int l;
int i;
int fspace;
 
/* extract a component */
ep = strchr(dp, PATH_SEP[0]);
if (ep == NULL)
ep = dp+strlen(dp);
 
/* find the length of that component */
l = ep - dp;
bp = fbuf;
fspace = sizeof fbuf - 2;
if (l > 0) {
/*
** If the length of the component is zero length,
** start from the current directory. If the
** component begins with "~", start from the
** user's $HOME environment variable. Otherwise
** take the path literally.
*/
 
if (*dp == '~' && (l == 1 ||
#if defined(DOSISH)
dp[1] == '\\' ||
#endif
dp[1] == '/')) {
char *home;
 
home = getenv("HOME");
if (home != NULL) {
i = strlen(home);
if ((fspace -= i) < 0)
goto toolong;
memcpy(bp, home, i);
bp += i;
}
dp++;
l--;
}
if (l > 0) {
if ((fspace -= l) < 0)
goto toolong;
memcpy(bp, dp, l);
bp += l;
}
 
/* add a "/" between directory and filename */
if (ep[-1] != '/')
*bp++ = '/';
}
 
/* now append the file name */
i = strlen(fname);
if ((fspace -= i) < 0) {
toolong:
fprintf(stderr, "openpath: pathname too long (ignored)\n");
*bp = '\0';
fprintf(stderr, "\tDirectory \"%s\"\n", fbuf);
fprintf(stderr, "\tFile \"%s\"\n", fname);
goto next;
}
memcpy(bp, fname, i + 1);
 
#if defined(DOSISH)
if (exe_flag) {
static const char extension[][5] = {
#if defined(MSDOS)
".com", ".exe", ".bat",
#if defined(DJGPP)
".btm", ".sh", ".ksh", ".pl", ".sed",
#endif
#elif defined(__EMX__) || defined(_WIN32)
".exe", ".com", ".cmd", ".bat",
/* end of __EMX__ or _WIN32 */
#else
".r", ".R", ".x", ".X", ".bat", ".BAT",
/* __human68k__ */
#endif
};
int j;
 
for (j = 0; j < sizeof(extension) / sizeof(extension[0]); j++) {
if (fspace < strlen(extension[j])) {
fprintf(stderr, "openpath: pathname too long (ignored)\n");
fprintf(stderr, "\tDirectory \"%.*s\"\n", (int) (bp - fbuf), fbuf);
fprintf(stderr, "\tFile \"%s%s\"\n", fname, extension[j]);
continue;
}
strcpy(bp + i, extension[j]);
#ifndef __MACOS__
if (stat(fbuf, &st) == 0)
return fbuf;
#else
if (mac_fullpath = _macruby_exist_file_in_libdir_as_posix_name(fbuf))
return mac_fullpath;
 
#endif
}
goto next;
}
#endif /* MSDOS or _WIN32 or __human68k__ or __EMX__ */
 
#ifndef __MACOS__
if (stat(fbuf, &st) == 0) {
if (exe_flag == 0) return fbuf;
/* looking for executable */
if (!S_ISDIR(st.st_mode) && eaccess(fbuf, X_OK) == 0)
return fbuf;
}
#else
if (mac_fullpath = _macruby_exist_file_in_libdir_as_posix_name(fbuf)) {
if (exe_flag == 0) return mac_fullpath;
/* looking for executable */
if (stat(mac_fullpath, &st) == 0) {
if (!S_ISDIR(st.st_mode) && eaccess(mac_fullpath, X_OK) == 0)
return mac_fullpath;
}
}
#endif
 
      next:
/* if not, and no other alternatives, life is bleak */
if (*ep == '\0') {
return NULL;
}
 
/* otherwise try the next component in the search path */
    }
}