| 1 |
/* |
| 2 |
Copyright (c) 1990-1999 Info-ZIP. All rights reserved. |
| 3 |
|
| 4 |
See the accompanying file LICENSE, version 1999-Oct-05 or later |
| 5 |
(the contents of which are also included in zip.h) for terms of use. |
| 6 |
If, for some reason, both of these files are missing, the Info-ZIP license |
| 7 |
also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html |
| 8 |
*/ |
| 9 |
#pragma library |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <string.h> |
| 14 |
#include <sc.h> |
| 15 |
#include <peek.h> |
| 16 |
#include <lub.h> |
| 17 |
#include <fdb.h> |
| 18 |
#include <fsa.h> |
| 19 |
#include "theos/stat.h" |
| 20 |
|
| 21 |
/* replacement for standard library functions stat and fstat */ |
| 22 |
|
| 23 |
int _stat_(struct stat* st, struct fdb* fdb); |
| 24 |
int _dstat_(struct stat* st); |
| 25 |
|
| 26 |
#define peekucb() peeknuc() |
| 27 |
|
| 28 |
/* map THEOS protection code to Unix modes */ |
| 29 |
|
| 30 |
unsigned short _tm2um_(char protect) |
| 31 |
{ |
| 32 |
unsigned short umask = 0; |
| 33 |
|
| 34 |
if (!(protect & _FDB_READ_PROTECT)) |
| 35 |
umask = S_IRUSR|S_IRGRP; |
| 36 |
|
| 37 |
if (!(protect & _FDB_WRITE_PROTECT)) |
| 38 |
umask |= S_IWUSR|S_IWGRP; |
| 39 |
|
| 40 |
if (!(protect & _FDB_EXECUTE_PROTECT)) |
| 41 |
umask |= S_IXUSR|S_IXGRP; |
| 42 |
|
| 43 |
if (!(protect & _FDB_ERASE_PROTECT)) |
| 44 |
umask |= S_IEUSR|S_IEGRP; |
| 45 |
|
| 46 |
if (!(protect & _FDB_SHARED_READ_PROTECT)) { |
| 47 |
if (_osmajor > 3) |
| 48 |
umask |= S_IROTH|S_IXOTH; |
| 49 |
else |
| 50 |
umask |= S_IROTH; |
| 51 |
} |
| 52 |
|
| 53 |
if (!(protect & _FDB_SHARED_WRITE_PROTECT)) |
| 54 |
umask |= S_IWOTH; |
| 55 |
|
| 56 |
if (!(protect & _FDB_MODIFIED)) { |
| 57 |
if (_osmajor > 3) |
| 58 |
umask |= S_IMODF; |
| 59 |
else |
| 60 |
umask |= S_IXOTH; |
| 61 |
} |
| 62 |
|
| 63 |
if (protect & _FDB_NOT_HIDDEN) |
| 64 |
umask |= S_INHID; |
| 65 |
|
| 66 |
return umask; |
| 67 |
} |
| 68 |
|
| 69 |
/* map Unix modes to THEOS protections */ |
| 70 |
|
| 71 |
char _um2tm_(unsigned short mask) |
| 72 |
{ |
| 73 |
char protect = 0; |
| 74 |
|
| 75 |
if (!(mask & (S_IRUSR|S_IRGRP))) |
| 76 |
protect |= _FDB_READ_PROTECT; |
| 77 |
|
| 78 |
if (!(mask & (S_IWUSR|S_IWGRP))) |
| 79 |
protect |= _FDB_WRITE_PROTECT; |
| 80 |
|
| 81 |
if (!(mask & (S_IXUSR|S_IXGRP))) |
| 82 |
protect |= _FDB_EXECUTE_PROTECT; |
| 83 |
|
| 84 |
if (!(mask & (S_IEUSR|S_IEGRP))) |
| 85 |
protect |= _FDB_ERASE_PROTECT; |
| 86 |
|
| 87 |
if (_osmajor < 4) { |
| 88 |
if (!(mask & S_IROTH)) |
| 89 |
protect |= _FDB_SHARED_READ_PROTECT; |
| 90 |
} else { |
| 91 |
if (!(mask & (S_IROTH|S_IXOTH))) |
| 92 |
protect |= _FDB_SHARED_READ_PROTECT; |
| 93 |
} |
| 94 |
|
| 95 |
if (!(mask & S_IWOTH)) |
| 96 |
protect |= _FDB_SHARED_WRITE_PROTECT; |
| 97 |
|
| 98 |
if (mask & S_IMODF && _osmajor > 3) |
| 99 |
protect |= _FDB_MODIFIED; |
| 100 |
|
| 101 |
if (mask & S_INHID && _osmajor > 3) |
| 102 |
protect |= _FDB_NOT_HIDDEN; |
| 103 |
|
| 104 |
return protect; |
| 105 |
} |
| 106 |
|
| 107 |
/* root directory stat */ |
| 108 |
|
| 109 |
static int rdirstat(const char* fn, struct stat *st) |
| 110 |
{ |
| 111 |
register char* p = strchr(fn, ':'); |
| 112 |
char drive; |
| 113 |
|
| 114 |
drive = p ? p[1] : 'S'; |
| 115 |
|
| 116 |
if (drive >= 'a' && drive <= 'Z') |
| 117 |
drive -= 0x40; |
| 118 |
|
| 119 |
memset(st, 0, sizeof(struct stat)); |
| 120 |
|
| 121 |
if (getlub(drive - 'A') != 255) { |
| 122 |
st->st_org = _FDB_STAT_DIRECTORY; |
| 123 |
st->st_mode = S_IFDIR|S_IRUSR|S_IWUSR|S_IROTH|S_IWOTH; |
| 124 |
st->st_nlink = 1; |
| 125 |
st->st_dev = st->st_rdev = drive - 'A'; |
| 126 |
st->st_uid = st->st_gid = getuid(); |
| 127 |
st->st_protect = _FDB_ERASE_PROTECT; |
| 128 |
return 0; |
| 129 |
} |
| 130 |
errno = _errnum = ENOENT; |
| 131 |
_errarg = fn; |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
#ifdef LOCATE_BUG |
| 136 |
|
| 137 |
/* locate fails when stating a file in root dir from a directory with a |
| 138 |
* relative path. Workaround by setting directory to root dir |
| 139 |
* getting the file directory block, then restoring the current directory. |
| 140 |
*/ |
| 141 |
|
| 142 |
struct fdb* __locate(const char* fn, char* buf, short* drv) |
| 143 |
{ |
| 144 |
struct fdb* fdb; |
| 145 |
char buf2[FILENAME_MAX]; |
| 146 |
char cwd[FILENAME_MAX]; |
| 147 |
char drive[3]; |
| 148 |
char* p; |
| 149 |
char* q; |
| 150 |
|
| 151 |
/* return if file found */ |
| 152 |
if (fdb = _locate(fn, buf, drv)) |
| 153 |
return fdb; |
| 154 |
|
| 155 |
/* if file name does not contain a path delimiter it really does not exist. |
| 156 |
*/ |
| 157 |
strcpy(buf2, fn); |
| 158 |
|
| 159 |
if ((p = strrchr(buf2, '/')) == NULL) |
| 160 |
return NULL; |
| 161 |
|
| 162 |
/* get drive name from file path */ |
| 163 |
q = strrchr(buf2, ':'); |
| 164 |
|
| 165 |
/* cat drive name if any to directory path */ |
| 166 |
if (q) { |
| 167 |
strncpy(drive, q, 2); |
| 168 |
drive[2] = '\0'; |
| 169 |
strcpy(p, q); |
| 170 |
} else |
| 171 |
*p = '\0'; |
| 172 |
/* save current directory */ |
| 173 |
getcwd(cwd, FILENAME_MAX); |
| 174 |
/* chdir to directory path */ |
| 175 |
chdir(buf2); |
| 176 |
/* get File Directory Block */ |
| 177 |
p = strrchr(fn, '/'); |
| 178 |
fdb = _locate(p + 1, buf, drv); |
| 179 |
/* restore current directory */ |
| 180 |
chdir(cwd); |
| 181 |
return fdb; |
| 182 |
} |
| 183 |
|
| 184 |
#undef _locate |
| 185 |
#define _locate() __locate() |
| 186 |
|
| 187 |
/* same cause, same consequence for fopen and open. |
| 188 |
*/ |
| 189 |
|
| 190 |
FILE* _fopen(const char* fn, const char* mode) |
| 191 |
{ |
| 192 |
FILE* fp; |
| 193 |
char buf[FILENAME_MAX]; |
| 194 |
short drv; |
| 195 |
|
| 196 |
/* prepend a path to current dir to avoid use of default library */ |
| 197 |
if (*fn != '.' && *fn != '/') { |
| 198 |
strcpy(buf, "./"); |
| 199 |
strcat(buf, fn); |
| 200 |
return fopen(buf, mode); |
| 201 |
} |
| 202 |
|
| 203 |
if (fp = fopen(fn, mode)) |
| 204 |
return fp; |
| 205 |
|
| 206 |
/* see comment for _locate */ |
| 207 |
if (_locate(fn, buf, &drv)) { |
| 208 |
fn = strrchr(fn, '/'); |
| 209 |
return fopen(fn, mode); |
| 210 |
} |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
#undef open |
| 215 |
int open(const char*, int, ...); |
| 216 |
|
| 217 |
int __open(const char* fn, int mode) |
| 218 |
{ |
| 219 |
int fd; |
| 220 |
char buf[FILENAME_MAX]; |
| 221 |
short drv; |
| 222 |
|
| 223 |
/* prepend a path to current dir to avoid use of default library */ |
| 224 |
if (*fn != '.' && *fn != '/') { |
| 225 |
strcpy(buf, "./"); |
| 226 |
strcat(buf, fn); |
| 227 |
return open(buf, mode); |
| 228 |
} |
| 229 |
|
| 230 |
if ((fd = open(fn, mode)) != EOF) |
| 231 |
return fd; |
| 232 |
|
| 233 |
/* see comment for _locate */ |
| 234 |
if (_locate(fn, buf, &drv)) { |
| 235 |
fn = strrchr(fn, '/'); |
| 236 |
if (fn) |
| 237 |
return open(fn, mode); |
| 238 |
} |
| 239 |
return EOF; |
| 240 |
} |
| 241 |
#endif |
| 242 |
|
| 243 |
/* replacement for standard file stat */ |
| 244 |
|
| 245 |
int _stat(const char *_fn, struct stat *st) |
| 246 |
{ |
| 247 |
char buf[FILENAME_MAX], buf2[FILENAME_MAX], buf3[FILENAME_MAX]; |
| 248 |
register struct fdb* fdb; |
| 249 |
register char* p; |
| 250 |
register char* fn; |
| 251 |
|
| 252 |
fn = strcpy(buf3, _fn); |
| 253 |
|
| 254 |
if (p = strrchr(fn, ':')) |
| 255 |
*p = 0; |
| 256 |
|
| 257 |
/* on current drive ./:d and .:m point to current dir |
| 258 |
* on another drive to root directory, workaround to avoid it */ |
| 259 |
|
| 260 |
if (! strcmp(fn, "/") || ! strcmp(fn, ".") || ! strcmp(fn, "./")) { |
| 261 |
if (p == NULL) { |
| 262 |
/* current dir on current drive */ |
| 263 |
fn = getcwd(buf2, FILENAME_MAX); |
| 264 |
/* getcwd returns NULL on root dir on drive S */ |
| 265 |
if (fn == NULL) |
| 266 |
fn = strcpy(buf2, "/:S"); |
| 267 |
/* getcwd returns /:d on root dir on any other drive */ |
| 268 |
if (fn[1] == ':') |
| 269 |
return rdirstat(fn, st); |
| 270 |
} else { |
| 271 |
*p = ':'; |
| 272 |
return rdirstat(fn, st); |
| 273 |
} |
| 274 |
if (p) |
| 275 |
*p = ':'; |
| 276 |
} else { |
| 277 |
if (p) |
| 278 |
*p = ':'; |
| 279 |
if (*fn != '.' && *fn != '/') { |
| 280 |
strcpy(buf2, "./"); |
| 281 |
fn = strcat(buf2, fn); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if (buf2 != fn) |
| 286 |
strcpy(buf2, fn); |
| 287 |
/* remove trailing slash before optional disk name */ |
| 288 |
if (p = strrchr(buf2, '/')) { |
| 289 |
if (p[1] == ':') { |
| 290 |
*p = p[1]; |
| 291 |
p[1] = p[2]; |
| 292 |
p[2] = p[3]; |
| 293 |
} else if (p[1] == '\0') |
| 294 |
*p = '\0'; |
| 295 |
} |
| 296 |
/* if fn is a file get file directory block structure and device */ |
| 297 |
if (fdb = _locate(buf2, buf, &st->st_dev)) { |
| 298 |
/* is it a file from another user... */ |
| 299 |
if (strchr(buf2, '\\') |
| 300 |
/* a public system file... */ |
| 301 |
|| fdb->fileowner == 0 |
| 302 |
/* or a file from the current user account ? */ |
| 303 |
|| fdb->fileowner == getuid()) |
| 304 |
/* yes, return stat */ |
| 305 |
return _stat_(st, fdb); |
| 306 |
else { |
| 307 |
/* no, say file doesn't exist */ |
| 308 |
errno = _errnum = ENOENT; |
| 309 |
_errarg = fn; |
| 310 |
return -1; |
| 311 |
} |
| 312 |
} |
| 313 |
/* else should be a device, get device number from device name */ |
| 314 |
st->st_rdev = st->st_dev = _lub_name(*fn == ':' ? fn+1 : fn); |
| 315 |
/* if it is really a device return device status */ |
| 316 |
if (st->st_dev != -1 && getlub(st->st_dev) != 255) |
| 317 |
return _dstat_(st); |
| 318 |
/* neither an existing file or a device name, return EOF */ |
| 319 |
st->st_rdev = st->st_dev = 0; |
| 320 |
errno = _errnum = ENOENT; |
| 321 |
_errarg = fn; |
| 322 |
return -1; |
| 323 |
} |
| 324 |
|
| 325 |
/* replacement for fstat */ |
| 326 |
|
| 327 |
int _fstat(int fd, struct stat *st) |
| 328 |
{ |
| 329 |
unsigned short fsanum; |
| 330 |
struct fsa fsa; |
| 331 |
register FILE *fp; |
| 332 |
int status; |
| 333 |
register int i; |
| 334 |
register char *p; |
| 335 |
|
| 336 |
if (fd < FOPEN_MAX) { |
| 337 |
fp = &stdin[fd]; |
| 338 |
/* get File Save Area number */ |
| 339 |
if (_fcntl(fp,1,0) & 0x80) { |
| 340 |
fsanum = (unsigned short) _fcntl(fp,83,0); |
| 341 |
st->st_dev = (unsigned short) _fcntl(fp,5,0); |
| 342 |
|
| 343 |
if (st->st_dev >= A_DISK && st->st_dev <= Z_DISK) { |
| 344 |
/* if opened file is a disk file */ |
| 345 |
/* copy far fsa in protected segment to local fsa */ |
| 346 |
for (i = 0, fsanum *= sizeof(fsa), p = (char *) &fsa; |
| 347 |
i < (sizeof(fsa)); |
| 348 |
i++, fsanum++, p++) |
| 349 |
*p = _peekfsa((char *) fsanum); |
| 350 |
/* build stat structure from fsa */ |
| 351 |
status = _stat_(st, (struct fdb*) &fsa); |
| 352 |
/* get blocksize */ |
| 353 |
if ((st->st_blksize = _fcntl(fp,817,0)) == 0) |
| 354 |
st->st_blksize = BUFSIZ; |
| 355 |
return status; |
| 356 |
} |
| 357 |
/* return device status */ |
| 358 |
return _dstat_(st); |
| 359 |
} |
| 360 |
} |
| 361 |
errno = _errnum = EBADF; |
| 362 |
return -1; |
| 363 |
} |
| 364 |
|
| 365 |
static int _isprt(int dev) |
| 366 |
{ |
| 367 |
return IS_PRT_LUB(dev); |
| 368 |
} |
| 369 |
|
| 370 |
/* device stat */ |
| 371 |
|
| 372 |
int _dstat_(st) |
| 373 |
register struct stat* st; |
| 374 |
{ |
| 375 |
register struct ucb* ucb; |
| 376 |
|
| 377 |
ucb = getucb(st->st_dev); |
| 378 |
st->st_ino = 0; |
| 379 |
if (st->st_dev <= Z_DISK |
| 380 |
|| (st->st_dev >= TAPE1 && st->st_dev <= TAPE4)) { |
| 381 |
st->st_mode = S_IFBLK | S_IWUSR | S_IRUSR; |
| 382 |
if (peekucb(&ucb->devowner) == 255) |
| 383 |
st->st_mode |= S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH; |
| 384 |
} else { |
| 385 |
st->st_mode = S_IFCHR | S_IWUSR; |
| 386 |
if (_isprt(st->st_dev)) |
| 387 |
st->st_mode |= S_IRUSR; |
| 388 |
if (peekucb(&ucb->devowner) == 255) { |
| 389 |
st->st_mode |= S_IWGRP | S_IWOTH; |
| 390 |
if (_isprt(st->st_dev)) |
| 391 |
st->st_mode |= S_IRGRP | S_IROTH; |
| 392 |
} |
| 393 |
} |
| 394 |
st->st_nlink = 1; |
| 395 |
st->st_uid = st->st_gid = getuid(); |
| 396 |
st->st_size = 0; |
| 397 |
st->st_atime = st->st_mtime = st->st_ctime = 0; |
| 398 |
st->st_rlen = 0; |
| 399 |
st->st_klen = 0; |
| 400 |
st->st_grow = 0; |
| 401 |
st->st_blksize = 0; |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
/* regular file stat */ |
| 406 |
|
| 407 |
int _stat_(st, fdb) |
| 408 |
register struct stat* st; |
| 409 |
register struct fdb* fdb; |
| 410 |
{ |
| 411 |
st->st_rdev = st->st_dev; |
| 412 |
st->st_ino = 0; |
| 413 |
st->st_org = fdb->filestat; |
| 414 |
|
| 415 |
/* map fdb file status to stat mode */ |
| 416 |
switch (fdb->filestat) { |
| 417 |
case _FDB_STAT_LIBRARY: st->st_mode = S_IFLIB; break; |
| 418 |
case _FDB_STAT_DIRECTORY: st->st_mode = S_IFDIR; break; |
| 419 |
case _FDB_STAT_STREAM: st->st_mode = S_IFREG; break; |
| 420 |
case _FDB_STAT_RELATIVE: st->st_mode = S_IFREL; break; |
| 421 |
case _FDB_STAT_KEYED: st->st_mode = S_IFKEY; break; |
| 422 |
case _FDB_STAT_INDEXED: st->st_mode = S_IFIND; break; |
| 423 |
case _FDB_STAT_RANDOM: st->st_mode = S_IFRND; break; |
| 424 |
case _FDB_STAT_PROGRAM: st->st_mode = S_IFR16; break; |
| 425 |
case _FDB_STAT_16_BIT_PROGRAM: st->st_mode = S_IFP16; break; |
| 426 |
case _FDB_STAT_32_BIT_PROGRAM: st->st_mode = S_IFP32; break; |
| 427 |
} |
| 428 |
|
| 429 |
/* map theos file protection codes to stat mode */ |
| 430 |
st->st_mode |= _tm2um_(st->st_protect = fdb->protect); |
| 431 |
st->st_nlink = 1; |
| 432 |
st->st_uid = st->st_gid = fdb->fileowner; |
| 433 |
st->st_size = fdb->filesize; |
| 434 |
st->st_atime = st->st_mtime = st->st_ctime = getfiledate(fdb); |
| 435 |
st->st_blksize = 0; |
| 436 |
/* specific theos information */ |
| 437 |
st->st_rlen = fdb->reclen; |
| 438 |
st->st_klen = fdb->keylen; |
| 439 |
st->st_grow = fdb->filegrow; |
| 440 |
return 0; |
| 441 |
} |
| 442 |
|
| 443 |
#include <direct.h> |
| 444 |
|
| 445 |
/* standard diropen fails on path endung with a '/', workaround */ |
| 446 |
|
| 447 |
struct dirent* _opendir(const char* dirpath) |
| 448 |
{ |
| 449 |
int l; |
| 450 |
char dirp[FILENAME_MAX]; |
| 451 |
struct dirent* dir; |
| 452 |
|
| 453 |
if (dirpath && (l = strlen(dirpath))) { |
| 454 |
if (dirpath[l - 1] == '/') { |
| 455 |
strcpy(dirp, dirpath); |
| 456 |
dirp[l - 1] = '\0'; |
| 457 |
return opendir(dirp); |
| 458 |
} |
| 459 |
} |
| 460 |
return opendir(dirpath); |
| 461 |
} |