Annotation of MiNT/src/dosdir.c, revision 1.1.1.6

1.1       root        1: /*
                      2: 
1.1.1.3   root        3: Copyright 1990,1991,1992 Eric R. Smith.
                      4: 
1.1.1.5   root        5: Copyright 1992,1993,1994 Atari Corporation.
1.1.1.3   root        6: 
                      7: All rights reserved.
1.1       root        8: 
                      9: */
                     10: 
                     11: 
                     12: 
                     13: /* DOS directory functions */
                     14: 
                     15: 
                     16: 
                     17: #include "mint.h"
                     18: 
                     19: 
                     20: 
1.1.1.5   root       21: extern int aliasdrv[]; /* in filesys.c */
                     22: 
                     23: 
                     24: 
1.1       root       25: /* change to a new drive: should always return a map of valid drives */
                     26: 
                     27: 
                     28: 
1.1.1.2   root       29: long ARGS_ON_STACK
1.1       root       30: 
                     31: d_setdrv(d)
                     32: 
                     33:        int d;
                     34: 
                     35: {
                     36: 
                     37:        long r;
                     38: 
                     39:        extern long dosdrvs;    /* in filesys.c */
                     40: 
                     41: 
                     42: 
                     43:        r = drvmap() | dosdrvs | PSEUDODRVS;
                     44: 
                     45: 
                     46: 
1.1.1.2   root       47:        TRACE(("Dsetdrv(%d)", d));
1.1       root       48: 
                     49:        if (d < 0 || d >= NUM_DRIVES || (r & (1L << d)) == 0) {
                     50: 
1.1.1.2   root       51:                DEBUG(("Dsetdrv: invalid drive %d", d));
1.1       root       52: 
                     53:                return r;
                     54: 
                     55:        }
                     56: 
                     57: 
                     58: 
                     59:        curproc->base->p_defdrv = curproc->curdrv = d;
                     60: 
                     61:        return r;
                     62: 
                     63: }
                     64: 
                     65: 
                     66: 
1.1.1.3   root       67: 
                     68: 
1.1.1.2   root       69: long ARGS_ON_STACK
1.1       root       70: 
                     71: d_getdrv()
                     72: 
                     73: {
                     74: 
1.1.1.2   root       75:        TRACE(("Dgetdrv"));
1.1       root       76: 
                     77:        return curproc->curdrv;
                     78: 
                     79: }
                     80: 
                     81: 
                     82: 
1.1.1.2   root       83: long ARGS_ON_STACK
1.1       root       84: 
                     85: d_free(buf, d)
                     86: 
                     87:        long *buf;
                     88: 
                     89:        int d;
                     90: 
                     91: {
                     92: 
1.1.1.3   root       93:        fcookie *dir = 0;
1.1       root       94: 
1.1.1.5   root       95:        FILESYS *fs;
                     96: 
                     97:        fcookie root;
                     98: 
                     99:        long r;
1.1       root      100: 
                    101: 
1.1.1.2   root      102: 
                    103:        TRACE(("Dfree(%d)", d));
1.1       root      104: 
                    105: 
                    106: 
                    107: /* drive 0 means current drive, otherwise it's d-1 */
                    108: 
                    109:        if (d)
                    110: 
                    111:                d = d-1;
                    112: 
                    113:        else
                    114: 
                    115:                d = curproc->curdrv;
                    116: 
                    117: 
                    118: 
1.1.1.5   root      119: /* If it's not a standard drive or an alias of one, get the pointer to
1.1.1.3   root      120: 
1.1.1.5   root      121:    the filesystem structure and use the root directory of the
1.1.1.3   root      122: 
1.1.1.5   root      123:    drive. */
1.1.1.2   root      124: 
                    125:        if (d < 0 || d >= NUM_DRIVES) {
                    126: 
                    127:                int i;
                    128: 
                    129: 
                    130: 
                    131:                for (i = 0; i < NUM_DRIVES; i++) {
                    132: 
                    133:                        if (aliasdrv[i] == d) {
                    134: 
                    135:                                d = i;
                    136: 
                    137:                                goto aliased;
                    138: 
                    139:                        }
                    140: 
1.1.1.5   root      141:                }
1.1.1.3   root      142: 
                    143: 
                    144: 
1.1.1.5   root      145:                fs = get_filesys (d);
1.1.1.3   root      146: 
1.1.1.5   root      147:                if (!fs)
1.1.1.3   root      148: 
1.1.1.5   root      149:                  return EDRIVE;
1.1.1.3   root      150: 
1.1.1.5   root      151:                r = fs->root (d, &root);
1.1.1.3   root      152: 
1.1.1.5   root      153:                if (r < 0)
1.1.1.3   root      154: 
1.1.1.5   root      155:                  return r;
1.1       root      156: 
1.1.1.5   root      157:                r = (*fs->dfree) (&root, buf);
                    158: 
                    159:                release_cookie (&root);
                    160: 
                    161:                return r;
1.1       root      162: 
1.1.1.2   root      163:        }
                    164: 
1.1       root      165: 
                    166: 
                    167: /* check for a media change -- we don't care much either way, but it
                    168: 
                    169:  * does keep the results more accurate
                    170: 
                    171:  */
                    172: 
                    173:        (void)disk_changed(d);
                    174: 
                    175: 
                    176: 
1.1.1.2   root      177: aliased:
                    178: 
                    179: 
                    180: 
1.1       root      181: /* use current directory, not root, since it's more likely that
                    182: 
                    183:  * programs are interested in the latter (this makes U: work much
                    184: 
                    185:  * better)
                    186: 
                    187:  */
                    188: 
                    189:        dir = &curproc->curdir[d];
                    190: 
                    191:        if (!dir->fs) {
                    192: 
1.1.1.2   root      193:                DEBUG(("Dfree: bad drive"));
1.1       root      194: 
                    195:                return EDRIVE;
                    196: 
                    197:        }
                    198: 
                    199: 
                    200: 
                    201:        return (*dir->fs->dfree)(dir, buf);
                    202: 
                    203: }
                    204: 
                    205: 
                    206: 
1.1.1.2   root      207: long ARGS_ON_STACK
1.1       root      208: 
                    209: d_create(path)
                    210: 
                    211:        const char *path;
                    212: 
                    213: {
                    214: 
                    215:        fcookie dir;
                    216: 
                    217:        long r;
                    218: 
1.1.1.4   root      219:        char temp1[PATH_MAX];
                    220: 
1.1       root      221: 
                    222: 
1.1.1.2   root      223:        TRACE(("Dcreate(%s)", path));
1.1       root      224: 
                    225: 
                    226: 
                    227:        r = path2cookie(path, temp1, &dir);
                    228: 
                    229:        if (r) {
                    230: 
1.1.1.2   root      231:                DEBUG(("Dcreate(%s): returning %ld", path, r));
1.1       root      232: 
                    233:                return r;       /* an error occured */
                    234: 
                    235:        }
                    236: 
                    237: /* check for write permission on the directory */
                    238: 
                    239:        r = dir_access(&dir, S_IWOTH);
                    240: 
                    241:        if (r) {
                    242: 
1.1.1.5   root      243:                DEBUG(("Dcreate(%s): write access to directory denied",path));
1.1       root      244: 
1.1.1.3   root      245:                release_cookie(&dir);
                    246: 
1.1       root      247:                return r;
                    248: 
                    249:        }
                    250: 
1.1.1.3   root      251:        r = (*dir.fs->mkdir)(&dir, temp1, DEFAULT_DIRMODE & ~curproc->umask);
                    252: 
                    253:        release_cookie(&dir);
                    254: 
                    255:        return r;
1.1       root      256: 
                    257: }
                    258: 
                    259: 
                    260: 
1.1.1.2   root      261: long ARGS_ON_STACK
1.1       root      262: 
                    263: d_delete(path)
                    264: 
                    265:        const char *path;
                    266: 
                    267: {
                    268: 
                    269:        fcookie parentdir, targdir;
                    270: 
                    271:        long r;
                    272: 
                    273:        PROC *p;
                    274: 
                    275:        int i;
                    276: 
                    277:        XATTR xattr;
                    278: 
1.1.1.4   root      279:        char temp1[PATH_MAX];
                    280: 
1.1       root      281: 
                    282: 
1.1.1.2   root      283:        TRACE(("Ddelete(%s)", path));
1.1       root      284: 
                    285: 
                    286: 
                    287:        r = path2cookie(path, temp1, &parentdir);
                    288: 
                    289: 
                    290: 
                    291:        if (r) {
                    292: 
1.1.1.2   root      293:                DEBUG(("Ddelete(%s): error %lx", path, r));
1.1       root      294: 
1.1.1.3   root      295:                release_cookie(&parentdir);
                    296: 
1.1       root      297:                return r;
                    298: 
                    299:        }
                    300: 
                    301: /* check for write permission on the directory which the target
                    302: 
                    303:  * is located
                    304: 
                    305:  */
                    306: 
1.1.1.2   root      307:        if ((r = dir_access(&parentdir, S_IWOTH)) != 0) {
1.1       root      308: 
1.1.1.2   root      309:                DEBUG(("Ddelete(%s): access to directory denied", path));
1.1       root      310: 
1.1.1.3   root      311:                release_cookie(&parentdir);
                    312: 
1.1       root      313:                return r;
                    314: 
                    315:        }
                    316: 
                    317: 
                    318: 
                    319: /* now get the info on the file itself */
                    320: 
                    321: 
                    322: 
                    323:        r = relpath2cookie(&parentdir, temp1, NULL, &targdir, 0);
                    324: 
1.1.1.3   root      325:        if (r) {
                    326: 
                    327: bailout:
                    328: 
                    329:                release_cookie(&parentdir);
1.1       root      330: 
1.1.1.2   root      331:                DEBUG(("Ddelete: error %ld on %s", r, path));
1.1       root      332: 
                    333:                return r;
                    334: 
                    335:        }
                    336: 
1.1.1.3   root      337:        if ((r = (*targdir.fs->getxattr)(&targdir, &xattr)) != 0) {
                    338: 
                    339:                release_cookie(&targdir);
                    340: 
                    341:                goto bailout;
                    342: 
                    343:        }
                    344: 
1.1       root      345: 
                    346: 
                    347: /* if the "directory" is a symbolic link, really unlink it */
                    348: 
                    349:        if ( (xattr.mode & S_IFMT) == S_IFLNK ) {
                    350: 
1.1.1.3   root      351:                r = (*parentdir.fs->remove)(&parentdir, temp1);
1.1       root      352: 
1.1.1.3   root      353:        } else if ( (xattr.mode & S_IFMT) != S_IFDIR ) {
1.1       root      354: 
1.1.1.2   root      355:                DEBUG(("Ddelete: %s is not a directory", path));
1.1       root      356: 
1.1.1.3   root      357:                r = EPTHNF;
1.1       root      358: 
1.1.1.3   root      359:        } else {
1.1       root      360: 
                    361: 
                    362: 
                    363: /* don't delete anyone else's root or current directory */
                    364: 
1.1.1.3   root      365:            for (p = proclist; p; p = p->gl_next) {
1.1       root      366: 
                    367:                if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
                    368: 
                    369:                        continue;
                    370: 
                    371:                for (i = 0; i < NUM_DRIVES; i++) {
                    372: 
                    373:                        if (samefile(&targdir, &p->root[i])) {
                    374: 
1.1.1.2   root      375:                                DEBUG(("Ddelete: directory %s is a root directory",
1.1       root      376: 
1.1.1.2   root      377:                                        path));
1.1       root      378: 
1.1.1.3   root      379: noaccess:
                    380: 
                    381:                                release_cookie(&targdir);
                    382: 
                    383:                                release_cookie(&parentdir);
                    384: 
1.1       root      385:                                return EACCDN;
                    386: 
                    387:                        } else if (samefile(&targdir, &p->curdir[i])) {
                    388: 
1.1.1.2   root      389:                                if (i == p->curdrv && p != curproc) {
1.1       root      390: 
1.1.1.2   root      391:                                        DEBUG(("Ddelete: directory %s is in use",
1.1       root      392: 
1.1.1.2   root      393:                                                path));
1.1       root      394: 
1.1.1.3   root      395:                                        goto noaccess;
1.1.1.2   root      396: 
                    397:                                } else {
1.1       root      398: 
1.1.1.3   root      399:                                        release_cookie(&p->curdir[i]);
                    400: 
                    401:                                        dup_cookie(&p->curdir[i], &p->root[i]);
1.1       root      402: 
                    403:                                } 
                    404: 
                    405:                        }
                    406: 
                    407:                }
                    408: 
1.1.1.3   root      409:            }
1.1       root      410: 
1.1.1.3   root      411:            release_cookie(&targdir);
1.1       root      412: 
1.1.1.3   root      413:            r = (*parentdir.fs->rmdir)(&parentdir, temp1);
1.1       root      414: 
1.1.1.3   root      415:        }
                    416: 
                    417:        release_cookie(&parentdir);
                    418: 
                    419:        return r;
1.1       root      420: 
                    421: }
                    422: 
                    423: 
                    424: 
1.1.1.2   root      425: long ARGS_ON_STACK
1.1       root      426: 
                    427: d_setpath(path)
                    428: 
                    429:        const char *path;
                    430: 
                    431: {
                    432: 
                    433:        fcookie dir;
                    434: 
                    435:        int drv = curproc->curdrv;
                    436: 
                    437:        int i;
                    438: 
                    439:        char c;
                    440: 
                    441:        long r;
                    442: 
                    443:        XATTR xattr;
                    444: 
                    445: 
                    446: 
1.1.1.2   root      447:        TRACE(("Dsetpath(%s)", path));
1.1       root      448: 
                    449: 
                    450: 
                    451:        r = path2cookie(path, follow_links, &dir);
                    452: 
                    453: 
                    454: 
                    455:        if (r) {
                    456: 
1.1.1.2   root      457:                DEBUG(("Dsetpath(%s): returning %ld", path, r));
1.1       root      458: 
                    459:                return r;
                    460: 
                    461:        }
                    462: 
                    463: 
                    464: 
                    465:        if (path[0] && path[1] == ':') {
                    466: 
                    467:                c = *path;
                    468: 
                    469:                if (c >= 'a' && c <= 'z')
                    470: 
                    471:                        drv = c-'a';
                    472: 
                    473:                else if (c >= 'A' && c <= 'Z')
                    474: 
                    475:                        drv = c-'A';
                    476: 
                    477:        }
                    478: 
                    479: 
                    480: 
                    481:        r = (*dir.fs->getxattr)(&dir, &xattr);
                    482: 
                    483: 
                    484: 
                    485:        if (r < 0) {
                    486: 
1.1.1.2   root      487:                DEBUG(("Dsetpath: file '%s': attributes not found", path));
1.1       root      488: 
1.1.1.3   root      489:                release_cookie(&dir);
                    490: 
1.1       root      491:                return r;
                    492: 
                    493:        }
                    494: 
                    495: 
                    496: 
                    497:        if (!(xattr.attr & FA_DIR)) {
                    498: 
1.1.1.2   root      499:                DEBUG(("Dsetpath(%s): not a directory",path));
1.1       root      500: 
1.1.1.3   root      501:                release_cookie(&dir);
                    502: 
1.1       root      503:                return EPTHNF;
                    504: 
                    505:        }
                    506: 
                    507: 
                    508: 
1.1.1.5   root      509:        if (denyaccess(&xattr, S_IROTH|S_IXOTH)) {
                    510: 
                    511:                DEBUG(("Dsetpath(%s): access denied", path));
                    512: 
                    513:                release_cookie(&dir);
                    514: 
                    515:                return EACCDN;
                    516: 
                    517:        }
                    518: 
1.1       root      519: /*
                    520: 
                    521:  * watch out for symbolic links; if c:\foo is a link to d:\bar, then
                    522: 
                    523:  * "cd c:\foo" should also change the drive to d:
                    524: 
                    525:  */
                    526: 
                    527:        if (drv != UNIDRV && dir.dev != curproc->root[drv].dev) {
                    528: 
                    529:                for (i = 0; i < NUM_DRIVES; i++) {
                    530: 
                    531:                        if (curproc->root[i].dev == dir.dev &&
                    532: 
                    533:                            curproc->root[i].fs == dir.fs) {
                    534: 
                    535:                                if (drv == curproc->curdrv)
                    536: 
                    537:                                        curproc->curdrv = i;
                    538: 
                    539:                                drv = i;
                    540: 
                    541:                                break;
                    542: 
                    543:                        }
                    544: 
                    545:                }
                    546: 
                    547:        }
                    548: 
1.1.1.3   root      549:        release_cookie(&curproc->curdir[drv]);
                    550: 
1.1       root      551:        curproc->curdir[drv] = dir;
                    552: 
                    553:        return 0;
                    554: 
                    555: }
                    556: 
                    557: 
                    558: 
1.1.1.3   root      559: /* jr: like d_getpath, except that the caller provides a limit
                    560: 
                    561:    for the max. number of characters to be put into the buffer.
                    562: 
                    563:    Inspired by POSIX.1, getcwd(), 5.2.2 */
                    564: 
                    565: 
                    566: 
1.1.1.2   root      567: long ARGS_ON_STACK
1.1       root      568: 
1.1.1.3   root      569: d_getcwd(path, drv, size)
1.1       root      570: 
                    571:        char *path;
                    572: 
1.1.1.3   root      573:        int drv, size;
1.1       root      574: 
                    575: {
                    576: 
                    577:        fcookie *dir, *root;
                    578: 
1.1.1.3   root      579:        long r;
                    580: 
                    581:        char buf[PATH_MAX];
                    582: 
                    583:        FILESYS *fs;
1.1       root      584: 
                    585: 
1.1.1.3   root      586: 
                    587:        TRACE(("Dgetcwd(%c, %d)", drv + '@', size));
1.1       root      588: 
                    589:        if (drv < 0 || drv > NUM_DRIVES)
                    590: 
                    591:                return EDRIVE;
                    592: 
                    593: 
                    594: 
                    595:        drv = (drv == 0) ? curproc->curdrv : drv-1;
                    596: 
                    597: 
                    598: 
                    599:        root = &curproc->root[drv];
                    600: 
                    601: 
                    602: 
                    603:        if (!root->fs) {        /* maybe not initialized yet? */
                    604: 
                    605:                changedrv(drv);
                    606: 
                    607:                root = &curproc->curdir[drv];
                    608: 
                    609:                if (!root->fs)
                    610: 
                    611:                        return EDRIVE;
                    612: 
                    613:        }
                    614: 
1.1.1.3   root      615:        fs = root->fs;
1.1       root      616: 
1.1.1.3   root      617:        dir = &curproc->curdir[drv];
1.1.1.2   root      618: 
                    619: 
                    620: 
1.1.1.3   root      621:        if (!(fs->fsflags & FS_LONGPATH)) {
1.1.1.2   root      622: 
1.1.1.3   root      623:                r = (*fs->getname)(root, dir, buf, PATH_MAX);
1.1.1.2   root      624: 
1.1.1.3   root      625:                if (r) return r;
1.1.1.2   root      626: 
1.1.1.3   root      627:                if (strlen(buf) < size) {
1.1.1.2   root      628: 
1.1.1.3   root      629:                        strcpy(path, buf);
1.1.1.2   root      630: 
1.1.1.3   root      631:                        return 0;
1.1.1.2   root      632: 
1.1.1.3   root      633:                } else {
1.1.1.2   root      634: 
1.1.1.3   root      635:                        return ERANGE;
1.1.1.2   root      636: 
1.1.1.3   root      637:                }
1.1.1.2   root      638: 
1.1.1.3   root      639:        }
1.1.1.2   root      640: 
1.1.1.3   root      641:        return (*fs->getname)(root, dir, path, size);
1.1.1.2   root      642: 
1.1.1.3   root      643: }
1.1.1.2   root      644: 
                    645: 
                    646: 
1.1.1.3   root      647: long ARGS_ON_STACK
1.1.1.2   root      648: 
1.1.1.3   root      649: d_getpath(path, drv)
1.1.1.2   root      650: 
1.1.1.3   root      651:        char *path;
1.1.1.2   root      652: 
1.1.1.3   root      653:        int drv;
1.1.1.2   root      654: 
1.1.1.3   root      655: {
1.1.1.2   root      656: 
1.1.1.3   root      657:        TRACE(("Dgetpath(%c)", drv + '@'));
1.1.1.2   root      658: 
1.1.1.3   root      659:        return d_getcwd(path, drv, PATH_MAX);
1.1.1.2   root      660: 
                    661: }
                    662: 
                    663: 
                    664: 
                    665: long ARGS_ON_STACK
1.1       root      666: 
                    667: f_setdta(dta)
                    668: 
                    669:        DTABUF *dta;
                    670: 
                    671: {
                    672: 
                    673: 
                    674: 
1.1.1.2   root      675:        TRACE(("Fsetdta: %lx", dta));
1.1       root      676: 
                    677:        curproc->dta = dta;
                    678: 
                    679:        curproc->base->p_dta = (char *)dta;
                    680: 
                    681:        return 0;
                    682: 
                    683: }
                    684: 
                    685: 
                    686: 
1.1.1.2   root      687: long ARGS_ON_STACK
1.1       root      688: 
                    689: f_getdta()
                    690: 
                    691: {
                    692: 
                    693:        long r;
                    694: 
                    695: 
                    696: 
                    697:        r = (long)curproc->dta;
                    698: 
1.1.1.2   root      699:        TRACE(("Fgetdta: returning %lx", r));
1.1       root      700: 
                    701:        return r;
                    702: 
                    703: }
                    704: 
                    705: 
                    706: 
                    707: /*
                    708: 
                    709:  * Fsfirst/next are actually implemented in terms of opendir/readdir/closedir.
                    710: 
                    711:  */
                    712: 
                    713: 
                    714: 
1.1.1.2   root      715: long ARGS_ON_STACK
1.1       root      716: 
                    717: f_sfirst(path, attrib)
                    718: 
                    719:        const char *path;
                    720: 
                    721:        int attrib;
                    722: 
                    723: {
                    724: 
                    725:        char *s, *slash;
                    726: 
                    727:        FILESYS *fs;
                    728: 
                    729:        fcookie dir, newdir;
                    730: 
                    731:        DTABUF *dta;
                    732: 
                    733:        DIR *dirh;
                    734: 
                    735:        XATTR xattr;
                    736: 
                    737:        long r;
                    738: 
                    739:        int i, havelabel;
                    740: 
1.1.1.4   root      741:        char temp1[PATH_MAX];
                    742: 
1.1       root      743: 
                    744: 
1.1.1.2   root      745:        TRACE(("Fsfirst(%s, %x)", path, attrib));
1.1       root      746: 
                    747: 
                    748: 
                    749:        r = path2cookie(path, temp1, &dir);
                    750: 
                    751: 
                    752: 
                    753:        if (r) {
                    754: 
1.1.1.2   root      755:                DEBUG(("Fsfirst(%s): path2cookie returned %ld", path, r));
1.1       root      756: 
                    757:                return r;
                    758: 
                    759:        }
                    760: 
                    761: 
                    762: 
                    763: /*
                    764: 
                    765:  * we need to split the last name (which may be a pattern) off from
                    766: 
                    767:  * the rest of the path, even if FS_KNOPARSE is true
                    768: 
                    769:  */
                    770: 
                    771:        slash = 0;
                    772: 
                    773:        s = temp1;
                    774: 
                    775:        while (*s) {
                    776: 
                    777:                if (*s == '\\')
                    778: 
                    779:                        slash = s;
                    780: 
                    781:                s++;
                    782: 
                    783:        }
                    784: 
                    785: 
                    786: 
                    787:        if (slash) {
                    788: 
                    789:                *slash++ = 0;   /* slash now points to a name or pattern */
                    790: 
                    791:                r = relpath2cookie(&dir, temp1, follow_links, &newdir, 0);
                    792: 
1.1.1.3   root      793:                release_cookie(&dir);
                    794: 
1.1       root      795:                if (r) {
                    796: 
1.1.1.2   root      797:                        DEBUG(("Fsfirst(%s): lookup returned %ld", path, r));
1.1       root      798: 
                    799:                        return r;
                    800: 
                    801:                }
                    802: 
                    803:                dir = newdir;
                    804: 
                    805:        } else {
                    806: 
                    807:                slash = temp1;
                    808: 
                    809:        }
                    810: 
                    811: 
                    812: 
                    813: /* BUG? what if there really is an empty file name? */
                    814: 
                    815:        if (!*slash) {
                    816: 
1.1.1.2   root      817:                DEBUG(("Fsfirst: empty pattern"));
1.1       root      818: 
                    819:                return EFILNF;
                    820: 
                    821:        }
                    822: 
                    823: 
                    824: 
                    825:        fs = dir.fs;
                    826: 
                    827:        dta = curproc->dta;
                    828: 
                    829: 
                    830: 
                    831: /* Now, see if we can find a DIR slot for the search. We use the following
                    832: 
                    833:  * heuristics to try to avoid destroying a slot:
                    834: 
                    835:  * (1) if the search doesn't use wildcards, don't bother with a slot
                    836: 
                    837:  * (2) if an existing slot was for the same DTA address, re-use it
                    838: 
                    839:  * (3) if there's a free slot, re-use it. Slots are freed when the
                    840: 
                    841:  *     corresponding search is terminated.
                    842: 
                    843:  */
                    844: 
                    845: 
                    846: 
                    847:        for (i = 0; i < NUM_SEARCH; i++) {
                    848: 
                    849:                if (curproc->srchdta[i] == dta) {
                    850: 
                    851:                        dirh = &curproc->srchdir[i];
                    852: 
                    853:                        if (dirh->fc.fs) {
                    854: 
                    855:                                (*dirh->fc.fs->closedir)(dirh);
                    856: 
1.1.1.3   root      857:                                release_cookie(&dirh->fc);
                    858: 
1.1       root      859:                                dirh->fc.fs = 0;
                    860: 
                    861:                        }
                    862: 
                    863:                        curproc->srchdta[i] = 0; /* slot is now free */
                    864: 
                    865:                }
                    866: 
                    867:        }
                    868: 
                    869: 
                    870: 
                    871: /* copy the pattern over into dta_pat into TOS 8.3 form */
                    872: 
                    873: /* remember that "slash" now points at the pattern (it follows the last \,
                    874: 
                    875:    if any)
                    876: 
                    877:  */
                    878: 
                    879:        copy8_3(dta->dta_pat, slash);
                    880: 
                    881: 
                    882: 
                    883: /* if attrib & FA_LABEL, read the volume label */
                    884: 
                    885: /* BUG: the label date and time are wrong. Does it matter?
                    886: 
                    887:  */
                    888: 
                    889:        havelabel = 0;
                    890: 
                    891:        if (attrib & FA_LABEL) {
                    892: 
1.1.1.3   root      893:                r = (*fs->readlabel)(&dir, dta->dta_name, TOS_NAMELEN+1);
1.1       root      894: 
                    895:                dta->dta_attrib = FA_LABEL;
                    896: 
                    897:                dta->dta_time = dta->dta_date = 0;
                    898: 
                    899:                dta->dta_size = 0;
                    900: 
                    901:                dta->magic = EVALID;
                    902: 
                    903:                if (r == 0 && !pat_match(dta->dta_name, dta->dta_pat))
                    904: 
                    905:                        r = EFILNF;
                    906: 
                    907:                if (attrib == FA_LABEL)
                    908: 
                    909:                        return r;
                    910: 
                    911:                else if (r == 0)
                    912: 
                    913:                        havelabel = 1;
                    914: 
                    915:        }
                    916: 
                    917: 
                    918: 
                    919:        if (!havelabel && has_wild(slash) == 0) { /* no wild cards in pattern */
                    920: 
                    921:                r = relpath2cookie(&dir, slash, follow_links, &newdir, 0);
                    922: 
                    923:                if (r == 0) {
                    924: 
                    925:                        r = (*newdir.fs->getxattr)(&newdir, &xattr);
                    926: 
1.1.1.3   root      927:                        release_cookie(&newdir);
                    928: 
1.1       root      929:                }
                    930: 
1.1.1.3   root      931:                release_cookie(&dir);
                    932: 
1.1       root      933:                if (r) {
                    934: 
1.1.1.2   root      935:                        DEBUG(("Fsfirst(%s): couldn't get file attributes",path));
1.1       root      936: 
                    937:                        return r;
                    938: 
                    939:                }
                    940: 
                    941:                dta->magic = EVALID;
                    942: 
                    943:                dta->dta_attrib = xattr.attr;
                    944: 
                    945:                dta->dta_time = xattr.mtime;
                    946: 
                    947:                dta->dta_date = xattr.mdate;
                    948: 
                    949:                dta->dta_size = xattr.size;
                    950: 
                    951:                strncpy(dta->dta_name, slash, TOS_NAMELEN-1);
                    952: 
                    953:                dta->dta_name[TOS_NAMELEN-1] = 0;
                    954: 
                    955:                if (curproc->domain == DOM_TOS &&
                    956: 
                    957:                    !(fs->fsflags & FS_CASESENSITIVE))
                    958: 
                    959:                        strupr(dta->dta_name);
                    960: 
                    961:                return 0;
                    962: 
                    963:        }
                    964: 
                    965: 
                    966: 
                    967: /* There is a wild card. Try to find a slot for an opendir/readdir
                    968: 
                    969:  * search. NOTE: we also come here if we were asked to search for
                    970: 
                    971:  * volume labels and found one.
                    972: 
                    973:  */
                    974: 
                    975:        for (i = 0; i < NUM_SEARCH; i++) {
                    976: 
                    977:                if (curproc->srchdta[i] == 0)
                    978: 
                    979:                        break;
                    980: 
                    981:        }
                    982: 
                    983:        if (i == NUM_SEARCH) {
                    984: 
1.1.1.2   root      985:                int oldest = 0; long oldtime = curproc->srchtim[0];
1.1       root      986: 
                    987: 
                    988: 
1.1.1.2   root      989:                DEBUG(("Fsfirst(%s): having to re-use a directory slot!",path));
1.1       root      990: 
                    991:                for (i = 1; i < NUM_SEARCH; i++) {
                    992: 
                    993:                        if (curproc->srchtim[i] < oldtime) {
                    994: 
                    995:                                oldest = i;
                    996: 
                    997:                                oldtime = curproc->srchtim[i];
                    998: 
                    999:                        }
                   1000: 
                   1001:                }
                   1002: 
                   1003:        /* OK, close this directory for re-use */
                   1004: 
                   1005:                i = oldest;
                   1006: 
                   1007:                dirh = &curproc->srchdir[i];
                   1008: 
                   1009:                if (dirh->fc.fs) {
                   1010: 
                   1011:                        (*dirh->fc.fs->closedir)(dirh);
                   1012: 
1.1.1.3   root     1013:                        release_cookie(&dirh->fc);
                   1014: 
1.1       root     1015:                        dirh->fc.fs = 0;
                   1016: 
                   1017:                }
                   1018: 
                   1019:                curproc->srchdta[i] = 0;
                   1020: 
                   1021:        }
                   1022: 
                   1023: 
                   1024: 
                   1025: /* check to see if we have read permission on the directory (and make
                   1026: 
1.1.1.2   root     1027:  * sure that it really is a directory!)
1.1       root     1028: 
                   1029:  */
                   1030: 
                   1031:        r = dir_access(&dir, S_IROTH);
                   1032: 
                   1033:        if (r) {
                   1034: 
1.1.1.3   root     1035:                DEBUG(("Fsfirst(%s): access to directory denied (error code %ld)", path, r));
                   1036: 
                   1037:                release_cookie(&dir);
1.1       root     1038: 
                   1039:                return r;
                   1040: 
                   1041:        }
                   1042: 
                   1043: 
                   1044: 
                   1045: /* set up the directory for a search */
                   1046: 
                   1047:        dirh = &curproc->srchdir[i];
                   1048: 
                   1049:        dirh->fc = dir;
                   1050: 
                   1051:        dirh->index = 0;
                   1052: 
                   1053:        dirh->flags = TOS_SEARCH;
                   1054: 
                   1055:        r = (*dir.fs->opendir)(dirh, dirh->flags);
                   1056: 
                   1057:        if (r != 0) {
                   1058: 
1.1.1.2   root     1059:                DEBUG(("Fsfirst(%s): couldn't open directory (error %ld)",
1.1       root     1060: 
1.1.1.2   root     1061:                        path, r));
1.1       root     1062: 
1.1.1.3   root     1063:                release_cookie(&dir);
                   1064: 
1.1       root     1065:                return r;
                   1066: 
                   1067:        }
                   1068: 
                   1069: 
                   1070: 
                   1071: /* mark the slot as in-use */
                   1072: 
                   1073:        curproc->srchdta[i] = dta;
                   1074: 
                   1075: 
                   1076: 
                   1077: /* set up the DTA for Fsnext */
                   1078: 
                   1079:        dta->index = i;
                   1080: 
                   1081:        dta->magic = SVALID;
                   1082: 
                   1083:        dta->dta_sattrib = attrib;
                   1084: 
                   1085: 
                   1086: 
                   1087: /* OK, now basically just do Fsnext, except that instead of ENMFIL we
                   1088: 
                   1089:  * return EFILNF.
                   1090: 
                   1091:  * NOTE: If we already have found a volume label from the search above,
                   1092: 
                   1093:  * then we skip the f_snext and just return that.
                   1094: 
                   1095:  */
                   1096: 
                   1097:        if (havelabel)
                   1098: 
                   1099:                return 0;
                   1100: 
                   1101: 
                   1102: 
                   1103:        r = f_snext();
                   1104: 
                   1105:        if (r == ENMFIL) r = EFILNF;
                   1106: 
                   1107:        if (r)
                   1108: 
1.1.1.2   root     1109:                TRACE(("Fsfirst: returning %ld", r));
1.1       root     1110: 
1.1.1.3   root     1111: /* release_cookie isn't necessary, since &dir is now stored in the
                   1112: 
                   1113:  * DIRH structure and will be released when the search is completed
                   1114: 
                   1115:  */
                   1116: 
1.1       root     1117:        return r;
                   1118: 
                   1119: }
                   1120: 
                   1121: 
                   1122: 
                   1123: /*
                   1124: 
                   1125:  * Counter for Fsfirst/Fsnext, so that we know which search slots are
                   1126: 
                   1127:  * least recently used. This is updated once per second by the code
                   1128: 
                   1129:  * in timeout.c.
                   1130: 
                   1131:  * BUG: 1/second is pretty low granularity
                   1132: 
                   1133:  */
                   1134: 
                   1135: 
                   1136: 
                   1137: long searchtime;
                   1138: 
                   1139: 
                   1140: 
1.1.1.2   root     1141: long ARGS_ON_STACK
1.1       root     1142: 
                   1143: f_snext()
                   1144: 
                   1145: {
                   1146: 
1.1.1.4   root     1147:        char buf[TOS_NAMELEN+1];
1.1       root     1148: 
                   1149:        DTABUF *dta = curproc->dta;
                   1150: 
                   1151:        FILESYS *fs;
                   1152: 
                   1153:        fcookie fc;
                   1154: 
                   1155:        int i;
                   1156: 
                   1157:        DIR *dirh;
                   1158: 
                   1159:        long r;
                   1160: 
                   1161:        XATTR xattr;
                   1162: 
                   1163: 
                   1164: 
1.1.1.2   root     1165:        TRACE(("Fsnext"));
1.1       root     1166: 
                   1167: 
                   1168: 
                   1169:        if (dta->magic == EVALID) {
                   1170: 
1.1.1.2   root     1171:                DEBUG(("Fsnext: DTA marked a failing search"));
1.1       root     1172: 
                   1173:                return ENMFIL;
                   1174: 
                   1175:        }
                   1176: 
                   1177:        if (dta->magic != SVALID) {
                   1178: 
1.1.1.2   root     1179:                DEBUG(("Fsnext: dta incorrectly set up"));
1.1       root     1180: 
                   1181:                return EINVFN;
                   1182: 
                   1183:        }
                   1184: 
                   1185: 
                   1186: 
                   1187:        i = dta->index;
                   1188: 
                   1189:        dirh = &curproc->srchdir[i];
                   1190: 
                   1191:        curproc->srchtim[i] = searchtime;
                   1192: 
                   1193: 
                   1194: 
                   1195:        fs = dirh->fc.fs;
                   1196: 
                   1197:        if (!fs)                /* oops -- the directory got closed somehow */
                   1198: 
                   1199:                return EINTRN;
                   1200: 
                   1201: 
                   1202: 
                   1203: /* BUG: f_snext and readdir should check for disk media changes */
                   1204: 
                   1205: 
                   1206: 
                   1207:        for(;;) {
                   1208: 
                   1209:                r = (*fs->readdir)(dirh, buf, TOS_NAMELEN+1, &fc);
                   1210: 
                   1211: 
                   1212: 
                   1213:                if (r == ENAMETOOLONG) {
                   1214: 
1.1.1.2   root     1215:                        DEBUG(("Fsnext: name too long"));
1.1       root     1216: 
                   1217:                        continue;       /* TOS programs never see these names */
                   1218: 
                   1219:                }
                   1220: 
                   1221:                if (r != 0) {
                   1222: 
                   1223: baderror:
                   1224: 
1.1.1.2   root     1225:                        if (dirh->fc.fs)
                   1226: 
                   1227:                                (void)(*fs->closedir)(dirh);
1.1       root     1228: 
1.1.1.3   root     1229:                        release_cookie(&dirh->fc);
                   1230: 
1.1       root     1231:                        dirh->fc.fs = 0;
                   1232: 
                   1233:                        curproc->srchdta[i] = 0;
                   1234: 
                   1235:                        dta->magic = EVALID;
                   1236: 
                   1237:                        if (r != ENMFIL)
                   1238: 
1.1.1.2   root     1239:                                DEBUG(("Fsnext: returning %ld", r));
1.1       root     1240: 
                   1241:                        return r;
                   1242: 
                   1243:                }
                   1244: 
                   1245: 
                   1246: 
                   1247:                if (!pat_match(buf, dta->dta_pat))
                   1248: 
1.1.1.3   root     1249:                {
                   1250: 
                   1251:                        release_cookie(&fc);
                   1252: 
1.1       root     1253:                        continue;       /* different patterns */
                   1254: 
1.1.1.3   root     1255:                }
                   1256: 
1.1       root     1257: 
                   1258: 
                   1259:        /* check for search attributes */
                   1260: 
                   1261:                r = (*fc.fs->getxattr)(&fc, &xattr);
                   1262: 
                   1263:                if (r) {
                   1264: 
1.1.1.2   root     1265:                        DEBUG(("Fsnext: couldn't get file attributes"));
1.1       root     1266: 
1.1.1.3   root     1267:                        release_cookie(&fc);
                   1268: 
1.1       root     1269:                        goto baderror;
                   1270: 
                   1271:                }
                   1272: 
                   1273:        /* if the file is a symbolic link, try to find what it's linked to */
                   1274: 
                   1275:                if ( (xattr.mode & S_IFMT) == S_IFLNK ) {
                   1276: 
                   1277:                        char linkedto[PATH_MAX];
                   1278: 
                   1279:                        r = (*fc.fs->readlink)(&fc, linkedto, PATH_MAX);
                   1280: 
1.1.1.3   root     1281:                        release_cookie(&fc);
                   1282: 
1.1       root     1283:                        if (r == 0) {
                   1284: 
                   1285:                        /* the "1" tells relpath2cookie that we read a link */
                   1286: 
                   1287:                            r = relpath2cookie(&dirh->fc, linkedto,
                   1288: 
                   1289:                                        follow_links, &fc, 1);
                   1290: 
1.1.1.3   root     1291:                            if (r == 0) {
1.1       root     1292: 
                   1293:                                r = (*fc.fs->getxattr)(&fc, &xattr);
                   1294: 
1.1.1.3   root     1295:                                release_cookie(&fc);
                   1296: 
                   1297:                            }
                   1298: 
1.1       root     1299:                        }
                   1300: 
                   1301:                        if (r) {
                   1302: 
1.1.1.2   root     1303:                                DEBUG(("Fsnext: couldn't follow link: error %ld",
1.1       root     1304: 
1.1.1.2   root     1305:                                        r));
1.1       root     1306: 
                   1307:                        }
                   1308: 
1.1.1.3   root     1309:                } else {
                   1310: 
                   1311:                        release_cookie(&fc);
                   1312: 
1.1       root     1313:                }
                   1314: 
                   1315: 
                   1316: 
                   1317:        /* silly TOS rules for matching attributes */
                   1318: 
                   1319:                if (xattr.attr == 0) break;
                   1320: 
                   1321:                if (xattr.attr & 0x21) break;
                   1322: 
                   1323:                if (dta->dta_sattrib & xattr.attr)
                   1324: 
                   1325:                        break;
                   1326: 
                   1327:        }
                   1328: 
                   1329: 
                   1330: 
                   1331: /* here, we have a match */
                   1332: 
                   1333:        dta->dta_attrib = xattr.attr;
                   1334: 
                   1335:        dta->dta_time = xattr.mtime;
                   1336: 
                   1337:        dta->dta_date = xattr.mdate;
                   1338: 
                   1339:        dta->dta_size = xattr.size;
                   1340: 
                   1341:        strcpy(dta->dta_name, buf);
                   1342: 
                   1343: 
                   1344: 
                   1345:        if (curproc->domain == DOM_TOS && !(fs->fsflags & FS_CASESENSITIVE)) {
                   1346: 
                   1347:                strupr(dta->dta_name);
                   1348: 
                   1349:        }
                   1350: 
                   1351:        return 0;
                   1352: 
                   1353: }
                   1354: 
                   1355: 
                   1356: 
1.1.1.2   root     1357: long ARGS_ON_STACK
1.1       root     1358: 
                   1359: f_attrib(name, rwflag, attr)
                   1360: 
                   1361:        const char *name;
                   1362: 
                   1363:        int rwflag;
                   1364: 
                   1365:        int attr;
                   1366: 
                   1367: {
                   1368: 
                   1369:        fcookie fc;
                   1370: 
                   1371:        XATTR xattr;
                   1372: 
                   1373:        long r;
                   1374: 
                   1375: 
                   1376: 
1.1.1.2   root     1377:        TRACE(("Fattrib(%s, %d)", name, attr));
1.1       root     1378: 
                   1379: 
                   1380: 
1.1.1.4   root     1381:        r = path2cookie(name, follow_links, &fc);
1.1       root     1382: 
                   1383: 
                   1384: 
                   1385:        if (r) {
                   1386: 
1.1.1.2   root     1387:                DEBUG(("Fattrib(%s): error %ld", name, r));
1.1       root     1388: 
                   1389:                return r;
                   1390: 
                   1391:        }
                   1392: 
                   1393: 
                   1394: 
                   1395:        r = (*fc.fs->getxattr)(&fc, &xattr);
                   1396: 
                   1397: 
                   1398: 
                   1399:        if (r) {
                   1400: 
1.1.1.2   root     1401:                DEBUG(("Fattrib(%s): getxattr returned %ld", name, r));
1.1       root     1402: 
1.1.1.3   root     1403:                release_cookie(&fc);
                   1404: 
1.1       root     1405:                return r;
                   1406: 
                   1407:        }
                   1408: 
                   1409: 
                   1410: 
                   1411:        if (rwflag) {
                   1412: 
                   1413:                if (attr & (FA_LABEL|FA_DIR)) {
                   1414: 
1.1.1.2   root     1415:                        DEBUG(("Fattrib(%s): illegal attributes specified",name));
1.1       root     1416: 
1.1.1.3   root     1417:                        r = EACCDN;
1.1       root     1418: 
                   1419:                } else if (curproc->euid && curproc->euid != xattr.uid) {
                   1420: 
1.1.1.2   root     1421:                        DEBUG(("Fattrib(%s): not the file's owner",name));
1.1       root     1422: 
1.1.1.3   root     1423:                        r = EACCDN;
1.1       root     1424: 
                   1425:                } else if (xattr.attr & (FA_LABEL|FA_DIR)) {
                   1426: 
1.1.1.2   root     1427:                        DEBUG(("Fattrib(%s): file is a volume label "
1.1       root     1428: 
1.1.1.2   root     1429:                              "or directory",name));
1.1       root     1430: 
1.1.1.3   root     1431:                        r = EACCDN;
                   1432: 
                   1433:                } else {
                   1434: 
                   1435:                        r = (*fc.fs->chattr)(&fc, attr);
1.1       root     1436: 
                   1437:                }
                   1438: 
1.1.1.3   root     1439:                release_cookie(&fc);
                   1440: 
                   1441:                return r;
1.1       root     1442: 
                   1443:        } else {
                   1444: 
1.1.1.3   root     1445:                release_cookie(&fc);
                   1446: 
1.1       root     1447:                return xattr.attr;
                   1448: 
                   1449:        }
                   1450: 
                   1451: }
                   1452: 
                   1453: 
                   1454: 
1.1.1.2   root     1455: long ARGS_ON_STACK
1.1       root     1456: 
                   1457: f_delete(name)
                   1458: 
                   1459:        const char *name;
                   1460: 
                   1461: {
                   1462: 
1.1.1.5   root     1463:        fcookie dir, fc;
1.1       root     1464: 
                   1465:        long r;
                   1466: 
1.1.1.4   root     1467:        char temp1[PATH_MAX];
                   1468: 
1.1.1.5   root     1469:        XATTR   xattr;
1.1       root     1470: 
                   1471: 
                   1472: 
1.1.1.5   root     1473:        TRACE(("Fdelete(%s)", name));
1.1       root     1474: 
                   1475: 
                   1476: 
1.1.1.5   root     1477: /* get a cookie for the directory the file is in */
1.1       root     1478: 
1.1.1.6 ! root     1479:        if (( r = path2cookie(name, temp1, &dir) ) != 0)
1.1       root     1480: 
1.1.1.5   root     1481:        {
1.1       root     1482: 
1.1.1.5   root     1483:                DEBUG(("Fdelete: couldn't get directory cookie: error %ld", r));
1.1       root     1484: 
                   1485:                return r;
                   1486: 
                   1487:        }
                   1488: 
                   1489: 
                   1490: 
                   1491: /* check for write permission on directory */
                   1492: 
                   1493:        r = dir_access(&dir, S_IWOTH);
                   1494: 
                   1495:        if (r) {
                   1496: 
1.1.1.2   root     1497:                DEBUG(("Fdelete(%s): write access to directory denied",name));
1.1       root     1498: 
1.1.1.5   root     1499:                release_cookie(&dir);
                   1500: 
                   1501:                return EACCDN;
                   1502: 
                   1503:        }
                   1504: 
                   1505: 
1.1.1.3   root     1506: 
1.1.1.5   root     1507: /* now get the file attributes */
1.1.1.3   root     1508: 
1.1.1.6 ! root     1509: /* TOS domain processes can only delete files if they have write permission
1.1.1.5   root     1510: 
1.1.1.6 ! root     1511:  * for them
1.1.1.5   root     1512: 
1.1.1.6 ! root     1513:  */
1.1.1.5   root     1514: 
1.1.1.6 ! root     1515:        if (curproc->domain == DOM_TOS) {
1.1.1.5   root     1516: 
1.1.1.6 ! root     1517:                if ( (r = (*dir.fs->lookup)(&dir, temp1, &fc) ) != 0) {
1.1.1.5   root     1518: 
1.1.1.6 ! root     1519:                        DEBUG(("Fdelete: error %ld while looking for %s", r, temp1));
1.1.1.5   root     1520: 
1.1.1.6 ! root     1521:                        release_cookie(&dir);
1.1.1.5   root     1522: 
1.1.1.6 ! root     1523:                        return r;
1.1.1.5   root     1524: 
1.1.1.6 ! root     1525:                }
1.1.1.5   root     1526: 
                   1527: 
                   1528: 
1.1.1.6 ! root     1529:                if (( r = (*fc.fs->getxattr)(&fc, &xattr)) < 0 )
1.1.1.5   root     1530: 
1.1.1.6 ! root     1531:                {
1.1.1.5   root     1532: 
1.1.1.6 ! root     1533:                        release_cookie(&dir);
1.1.1.5   root     1534: 
1.1.1.6 ! root     1535:                        release_cookie(&fc);
1.1.1.5   root     1536: 
1.1.1.6 ! root     1537:                        DEBUG(("Fdelete: couldn't get file attributes: error %ld", r));
1.1.1.5   root     1538: 
1.1.1.6 ! root     1539:                        return r;
1.1.1.5   root     1540: 
1.1.1.6 ! root     1541:                }
1.1.1.5   root     1542: 
1.1.1.6 ! root     1543:        /* see if we're allowed to kill it */
1.1.1.5   root     1544: 
1.1.1.6 ! root     1545:                if (denyaccess(&xattr, S_IWOTH)) {
        !          1546: 
        !          1547:                        release_cookie(&dir);
        !          1548: 
        !          1549:                        release_cookie(&fc);
        !          1550: 
        !          1551:                        DEBUG(("Fdelete: file access denied"));
        !          1552: 
        !          1553:                        return EACCDN;
        !          1554: 
        !          1555:                }
        !          1556: 
        !          1557:                release_cookie(&fc);
1.1       root     1558: 
                   1559:        }
                   1560: 
1.1.1.5   root     1561: 
                   1562: 
                   1563:        r = (*dir.fs->remove)(&dir,temp1);
                   1564: 
                   1565: 
                   1566: 
1.1.1.3   root     1567:        release_cookie(&dir);
1.1       root     1568: 
1.1.1.3   root     1569:        return r;
1.1       root     1570: 
                   1571: }
                   1572: 
                   1573: 
                   1574: 
1.1.1.2   root     1575: long ARGS_ON_STACK
1.1       root     1576: 
                   1577: f_rename(junk, old, new)
                   1578: 
                   1579:        int junk;               /* ignored, for TOS compatibility */
                   1580: 
                   1581:        const char *old, *new;
                   1582: 
                   1583: {
                   1584: 
                   1585:        fcookie olddir, newdir, oldfil;
                   1586: 
                   1587:        XATTR xattr;
                   1588: 
1.1.1.4   root     1589:        char temp1[PATH_MAX], temp2[PATH_MAX];
1.1       root     1590: 
                   1591:        long r;
                   1592: 
                   1593: 
                   1594: 
1.1.1.2   root     1595:        UNUSED(junk);
                   1596: 
                   1597: 
                   1598: 
                   1599:        TRACE(("Frename(%s, %s)", old, new));
1.1       root     1600: 
                   1601: 
                   1602: 
                   1603:        r = path2cookie(old, temp2, &olddir);
                   1604: 
                   1605:        if (r) {
                   1606: 
1.1.1.2   root     1607:                DEBUG(("Frename(%s,%s): error parsing old name",old,new));
1.1       root     1608: 
                   1609:                return r;
                   1610: 
                   1611:        }
                   1612: 
                   1613: /* check for permissions on the old file
                   1614: 
                   1615:  * GEMDOS doesn't allow rename if the file is FA_RDONLY
                   1616: 
                   1617:  * we enforce this restriction only on regular files; processes,
                   1618: 
                   1619:  * directories, and character special files can be renamed at will
                   1620: 
                   1621:  */
                   1622: 
1.1.1.3   root     1623:        r = relpath2cookie(&olddir, temp2, (char *)0, &oldfil, 0);
1.1       root     1624: 
                   1625:        if (r) {
                   1626: 
1.1.1.2   root     1627:                DEBUG(("Frename(%s,%s): old file not found",old,new));
1.1       root     1628: 
1.1.1.3   root     1629:                release_cookie(&olddir);
                   1630: 
1.1       root     1631:                return r;
                   1632: 
                   1633:        }
                   1634: 
                   1635:        r = (*oldfil.fs->getxattr)(&oldfil, &xattr);
                   1636: 
1.1.1.3   root     1637:        release_cookie(&oldfil);
                   1638: 
1.1       root     1639:        if (r ||
                   1640: 
                   1641:            ((xattr.mode & S_IFMT) == S_IFREG && (xattr.attr & FA_RDONLY)) )
                   1642: 
                   1643:        {
                   1644: 
1.1.1.2   root     1645:                DEBUG(("Frename(%s,%s): access to old file not granted",old,new));
1.1       root     1646: 
1.1.1.3   root     1647:                release_cookie(&olddir);
                   1648: 
1.1       root     1649:                return EACCDN;
                   1650: 
                   1651:        }
                   1652: 
                   1653:        r = path2cookie(new, temp1, &newdir);
                   1654: 
                   1655:        if (r) {
                   1656: 
1.1.1.2   root     1657:                DEBUG(("Frename(%s,%s): error parsing new name",old,new));
1.1       root     1658: 
1.1.1.3   root     1659:                release_cookie(&olddir);
                   1660: 
1.1       root     1661:                return r;
                   1662: 
                   1663:        }
                   1664: 
                   1665: 
                   1666: 
                   1667:        if (newdir.fs != olddir.fs) {
                   1668: 
1.1.1.2   root     1669:                DEBUG(("Frename(%s,%s): different file systems",old,new));
1.1       root     1670: 
1.1.1.3   root     1671:                release_cookie(&olddir);
                   1672: 
                   1673:                release_cookie(&newdir);
                   1674: 
1.1       root     1675:                return EXDEV;   /* cross device rename */
                   1676: 
                   1677:        }
                   1678: 
                   1679: 
                   1680: 
                   1681: /* check for write permission on both directories */
                   1682: 
                   1683:        r = dir_access(&olddir, S_IWOTH);
                   1684: 
                   1685:        if (!r) r = dir_access(&newdir, S_IWOTH);
                   1686: 
                   1687:        if (r) {
                   1688: 
1.1.1.2   root     1689:                DEBUG(("Frename(%s,%s): access to a directory denied",old,new));
1.1       root     1690: 
1.1.1.3   root     1691:        } else {
                   1692: 
                   1693:                r = (*newdir.fs->rename)(&olddir, temp2, &newdir, temp1);
1.1       root     1694: 
                   1695:        }
                   1696: 
1.1.1.3   root     1697:        release_cookie(&olddir);
                   1698: 
                   1699:        release_cookie(&newdir);
                   1700: 
                   1701:        return r;
1.1       root     1702: 
                   1703: }
                   1704: 
                   1705: 
                   1706: 
                   1707: /*
                   1708: 
                   1709:  * GEMDOS extension: Dpathconf(name, which)
                   1710: 
                   1711:  * returns information about filesystem-imposed limits; "name" is the name
                   1712: 
                   1713:  * of a file or directory about which the limit information is requested;
                   1714: 
                   1715:  * "which" is the limit requested, as follows:
                   1716: 
                   1717:  *     -1      max. value of "which" allowed
                   1718: 
                   1719:  *     0       internal limit on open files, if any
                   1720: 
                   1721:  *     1       max. number of links to a file  {LINK_MAX}
                   1722: 
                   1723:  *     2       max. path name length           {PATH_MAX}
                   1724: 
                   1725:  *     3       max. file name length           {NAME_MAX}
                   1726: 
                   1727:  *     4       no. of bytes in atomic write to FIFO {PIPE_BUF}
                   1728: 
                   1729:  *     5       file name truncation rules
                   1730: 
                   1731:  *     6       file name case translation rules
                   1732: 
                   1733:  *
                   1734: 
                   1735:  * unlimited values are returned as 0x7fffffffL
                   1736: 
                   1737:  *
                   1738: 
                   1739:  * see also Sysconf() in dos.c
                   1740: 
                   1741:  */
                   1742: 
                   1743: 
                   1744: 
1.1.1.2   root     1745: long ARGS_ON_STACK
1.1       root     1746: 
                   1747: d_pathconf(name, which)
                   1748: 
                   1749:        const char *name;
                   1750: 
                   1751:        int which;
                   1752: 
                   1753: {
                   1754: 
                   1755:        fcookie dir;
                   1756: 
                   1757:        long r;
                   1758: 
                   1759: 
                   1760: 
                   1761:        r = path2cookie(name, (char *)0, &dir);
                   1762: 
                   1763:        if (r) {
                   1764: 
1.1.1.2   root     1765:                DEBUG(("Dpathconf(%s): bad path",name));
1.1       root     1766: 
                   1767:                return r;
                   1768: 
                   1769:        }
                   1770: 
                   1771:        r = (*dir.fs->pathconf)(&dir, which);
                   1772: 
                   1773:        if (which == DP_CASE && r == EINVFN) {
                   1774: 
                   1775:        /* backward compatibility with old .XFS files */
                   1776: 
1.1.1.3   root     1777:                r = (dir.fs->fsflags & FS_CASESENSITIVE) ? DP_CASESENS :
1.1       root     1778: 
                   1779:                                DP_CASEINSENS;
                   1780: 
                   1781:        }
                   1782: 
1.1.1.3   root     1783:        release_cookie(&dir);
                   1784: 
1.1       root     1785:        return r;
                   1786: 
                   1787: }
                   1788: 
                   1789: 
                   1790: 
                   1791: /*
                   1792: 
                   1793:  * GEMDOS extension: Opendir/Readdir/Rewinddir/Closedir offer a new,
                   1794: 
                   1795:  * POSIX-like alternative to Fsfirst/Fsnext, and as a bonus allow for
                   1796: 
                   1797:  * arbitrary length file names
                   1798: 
                   1799:  */
                   1800: 
                   1801: 
                   1802: 
1.1.1.2   root     1803: long ARGS_ON_STACK
1.1       root     1804: 
                   1805: d_opendir(name, flag)
                   1806: 
                   1807:        const char *name;
                   1808: 
                   1809:        int flag;
                   1810: 
                   1811: {
                   1812: 
                   1813:        DIR *dirh;
                   1814: 
                   1815:        fcookie dir;
                   1816: 
                   1817:        long r;
                   1818: 
                   1819: 
                   1820: 
                   1821:        r = path2cookie(name, follow_links, &dir);
                   1822: 
                   1823:        if (r) {
                   1824: 
1.1.1.2   root     1825:                DEBUG(("Dopendir(%s): error %ld", name, r));
1.1       root     1826: 
                   1827:                return r;
                   1828: 
                   1829:        }
                   1830: 
                   1831:        r = dir_access(&dir, S_IROTH);
                   1832: 
                   1833:        if (r) {
                   1834: 
1.1.1.2   root     1835:                DEBUG(("Dopendir(%s): read permission denied", name));
1.1       root     1836: 
1.1.1.3   root     1837:                release_cookie(&dir);
                   1838: 
1.1       root     1839:                return r;
                   1840: 
                   1841:        }
                   1842: 
                   1843: 
                   1844: 
1.1.1.3   root     1845:        dirh = (DIR *)kmalloc(SIZEOF(DIR));
                   1846: 
                   1847:        if (!dirh) {
                   1848: 
                   1849:                release_cookie(&dir);
1.1       root     1850: 
1.1.1.3   root     1851:                return ENSMEM;
                   1852: 
                   1853:        }
1.1       root     1854: 
                   1855: 
                   1856: 
                   1857:        dirh->fc = dir;
                   1858: 
                   1859:        dirh->index = 0;
                   1860: 
                   1861:        dirh->flags = flag;
                   1862: 
                   1863:        r = (*dir.fs->opendir)(dirh, flag);
                   1864: 
                   1865:        if (r) {
                   1866: 
1.1.1.2   root     1867:                DEBUG(("d_opendir(%s): opendir returned %ld", name, r));
1.1       root     1868: 
1.1.1.3   root     1869:                release_cookie(&dir);
                   1870: 
                   1871:                kfree(dirh);
1.1       root     1872: 
                   1873:                return r;
                   1874: 
                   1875:        }
                   1876: 
1.1.1.3   root     1877: 
                   1878: 
                   1879: /* we keep a chain of open directories so that if a process
                   1880: 
                   1881:  * terminates without closing them all, we can clean up
                   1882: 
                   1883:  */
                   1884: 
                   1885:        dirh->next = curproc->searches;
                   1886: 
                   1887:        curproc->searches = dirh;
                   1888: 
                   1889: 
                   1890: 
1.1       root     1891:        return (long)dirh;
                   1892: 
                   1893: }
                   1894: 
                   1895: 
                   1896: 
1.1.1.2   root     1897: long ARGS_ON_STACK
1.1       root     1898: 
                   1899: d_readdir(len, handle, buf)
                   1900: 
                   1901:        int len;
                   1902: 
                   1903:        long handle;
                   1904: 
                   1905:        char *buf;
                   1906: 
                   1907: {
                   1908: 
                   1909:        DIR *dirh = (DIR *)handle;
                   1910: 
                   1911:        fcookie fc;
                   1912: 
1.1.1.3   root     1913:        long r;
                   1914: 
1.1       root     1915: 
                   1916: 
                   1917:        if (!dirh->fc.fs)
                   1918: 
                   1919:                return EIHNDL;
                   1920: 
1.1.1.3   root     1921:        r = (*dirh->fc.fs->readdir)(dirh, buf, len, &fc);
                   1922: 
                   1923:        if (r == 0)
                   1924: 
                   1925:                release_cookie(&fc);
                   1926: 
                   1927:        return r;
1.1       root     1928: 
                   1929: }
                   1930: 
                   1931: 
                   1932: 
1.1.1.6 ! root     1933: /* jr: just as d_readdir, but also returns XATTR structure (not
        !          1934: 
        !          1935:    following links). Note that the return value reflects the
        !          1936: 
        !          1937:    result of the Dreaddir operation, the result of the Fxattr
        !          1938: 
        !          1939:    operation is stored in long *xret */
        !          1940: 
        !          1941: 
        !          1942: 
        !          1943: long ARGS_ON_STACK
        !          1944: 
        !          1945: d_xreaddir(len, handle, buf, xattr, xret)
        !          1946: 
        !          1947:        int len;
        !          1948: 
        !          1949:        long handle;
        !          1950: 
        !          1951:        char *buf;
        !          1952: 
        !          1953:        XATTR *xattr;
        !          1954: 
        !          1955:        long *xret;
        !          1956: 
        !          1957: {
        !          1958: 
        !          1959:        DIR *dirh = (DIR *)handle;
        !          1960: 
        !          1961:        fcookie fc;
        !          1962: 
        !          1963:        long r;
        !          1964: 
        !          1965: 
        !          1966: 
        !          1967:        if (!dirh->fc.fs) return EIHNDL;
        !          1968: 
        !          1969:        r = (*dirh->fc.fs->readdir)(dirh, buf, len, &fc);
        !          1970: 
        !          1971:        if (r != E_OK) return r;
        !          1972: 
        !          1973: 
        !          1974: 
        !          1975:        *xret = (*fc.fs->getxattr)(&fc, xattr);
        !          1976: 
        !          1977:        
        !          1978: 
        !          1979:        release_cookie(&fc);
        !          1980: 
        !          1981:        return r;
        !          1982: 
        !          1983: }
        !          1984: 
        !          1985: 
        !          1986: 
        !          1987: 
        !          1988: 
1.1.1.2   root     1989: long ARGS_ON_STACK
1.1       root     1990: 
                   1991: d_rewind(handle)
                   1992: 
                   1993:        long handle;
                   1994: 
                   1995: {
                   1996: 
                   1997:        DIR *dirh = (DIR *)handle;
                   1998: 
                   1999: 
                   2000: 
                   2001:        if (!dirh->fc.fs)
                   2002: 
                   2003:                return EIHNDL;
                   2004: 
                   2005:        return (*dirh->fc.fs->rewinddir)(dirh);
                   2006: 
                   2007: }
                   2008: 
                   2009: 
                   2010: 
1.1.1.3   root     2011: /*
                   2012: 
                   2013:  * NOTE: there is also code in terminate() in dosmem.c that
                   2014: 
                   2015:  * does automatic closes of directory searches.
                   2016: 
                   2017:  * If you change d_closedir(), you may also need to change
                   2018: 
                   2019:  * terminate().
                   2020: 
                   2021:  */
                   2022: 
                   2023: 
                   2024: 
1.1.1.2   root     2025: long ARGS_ON_STACK
1.1       root     2026: 
                   2027: d_closedir(handle)
                   2028: 
                   2029:        long handle;
                   2030: 
                   2031: {
                   2032: 
                   2033:        long r;
                   2034: 
                   2035:        DIR *dirh = (DIR *)handle;
                   2036: 
1.1.1.3   root     2037:        DIR **where;
                   2038: 
1.1       root     2039: 
                   2040: 
1.1.1.3   root     2041:        where = &curproc->searches;
                   2042: 
                   2043:        while (*where && *where != dirh) {
                   2044: 
                   2045:                where = &((*where)->next);
                   2046: 
                   2047:        }
                   2048: 
                   2049:        if (!*where) {
                   2050: 
                   2051:                DEBUG(("Dclosedir: not an open directory"));
                   2052: 
                   2053:                return EIHNDL;
                   2054: 
                   2055:        }
                   2056: 
                   2057: 
                   2058: 
                   2059: /* unlink the directory from the chain */
                   2060: 
                   2061:        *where = dirh->next;
                   2062: 
                   2063: 
                   2064: 
1.1.1.4   root     2065:        if (dirh->fc.fs) {
                   2066: 
                   2067:                r = (*dirh->fc.fs->closedir)(dirh);
1.1       root     2068: 
1.1.1.4   root     2069:                release_cookie(&dirh->fc);
1.1.1.3   root     2070: 
1.1.1.4   root     2071:        } else {
                   2072: 
                   2073:                r = 0;
                   2074: 
                   2075:        }
1.1       root     2076: 
                   2077: 
                   2078: 
                   2079:        if (r) {
                   2080: 
1.1.1.2   root     2081:                DEBUG(("Dclosedir: error %ld", r));
1.1       root     2082: 
                   2083:        }
                   2084: 
1.1.1.3   root     2085:        kfree(dirh);
1.1       root     2086: 
                   2087:        return r;
                   2088: 
                   2089: }
                   2090: 
                   2091: 
                   2092: 
                   2093: /*
                   2094: 
                   2095:  * GEMDOS extension: Fxattr gets extended attributes for a file. "flag"
                   2096: 
                   2097:  * is 0 if symbolic links are to be followed (like stat), 1 if not (like
                   2098: 
                   2099:  * lstat).
                   2100: 
                   2101:  */
                   2102: 
                   2103: 
                   2104: 
1.1.1.2   root     2105: long ARGS_ON_STACK
1.1       root     2106: 
                   2107: f_xattr(flag, name, xattr)
                   2108: 
                   2109:        int flag;
                   2110: 
                   2111:        const char *name;
                   2112: 
                   2113:        XATTR *xattr;
                   2114: 
                   2115: {
                   2116: 
                   2117:        fcookie fc;
                   2118: 
                   2119:        long r;
                   2120: 
                   2121: 
                   2122: 
1.1.1.2   root     2123:        TRACE(("Fxattr(%d, %s)", flag, name));
1.1       root     2124: 
                   2125: 
                   2126: 
                   2127:        r = path2cookie(name, flag ? (char *)0 : follow_links, &fc);
                   2128: 
                   2129:        if (r) {
                   2130: 
1.1.1.2   root     2131:                DEBUG(("Fxattr(%s): path2cookie returned %ld", name, r));
1.1       root     2132: 
                   2133:                return r;
                   2134: 
                   2135:        }
                   2136: 
                   2137:        r = (*fc.fs->getxattr)(&fc, xattr);
                   2138: 
                   2139:        if (r) {
                   2140: 
1.1.1.2   root     2141:                DEBUG(("Fxattr(%s): returning %ld", name, r));
1.1       root     2142: 
                   2143:        }
                   2144: 
1.1.1.3   root     2145:        release_cookie(&fc);
                   2146: 
1.1       root     2147:        return r;
                   2148: 
                   2149: }
                   2150: 
                   2151: 
                   2152: 
                   2153: /*
                   2154: 
                   2155:  * GEMDOS extension: Flink(old, new) creates a hard link named "new"
                   2156: 
                   2157:  * to the file "old".
                   2158: 
                   2159:  */
                   2160: 
                   2161: 
                   2162: 
1.1.1.2   root     2163: long ARGS_ON_STACK
1.1       root     2164: 
                   2165: f_link(old, new)
                   2166: 
                   2167:        const char *old, *new;
                   2168: 
                   2169: {
                   2170: 
                   2171:        fcookie olddir, newdir;
                   2172: 
1.1.1.4   root     2173:        char temp1[PATH_MAX], temp2[PATH_MAX];
1.1       root     2174: 
                   2175:        long r;
                   2176: 
                   2177: 
                   2178: 
1.1.1.2   root     2179:        TRACE(("Flink(%s, %s)", old, new));
1.1       root     2180: 
                   2181: 
                   2182: 
                   2183:        r = path2cookie(old, temp2, &olddir);
                   2184: 
                   2185:        if (r) {
                   2186: 
1.1.1.2   root     2187:                DEBUG(("Flink(%s,%s): error parsing old name",old,new));
1.1       root     2188: 
                   2189:                return r;
                   2190: 
                   2191:        }
                   2192: 
                   2193:        r = path2cookie(new, temp1, &newdir);
                   2194: 
                   2195:        if (r) {
                   2196: 
1.1.1.2   root     2197:                DEBUG(("Flink(%s,%s): error parsing new name",old,new));
1.1       root     2198: 
1.1.1.3   root     2199:                release_cookie(&olddir);
                   2200: 
1.1       root     2201:                return r;
                   2202: 
                   2203:        }
                   2204: 
                   2205: 
                   2206: 
                   2207:        if (newdir.fs != olddir.fs) {
                   2208: 
1.1.1.2   root     2209:                DEBUG(("Flink(%s,%s): different file systems",old,new));
1.1       root     2210: 
1.1.1.3   root     2211:                release_cookie(&olddir);
                   2212: 
                   2213:                release_cookie(&newdir);
                   2214: 
1.1       root     2215:                return EXDEV;   /* cross device link */
                   2216: 
                   2217:        }
                   2218: 
                   2219: 
                   2220: 
                   2221: /* check for write permission on the destination directory */
                   2222: 
                   2223: 
                   2224: 
                   2225:        r = dir_access(&newdir, S_IWOTH);
                   2226: 
                   2227:        if (r) {
                   2228: 
1.1.1.2   root     2229:                DEBUG(("Flink(%s,%s): access to directory denied",old,new));
1.1       root     2230: 
1.1.1.3   root     2231:        } else
1.1       root     2232: 
1.1.1.3   root     2233:                r = (*newdir.fs->hardlink)(&olddir, temp2, &newdir, temp1);
                   2234: 
                   2235:        release_cookie(&olddir);
1.1       root     2236: 
1.1.1.3   root     2237:        release_cookie(&newdir);
                   2238: 
                   2239:        return r;
1.1       root     2240: 
                   2241: }
                   2242: 
                   2243: 
                   2244: 
                   2245: /*
                   2246: 
                   2247:  * GEMDOS extension: Fsymlink(old, new): create a symbolic link named
                   2248: 
                   2249:  * "new" that contains the path "old".
                   2250: 
                   2251:  */
                   2252: 
                   2253: 
                   2254: 
1.1.1.2   root     2255: long ARGS_ON_STACK
1.1       root     2256: 
                   2257: f_symlink(old, new)
                   2258: 
                   2259:        const char *old, *new;
                   2260: 
                   2261: {
                   2262: 
                   2263:        fcookie newdir;
                   2264: 
                   2265:        long r;
                   2266: 
1.1.1.4   root     2267:        char temp1[PATH_MAX];
                   2268: 
1.1       root     2269: 
                   2270: 
1.1.1.2   root     2271:        TRACE(("Fsymlink(%s, %s)", old, new));
1.1       root     2272: 
                   2273: 
                   2274: 
                   2275:        r = path2cookie(new, temp1, &newdir);
                   2276: 
                   2277:        if (r) {
                   2278: 
1.1.1.2   root     2279:                DEBUG(("Fsymlink(%s,%s): error parsing %s", old,new,new));
1.1       root     2280: 
                   2281:                return r;
                   2282: 
                   2283:        }
                   2284: 
                   2285:        r = dir_access(&newdir, S_IWOTH);
                   2286: 
                   2287:        if (r) {
                   2288: 
1.1.1.2   root     2289:                DEBUG(("Fsymlink(%s,%s): access to directory denied",old,new));
1.1       root     2290: 
1.1.1.3   root     2291:        } else
1.1       root     2292: 
1.1.1.3   root     2293:                r = (*newdir.fs->symlink)(&newdir, temp1, old);
                   2294: 
                   2295:        release_cookie(&newdir);
1.1       root     2296: 
1.1.1.3   root     2297:        return r;
1.1       root     2298: 
                   2299: }
                   2300: 
                   2301: 
                   2302: 
                   2303: /*
                   2304: 
                   2305:  * GEMDOS extension: Freadlink(buflen, buf, linkfile):
                   2306: 
                   2307:  * read the contents of the symbolic link "linkfile" into the buffer
                   2308: 
                   2309:  * "buf", which has length "buflen".
                   2310: 
                   2311:  */
                   2312: 
                   2313: 
                   2314: 
1.1.1.2   root     2315: long ARGS_ON_STACK
1.1       root     2316: 
                   2317: f_readlink(buflen, buf, linkfile)
                   2318: 
                   2319:        int buflen;
                   2320: 
                   2321:        char *buf;
                   2322: 
                   2323:        const char *linkfile;
                   2324: 
                   2325: {
                   2326: 
                   2327:        fcookie file;
                   2328: 
                   2329:        long r;
                   2330: 
                   2331:        XATTR xattr;
                   2332: 
                   2333: 
                   2334: 
1.1.1.2   root     2335:        TRACE(("Freadlink(%s)", linkfile));
1.1       root     2336: 
                   2337: 
                   2338: 
                   2339:        r = path2cookie(linkfile, (char *)0, &file);
                   2340: 
                   2341:        if (r) {
                   2342: 
1.1.1.2   root     2343:                DEBUG(("Freadlink: unable to find %s", linkfile));
1.1       root     2344: 
                   2345:                return r;
                   2346: 
                   2347:        }
                   2348: 
                   2349:        r = (*file.fs->getxattr)(&file, &xattr);
                   2350: 
                   2351:        if (r) {
                   2352: 
1.1.1.2   root     2353:                DEBUG(("Freadlink: unable to get attributes for %s", linkfile));
1.1       root     2354: 
1.1.1.3   root     2355:        } else if ( (xattr.mode & S_IFMT) == S_IFLNK )
1.1       root     2356: 
1.1.1.3   root     2357:                r = (*file.fs->readlink)(&file, buf, buflen);
1.1       root     2358: 
1.1.1.3   root     2359:        else {
1.1       root     2360: 
1.1.1.3   root     2361:                DEBUG(("Freadlink: %s is not a link", linkfile));
1.1       root     2362: 
1.1.1.3   root     2363:                r = EACCDN;
1.1       root     2364: 
1.1.1.3   root     2365:        }
1.1       root     2366: 
1.1.1.3   root     2367:        release_cookie(&file);
1.1       root     2368: 
1.1.1.3   root     2369:        return r;
1.1       root     2370: 
                   2371: }
                   2372: 
                   2373: 
                   2374: 
                   2375: /*
                   2376: 
                   2377:  * GEMDOS extension: Dcntl(): do file system specific functions
                   2378: 
                   2379:  */
                   2380: 
                   2381: 
                   2382: 
1.1.1.2   root     2383: long ARGS_ON_STACK
1.1       root     2384: 
                   2385: d_cntl(cmd, name, arg)
                   2386: 
                   2387:        int cmd;
                   2388: 
                   2389:        const char *name;
                   2390: 
                   2391:        long arg;
                   2392: 
                   2393: {
                   2394: 
                   2395:        fcookie dir;
                   2396: 
                   2397:        long r;
                   2398: 
1.1.1.4   root     2399:        char temp1[PATH_MAX];
                   2400: 
1.1       root     2401: 
                   2402: 
1.1.1.2   root     2403:        TRACE(("Dcntl(cmd=%x, file=%s, arg=%lx)", cmd, name, arg));
1.1       root     2404: 
                   2405: 
                   2406: 
                   2407:        r = path2cookie(name, temp1, &dir);
                   2408: 
                   2409:        if (r) {
                   2410: 
1.1.1.2   root     2411:                DEBUG(("Dcntl: couldn't find %s", name));
1.1       root     2412: 
                   2413:                return r;
                   2414: 
                   2415:        }
                   2416: 
1.1.1.3   root     2417:        r = (*dir.fs->fscntl)(&dir, temp1, cmd, arg);
                   2418: 
                   2419:        release_cookie(&dir);
                   2420: 
                   2421:        return r;
1.1       root     2422: 
                   2423: }
                   2424: 
                   2425: 
                   2426: 
                   2427: /*
                   2428: 
                   2429:  * GEMDOS extension: Fchown(name, uid, gid) changes the user and group
                   2430: 
                   2431:  * ownerships of a file to "uid" and "gid" respectively.
                   2432: 
                   2433:  */
                   2434: 
                   2435: 
                   2436: 
1.1.1.2   root     2437: long ARGS_ON_STACK
1.1       root     2438: 
                   2439: f_chown(name, uid, gid)
                   2440: 
                   2441:        const char *name;
                   2442: 
                   2443:        int uid, gid;
                   2444: 
                   2445: {
                   2446: 
                   2447:        fcookie fc;
                   2448: 
                   2449:        XATTR xattr;
                   2450: 
                   2451:        long r;
                   2452: 
                   2453: 
                   2454: 
1.1.1.2   root     2455:        TRACE(("Fchown(%s, %d, %d)", name, uid, gid));
1.1       root     2456: 
                   2457: 
                   2458: 
1.1.1.6 ! root     2459:        r = path2cookie(name, NULL, &fc);
1.1       root     2460: 
                   2461:        if (r) {
                   2462: 
1.1.1.2   root     2463:                DEBUG(("Fchown(%s): error %ld", name, r));
1.1       root     2464: 
                   2465:                return r;
                   2466: 
                   2467:        }
                   2468: 
                   2469: 
                   2470: 
                   2471: /* MiNT acts like _POSIX_CHOWN_RESTRICTED: a non-privileged process can
                   2472: 
                   2473:  * only change the ownership of a file that is owned by this user, to
                   2474: 
                   2475:  * the effective group id of the process
                   2476: 
                   2477:  */
                   2478: 
                   2479:        if (curproc->euid) {
                   2480: 
1.1.1.3   root     2481:                if (curproc->egid != gid)
1.1       root     2482: 
1.1.1.3   root     2483:                        r = EACCDN;
                   2484: 
                   2485:                else
                   2486: 
                   2487:                        r = (*fc.fs->getxattr)(&fc, &xattr);
1.1       root     2488: 
                   2489:                if (r) {
                   2490: 
1.1.1.2   root     2491:                        DEBUG(("Fchown(%s): unable to get file attributes",name));
1.1       root     2492: 
1.1.1.3   root     2493:                        release_cookie(&fc);
                   2494: 
1.1       root     2495:                        return r;
                   2496: 
                   2497:                }
                   2498: 
                   2499:                if (xattr.uid != curproc->euid || xattr.uid != uid) {
                   2500: 
1.1.1.2   root     2501:                        DEBUG(("Fchown(%s): not the file's owner",name));
1.1       root     2502: 
1.1.1.3   root     2503:                        release_cookie(&fc);
                   2504: 
1.1       root     2505:                        return EACCDN;
                   2506: 
                   2507:                }
                   2508: 
                   2509:        }
                   2510: 
1.1.1.3   root     2511:        r = (*fc.fs->chown)(&fc, uid, gid);
                   2512: 
                   2513:        release_cookie(&fc);
                   2514: 
                   2515:        return r;
1.1       root     2516: 
                   2517: }
                   2518: 
                   2519: 
                   2520: 
                   2521: /*
                   2522: 
                   2523:  * GEMDOS extension: Fchmod(file, mode) changes a file's access
                   2524: 
                   2525:  * permissions.
                   2526: 
                   2527:  */
                   2528: 
                   2529: 
                   2530: 
1.1.1.2   root     2531: long ARGS_ON_STACK
1.1       root     2532: 
                   2533: f_chmod(name, mode)
                   2534: 
                   2535:        const char *name;
                   2536: 
                   2537:        unsigned mode;
                   2538: 
                   2539: {
                   2540: 
                   2541:        fcookie fc;
                   2542: 
                   2543:        long r;
                   2544: 
                   2545:        XATTR xattr;
                   2546: 
                   2547: 
                   2548: 
1.1.1.2   root     2549:        TRACE(("Fchmod(%s, %o)", name, mode));
1.1       root     2550: 
                   2551:        r = path2cookie(name, follow_links, &fc);
                   2552: 
                   2553:        if (r) {
                   2554: 
1.1.1.2   root     2555:                DEBUG(("Fchmod(%s): error %ld", name, r));
1.1       root     2556: 
                   2557:                return r;
                   2558: 
                   2559:        }
                   2560: 
                   2561:        r = (*fc.fs->getxattr)(&fc, &xattr);
                   2562: 
                   2563:        if (r) {
                   2564: 
1.1.1.2   root     2565:                DEBUG(("Fchmod(%s): couldn't get file attributes",name));
1.1       root     2566: 
                   2567:        }
                   2568: 
1.1.1.3   root     2569:        else if (curproc->euid && curproc->euid != xattr.uid) {
1.1       root     2570: 
1.1.1.2   root     2571:                DEBUG(("Fchmod(%s): not the file's owner",name));
1.1       root     2572: 
1.1.1.3   root     2573:                r = EACCDN;
1.1       root     2574: 
1.1.1.3   root     2575:        } else {
1.1       root     2576: 
1.1.1.3   root     2577:                r = (*fc.fs->chmode)(&fc, mode & ~S_IFMT);
1.1       root     2578: 
1.1.1.3   root     2579:                if (r) DEBUG(("Fchmod: error %ld", r));
                   2580: 
                   2581:        }
                   2582: 
                   2583:        release_cookie(&fc);
1.1       root     2584: 
                   2585:        return r;
                   2586: 
                   2587: }
                   2588: 
                   2589: 
                   2590: 
                   2591: /*
                   2592: 
                   2593:  * GEMDOS extension: Dlock(mode, dev): locks or unlocks access to
                   2594: 
1.1.1.3   root     2595:  * a BIOS device. "mode" bit 0 is 0 for unlock, 1 for lock; "dev" is a
1.1       root     2596: 
                   2597:  * BIOS device (0 for A:, 1 for B:, etc.).
                   2598: 
1.1.1.3   root     2599:  *
                   2600: 
1.1       root     2601:  * Returns: 0 if the operation was successful
                   2602: 
                   2603:  *          EACCDN if a lock attempt is made on a drive that is being
                   2604: 
                   2605:  *            used
                   2606: 
                   2607:  *         ELOCKED if the drive is locked by another process
                   2608: 
                   2609:  *         ENSLOCK if a program attempts to unlock a drive it
                   2610: 
                   2611:  *            hasn't locked.
                   2612: 
1.1.1.3   root     2613:  * ++jr: if mode bit 1 is set, then instead of returning ELOCKED the
                   2614: 
                   2615:  * pid of the process which has locked the drive is returned (unless
                   2616: 
                   2617:  * it was locked by pid 0, in which case ELOCKED is still returned).
                   2618: 
1.1       root     2619:  */
                   2620: 
                   2621: 
                   2622: 
                   2623: PROC *dlockproc[NUM_DRIVES];
                   2624: 
                   2625: 
                   2626: 
1.1.1.2   root     2627: long ARGS_ON_STACK
1.1       root     2628: 
                   2629: d_lock(mode, dev)
                   2630: 
                   2631:        int mode, dev;
                   2632: 
                   2633: {
                   2634: 
                   2635:        PROC *p;
                   2636: 
                   2637:        FILEPTR *f;
                   2638: 
                   2639:        int i;
                   2640: 
                   2641: 
                   2642: 
1.1.1.2   root     2643:        TRACE(("Dlock(%x,%c:)", mode, dev+'A'));
1.1       root     2644: 
                   2645:        if (dev < 0 || dev >= NUM_DRIVES) return EDRIVE;
                   2646: 
1.1.1.2   root     2647:        if (aliasdrv[dev]) {
                   2648: 
                   2649:                dev = aliasdrv[dev] - 1;
1.1       root     2650: 
1.1.1.2   root     2651:                if (dev < 0 || dev >= NUM_DRIVES)
                   2652: 
                   2653:                        return EDRIVE;
                   2654: 
                   2655:        }
1.1       root     2656: 
                   2657:        if ( (mode&1) == 0) {   /* unlock */
                   2658: 
                   2659:                if (dlockproc[dev] == curproc) {
                   2660: 
                   2661:                        dlockproc[dev] = 0;
                   2662: 
                   2663:                        changedrv(dev);
                   2664: 
                   2665:                        return 0;
                   2666: 
                   2667:                }
                   2668: 
1.1.1.2   root     2669:                DEBUG(("Dlock: no such lock"));
1.1       root     2670: 
                   2671:                return ENSLOCK;
                   2672: 
                   2673:        }
                   2674: 
                   2675: 
                   2676: 
                   2677: /* code for locking */
                   2678: 
                   2679: /* is the drive already locked? */
                   2680: 
                   2681:        if (dlockproc[dev]) {
                   2682: 
1.1.1.2   root     2683:                DEBUG(("Dlock: drive already locked"));
1.1       root     2684: 
1.1.1.5   root     2685: #if 0
                   2686: 
1.1.1.3   root     2687:                if (dlockproc[dev] == curproc) return 0;
                   2688: 
1.1.1.5   root     2689: #endif
                   2690: 
1.1.1.3   root     2691:                if (dlockproc[dev]->pid == 0) return ELOCKED;
                   2692: 
                   2693:                return (mode & 2) ? dlockproc[dev]->pid : ELOCKED;
1.1       root     2694: 
                   2695:        }
                   2696: 
                   2697: /* see if the drive is in use */
                   2698: 
                   2699:        for (p = proclist; p; p = p->gl_next) {
                   2700: 
                   2701:                if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
                   2702: 
                   2703:                        continue;
                   2704: 
                   2705:                for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
                   2706: 
1.1.1.6 ! root     2707:                        if ( ((f = p->handle[i]) != 0) && 
        !          2708: 
        !          2709:                             (f != (FILEPTR *)1) && (f->fc.dev == dev) ) {
1.1       root     2710: 
1.1.1.2   root     2711:                DEBUG(("Dlock: process %d has an open handle on the drive", p->pid));
1.1       root     2712: 
1.1.1.3   root     2713:                                if (p->pid == 0) return EACCDN;
1.1       root     2714: 
1.1.1.3   root     2715:                                return (mode & 2) ? p->pid : EACCDN;
1.1       root     2716: 
                   2717:                        }
                   2718: 
                   2719:                }
                   2720: 
                   2721:        }
                   2722: 
                   2723: 
                   2724: 
                   2725: /* if we reach here, the drive is not in use */
                   2726: 
                   2727: /* we lock it by setting dlockproc and by setting all root and current
                   2728: 
                   2729:  * directories referring to the device to a null file system
                   2730: 
                   2731:  */
                   2732: 
                   2733:        for (p = proclist; p; p = p->gl_next) {
                   2734: 
                   2735:                for (i = 0; i < NUM_DRIVES; i++) {
                   2736: 
1.1.1.3   root     2737:                        if (p->root[i].dev == dev) {
                   2738: 
                   2739:                                release_cookie(&p->root[i]);
1.1       root     2740: 
                   2741:                                p->root[i].fs = 0;
                   2742: 
1.1.1.3   root     2743:                        }
                   2744: 
                   2745:                        if (p->curdir[i].dev == dev) {
                   2746: 
                   2747:                                release_cookie(&p->curdir[i]);
1.1       root     2748: 
                   2749:                                p->curdir[i].fs = 0;
                   2750: 
1.1.1.3   root     2751:                        }
                   2752: 
1.1       root     2753:                }
                   2754: 
                   2755:        }
                   2756: 
                   2757: 
                   2758: 
                   2759:        dlockproc[dev] = curproc;
                   2760: 
                   2761:        return 0;
                   2762: 
                   2763: }
                   2764: 
1.1.1.6 ! root     2765: 
        !          2766: 
        !          2767: /* jr: GEMDOS-extensions Dreadlabel() and Dwritelabel() */
        !          2768: 
        !          2769: 
        !          2770: 
        !          2771: long ARGS_ON_STACK
        !          2772: 
        !          2773: d_readlabel(name, label, namelen)
        !          2774: 
        !          2775:        const char *name;
        !          2776: 
        !          2777:        char *label;
        !          2778: 
        !          2779:        int namelen;
        !          2780: 
        !          2781: {
        !          2782: 
        !          2783:        fcookie dir;
        !          2784: 
        !          2785:        long r;
        !          2786: 
        !          2787: 
        !          2788: 
        !          2789:        r = path2cookie(name, (char *)0, &dir);
        !          2790: 
        !          2791:        if (r) {
        !          2792: 
        !          2793:                DEBUG(("Dreadlabel(%s): bad path",name));
        !          2794: 
        !          2795:                return r;
        !          2796: 
        !          2797:        }
        !          2798: 
        !          2799:        r = (*dir.fs->readlabel)(&dir, label, namelen);
        !          2800: 
        !          2801: 
        !          2802: 
        !          2803:        release_cookie(&dir);
        !          2804: 
        !          2805:        return r;
        !          2806: 
        !          2807: }
        !          2808: 
        !          2809: 
        !          2810: 
        !          2811: long ARGS_ON_STACK
        !          2812: 
        !          2813: d_writelabel(name, label)
        !          2814: 
        !          2815:        const char *name;
        !          2816: 
        !          2817:        const char *label;
        !          2818: 
        !          2819: {
        !          2820: 
        !          2821:        fcookie dir;
        !          2822: 
        !          2823:        long r;
        !          2824: 
        !          2825: 
        !          2826: 
        !          2827:        r = path2cookie(name, (char *)0, &dir);
        !          2828: 
        !          2829:        if (r) {
        !          2830: 
        !          2831:                DEBUG(("Dwritelabel(%s): bad path",name));
        !          2832: 
        !          2833:                return r;
        !          2834: 
        !          2835:        }
        !          2836: 
        !          2837:        r = (*dir.fs->writelabel)(&dir, label);
        !          2838: 
        !          2839: 
        !          2840: 
        !          2841:        release_cookie(&dir);
        !          2842: 
        !          2843:        return r;
        !          2844: 
        !          2845: }
        !          2846: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.