| 1 |
/* emtexdir.c -- written by Eberhard Mattes, donated to the public domain */ |
| 2 |
|
| 3 |
#if defined (__EMX__) |
| 4 |
#include <emx/syscalls.h> |
| 5 |
#else |
| 6 |
#include "emdir.h" |
| 7 |
#endif |
| 8 |
#if defined(DJGPP) || defined(GO32) |
| 9 |
#include <unistd.h> |
| 10 |
#endif |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <fcntl.h> |
| 14 |
#include <io.h> |
| 15 |
#include "emtexdir.h" |
| 16 |
|
| 17 |
#define FALSE 0 |
| 18 |
#define TRUE 1 |
| 19 |
|
| 20 |
void (*emtex_dir_find_callback)(const char *name, int ok) = NULL; |
| 21 |
|
| 22 |
static int setup_add (struct emtex_dir *dst, const char *path) |
| 23 |
{ |
| 24 |
char *p; |
| 25 |
|
| 26 |
if (dst->used >= dst->alloc) |
| 27 |
{ |
| 28 |
dst->alloc += 8; |
| 29 |
dst->list = realloc (dst->list, dst->alloc * sizeof (*dst->list)); |
| 30 |
if (dst->list == NULL) |
| 31 |
return (FALSE); |
| 32 |
} |
| 33 |
p = strdup (path); |
| 34 |
if (p == NULL) |
| 35 |
return (FALSE); |
| 36 |
dst->list[dst->used++] = p; |
| 37 |
return (TRUE); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
static int setup_subdir (struct emtex_dir *dst, char *path, size_t add, |
| 42 |
unsigned flags, int recurse) |
| 43 |
{ |
| 44 |
int ok, i, end, len; |
| 45 |
#if defined (__EMX__) |
| 46 |
struct _find find; |
| 47 |
#else |
| 48 |
struct ll_findbuffer find; |
| 49 |
#endif |
| 50 |
|
| 51 |
i = dst->used; |
| 52 |
strcpy (path + add, "*.*"); |
| 53 |
#if defined (__EMX__) |
| 54 |
ok = __findfirst (path, 0x10, &find) == 0; |
| 55 |
#else |
| 56 |
ok = ll_findfirst (path, 0x10, &find); |
| 57 |
#endif |
| 58 |
while (ok) |
| 59 |
{ |
| 60 |
if ((find.attr & 0x10) |
| 61 |
&& !(strcmp (find.name, ".") == 0 || strcmp (find.name, "..") == 0)) |
| 62 |
{ |
| 63 |
len = strlen (find.name); |
| 64 |
memcpy (path + add, find.name, len); |
| 65 |
path[add+len] = '\\'; |
| 66 |
path[add+len+1] = 0; |
| 67 |
if (!setup_add (dst, path)) |
| 68 |
return (FALSE); |
| 69 |
} |
| 70 |
#if defined (__EMX__) |
| 71 |
ok = __findnext (&find) == 0; |
| 72 |
#else |
| 73 |
ok = ll_findnext (&find); |
| 74 |
#endif |
| 75 |
} |
| 76 |
if (recurse) |
| 77 |
{ |
| 78 |
end = dst->used; |
| 79 |
while (i < end) |
| 80 |
{ |
| 81 |
strcpy (path, dst->list[i]); |
| 82 |
if (!setup_subdir (dst, path, strlen (path), flags, TRUE)) |
| 83 |
return (FALSE); |
| 84 |
++i; |
| 85 |
} |
| 86 |
} |
| 87 |
return (TRUE); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
static int setup_dir (struct emtex_dir *dst, char *path, |
| 92 |
const char *base_dir, size_t base_dir_len, |
| 93 |
const char *sub_dir, |
| 94 |
unsigned flags) |
| 95 |
{ |
| 96 |
size_t i, len; |
| 97 |
|
| 98 |
memcpy (path, base_dir, base_dir_len); |
| 99 |
i = base_dir_len; |
| 100 |
if ((flags & EDS_BANG) && sub_dir == NULL) |
| 101 |
{ |
| 102 |
flags &= ~(EDS_ONESUBDIR|EDS_ALLSUBDIR); |
| 103 |
if (i >= 2 && path[i-1] == '!' && path[i-2] == '!') |
| 104 |
{ |
| 105 |
flags |= EDS_ALLSUBDIR; |
| 106 |
i -= 2; |
| 107 |
} |
| 108 |
else if (i >= 1 && path[i-1] == '!') |
| 109 |
{ |
| 110 |
flags |= EDS_ONESUBDIR; |
| 111 |
--i; |
| 112 |
} |
| 113 |
} |
| 114 |
if (sub_dir != NULL && *sub_dir != 0) |
| 115 |
{ |
| 116 |
if (i != 0 && path[i-1] != ':' && path[i-1] != '/' && path[i-1] != '\\') |
| 117 |
path[i++] = '\\'; |
| 118 |
len = strlen (sub_dir); |
| 119 |
memcpy (path+i, sub_dir, len); |
| 120 |
i += len; |
| 121 |
} |
| 122 |
if (path[i-1] != ':' && path[i-1] != '/' && path[i-1] != '\\') |
| 123 |
path[i++] = '\\'; |
| 124 |
path[i] = 0; |
| 125 |
if (!setup_add (dst, path)) |
| 126 |
return (FALSE); |
| 127 |
if (flags & EDS_ALLSUBDIR) |
| 128 |
return (setup_subdir (dst, path, i, flags, TRUE)); |
| 129 |
else if (flags & EDS_ONESUBDIR) |
| 130 |
return (setup_subdir (dst, path, i, flags, FALSE)); |
| 131 |
else |
| 132 |
return (TRUE); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
/*static */int setup_list (struct emtex_dir *dst, char *path, |
| 137 |
const char *list, unsigned flags) |
| 138 |
{ |
| 139 |
const char *end; |
| 140 |
size_t i; |
| 141 |
|
| 142 |
for (;;) |
| 143 |
{ |
| 144 |
while (*list == ' ' || *list == '\t') |
| 145 |
++list; |
| 146 |
if (*list == 0) |
| 147 |
return (TRUE); |
| 148 |
end = list; |
| 149 |
while (*end != 0 && *end != ';') |
| 150 |
++end; |
| 151 |
i = end - list; |
| 152 |
while (i > 0 && (list[i-1] == ' ' || list[i-1] == '\t')) |
| 153 |
--i; |
| 154 |
if (i != 0 && !setup_dir (dst, path, list, i, NULL, flags)) |
| 155 |
return (FALSE); |
| 156 |
if (*end == 0) |
| 157 |
return (TRUE); |
| 158 |
list = end + 1; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
int emtex_dir_setup (struct emtex_dir *ed, const char *env, const char *dir, |
| 164 |
unsigned flags) |
| 165 |
{ |
| 166 |
const char *val; |
| 167 |
char path[260]; |
| 168 |
|
| 169 |
ed->alloc = 0; |
| 170 |
ed->used = 0; |
| 171 |
ed->list = NULL; |
| 172 |
if (env != NULL && (val = getenv (env)) != NULL) |
| 173 |
return (setup_list (ed, path, val, flags)); |
| 174 |
else if ((val = getenv ("EMTEXDIR")) != NULL) |
| 175 |
return (setup_dir (ed, path, val, strlen (val), dir, flags)); |
| 176 |
else |
| 177 |
return (setup_dir (ed, path, "\\emtex", 6, dir, flags)); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
static void pretty (char *path, unsigned flags) |
| 182 |
{ |
| 183 |
char *p; |
| 184 |
|
| 185 |
if (flags & EDF_FSLASH) |
| 186 |
for (p = path; *p != 0; ++p) |
| 187 |
if (*p == '\\') |
| 188 |
*p = '/'; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
#define ADDCHAR(C) \ |
| 193 |
if (dst_size < 1) return (EDT_TOOLONG); \ |
| 194 |
*dst++ = (C); --dst_size |
| 195 |
|
| 196 |
int emtex_dir_trunc (char *dst, size_t dst_size, const char *src, |
| 197 |
unsigned flags, int method) |
| 198 |
{ |
| 199 |
int len, truncated, dot; |
| 200 |
|
| 201 |
if (src[0] != 0 && src[1] == ':') |
| 202 |
{ |
| 203 |
ADDCHAR (src[0]); |
| 204 |
ADDCHAR (src[1]); |
| 205 |
src += 2; |
| 206 |
} |
| 207 |
|
| 208 |
truncated = FALSE; dot = FALSE; len = 0; |
| 209 |
for (;;) |
| 210 |
{ |
| 211 |
switch (*src) |
| 212 |
{ |
| 213 |
case 0: |
| 214 |
ADDCHAR (0); |
| 215 |
return (truncated ? EDT_CHANGED : EDT_UNCHANGED); |
| 216 |
|
| 217 |
case ':': |
| 218 |
return (EDT_INVALID); |
| 219 |
|
| 220 |
case '/': |
| 221 |
case '\\': |
| 222 |
ADDCHAR (*src); |
| 223 |
len = 0; dot = FALSE; |
| 224 |
break; |
| 225 |
|
| 226 |
case '.': |
| 227 |
if (dot) |
| 228 |
return (EDT_INVALID); |
| 229 |
ADDCHAR (*src); |
| 230 |
|
| 231 |
/* ".." is allowed -- don't return EDT_INVALID for the next |
| 232 |
dot. */ |
| 233 |
|
| 234 |
if (!(len == 0 && src[1] == '.' |
| 235 |
&& (src[2] == 0 || src[2] == '/' || src[2] == '\\'))) |
| 236 |
{ |
| 237 |
len = 0; dot = TRUE; |
| 238 |
} |
| 239 |
break; |
| 240 |
|
| 241 |
default: |
| 242 |
if (dot && len == 3) |
| 243 |
truncated = TRUE; |
| 244 |
else if (!dot && len == 8) |
| 245 |
{ |
| 246 |
truncated = TRUE; |
| 247 |
if (method == 0) |
| 248 |
{ |
| 249 |
dst[-3] = dst[-2]; |
| 250 |
dst[-2] = dst[-1]; |
| 251 |
dst[-1] = *src; |
| 252 |
} |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
ADDCHAR (*src); |
| 257 |
++len; |
| 258 |
} |
| 259 |
break; |
| 260 |
} |
| 261 |
++src; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
static int find2 (const char *name, unsigned flags) |
| 267 |
{ |
| 268 |
int ok; |
| 269 |
|
| 270 |
ok = (access (name, 4) == 0); |
| 271 |
if (flags & EDF_TRACE) |
| 272 |
emtex_dir_find_callback (name, ok); |
| 273 |
return (ok); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
static int find1 (char *path, size_t path_size, const char *dir, |
| 278 |
const char *fname, unsigned flags) |
| 279 |
{ |
| 280 |
char buf[260]; |
| 281 |
int method, rc; |
| 282 |
size_t len, tmp; |
| 283 |
|
| 284 |
len = 0; |
| 285 |
if (dir != NULL) |
| 286 |
{ |
| 287 |
tmp = strlen (dir); |
| 288 |
if (tmp >= sizeof (buf)) |
| 289 |
return (FALSE); |
| 290 |
memcpy (buf, dir, tmp); |
| 291 |
len = tmp; |
| 292 |
} |
| 293 |
tmp = strlen (fname); |
| 294 |
if (len + tmp >= sizeof (buf)) |
| 295 |
return (FALSE); |
| 296 |
memcpy (buf + len, fname, tmp + 1); |
| 297 |
len += tmp; |
| 298 |
#if 0 /* wkim */ |
| 299 |
/* disabled for Win95's long file name support */ |
| 300 |
/* -- Wonkoo Kim ([email protected]), May 18, 1997 */ |
| 301 |
if (_osmode == DOS_MODE) |
| 302 |
{ |
| 303 |
rc = emtex_dir_trunc (path, path_size, buf, flags, EDT_5_PLUS_3); |
| 304 |
if ((rc == EDT_UNCHANGED || rc == EDT_CHANGED) && find2 (path, flags)) |
| 305 |
{ |
| 306 |
pretty (path, flags); |
| 307 |
return (TRUE); |
| 308 |
} |
| 309 |
rc = emtex_dir_trunc (path, path_size, buf, flags, EDT_8); |
| 310 |
if (rc == EDT_CHANGED && find2 (path, flags)) |
| 311 |
{ |
| 312 |
pretty (path, flags); |
| 313 |
return (TRUE); |
| 314 |
} |
| 315 |
return (FALSE); |
| 316 |
} |
| 317 |
else |
| 318 |
#endif /* wkim */ |
| 319 |
{ |
| 320 |
if (len < path_size && find2 (buf, flags)) |
| 321 |
{ |
| 322 |
memcpy (path, buf, len + 1); |
| 323 |
pretty (path, flags); |
| 324 |
return (TRUE); |
| 325 |
} |
| 326 |
for (method = 0; method < 2; ++method) |
| 327 |
{ |
| 328 |
rc = emtex_dir_trunc (path, path_size, buf, flags, method); |
| 329 |
if (rc == EDT_CHANGED && find2 (path, flags)) |
| 330 |
{ |
| 331 |
pretty (path, flags); |
| 332 |
return (TRUE); |
| 333 |
} |
| 334 |
} |
| 335 |
return (FALSE); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
int emtex_dir_find (char *path, size_t path_size, |
| 341 |
const struct emtex_dir *ed, |
| 342 |
const char *fname, unsigned flags) |
| 343 |
{ |
| 344 |
int i, absp; |
| 345 |
const char *p; |
| 346 |
|
| 347 |
absp = FALSE; |
| 348 |
for (p = fname; *p != 0; ++p) |
| 349 |
if (*p == ':' || *p == '/' || *p == '\\') |
| 350 |
{ |
| 351 |
absp = TRUE; |
| 352 |
break; |
| 353 |
} |
| 354 |
|
| 355 |
if (absp) |
| 356 |
return (find1 (path, path_size, NULL, fname, flags)); |
| 357 |
|
| 358 |
if ((flags & EDF_CWD) && find1 (path, path_size, NULL, fname, flags)) |
| 359 |
return (TRUE); |
| 360 |
|
| 361 |
for (i = 0; i < ed->used; ++i) |
| 362 |
if (find1 (path, path_size, ed->list[i], fname, flags)) |
| 363 |
return (TRUE); |
| 364 |
return (FALSE); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
#if defined (TEST) |
| 369 |
|
| 370 |
#include <stdio.h> |
| 371 |
|
| 372 |
int main (int argc, char *argv[]) |
| 373 |
{ |
| 374 |
struct emtex_dir ed; |
| 375 |
int i; |
| 376 |
unsigned flags1, flags2; |
| 377 |
char path[260]; |
| 378 |
|
| 379 |
if (argc != 6) |
| 380 |
{ |
| 381 |
puts ("Usage: emtexdir <flags> <flags> <env> <dir> <fname>"); |
| 382 |
return (1); |
| 383 |
} |
| 384 |
|
| 385 |
flags1 = (unsigned)strtol (argv[1], NULL, 0); |
| 386 |
flags2 = (unsigned)strtol (argv[2], NULL, 0); |
| 387 |
|
| 388 |
if (!emtex_dir_setup (&ed, argv[3], argv[4], flags1)) |
| 389 |
{ |
| 390 |
fputs ("emtex_dir_setup failed\n", stderr); |
| 391 |
return (2); |
| 392 |
} |
| 393 |
|
| 394 |
printf ("Directories:\n"); |
| 395 |
for (i = 0; i < ed.used; ++i) |
| 396 |
printf (" %s\n", ed.list[i]); |
| 397 |
|
| 398 |
if (!emtex_dir_find (path, sizeof (path), &ed, argv[5], flags2)) |
| 399 |
puts ("File not found"); |
| 400 |
else |
| 401 |
printf ("Path: %s\n", path); |
| 402 |
return (0); |
| 403 |
} |
| 404 |
|
| 405 |
#endif |