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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
|
/*
* dosemu.c
*
* UNIX Function Emulation for DOS
*/
#include "fd.h"
#include "func.h"
#if !defined (_NODOSDRIVE) && defined (CODEEUC)
extern int noconv;
#endif
#ifdef _NODOSDRIVE
# if defined (DNAMESIZE) && DNAMESIZE < (MAXNAMLEN + 1)
typedef struct _st_dirent {
char buf[sizeof(struct dirent) - DNAMESIZE + MAXNAMLEN + 1];
} st_dirent;
# else
typedef struct dirent st_dirent;
# endif
#endif
#if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE)
typedef struct _opendirpath_t {
DIR *dirp;
char *path;
} opendirpath_t;
#endif
#ifdef CYGWIN
#include <sys/cygwin.h>
static struct dirent *NEAR pseudoreaddir __P_((DIR *));
#else
#define pseudoreaddir readdir
#endif
#ifndef _NODOSDRIVE
int lastdrv = -1;
int dosdrive = 0;
#endif
static char cachecwd[MAXPATHLEN] = "";
#if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE)
static opendirpath_t *dirpathlist = NULL;
static int maxdirpath = 0;
#endif
#ifndef _NODOSDRIVE
int _dospath(path)
char *path;
{
if (!dosdrive) return(0);
return((isalpha2(*path) && path[1] == ':') ? *path : 0);
}
int dospath(path, buf)
char *path, *buf;
{
char *cp, tmp[MAXPATHLEN];
int drive, len;
if (!dosdrive) return(0);
if (_dospath(path)) cp = path;
else if (*path == _SC_ || !_dospath(cachecwd)) return(0);
else cp = cachecwd;
drive = *cp;
if (!buf) return(drive);
len = MAXPATHLEN - 1;
if (cp == buf) {
strncpy2(tmp, cp, len);
cp = tmp;
}
# ifdef CODEEUC
if (!noconv || cp != path)
buf[ujis2sjis(buf, (u_char *)cp, len)] = '\0';
else
# endif
strncpy2(buf, cp, len);
if (cp != path && *path) {
cp = strcatdelim(buf);
len -= cp - buf;
# ifdef CODEEUC
if (!noconv) cp[ujis2sjis(cp, (u_char *)path, len)] = '\0';
else
# endif
strncpy2(cp, path, len);
}
return(drive);
}
#endif /* !_NODOSDRIVE */
DIR *Xopendir(path)
char *path;
{
#if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE) || defined (CYGWIN)
char buf[MAXPATHLEN];
#endif
DIR *dirp;
char *cp, conv[MAXPATHLEN];
cp = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(cp)) dirp = dosopendir(cp);
else
#endif
dirp = opendir(cp);
if (!dirp) return(NULL);
#if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE) || defined (CYGWIN)
realpath2(path, buf, 1);
#endif
#if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE)
dirpathlist = (opendirpath_t *)realloc2(dirpathlist,
++maxdirpath * sizeof(opendirpath_t));
cp = convput(conv, buf, 0, 1, NULL, NULL);
dirpathlist[maxdirpath - 1].dirp = dirp;
dirpathlist[maxdirpath - 1].path = strdup2(cp);
#endif
#ifdef CYGWIN
#define opendir_saw_u_cygdrive (1 << (8 * sizeof(dirp -> __flags) - 2))
#define opendir_saw_s_cygdrive (1 << (8 * sizeof(dirp -> __flags) - 3))
if (buf[0] != _SC_ || buf[1])
dirp -> __flags |=
opendir_saw_u_cygdrive | opendir_saw_s_cygdrive;
#endif
return(dirp);
}
int Xclosedir(dirp)
DIR *dirp;
{
#if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE)
if (dirp) {
int i;
for (i = maxdirpath - 1; i >= 0; i--)
if (dirp == dirpathlist[i].dirp) break;
if (i >= 0) {
free(dirpathlist[i++].path);
memmove((char *)(&(dirpathlist[i - 1])),
(char *)(&(dirpathlist[i])),
(maxdirpath-- - i) * sizeof(opendirpath_t));
if (maxdirpath <= 0) {
free(dirpathlist);
dirpathlist = NULL;
}
}
}
#endif /* !_NOKANJICONV || !_NOROCKRIDGE */
#ifndef _NODOSDRIVE
if (((dosDIR *)dirp) -> dd_id == DID_IFDOSDRIVE)
return(dosclosedir(dirp));
#endif
closedir(dirp);
return(0);
}
#ifdef CYGWIN
static struct dirent *NEAR pseudoreaddir(dirp)
DIR *dirp;
{
static char *upath = NULL;
static char *spath = NULL;
struct dirent *dp;
if (!upath) {
char ubuf[MAXPATHLEN], sbuf[MAXPATHLEN];
cygwin_internal(CW_GET_CYGDRIVE_PREFIXES, ubuf, sbuf);
for (upath = ubuf; *upath == _SC_; upath++);
# ifdef DEBUG
_mtrace_file = "pseudoreaddir(upath)";
# endif
upath = strdup2(upath);
for (spath = sbuf; *spath == _SC_; spath++);
if (*upath && !strpathcmp(spath, upath)) *spath = '\0';
# ifdef DEBUG
_mtrace_file = "pseudoreaddir(spath)";
# endif
spath = strdup2(spath);
}
dp = readdir(dirp);
if (dirp -> __d_cookie != __DIRENT_COOKIE) return(dp);
if (!(*upath)) dirp -> __flags |= opendir_saw_u_cygdrive;
if (!(*spath)) dirp -> __flags |= opendir_saw_s_cygdrive;
if (dp) {
if (*upath && !(dirp -> __flags & opendir_saw_u_cygdrive)
&& !strpathcmp(dp -> d_name, upath))
dirp -> __flags |= opendir_saw_u_cygdrive;
else if (*spath && !(dirp -> __flags & opendir_saw_s_cygdrive)
&& !strpathcmp(dp -> d_name, spath))
dirp -> __flags |= opendir_saw_s_cygdrive;
}
else {
if (!(dirp -> __flags & opendir_saw_u_cygdrive)) {
dp = dirp -> __d_dirent;
strcpy(dp -> d_name, upath);
dirp -> __flags |= opendir_saw_u_cygdrive;
dirp -> __d_position++;
}
else if (!(dirp -> __flags & opendir_saw_s_cygdrive)) {
dp = dirp -> __d_dirent;
strcpy(dp -> d_name, spath);
dirp -> __flags |= opendir_saw_s_cygdrive;
dirp -> __d_position++;
}
}
return(dp);
}
#endif /* CYGWIN */
struct dirent *Xreaddir(dirp)
DIR *dirp;
{
#if defined (_NODOSDRIVE) && defined (_NOKANJIFCONV) \
&& defined (_NOROCKRIDGE)
return(pseudoreaddir(dirp));
#else /* !_NODOSDRIVE || !_NOKANJIFCONV || !_NOROCKRIDGE */
# if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE)
char path[MAXPATHLEN], conv[MAXPATHLEN];
int i;
# endif
static st_dirent buf;
struct dirent *dp;
char *src, *dest;
int dos;
# ifndef _NODOSDRIVE
if (((dosDIR *)dirp) -> dd_id == DID_IFDOSDRIVE) {
dp = dosreaddir(dirp);
dos = 1;
}
else
# endif
{
dp = pseudoreaddir(dirp);
dos = 0;
}
if (!dp) return(NULL);
src = dp -> d_name;
dest = ((struct dirent *)&buf) -> d_name;
memcpy((char *)(&buf), (char *)dp, dest - (char *)&buf);
if (isdotdir(src)) {
strcpy(dest, src);
return((struct dirent *)&buf);
}
# if !defined (_NOKANJIFCONV) || !defined (_NOROCKRIDGE)
for (i = maxdirpath - 1; i >= 0; i--)
if (dirp == dirpathlist[i].dirp) break;
if (i >= 0) {
strcatdelim2(path, dirpathlist[i].path, src);
if (convget(conv, path, dos) == conv) {
if ((src = strrdelim(conv, 0))) src++;
else src = conv;
}
strcpy(dest, src);
}
else
# endif /* !_NOKANJIFCONV || !_NOROCKRIDGE */
# if !defined (_NODOSDRIVE) && defined (CODEEUC)
if (dos && !noconv)
dest[sjis2ujis(dest, (u_char *)src, MAXNAMLEN)] = '\0';
else
# endif
strcpy(dest, src);
return((struct dirent *)&buf);
#endif /* !_NODOSDRIVE || !_NOKANJICONV || !_NOROCKRIDGE */
}
VOID Xrewinddir(dirp)
DIR *dirp;
{
#ifndef _NODOSDRIVE
if (((dosDIR *)dirp) -> dd_id == DID_IFDOSDRIVE) dosrewinddir(dirp);
#endif
rewinddir(dirp);
}
int Xchdir(path)
char *path;
{
#ifndef _NODOSDRIVE
int dd, drive;
#endif
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifdef _NODOSDRIVE
if (chdir(path) < 0) return(-1);
#else /* !_NODOSDRIVE */
if ((drive = _dospath(path))) {
if ((dd = preparedrv(drive)) < 0) return(-1);
if (doschdir(path) < 0) {
shutdrv(dd);
return(-1);
}
if (chdir(_SS_) < 0) return(-1);
if (lastdrv >= 0) {
if ((lastdrv % DOSNOFILE) != (dd % DOSNOFILE))
shutdrv(lastdrv);
else dd = lastdrv;
}
lastdrv = dd;
}
else {
if (chdir(path) < 0) return(-1);
if (lastdrv >= 0) shutdrv(lastdrv);
lastdrv = -1;
}
#endif /* !_NODOSDRIVE */
if (!Xgetwd(cachecwd)) {
*cachecwd = '\0';
return(-1);
}
return(0);
}
char *Xgetwd(path)
char *path;
{
char *cp, conv[MAXPATHLEN];
if (path == cachecwd || !*cachecwd) /*EMPTY*/;
#ifndef _NODOSDRIVE
else if (dosdrive && _dospath(cachecwd)) /*EMPTY*/;
#endif
else {
strcpy(path, cachecwd);
return(path);
}
#ifndef _NODOSDRIVE
if (dosdrive && lastdrv >= 0) {
if (!(cp = dosgetcwd(path, MAXPATHLEN))) return(NULL);
if (isupper2(cp[0])) {
int i;
for (i = 2; cp[i]; i++) {
if (issjis1((u_char)(cp[i]))) i++;
else cp[i] = tolower2(cp[i]);
}
}
cp = convget(conv, cp, 1);
}
else
#endif /* !_NODOSDRIVE */
{
#ifdef USEGETWD
cp = (char *)getwd(path);
#else
cp = (char *)getcwd(path, MAXPATHLEN);
#endif
if (!cp) return(NULL);
cp = convget(conv, cp, 0);
}
if (cp == path) return(cp);
strcpy(path, cp);
return(path);
}
int Xstat(path, stp)
char *path;
struct stat *stp;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosstat(path, stp));
#endif
return(stat(path, stp) ? -1 : 0);
}
int Xlstat(path, stp)
char *path;
struct stat *stp;
{
#ifndef _NOROCKRIDGE
char rpath[MAXPATHLEN];
#endif
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 0, rpath, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) {
if (doslstat(path, stp) < 0) return(-1);
}
else
#endif
if (lstat(path, stp)) return(-1);
#ifndef _NOROCKRIDGE
if (*rpath) rrlstat(rpath, stp);
#endif
return(0);
}
int Xaccess(path, mode)
char *path;
int mode;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosaccess(path, mode));
#endif
return(access(path, mode) ? -1 : 0);
}
int Xsymlink(name1, name2)
char *name1, *name2;
{
char conv1[MAXPATHLEN], conv2[MAXPATHLEN];
name1 = convput(conv1, name1, 1, 0, NULL, NULL);
name2 = convput(conv2, name2, 1, 0, NULL, NULL);
#ifndef _NODOSDRIVE
if ((_dospath(name1) || _dospath(name2)))
return(dossymlink(name1, name2));
#endif
return(symlink(name1, name2) ? -1 : 0);
}
int Xreadlink(path, buf, bufsiz)
char *path, *buf;
int bufsiz;
{
char conv[MAXPATHLEN], lbuf[MAXPATHLEN + 1];
int c, len;
path = convput(conv, path, 1, 0, lbuf, &c);
#ifndef _NOROCKRIDGE
if (*lbuf && (len = rrreadlink(lbuf, lbuf, MAXPATHLEN)) >= 0)
/*EMPTY*/;
else
#endif
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosreadlink(path, buf, bufsiz));
else
#endif
if ((len = readlink(path, lbuf, MAXPATHLEN)) < 0) return(-1);
lbuf[len] = '\0';
#ifndef _NOKANJIFCONV
path = kanjiconv2(conv, lbuf, MAXPATHLEN - 1, c, DEFCODE, L_FNAME);
#else
path = lbuf;
#endif
for (len = 0; len < bufsiz && path[len]; len++) buf[len] = path[len];
return(len);
}
int Xchmod(path, mode)
char *path;
int mode;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(doschmod(path, mode));
#endif
return(chmod(path, mode) ? -1 : 0);
}
#ifdef USEUTIME
int Xutime(path, times)
char *path;
struct utimbuf *times;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
# ifndef _NODOSDRIVE
if (_dospath(path)) return(dosutime(path, times));
# endif
return(utime(path, times) ? -1 : 0);
}
#else /* !USEUTIME */
int Xutimes(path, tvp)
char *path;
struct timeval tvp[2];
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
# ifndef _NODOSDRIVE
if (_dospath(path)) return(dosutimes(path, tvp));
# endif
return(utimes(path, tvp) ? -1 : 0);
}
#endif /* !USEUTIME */
int Xunlink(path)
char *path;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosunlink(path));
#endif
return(unlink(path) ? -1 : 0);
}
int Xrename(from, to)
char *from, *to;
{
char conv1[MAXPATHLEN], conv2[MAXPATHLEN];
from = convput(conv1, from, 1, 0, NULL, NULL);
to = convput(conv2, to, 1, 0, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(from)) {
if (!_dospath(to)) {
errno = EXDEV;
return(-1);
}
return(dosrename(from, to));
}
else if (_dospath(to)) {
errno = EXDEV;
return(-1);
}
#endif
return(rename(from, to) ? -1 : 0);
}
int Xopen(path, flags, mode)
char *path;
int flags, mode;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosopen(path, flags, mode));
#endif
return(open(path, flags, mode));
}
#ifndef _NODOSDRIVE
int Xclose(fd)
int fd;
{
if (fd >= DOSFDOFFSET) return(dosclose(fd));
return(close(fd));
}
int Xread(fd, buf, nbytes)
int fd;
char *buf;
int nbytes;
{
if (fd >= DOSFDOFFSET) return(dosread(fd, buf, nbytes));
return(read(fd, buf, nbytes));
}
int Xwrite(fd, buf, nbytes)
int fd;
char *buf;
int nbytes;
{
if (fd >= DOSFDOFFSET) return(doswrite(fd, buf, nbytes));
return(write(fd, buf, nbytes));
}
off_t Xlseek(fd, offset, whence)
int fd;
off_t offset;
int whence;
{
if (fd >= DOSFDOFFSET) return(doslseek(fd, offset, whence));
return(lseek(fd, offset, whence));
}
int Xdup(oldd)
int oldd;
{
if (oldd >= DOSFDOFFSET) {
errno = EBADF;
return(-1);
}
return(dup(oldd));
}
int Xdup2(oldd, newd)
int oldd, newd;
{
if (oldd == newd) return(newd);
if (oldd >= DOSFDOFFSET || newd >= DOSFDOFFSET) {
errno = EBADF;
return(-1);
}
return(dup2(oldd, newd));
}
#endif /* !_NODOSDRIVE */
int Xmkdir(path, mode)
char *path;
int mode;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosmkdir(path, mode));
#endif
return(mkdir(path, mode) ? -1 : 0);
}
int Xrmdir(path)
char *path;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosrmdir(path));
#endif
return(rmdir(path) ? -1 : 0);
}
FILE *Xfopen(path, type)
char *path, *type;
{
char conv[MAXPATHLEN];
path = convput(conv, path, 1, 1, NULL, NULL);
#ifndef _NODOSDRIVE
if (_dospath(path)) return(dosfopen(path, type));
#endif
return(fopen(path, type));
}
#ifndef _NODOSDRIVE
FILE *Xfdopen(fd, type)
int fd;
char *type;
{
if (fd >= DOSFDOFFSET) return(dosfdopen(fd, type));
return(fdopen(fd, type));
}
int Xfclose(stream)
FILE *stream;
{
if (dosfileno(stream) >= 0) return(dosfclose(stream));
return(fclose(stream));
}
int Xfeof(stream)
FILE *stream;
{
if (dosfileno(stream) >= 0) return(dosfeof(stream));
return(feof(stream));
}
int Xfread(buf, size, nitems, stream)
char *buf;
int size, nitems;
FILE *stream;
{
if (dosfileno(stream) >= 0)
return(dosfread(buf, size, nitems, stream));
return(fread(buf, size, nitems, stream));
}
int Xfwrite(buf, size, nitems, stream)
char *buf;
int size, nitems;
FILE *stream;
{
if (dosfileno(stream) >= 0)
return(dosfwrite(buf, size, nitems, stream));
return(fwrite(buf, size, nitems, stream));
}
int Xfflush(stream)
FILE *stream;
{
if (dosfileno(stream) >= 0) return(dosfflush(stream));
return(fflush(stream));
}
int Xfgetc(stream)
FILE *stream;
{
if (dosfileno(stream) >= 0) return(dosfgetc(stream));
return(fgetc(stream));
}
int Xfputc(c, stream)
int c;
FILE *stream;
{
if (dosfileno(stream) >= 0) return(dosfputc(c, stream));
return(fputc(c, stream));
}
char *Xfgets(s, n, stream)
char *s;
int n;
FILE *stream;
{
if (dosfileno(stream) >= 0) return(dosfgets(s, n, stream));
return(fgets(s, n, stream));
}
int Xfputs(s, stream)
char *s;
FILE *stream;
{
if (dosfileno(stream) >= 0) return(dosfputs(s, stream));
return(fputs(s, stream));
}
#endif /* !_NODOSDRIVE */
|