Annotation of MiNT/src/filesys.c, revision 1.1.1.5

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 Corp.
1.1.1.3   root        6: 
                      7: All rights reserved.
1.1       root        8: 
                      9: */
                     10: 
                     11: 
                     12: 
                     13: /*
                     14: 
                     15:  * various file system interface things
                     16: 
                     17:  */
                     18: 
                     19: 
                     20: 
                     21: #include "mint.h"
                     22: 
                     23: 
                     24: 
1.1.1.3   root       25: #define PATH2COOKIE_DB(x) TRACE(x)
                     26: 
                     27: 
                     28: 
1.1       root       29: FILESYS *active_fs;
                     30: 
                     31: FILESYS *drives[NUM_DRIVES];
                     32: 
1.1.1.3   root       33: extern FILESYS tos_filesys;    /* declaration needed for debugging only */
                     34: 
1.1       root       35: 
                     36: 
1.1.1.2   root       37: /* "aliased" drives are different names
                     38: 
                     39:  * for real drives/directories
                     40: 
                     41:  * if drive d is an alias for c:\usr,
                     42: 
                     43:  * then alias_drv[3] == 2 (the real
                     44: 
                     45:  * drive) and aliases has bit (1L << 3)
                     46: 
                     47:  * set.
                     48: 
                     49:  * NOTE: if aliasdrv[d] is 0, then d is not an aliased drive,
                     50: 
                     51:  * otherwise d is aliased to drive aliasdrv[d]-1
                     52: 
                     53:  * (e.g. if drive A: is aliased to B:\FOO, then
                     54: 
                     55:  * aliasdrv[0] == 'B'-'A'+1 == 2). Always remember to
                     56: 
                     57:  * compensate for the extra 1 when dereferencing aliasdrv!
                     58: 
                     59:  */
                     60: 
                     61: int    aliasdrv[NUM_DRIVES];
                     62: 
                     63: 
                     64: 
1.1       root       65: FILEPTR *flist;                /* a list of free file pointers */
                     66: 
                     67: 
                     68: 
                     69: char follow_links[1];  /* dummy "name" used as a parameter to path2cookie */
                     70: 
                     71: 
                     72: 
                     73: /* vector of valid drives, according to GEMDOS */
                     74: 
                     75: /* note that this isn't necessarily the same as what the BIOS thinks of
                     76: 
                     77:  * as valid
                     78: 
                     79:  */
                     80: 
                     81: long dosdrvs;
                     82: 
                     83: 
                     84: 
                     85: /*
                     86: 
                     87:  * Initialize a specific drive. This is called whenever a new drive
                     88: 
                     89:  * is accessed, or when media change occurs on an old drive.
                     90: 
                     91:  * Assumption: at this point, active_fs is a valid pointer
                     92: 
                     93:  * to a list of file systems.
                     94: 
                     95:  */
                     96: 
                     97: 
                     98: 
                     99: /* table of processes holding locks on drives */
                    100: 
                    101: extern PROC *dlockproc[];      /* in dosdir.c */
                    102: 
                    103: 
                    104: 
                    105: void
                    106: 
                    107: init_drive(i)
                    108: 
                    109:        int i;
                    110: 
                    111: {
                    112: 
                    113:        long r;
                    114: 
                    115:        FILESYS *fs;
                    116: 
                    117:        fcookie root_dir;
                    118: 
                    119: 
                    120: 
1.1.1.2   root      121:        TRACE(("init_drive(%c)", i+'A'));
1.1       root      122: 
                    123: 
                    124: 
                    125:        drives[i] = 0;          /* no file system */
                    126: 
                    127:        if (i >= 0 && i < NUM_DRIVES) {
                    128: 
                    129:                if (dlockproc[i]) return;
                    130: 
                    131:        }
                    132: 
                    133: 
                    134: 
                    135:        for (fs = active_fs; fs; fs = fs->next) {
                    136: 
                    137:                r = (*fs->root)(i, &root_dir);
                    138: 
                    139:                if (r == 0) {
                    140: 
                    141:                        drives[i] = root_dir.fs;
                    142: 
1.1.1.3   root      143:                        release_cookie(&root_dir);
                    144: 
1.1       root      145:                        break;
                    146: 
                    147:                }
                    148: 
                    149:        }
                    150: 
                    151: }
                    152: 
                    153: 
                    154: 
                    155: /*
                    156: 
                    157:  * initialize the file system
                    158: 
                    159:  */
                    160: 
                    161: 
                    162: 
                    163: #define NUMFPS 40      /* initial number of file pointers */
                    164: 
                    165: 
                    166: 
                    167: void
                    168: 
                    169: init_filesys()
                    170: 
                    171: {
                    172: 
                    173:        static FILEPTR initial[NUMFPS+1];
                    174: 
                    175:        int i;
                    176: 
                    177:        extern FILESYS tos_filesys, bios_filesys, pipe_filesys,
                    178: 
                    179:                proc_filesys, uni_filesys;
                    180: 
                    181: 
                    182: 
                    183: /* get the vector of connected GEMDOS drives */
                    184: 
                    185:        dosdrvs = Dsetdrv(Dgetdrv()) | drvmap();
                    186: 
                    187: 
                    188: 
                    189: /* set up some initial file pointers */
                    190: 
                    191:        for (i = 0; i < NUMFPS; i++) {
                    192: 
                    193:                initial[i].devinfo = (ulong) (&initial[i+1]);
                    194: 
                    195:        }
                    196: 
                    197:        initial[NUMFPS].devinfo = 0;
                    198: 
                    199:        flist = initial;
                    200: 
                    201: 
                    202: 
                    203: /* set up the file systems */
                    204: 
                    205:        tos_filesys.next = 0;
                    206: 
                    207:        bios_filesys.next = &tos_filesys;
                    208: 
                    209:        pipe_filesys.next = &bios_filesys;
                    210: 
                    211:        proc_filesys.next = &pipe_filesys;
                    212: 
                    213:        uni_filesys.next = &proc_filesys;
                    214: 
                    215: 
                    216: 
                    217:        active_fs = &uni_filesys;
                    218: 
                    219: 
                    220: 
                    221: /* initialize the BIOS file system */
                    222: 
                    223:        biosfs_init();
                    224: 
                    225: 
                    226: 
                    227: /* initialize the unified file system */
                    228: 
                    229:        unifs_init();
                    230: 
                    231: }
                    232: 
                    233: 
                    234: 
                    235: /*
                    236: 
                    237:  * load file systems from disk
                    238: 
                    239:  * this routine is called after process 0 is set up, but before any user
                    240: 
                    241:  * processes are run
                    242: 
                    243:  *
                    244: 
                    245:  * NOTE that a number of directory changes take place here: we look first
                    246: 
1.1.1.4   root      247:  * in the current directory, then in the directory \mint.
1.1       root      248: 
                    249:  */
                    250: 
                    251: 
                    252: 
1.1.1.2   root      253: typedef FILESYS * ARGS_ON_STACK (*FSFUNC) P_((struct kerinfo *));
1.1       root      254: 
                    255: 
                    256: 
1.1.1.4   root      257: /* uk: made this lie outside of functions, as load_filesys() and
                    258: 
                    259:  *     load_devdriver() need access to it.
                    260: 
                    261:  */
                    262: 
                    263: #define NPATHS 3
                    264: 
                    265: static const char *ext_paths[NPATHS] = {"", "\\MINT", "\\MULTITOS"};
                    266: 
                    267: 
                    268: 
                    269: 
                    270: 
1.1       root      271: void
                    272: 
                    273: load_filesys()
                    274: 
                    275: {
                    276: 
                    277:        long r;
                    278: 
                    279:        BASEPAGE *b;
                    280: 
                    281:        FILESYS *fs;
                    282: 
                    283:        FSFUNC initf;
                    284: 
                    285:        static DTABUF dta;
                    286: 
                    287:        int i;
                    288: 
                    289:        extern struct kerinfo kernelinfo; /* in main.c */
                    290: 
1.1.1.2   root      291:        char curpath[PATH_MAX];
                    292: 
1.1.1.3   root      293:        MEMREGION *xfsreg;
                    294: 
1.1       root      295: 
                    296: 
                    297:        curproc->dta = &dta;
                    298: 
1.1.1.2   root      299:        d_getpath(curpath,0);
                    300: 
1.1       root      301: 
                    302: 
                    303:        for (i = 0; i < NPATHS; i++) {
                    304: 
1.1.1.4   root      305:            if (*ext_paths[i]) {
1.1.1.2   root      306: 
                    307: /* don't bother checking the current directory twice! */
                    308: 
1.1.1.4   root      309:                    if (!stricmp(ext_paths[i],curpath))
1.1.1.2   root      310: 
                    311:                        r = -1;
                    312: 
                    313:                    else
                    314: 
1.1.1.4   root      315:                        r = d_setpath(ext_paths[i]);
1.1.1.2   root      316: 
                    317:            }
                    318: 
                    319:            else
                    320: 
                    321:                    r = 0;
                    322: 
                    323: 
                    324: 
                    325:            if (r == 0)
                    326: 
                    327:                    r = f_sfirst("*.xfs", 0);
1.1       root      328: 
                    329: 
                    330: 
                    331:            while (r == 0) {
                    332: 
                    333:                b = (BASEPAGE *)p_exec(3, dta.dta_name, (char *)"", (char *)0);
                    334: 
                    335:                if ( ((long)b) < 0 ) {
                    336: 
1.1.1.2   root      337:                        DEBUG(("Error loading file system %s", dta.dta_name));
                    338: 
                    339:                        r = f_snext();
1.1       root      340: 
                    341:                        continue;
                    342: 
                    343:                }
                    344: 
                    345:        /* we leave a little bit of slop at the end of the loaded stuff */
                    346: 
                    347:                m_shrink(0, (virtaddr)b, 512 + b->p_tlen + b->p_dlen + b->p_blen);
                    348: 
                    349:                initf = (FSFUNC)b->p_tbase;
                    350: 
1.1.1.2   root      351:                TRACE(("initializing %s", dta.dta_name));
1.1       root      352: 
1.1.1.2   root      353:                fs = (*initf)(&kernelinfo);
1.1       root      354: 
                    355: 
                    356: 
                    357:                if (fs) {
                    358: 
1.1.1.2   root      359:                        TRACE(("%s loaded OK", dta.dta_name));
1.1       root      360: 
1.1.1.3   root      361:        /* put the loaded XFS into super accesible memory */
                    362: 
                    363:                        xfsreg = addr2region( (long) b );
                    364: 
                    365:                        mark_region(xfsreg, PROT_S);
                    366: 
1.1.1.4   root      367: 
                    368: 
1.1.1.3   root      369:        /* link it into the list of drivers */
                    370: 
1.1.1.4   root      371:        /* uk: but only if it has not installed itself via Dcntl()
                    372: 
                    373:         *     after checking if file system is already installed,
                    374: 
                    375:         *     so we know for sure that each file system in at most
                    376: 
                    377:         *     once in the chain (important for removal!)
                    378: 
                    379:         * also note: this doesn't preclude loading two different
                    380: 
                    381:         * instances of the same file system driver, e.g. it's perfectly
                    382: 
                    383:         * OK to have a "cdromy1.xfs" and "cdromz2.xfs"; the check below
                    384: 
                    385:         * just makes sure that a given instance of a file system is
                    386: 
                    387:         * installed at most once. I.e., it prevents cdromy1.xfs from being
                    388: 
                    389:         * installed twice.
                    390: 
                    391:         */
1.1       root      392: 
1.1.1.4   root      393:                        if ((FILESYS*)1L != fs) {
                    394: 
                    395:                                FILESYS *f = active_fs;
                    396: 
                    397:                                for (;  f;  f = f->next)
                    398: 
                    399:                                        if (f == fs)
                    400: 
                    401:                                                break;
                    402: 
                    403:                                if (!f) {   /* we ran completly through the list */
                    404: 
                    405:                                        fs->next = active_fs;
                    406: 
                    407:                                        active_fs = fs;
                    408: 
                    409:                                }
                    410: 
                    411:                        }
1.1       root      412: 
                    413:                } else {
                    414: 
1.1.1.2   root      415:                        DEBUG(("%s returned null", dta.dta_name));
1.1       root      416: 
1.1.1.3   root      417:                        m_free((virtaddr)b);
                    418: 
1.1       root      419:                }
                    420: 
                    421:                r = f_snext();
                    422: 
                    423:            }
                    424: 
                    425:        }
                    426: 
                    427: 
                    428: 
1.1.1.4   root      429: #if 0
                    430: 
1.1       root      431: /* here, we invalidate all old drives EXCEPT for ones we're already using (at
                    432: 
                    433:  * this point, only the bios devices should be open)
                    434: 
                    435:  * this gives newly loaded file systems a chance to replace the
                    436: 
                    437:  * default tosfs.c
                    438: 
                    439:  */
                    440: 
                    441:        for (i = 0; i < NUM_DRIVES; i++) {
                    442: 
                    443:                if (d_lock(1, i) == 0)  /* lock if possible */
                    444: 
                    445:                        d_lock(0, i);   /* and then unlock */
                    446: 
                    447:        }
                    448: 
1.1.1.4   root      449: #endif
                    450: 
1.1       root      451: }
                    452: 
                    453: 
                    454: 
1.1.1.4   root      455: 
                    456: 
                    457: /*
                    458: 
                    459:  * uk: load device driver in files called *.xdd (external device driver)
                    460: 
                    461:  *     from disk
                    462: 
                    463:  * maybe this should go into biosfs.c ??
                    464: 
                    465:  *
                    466: 
                    467:  * this routine is called after process 0 is set up, but before any user
                    468: 
                    469:  * processes are run, but before the loadable file systems come in,
                    470: 
                    471:  * so they can make use of external device drivers
                    472: 
                    473:  *
                    474: 
                    475:  * NOTE that a number of directory changes take place here: we look first
                    476: 
                    477:  * in the current directory, then in the directory \mint, and finally
                    478: 
                    479:  * the d_lock() calls force us into the root directory.
                    480: 
                    481:  * ??? what d_lock() calls ???
                    482: 
                    483:  */
                    484: 
                    485: 
                    486: 
                    487: typedef DEVDRV * ARGS_ON_STACK (*DEVFUNC) P_((struct kerinfo *));
                    488: 
                    489: 
                    490: 
                    491: #define DEV_SELFINST  ((DEVDRV*)1L)  /* dev driver did dcntl() already */
                    492: 
                    493: 
                    494: 
                    495: void
                    496: 
                    497: load_devdriver()
                    498: 
                    499: {
                    500: 
                    501:        long r;
                    502: 
                    503:        BASEPAGE *b;
                    504: 
                    505:        DEVDRV *dev;
                    506: 
                    507:        DEVFUNC initf;
                    508: 
                    509:        struct dev_descr the_dev;
                    510: 
                    511:        static DTABUF dta;
                    512: 
                    513:        int i;
                    514: 
                    515:        extern struct kerinfo kernelinfo; /* in main.c */
                    516: 
                    517:        char curpath[PATH_MAX];
                    518: 
                    519:        char dev_name[PATH_MAX];  /* a bit long, but one never knows... */
                    520: 
                    521:        char ch, *p;
                    522: 
                    523:        MEMREGION *xddreg;
                    524: 
                    525: 
                    526: 
                    527: 
                    528: 
                    529:        curproc->dta = &dta;
                    530: 
                    531:        d_getpath(curpath,0);
                    532: 
                    533: 
                    534: 
                    535:        for (i = 0; i < NPATHS; i++) {
                    536: 
                    537:            if (*ext_paths[i]) {
                    538: 
                    539: /* don't bother checking the current directory twice! */
                    540: 
                    541:                    if (!stricmp(ext_paths[i],curpath))
                    542: 
                    543:                        r = -1;
                    544: 
                    545:                    else
                    546: 
                    547:                        r = d_setpath(ext_paths[i]);
                    548: 
                    549:            }
                    550: 
                    551:            else
                    552: 
                    553:                    r = 0;
                    554: 
                    555: 
                    556: 
                    557:            if (r == 0)
                    558: 
                    559:                    r = f_sfirst("*.xdd", 0);
                    560: 
                    561: 
                    562: 
                    563:            while (r == 0) {
                    564: 
                    565:                b = (BASEPAGE *)p_exec(3, dta.dta_name, (char *)"", (char *)0);
                    566: 
                    567:                if ( ((long)b) < 0 ) {
                    568: 
                    569:                        DEBUG(("Error loading device driver %s", dta.dta_name));
                    570: 
                    571:                        r = f_snext();
                    572: 
                    573:                        continue;
                    574: 
                    575:                }
                    576: 
                    577:        /* we leave a little bit of slop at the end of the loaded stuff */
                    578: 
                    579:                m_shrink(0, (virtaddr)b, 512 + b->p_tlen + b->p_dlen + b->p_blen);
                    580: 
                    581:                initf = (DEVFUNC)b->p_tbase;
                    582: 
                    583:                TRACE(("initializing %s", dta.dta_name));
                    584: 
                    585:                dev = (*initf)(&kernelinfo);
                    586: 
                    587: 
                    588: 
                    589:                if (dev) {
                    590: 
                    591:                        if (DEV_SELFINST != dev) {
                    592: 
                    593:        /* we need to install the device driver ourselves */
                    594: 
                    595:                                the_dev.driver = dev;
                    596: 
                    597:                                the_dev.dinfo = 0;
                    598: 
                    599:                                the_dev.flags = 0;
                    600: 
                    601:                                the_dev.tty = (struct tty*)0L;
                    602: 
                    603:                                the_dev.reserved[0] = the_dev.reserved[1] = 0;
                    604: 
                    605:                                the_dev.reserved[2] = the_dev.reserved[3] = 0;
                    606: 
                    607:                                p = dta.dta_name;
                    608: 
1.1.1.5 ! root      609:        /* copy the dev. driver name, converting to lower case */
        !           610: 
        !           611:                                while (*p && *p != '.') {
        !           612: 
        !           613:                                        *p = tolower(*p);
        !           614: 
        !           615:                                        p++;
        !           616: 
        !           617:                                }
1.1.1.4   root      618: 
                    619:                                ch = *p;
                    620: 
                    621:                                *p = '\0';  /* we dont want the extension */
                    622: 
                    623:                                strcpy(dev_name, "u:\\dev\\");
                    624: 
                    625:                                strcat(dev_name, dta.dta_name);
                    626: 
                    627:                                *p = ch;
                    628: 
                    629:                                r = d_cntl(DEV_INSTALL, dev_name, (long)&the_dev);
                    630: 
                    631:                                if (r <= 0) {
                    632: 
                    633:                                        DEBUG(("Error installing device driver %s", dta.dta_name));
                    634: 
                    635:                                        r = f_snext();
                    636: 
                    637:                                        continue;
                    638: 
                    639:                                }
                    640: 
                    641:                        }
                    642: 
                    643:                        TRACE(("%s loaded OK", dta.dta_name));
                    644: 
                    645:        /* put the loaded XDD into super accesible memory */
                    646: 
                    647:                        xddreg = addr2region( (long) b );
                    648: 
                    649:                        mark_region(xddreg, PROT_S);
                    650: 
                    651:                } else {
                    652: 
                    653:                        DEBUG(("%s returned null", dta.dta_name));
                    654: 
                    655:                        m_free((virtaddr)b);
                    656: 
                    657:                }
                    658: 
                    659:                r = f_snext();
                    660: 
                    661:            }
                    662: 
                    663:        }
                    664: 
                    665: }
                    666: 
                    667:  
                    668: 
                    669: 
                    670: 
1.1       root      671: void
                    672: 
                    673: close_filesys()
                    674: 
                    675: {
                    676: 
                    677:        PROC *p;
                    678: 
                    679:        FILEPTR *f;
                    680: 
                    681:        int i;
                    682: 
                    683: 
                    684: 
1.1.1.2   root      685:        TRACE(("close_filesys"));
1.1       root      686: 
                    687: /* close every open file */
                    688: 
                    689:        for (p = proclist; p; p = p->gl_next) {
                    690: 
                    691:                for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
                    692: 
                    693:                        if ( (f = p->handle[i]) != 0) {
                    694: 
                    695:                                if (p->wait_q == TSR_Q || p->wait_q == ZOMBIE_Q)
                    696: 
                    697:                                        ALERT("Open file for dead process?");
                    698: 
                    699:                                do_pclose(p, f);
                    700: 
                    701:                        }
                    702: 
                    703:                }
                    704: 
                    705:        }
                    706: 
                    707: }
                    708: 
                    709: 
                    710: 
                    711: /*
                    712: 
                    713:  * "media change" routine: called when a media change is detected on device
                    714: 
                    715:  * d, which may or may not be a BIOS device. All handles associated with
                    716: 
                    717:  * the device are closed, and all directories invalidated. This routine
                    718: 
                    719:  * does all the dirty work, and is called automatically when
                    720: 
                    721:  * disk_changed detects a media change.
                    722: 
                    723:  */
                    724: 
                    725: 
                    726: 
1.1.1.2   root      727: void ARGS_ON_STACK 
1.1       root      728: 
                    729: changedrv(d)
                    730: 
                    731:        unsigned d;
                    732: 
                    733: {
                    734: 
                    735:        PROC *p;
                    736: 
                    737:        int i;
                    738: 
                    739:        FILEPTR *f;
                    740: 
                    741:        FILESYS *fs;
                    742: 
1.1.1.3   root      743:        SHTEXT *stext;
                    744: 
                    745:        extern SHTEXT *text_reg;        /* in mem.c */
                    746: 
1.1       root      747:        DIR *dirh;
                    748: 
                    749:        fcookie dir;
                    750: 
1.1.1.5 ! root      751:        int warned = (d & 0xf000) == PROC_RDEV_BASE;
1.1       root      752: 
1.1.1.2   root      753:        long r;
                    754: 
                    755: 
                    756: 
                    757: /* if an aliased drive, change the *real* device */
                    758: 
                    759:        if (d < NUM_DRIVES && aliasdrv[d]) {
                    760: 
                    761:                d = aliasdrv[d] - 1;    /* see NOTE above */
                    762: 
                    763:        }
1.1       root      764: 
                    765: 
                    766: 
                    767: /* re-initialize the device, if it was a BIOS device */
                    768: 
                    769:        if (d < NUM_DRIVES) {
                    770: 
                    771:                fs = drives[d];
                    772: 
                    773:                if (fs) {
                    774: 
                    775:                        (void)(*fs->dskchng)(d);
                    776: 
                    777:                }
                    778: 
                    779:                init_drive(d);
                    780: 
                    781:        }
                    782: 
                    783: 
                    784: 
                    785:        for (p = proclist; p; p = p->gl_next) {
                    786: 
                    787:        /* invalidate all open files on this device */
                    788: 
                    789:                for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
                    790: 
                    791:                        if (((f = p->handle[i]) != 0) && (f->fc.dev == d)) {
                    792: 
                    793:                            if (!warned) {
                    794: 
                    795:                                ALERT(
                    796: 
                    797: "Files were open on a changed drive (0x%x)!", d);
                    798: 
                    799:                                warned++;
                    800: 
                    801:                            }
                    802: 
                    803: 
                    804: 
                    805: /* we set f->dev to NULL to indicate to do_pclose that this is an
                    806: 
                    807:  * emergency close, and that it shouldn't try to make any
                    808: 
                    809:  * calls to the device driver since the file has gone away
                    810: 
                    811:  */
                    812: 
                    813:                            f->dev = NULL;
                    814: 
                    815:                            (void)do_pclose(p, f);
                    816: 
1.1.1.2   root      817: /* we could just zero the handle, but this could lead to confusion if
                    818: 
                    819:  * a process doesn't realize that there's been a media change, Fopens
                    820: 
                    821:  * a new file, and gets the same handle back. So, we force the
                    822: 
                    823:  * handle to point to /dev/null.
                    824: 
                    825:  */
                    826: 
                    827:                            p->handle[i] =
                    828: 
                    829:                                do_open("U:\\DEV\\NULL", O_RDWR, 0, (XATTR *)0);
1.1       root      830: 
                    831:                        }
                    832: 
                    833:                }
                    834: 
                    835: 
                    836: 
                    837:        /* terminate any active directory searches on the drive */
                    838: 
                    839:                for (i = 0; i < NUM_SEARCH; i++) {
                    840: 
1.1.1.2   root      841:                        dirh = &p->srchdir[i];
1.1       root      842: 
1.1.1.4   root      843:                        if (p->srchdta[i] && dirh->fc.fs && dirh->fc.dev == d) {
1.1       root      844: 
1.1.1.2   root      845:                                TRACE(("closing search for process %d", p->pid));
                    846: 
1.1.1.3   root      847:                                release_cookie(&dirh->fc);
                    848: 
1.1       root      849:                                dirh->fc.fs = 0;
                    850: 
1.1.1.2   root      851:                                p->srchdta[i] = 0;
1.1       root      852: 
                    853:                        }
                    854: 
                    855:                }
                    856: 
                    857: 
                    858: 
1.1.1.4   root      859:                for (dirh = p->searches; dirh; dirh = dirh->next) {
                    860: 
                    861:                    /* If this search is on the changed drive, release
                    862: 
                    863:                       the cookie, but do *not* free it, since the
                    864: 
                    865:                       user could later call closedir on it. */
                    866: 
                    867:                        if (dirh->fc.fs && dirh->fc.dev == d) {
                    868: 
                    869:                                release_cookie (&dirh->fc);
                    870: 
                    871:                                dirh->fc.fs = 0;
                    872: 
                    873:                        }
                    874: 
                    875:                }
                    876: 
                    877:                    
                    878: 
1.1       root      879:                if (d >= NUM_DRIVES) continue;
                    880: 
                    881: 
                    882: 
                    883:        /* change any active directories on the device to the (new) root */
                    884: 
                    885:                fs = drives[d];
                    886: 
                    887:                if (fs) {
                    888: 
                    889:                        r = (*fs->root)(d, &dir);
                    890: 
                    891:                        if (r != E_OK) dir.fs = 0;
                    892: 
                    893:                } else {
                    894: 
                    895:                        dir.fs = 0; dir.dev = d;
                    896: 
                    897:                }
                    898: 
                    899: 
                    900: 
                    901:                for (i = 0; i < NUM_DRIVES; i++) {
                    902: 
1.1.1.3   root      903:                        if (p->root[i].dev == d) {
                    904: 
                    905:                                release_cookie(&p->root[i]);
1.1       root      906: 
1.1.1.3   root      907:                                dup_cookie(&p->root[i], &dir);
                    908: 
                    909:                        }
1.1       root      910: 
1.1.1.3   root      911:                        if (p->curdir[i].dev == d) {
1.1       root      912: 
1.1.1.3   root      913:                                release_cookie(&p->curdir[i]);
                    914: 
                    915:                                dup_cookie(&p->curdir[i], &dir);
                    916: 
                    917:                        }
                    918: 
                    919:                }
                    920: 
                    921:                release_cookie(&dir);
                    922: 
                    923:        }
                    924: 
                    925: 
                    926: 
                    927: /* free any file descriptors associated with shared text regions */
                    928: 
                    929:        for (stext = text_reg; stext; stext = stext->next) {
                    930: 
                    931:                f = stext->f;
                    932: 
                    933:                if (f->fc.dev == d) {
                    934: 
                    935:                        f->dev = NULL;
                    936: 
                    937:                        do_pclose(rootproc, f);
                    938: 
                    939:                        stext->f = 0;
1.1       root      940: 
                    941:                }
                    942: 
                    943:        }
                    944: 
                    945: }
                    946: 
                    947: 
                    948: 
                    949: /*
                    950: 
                    951:  * check for media change: if the drive has changed, call changedrv to
                    952: 
                    953:  * invalidate any open files and file handles associated with it, and
                    954: 
                    955:  * call the file system's media change routine.
                    956: 
1.1.1.4   root      957:  * returns: 0 if no change, 1 if change, negative number for error
1.1       root      958: 
                    959:  */
                    960: 
                    961: 
                    962: 
                    963: int
                    964: 
                    965: disk_changed(d)
                    966: 
                    967:        int d;
                    968: 
                    969: {
                    970: 
                    971:        short r;
                    972: 
                    973:        FILESYS *fs;
                    974: 
                    975:        static char tmpbuf[8192];
                    976: 
                    977: 
                    978: 
                    979: /* for now, only check BIOS devices */
                    980: 
                    981:        if (d < 0 || d >= NUM_DRIVES)
                    982: 
                    983:                return 0;
                    984: 
1.1.1.2   root      985: /* watch out for aliased drives */
                    986: 
                    987:        if (aliasdrv[d]) {
                    988: 
                    989:                d = aliasdrv[d] - 1;
                    990: 
                    991:                if (d < 0 || d >= NUM_DRIVES)
                    992: 
                    993:                        return 0;
                    994: 
                    995:        }
                    996: 
1.1       root      997: 
                    998: 
                    999: /* has the drive been initialized yet? If not, then initialize it and return
                   1000: 
                   1001:  * "no change"
                   1002: 
                   1003:  */
                   1004: 
1.1.1.2   root     1005:        fs = drives[d];
                   1006: 
                   1007:        if (!fs) {
1.1       root     1008: 
1.1.1.2   root     1009:                TRACE(("drive %c not yet initialized", d+'A'));
1.1       root     1010: 
                   1011:                changedrv(d);
                   1012: 
                   1013:                return 0;
                   1014: 
                   1015:        }
                   1016: 
                   1017: 
                   1018: 
                   1019: /* We have to do this stuff no matter what, because someone may have installed
                   1020: 
                   1021:  * vectors to force a media change...
                   1022: 
                   1023:  * PROBLEM: AHDI may get upset if the drive isn't valid.
                   1024: 
                   1025:  * SOLUTION: don't change the default PSEUDODRIVES setting!
                   1026: 
                   1027:  */
                   1028: 
1.1.1.4   root     1029: 
                   1030: 
                   1031: TRACE(("calling mediach(%d)",d));
                   1032: 
                   1033:        r = (int)mediach(d);
                   1034: 
                   1035: TRACE(("mediach(%d) == %d", d, r));
                   1036: 
                   1037: 
                   1038: 
1.1.1.5 ! root     1039:        if (r < 0) {
        !          1040: 
        !          1041: /* KLUDGE: some .XFS drivers don't install BIOS vectors, and so we'll
        !          1042: 
        !          1043:  * always get EUNDEV back from them. This isn't recommended (since there
        !          1044: 
        !          1045:  * are other programs than MiNT that may ask for BIOS functions from
        !          1046: 
        !          1047:  * any installed drives). This is a temporary work-around until those
        !          1048: 
        !          1049:  * .XFSes are changed to either install BIOS vectors or to use the
        !          1050: 
        !          1051:  * new U: Dcntl() calls to install themselves.
        !          1052: 
        !          1053:  * Note that EUNDEV must be tested for drives A-C, or else booting may
        !          1054: 
        !          1055:  * not work properly.
        !          1056: 
        !          1057:  */
        !          1058: 
        !          1059:                if (d > 2 && r == EUNDEV)
        !          1060: 
        !          1061:                        r = 2;          /* assume there may be a change */
        !          1062: 
        !          1063:                else
        !          1064: 
        !          1065:                        return r;
        !          1066: 
        !          1067:        }
1.1       root     1068: 
                   1069:        if (r == 1) {           /* drive _may_ have changed */
                   1070: 
                   1071:                r = rwabs(0, tmpbuf, 1, 0, d, 0L);      /* check the BIOS */
                   1072: 
                   1073:                if (r != E_CHNG) {                      /* nope, no change */
                   1074: 
1.1.1.4   root     1075:                        TRACE(("rwabs returned %d", r));
                   1076: 
                   1077:                        return (r < 0) ? r : 0;
1.1       root     1078: 
                   1079:                }
                   1080: 
                   1081:                r = 2;                  /* drive was definitely changed */
                   1082: 
                   1083:        }
                   1084: 
                   1085:        if (r == 2) {
                   1086: 
1.1.1.4   root     1087:                TRACE(("definite media change"));
                   1088: 
1.1       root     1089:                fs = drives[d];         /* get filesystem associated with drive */
                   1090: 
                   1091:                if ((*fs->dskchng)(d)) { /* does the fs agree that it changed? */
                   1092: 
1.1.1.2   root     1093:                        drives[d] = 0;
                   1094: 
1.1       root     1095:                        changedrv(d);   /* yes -- do the change */
                   1096: 
                   1097:                        return 1;
                   1098: 
                   1099:                }
                   1100: 
                   1101:        }
                   1102: 
                   1103:        return 0;
                   1104: 
                   1105: }
                   1106: 
                   1107: 
                   1108: 
                   1109: /*
                   1110: 
                   1111:  * routines for parsing path names
                   1112: 
                   1113:  */
                   1114: 
                   1115: 
                   1116: 
                   1117: #define DIRSEP(p) ((p) == '\\')
                   1118: 
                   1119: 
                   1120: 
                   1121: /*
                   1122: 
                   1123:  * relpath2cookie converts a TOS file name into a file cookie representing
                   1124: 
                   1125:  * the directory the file resides in, and a character string representing
                   1126: 
                   1127:  * the name of the file in that directory. The character string is
                   1128: 
                   1129:  * copied into the "lastname" array. If lastname is NULL, then the cookie
                   1130: 
                   1131:  * returned actually represents the file, instead of just the directory
                   1132: 
                   1133:  * the file is in.
                   1134: 
                   1135:  *
                   1136: 
                   1137:  * note that lastname, if non-null, should be big enough to contain all the
                   1138: 
                   1139:  * characters in "path", since if the file system doesn't want the kernel
                   1140: 
                   1141:  * to do path name parsing we may end up just copying path to lastname
                   1142: 
                   1143:  * and returning the current or root directory, as appropriate
                   1144: 
                   1145:  *
                   1146: 
                   1147:  * "relto" is the directory relative to which the search should start.
                   1148: 
                   1149:  * if you just want the current directory, use path2cookie instead.
                   1150: 
                   1151:  *
                   1152: 
                   1153:  */
                   1154: 
                   1155: 
                   1156: 
                   1157: #define MAX_LINKS 4
                   1158: 
                   1159: 
                   1160: 
                   1161: long
                   1162: 
                   1163: relpath2cookie(relto, path, lastname, res, depth)
                   1164: 
                   1165:        fcookie *relto;
                   1166: 
                   1167:        const char *path;
                   1168: 
                   1169:        char *lastname;
                   1170: 
                   1171:        fcookie *res;
                   1172: 
                   1173:        int depth;
                   1174: 
                   1175: {
                   1176: 
1.1.1.3   root     1177:        fcookie dir;
1.1       root     1178: 
                   1179:        int drv;
                   1180: 
                   1181:        int len;
                   1182: 
                   1183:        char c, *s;
                   1184: 
                   1185:        XATTR xattr;
                   1186: 
                   1187:        static char newpath[16] = "U:\\DEV\\";
                   1188: 
                   1189:        char temp2[PATH_MAX];
                   1190: 
                   1191:        char linkstuff[PATH_MAX];
                   1192: 
1.1.1.3   root     1193:        long r;
1.1       root     1194: 
                   1195: 
                   1196: 
                   1197: /* dolast: 0 == return a cookie for the directory the file is in
                   1198: 
                   1199:  *         1 == return a cookie for the file itself, don't follow links
                   1200: 
                   1201:  *        2 == return a cookie for whatever the file points at
                   1202: 
                   1203:  */
                   1204: 
                   1205:        int dolast = 0;
                   1206: 
                   1207:        int i = 0;
                   1208: 
                   1209: 
                   1210: 
                   1211:        if (!lastname) {
                   1212: 
                   1213:                dolast = 1;
                   1214: 
                   1215:                lastname = temp2;
                   1216: 
                   1217:        } else if (lastname == follow_links) {
                   1218: 
                   1219:                dolast = 2;
                   1220: 
                   1221:                lastname = temp2;
                   1222: 
                   1223:        }
                   1224: 
                   1225: 
                   1226: 
                   1227:        *lastname = 0;
                   1228: 
                   1229: 
                   1230: 
1.1.1.3   root     1231: PATH2COOKIE_DB(("relpath2cookie(%s, dolast=%d, depth=%d)", path, dolast, depth));
                   1232: 
                   1233: 
                   1234: 
                   1235:        if (depth > MAX_LINKS) {
                   1236: 
                   1237:                DEBUG(("Too many symbolic links"));
                   1238: 
                   1239:                return ELOOP;
                   1240: 
                   1241:        }
                   1242: 
1.1       root     1243: /* special cases: CON:, AUX:, etc. should be converted to U:\DEV\CON,
                   1244: 
                   1245:  * U:\DEV\AUX, etc.
                   1246: 
                   1247:  */
                   1248: 
                   1249:        if (strlen(path) == 4 && path[3] == ':') {
                   1250: 
                   1251:                strncpy(newpath+7, path, 3);
                   1252: 
                   1253:                path = newpath;
                   1254: 
                   1255:        }
                   1256: 
                   1257: 
                   1258: 
                   1259: /* first, check for a drive letter */
                   1260: 
                   1261: /* BUG: a '\' at the start of a symbolic link is relative to the current
                   1262: 
                   1263:  * drive of the process, not the drive the link is located on
                   1264: 
                   1265:  */
                   1266: 
1.1.1.3   root     1267:        if (path[1] == ':') {
1.1       root     1268: 
                   1269:                c = path[0];
                   1270: 
                   1271:                if (c >= 'a' && c <= 'z')
                   1272: 
                   1273:                        drv = c - 'a';
                   1274: 
                   1275:                else if (c >= 'A' && c <= 'Z')
                   1276: 
                   1277:                        drv = c - 'A';
                   1278: 
                   1279:                else
                   1280: 
                   1281:                        goto nodrive;
                   1282: 
                   1283:                path += 2;
                   1284: 
                   1285:                i = 1;          /* remember that we saw a drive letter */
                   1286: 
                   1287:        } else {
                   1288: 
                   1289: nodrive:
                   1290: 
                   1291:                drv = curproc->curdrv;
                   1292: 
                   1293:        }
                   1294: 
                   1295: 
                   1296: 
                   1297: /* see if the path is rooted from '\\' */
                   1298: 
                   1299:        if (DIRSEP(*path)) {
                   1300: 
                   1301:                while(DIRSEP(*path))path++;
                   1302: 
1.1.1.3   root     1303:                dup_cookie(&dir, &curproc->root[drv]);
1.1       root     1304: 
                   1305:        } else {
                   1306: 
                   1307:                if (i)  {       /* an explicit drive letter was given */
                   1308: 
1.1.1.3   root     1309:                        dup_cookie(&dir, &curproc->curdir[drv]);
1.1       root     1310: 
                   1311:                }
                   1312: 
                   1313:                else
                   1314: 
1.1.1.3   root     1315:                        dup_cookie(&dir, relto);
1.1       root     1316: 
                   1317:        }
                   1318: 
                   1319: 
                   1320: 
                   1321:        if (!dir.fs) {
                   1322: 
                   1323:                changedrv(dir.dev);
                   1324: 
1.1.1.3   root     1325:                dup_cookie(&dir, &curproc->root[drv]);
1.1       root     1326: 
                   1327:        }
                   1328: 
                   1329: 
                   1330: 
                   1331:        if (!dir.fs) {
                   1332: 
1.1.1.3   root     1333:                DEBUG(("path2cookie: no file system: returning EDRIVE"));
1.1       root     1334: 
                   1335:                return EDRIVE;
                   1336: 
                   1337:        }
                   1338: 
                   1339: 
                   1340: 
1.1.1.3   root     1341:        /* here's where we come when we've gone across a mount point */
1.1       root     1342: 
1.1.1.3   root     1343:        
1.1       root     1344: 
1.1.1.3   root     1345: restart_mount:
1.1       root     1346: 
                   1347: 
                   1348: 
1.1.1.3   root     1349:        if (!*path) {           /* nothing more to do */
1.1       root     1350: 
1.1.1.3   root     1351: PATH2COOKIE_DB(("relpath2cookie: no more path, returning 0"));
1.1       root     1352: 
1.1.1.3   root     1353:                *res = dir;
1.1       root     1354: 
1.1.1.3   root     1355:                return 0;
                   1356: 
                   1357:        }
1.1       root     1358: 
                   1359: 
                   1360: 
                   1361: /* see if there has been a disk change; if so, return E_CHNG.
                   1362: 
                   1363:  * path2cookie will restart the search automatically; other functions
                   1364: 
                   1365:  * that call relpath2cookie directly will have to fail gracefully
                   1366: 
                   1367:  */
                   1368: 
1.1.1.4   root     1369:        if ((r = disk_changed(dir.dev)) != 0) {
1.1       root     1370: 
1.1.1.3   root     1371:                release_cookie(&dir);
                   1372: 
1.1.1.4   root     1373:                if (r > 0) r = E_CHNG;
                   1374: 
                   1375: PATH2COOKIE_DB(("relpath2cookie: returning %d", r));
1.1.1.3   root     1376: 
1.1.1.4   root     1377:                return r;
1.1       root     1378: 
                   1379:        }
                   1380: 
                   1381: 
                   1382: 
1.1.1.3   root     1383: 
                   1384: 
1.1       root     1385:        if (dir.fs->fsflags & FS_KNOPARSE) {
                   1386: 
                   1387:                if (!dolast) {
                   1388: 
1.1.1.3   root     1389: PATH2COOKIE_DB(("fs is a KNOPARSE, nothing to do"));
                   1390: 
1.1       root     1391:                        strncpy(lastname, path, PATH_MAX-1);
                   1392: 
                   1393:                        lastname[PATH_MAX - 1] = 0;
                   1394: 
                   1395:                        r = 0;
                   1396: 
1.1.1.3   root     1397:                        *res = dir;
                   1398: 
1.1       root     1399:                } else {
                   1400: 
1.1.1.3   root     1401: PATH2COOKIE_DB(("fs is a KNOPARSE, calling lookup"));
                   1402: 
1.1       root     1403:                        r = (*dir.fs->lookup)(&dir, path, res);
                   1404: 
1.1.1.3   root     1405:                        if (r == EMOUNT) {      /* hmmm... a ".." at a mount point, maybe */
1.1       root     1406: 
1.1.1.3   root     1407:                                fcookie mounteddir;
1.1       root     1408: 
1.1.1.3   root     1409:                                r = (*dir.fs->root)(dir.dev, &mounteddir);
1.1       root     1410: 
1.1.1.3   root     1411:                                if (r == 0 && drv == UNIDRV) {
1.1       root     1412: 
1.1.1.3   root     1413:                                        if (dir.fs == mounteddir.fs &&
1.1       root     1414: 
1.1.1.3   root     1415:                                            dir.index == mounteddir.index &&
1.1       root     1416: 
1.1.1.3   root     1417:                                            dir.dev == mounteddir.dev) {
1.1       root     1418: 
1.1.1.3   root     1419:                                                release_cookie(&dir);
1.1       root     1420: 
1.1.1.3   root     1421:                                                release_cookie(&mounteddir);
1.1       root     1422: 
1.1.1.3   root     1423:                                                dup_cookie(&dir, &curproc->root[UNIDRV]);
1.1       root     1424: 
1.1.1.3   root     1425:                                                TRACE(("path2cookie: restarting from mount point"));
1.1       root     1426: 
1.1.1.3   root     1427:                                                goto restart_mount;
1.1       root     1428: 
1.1.1.3   root     1429:                                        }
1.1       root     1430: 
1.1.1.3   root     1431:                                } else {
1.1       root     1432: 
1.1.1.3   root     1433:                                        if (r == 0)
1.1       root     1434: 
1.1.1.3   root     1435:                                                release_cookie(&mounteddir);
1.1       root     1436: 
1.1.1.3   root     1437:                                        r = 0;
1.1       root     1438: 
1.1.1.3   root     1439:                                }
1.1       root     1440: 
1.1.1.3   root     1441:                        }
1.1       root     1442: 
1.1.1.3   root     1443:                        release_cookie(&dir);
1.1       root     1444: 
1.1.1.3   root     1445:                }
1.1       root     1446: 
1.1.1.3   root     1447:                PATH2COOKIE_DB(("relpath2cookie: returning %ld", r));
1.1       root     1448: 
1.1.1.3   root     1449:                return r;
1.1       root     1450: 
1.1.1.3   root     1451:        }
1.1       root     1452: 
                   1453: 
                   1454: 
                   1455: 
                   1456: 
1.1.1.3   root     1457: /* parse all but (possibly) the last component of the path name */
1.1       root     1458: 
1.1.1.3   root     1459: /* rules here: at the top of the loop, &dir is the cookie of
1.1       root     1460: 
1.1.1.3   root     1461:  * the directory we're in now, xattr is its attributes, and res is unset
1.1       root     1462: 
1.1.1.3   root     1463:  * at the end of the loop, &dir is unset, and either r is nonzero
1.1       root     1464: 
1.1.1.3   root     1465:  * (to indicate an error) or res is set to the final result
1.1       root     1466: 
1.1.1.3   root     1467:  */
1.1       root     1468: 
1.1.1.3   root     1469:        r = (dir.fs->getxattr)(&dir, &xattr);
1.1       root     1470: 
1.1.1.3   root     1471:        if (r) {
1.1       root     1472: 
1.1.1.3   root     1473:                DEBUG(("couldn't get directory attributes"));
1.1       root     1474: 
1.1.1.3   root     1475:                release_cookie(&dir);
1.1       root     1476: 
1.1.1.3   root     1477:                return EINTRN;
1.1       root     1478: 
1.1.1.3   root     1479:        }
1.1       root     1480: 
                   1481: 
                   1482: 
1.1.1.3   root     1483:        while (*path) {
1.1       root     1484: 
                   1485: 
                   1486: 
1.1.1.3   root     1487:        /* now we must have a directory, since there are more things in the path */
1.1       root     1488: 
1.1.1.3   root     1489:                if ((xattr.mode & S_IFMT) != S_IFDIR) {
1.1       root     1490: 
1.1.1.3   root     1491: PATH2COOKIE_DB(("relpath2cookie: not a directory, returning EPTHNF"));
1.1       root     1492: 
1.1.1.3   root     1493:                        release_cookie(&dir);
                   1494: 
                   1495:                        r = EPTHNF;
1.1       root     1496: 
                   1497:                        break;
                   1498: 
                   1499:                }
                   1500: 
1.1.1.3   root     1501:        /* we must also have search permission for the directory */
1.1       root     1502: 
1.1.1.3   root     1503:                if (denyaccess(&xattr, S_IXOTH)) {
1.1       root     1504: 
1.1.1.3   root     1505:                        DEBUG(("search permission in directory denied"));
1.1       root     1506: 
1.1.1.3   root     1507:                        release_cookie(&dir);
1.1       root     1508: 
1.1.1.3   root     1509:                        r = EPTHNF;
1.1       root     1510: 
1.1.1.3   root     1511:                        break;
1.1       root     1512: 
1.1.1.3   root     1513:                }
1.1       root     1514: 
                   1515: 
                   1516: 
1.1.1.3   root     1517:        /* if there's nothing left in the path, we can break here */
1.1       root     1518: 
1.1.1.3   root     1519:                if (!*path) {
1.1       root     1520: 
1.1.1.3   root     1521: PATH2COOKIE_DB(("relpath2cookie: no more path, breaking (1)"));
1.1       root     1522: 
1.1.1.3   root     1523:                        *res = dir;
1.1       root     1524: 
1.1.1.3   root     1525:                        break;
1.1       root     1526: 
1.1.1.3   root     1527:                }
1.1       root     1528: 
                   1529:        /* next, peel off the next name in the path */
                   1530: 
                   1531:                len = 0;
                   1532: 
                   1533:                s = lastname;
                   1534: 
                   1535:                c = *path;
                   1536: 
                   1537:                while (c && !DIRSEP(c)) {
                   1538: 
                   1539:                        if (len++ < PATH_MAX)
                   1540: 
                   1541:                                *s++ = c;
                   1542: 
                   1543:                        c = *++path;
                   1544: 
                   1545:                }
                   1546: 
                   1547:                *s = 0;
                   1548: 
                   1549: 
                   1550: 
1.1.1.3   root     1551:        /* if there are no more names in the path, and we don't want
                   1552: 
                   1553:         * to actually look up the last name, then we're done
                   1554: 
                   1555:         */
                   1556: 
                   1557:                if (dolast == 0 && !*path) {
1.1       root     1558: 
1.1.1.3   root     1559:                        *res = dir;
1.1       root     1560: 
1.1.1.3   root     1561: PATH2COOKIE_DB(("relpath2cookie: no more path, breaking (2)"));
1.1       root     1562: 
                   1563:                        break;
                   1564: 
1.1.1.3   root     1565:                }
                   1566: 
                   1567: 
                   1568: 
                   1569: 
                   1570: 
                   1571:        /* 
                   1572: 
                   1573:         * skip trailing slashes
                   1574: 
                   1575:         */
                   1576: 
                   1577:                while (DIRSEP(*path)) path++;
                   1578: 
                   1579: 
                   1580: 
                   1581: PATH2COOKIE_DB(("relpath2cookie: looking up [%s]", lastname));
                   1582: 
1.1       root     1583: 
                   1584: 
                   1585:                r = (*dir.fs->lookup)(&dir, lastname, res);
                   1586: 
1.1.1.3   root     1587:                if (r == EMOUNT) {
1.1       root     1588: 
1.1.1.3   root     1589:                        fcookie mounteddir;
1.1       root     1590: 
1.1.1.3   root     1591:                        r = (*dir.fs->root)(dir.dev, &mounteddir);
                   1592: 
                   1593:                        if (r == 0 && drv == UNIDRV) {
                   1594: 
                   1595:                                if (samefile(&dir, &mounteddir)) {
                   1596: 
                   1597:                                        release_cookie(&dir);
                   1598: 
                   1599:                                        release_cookie(&mounteddir);
                   1600: 
                   1601:                                        dup_cookie(&dir, &curproc->root[UNIDRV]);
                   1602: 
                   1603:                                        TRACE(("path2cookie: restarting from mount point"));
                   1604: 
                   1605:                                        goto restart_mount;
                   1606: 
                   1607:                                } else if (r == 0) {
                   1608: 
                   1609:                                        r = EINTRN;
                   1610: 
                   1611:                                        release_cookie(&mounteddir);
                   1612: 
                   1613:                                        release_cookie(&dir);
                   1614: 
                   1615:                                        break;
                   1616: 
                   1617:                                }
                   1618: 
                   1619:                        } else if (r == 0) {
                   1620: 
                   1621:                                release_cookie(&mounteddir);
                   1622: 
                   1623:                        } else {
                   1624: 
                   1625:                                release_cookie(&dir);
                   1626: 
                   1627:                                break;
                   1628: 
                   1629:                        }
                   1630: 
                   1631:                } else if (r) {
                   1632: 
1.1.1.5 ! root     1633:                        if (r == EFILNF && *path) {
        !          1634: 
        !          1635:                        /* the "file" we didn't find was treated as a directory */
        !          1636: 
        !          1637:                                r = EPTHNF;
        !          1638: 
        !          1639:                        }
        !          1640: 
1.1.1.3   root     1641:                        release_cookie(&dir);
1.1       root     1642: 
                   1643:                        break;
                   1644: 
                   1645:                }
                   1646: 
                   1647: 
                   1648: 
1.1.1.3   root     1649:        /* check for a symbolic link */
                   1650: 
                   1651:                r = (res->fs->getxattr)(res, &xattr);
                   1652: 
                   1653:                if (r != 0) {
                   1654: 
                   1655:                        DEBUG(("path2cookie: couldn't get file attributes"));
                   1656: 
                   1657:                        release_cookie(&dir);
                   1658: 
                   1659:                        release_cookie(res);
                   1660: 
                   1661:                        break;
                   1662: 
                   1663:                }
                   1664: 
1.1       root     1665: 
                   1666: 
1.1.1.3   root     1667:        /* if the file is a link, and we're following links, follow it */
1.1       root     1668: 
1.1.1.3   root     1669:                if ( (xattr.mode & S_IFMT) == S_IFLNK && (*path || dolast > 1)) {
1.1       root     1670: 
1.1.1.3   root     1671:                        r = (res->fs->readlink)(res, linkstuff, PATH_MAX);
1.1       root     1672: 
1.1.1.3   root     1673:                        release_cookie(res);
1.1       root     1674: 
1.1.1.3   root     1675:                        if (r) {
1.1       root     1676: 
1.1.1.3   root     1677:                                DEBUG(("error reading symbolic link"));
1.1       root     1678: 
1.1.1.3   root     1679:                                release_cookie(&dir);
1.1       root     1680: 
1.1.1.3   root     1681:                                break;
1.1       root     1682: 
1.1.1.3   root     1683:                        }
1.1       root     1684: 
1.1.1.3   root     1685:                        r = relpath2cookie(&dir, linkstuff, follow_links, res,
1.1       root     1686: 
1.1.1.3   root     1687:                                                depth+1);
1.1       root     1688: 
1.1.1.3   root     1689:                        release_cookie(&dir);
                   1690: 
                   1691:                        if (r) {
                   1692: 
                   1693:                                DEBUG(("error following symbolic link"));
                   1694: 
                   1695:                                break;
1.1       root     1696: 
                   1697:                        }
                   1698: 
1.1.1.3   root     1699:                        dir = *res;
1.1       root     1700: 
1.1.1.3   root     1701:                        (void)(res->fs->getxattr)(res, &xattr);
                   1702: 
                   1703:                } else {
                   1704: 
                   1705:                        release_cookie(&dir);
                   1706: 
                   1707:                        dir = *res;
                   1708: 
                   1709:                }
1.1       root     1710: 
                   1711:        }
                   1712: 
                   1713: 
                   1714: 
1.1.1.3   root     1715:        PATH2COOKIE_DB(("relpath2cookie: returning %ld", r));
                   1716: 
1.1       root     1717:        return r;
                   1718: 
                   1719: }
                   1720: 
                   1721: 
                   1722: 
                   1723: #define MAX_TRYS 8
                   1724: 
                   1725: 
                   1726: 
                   1727: long
                   1728: 
                   1729: path2cookie(path, lastname, res)
                   1730: 
                   1731:        const char *path;
                   1732: 
                   1733:        char *lastname;
                   1734: 
                   1735:        fcookie *res;
                   1736: 
                   1737: {
                   1738: 
                   1739:        fcookie *dir;
                   1740: 
                   1741:        long r;
                   1742: 
                   1743: /* AHDI sometimes will keep insisting that a media change occured;
                   1744: 
1.1.1.3   root     1745:  * we limit the number of retrys to avoid hanging the system
1.1       root     1746: 
                   1747:  */
                   1748: 
                   1749:        int trycnt = 0;
                   1750: 
                   1751: 
                   1752: 
                   1753:        dir = &curproc->curdir[curproc->curdrv];
                   1754: 
                   1755: 
                   1756: 
                   1757:        do {
                   1758: 
1.1.1.3   root     1759:                r = relpath2cookie(dir, path, lastname, res, 0);
1.1       root     1760: 
                   1761:                if (r == E_CHNG)
                   1762: 
1.1.1.2   root     1763:                        DEBUG(("path2cookie: restarting due to media change"));
1.1       root     1764: 
                   1765:        } while (r == E_CHNG && trycnt++ < MAX_TRYS);
                   1766: 
                   1767: 
                   1768: 
                   1769:        return r;
                   1770: 
                   1771: }
                   1772: 
                   1773: 
                   1774: 
                   1775: /*
                   1776: 
1.1.1.3   root     1777:  * release_cookie: tell the file system owner that a cookie is no
                   1778: 
                   1779:  * longer in use by the kernel
                   1780: 
                   1781:  */
                   1782: 
                   1783: void
                   1784: 
                   1785: release_cookie(fc)
                   1786: 
                   1787:        fcookie *fc;
                   1788: 
                   1789: {
                   1790: 
                   1791:        FILESYS *fs;
                   1792: 
                   1793: 
                   1794: 
                   1795:        if (fc) {
                   1796: 
                   1797:                fs = fc->fs;
                   1798: 
                   1799:                if (fs && fs->release) {
                   1800: 
                   1801:                        (void)(*fs->release)(fc);
                   1802: 
                   1803:                }
                   1804: 
                   1805:        }
                   1806: 
                   1807: }
                   1808: 
                   1809: 
                   1810: 
                   1811: /*
                   1812: 
                   1813:  * Make a new cookie (newc) which is a duplicate of the old cookie
                   1814: 
                   1815:  * (oldc). This may be something the file system is interested in,
                   1816: 
                   1817:  * so we give it a chance to do the duplication; if it doesn't
                   1818: 
                   1819:  * want to, we just copy.
                   1820: 
                   1821:  */
                   1822: 
                   1823: 
                   1824: 
                   1825: void
                   1826: 
                   1827: dup_cookie(newc, oldc)
                   1828: 
                   1829:        fcookie *newc, *oldc;
                   1830: 
                   1831: {
                   1832: 
                   1833:        FILESYS *fs = oldc->fs;
                   1834: 
                   1835: 
                   1836: 
                   1837:        if (fs && fs->release && fs->dupcookie) {
                   1838: 
                   1839:                (void)(*fs->dupcookie)(newc, oldc);
                   1840: 
                   1841:        } else {
                   1842: 
                   1843:                *newc = *oldc;
                   1844: 
                   1845:        }
                   1846: 
                   1847: }
                   1848: 
                   1849: 
                   1850: 
                   1851: /*
                   1852: 
1.1       root     1853:  * new_fileptr, dispose_fileptr: allocate (deallocate) a file pointer
                   1854: 
                   1855:  */
                   1856: 
                   1857: 
                   1858: 
                   1859: FILEPTR *
                   1860: 
                   1861: new_fileptr()
                   1862: 
                   1863: {
                   1864: 
                   1865:        FILEPTR *f;
                   1866: 
                   1867: 
                   1868: 
1.1.1.2   root     1869:        if ((f = flist) != 0) {
1.1       root     1870: 
                   1871:                flist = f->next;
                   1872: 
                   1873:                f->next = 0;
                   1874: 
                   1875:                return f;
                   1876: 
                   1877:        }
                   1878: 
                   1879:        f = kmalloc(SIZEOF(FILEPTR));
                   1880: 
                   1881:        if (!f) {
                   1882: 
                   1883:                FATAL("new_fileptr: out of memory");
                   1884: 
                   1885:        }
                   1886: 
                   1887:        else {
                   1888: 
                   1889:                f->next = 0;
                   1890: 
                   1891:        }
                   1892: 
                   1893:        return f;
                   1894: 
                   1895: }
                   1896: 
                   1897: 
                   1898: 
                   1899: void
                   1900: 
                   1901: dispose_fileptr(f)
                   1902: 
                   1903:        FILEPTR *f;
                   1904: 
                   1905: {
                   1906: 
                   1907:        if (f->links != 0) {
                   1908: 
                   1909:                FATAL("dispose_fileptr: f->links == %d", f->links);
                   1910: 
                   1911:        }
                   1912: 
                   1913:        f->next = flist;
                   1914: 
                   1915:        flist = f;
                   1916: 
                   1917: }
                   1918: 
                   1919: 
                   1920: 
                   1921: /*
                   1922: 
                   1923:  * denyshare(list, f): "list" points at the first FILEPTR in a
                   1924: 
                   1925:  * chained list of open FILEPTRS referring to the same file;
                   1926: 
                   1927:  * f is a newly opened FILEPTR. Every FILEPTR in the given list is
                   1928: 
                   1929:  * checked to see if its "open" mode (in list->flags) is compatible with
                   1930: 
                   1931:  * the open mode in f->flags. If not (for example, if f was opened with
                   1932: 
                   1933:  * a "read" mode and some other file has the O_DENYREAD share mode),
                   1934: 
                   1935:  * then 1 is returned. If all the open FILEPTRs in the list are
                   1936: 
                   1937:  * compatible with f, then 0 is returned.
                   1938: 
                   1939:  * This is not as complicated as it sounds. In practice, just keep a
                   1940: 
                   1941:  * list of open FILEPTRs attached to each file, and put something like
                   1942: 
                   1943:  *     if (denyshare(thisfile->openfileptrlist, newfileptr))
                   1944: 
                   1945:  *             return EACCDN;
                   1946: 
                   1947:  * in the device open routine.
                   1948: 
                   1949:  */
                   1950: 
                   1951: 
                   1952: 
1.1.1.2   root     1953: int ARGS_ON_STACK 
1.1       root     1954: 
                   1955: denyshare(list, f)
                   1956: 
                   1957:        FILEPTR *list, *f;
                   1958: 
                   1959: {
                   1960: 
1.1.1.3   root     1961:        int newrm, newsm;       /* new read and sharing mode */
1.1       root     1962: 
1.1.1.3   root     1963:        int oldrm, oldsm;       /* read and sharing mode of already opened file */
1.1       root     1964: 
                   1965:        int i;
                   1966: 
                   1967: 
                   1968: 
                   1969:        newrm = f->flags & O_RWMODE;
                   1970: 
                   1971:        newsm = f->flags & O_SHMODE;
                   1972: 
                   1973: 
                   1974: 
1.1.1.3   root     1975: /*
                   1976: 
                   1977:  * O_EXEC gets treated the same as O_RDONLY for our purposes
                   1978: 
                   1979:  */
                   1980: 
                   1981:        if (newrm == O_EXEC) newrm = O_RDONLY;
                   1982: 
                   1983: 
                   1984: 
                   1985: /* New meaning for O_COMPAT: deny write access to all _other_
                   1986: 
                   1987:  * processes.
                   1988: 
                   1989:  */
                   1990: 
                   1991: 
                   1992: 
1.1       root     1993:        for ( ; list; list = list->next) {
                   1994: 
                   1995:                oldrm = list->flags & O_RWMODE;
                   1996: 
1.1.1.3   root     1997:                if (oldrm == O_EXEC) oldrm = O_RDONLY;
                   1998: 
1.1       root     1999:                oldsm = list->flags & O_SHMODE;
                   2000: 
                   2001:                if (oldsm == O_DENYW || oldsm == O_DENYRW) {
                   2002: 
1.1.1.3   root     2003:                        if (newrm != O_RDONLY) {
1.1       root     2004: 
1.1.1.2   root     2005:                                DEBUG(("write access denied"));
1.1       root     2006: 
                   2007:                                return 1;
                   2008: 
                   2009:                        }
                   2010: 
                   2011:                }
                   2012: 
                   2013:                if (oldsm == O_DENYR || oldsm == O_DENYRW) {
                   2014: 
                   2015:                        if (newrm != O_WRONLY) {
                   2016: 
1.1.1.2   root     2017:                                DEBUG(("read access denied"));
1.1       root     2018: 
                   2019:                                return 1;
                   2020: 
                   2021:                        }
                   2022: 
                   2023:                }
                   2024: 
                   2025:                if (newsm == O_DENYW || newsm == O_DENYRW) {
                   2026: 
1.1.1.3   root     2027:                        if (oldrm != O_RDONLY) {
1.1       root     2028: 
1.1.1.2   root     2029:                                DEBUG(("couldn't deny writes"));
1.1       root     2030: 
                   2031:                                return 1;
                   2032: 
                   2033:                        }
                   2034: 
                   2035:                }
                   2036: 
                   2037:                if (newsm == O_DENYR || newsm == O_DENYRW) {
                   2038: 
                   2039:                        if (oldrm != O_WRONLY) {
                   2040: 
1.1.1.2   root     2041:                                DEBUG(("couldn't deny reads"));
1.1       root     2042: 
                   2043:                                return 1;
                   2044: 
                   2045:                        }
                   2046: 
                   2047:                }
                   2048: 
                   2049: /* If either sm == O_COMPAT, then we check to make sure
                   2050: 
                   2051:    that the file pointers are owned by the same process (O_COMPAT means
                   2052: 
1.1.1.3   root     2053:    "deny writes to any other processes"). This isn't quite the same
1.1       root     2054: 
1.1.1.3   root     2055:    as the Atari spec, which says O_COMPAT means "deny access to other
1.1       root     2056: 
1.1.1.3   root     2057:    processes." We should fix the spec.
1.1       root     2058: 
1.1.1.3   root     2059:  */
1.1       root     2060: 
1.1.1.3   root     2061:                if ((newsm == O_COMPAT && newrm != O_RDONLY && oldrm != O_RDONLY) ||
1.1       root     2062: 
1.1.1.3   root     2063:                    (oldsm == O_COMPAT && newrm != O_RDONLY)) {
1.1       root     2064: 
                   2065:                        for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
                   2066: 
                   2067:                                if (curproc->handle[i] == list)
                   2068: 
                   2069:                                        goto found;
                   2070: 
                   2071:                        }
                   2072: 
                   2073:                /* old file pointer is not open by this process */
                   2074: 
1.1.1.3   root     2075:                        DEBUG(("O_COMPAT file was opened for writing by another process"));
1.1       root     2076: 
                   2077:                        return 1;
                   2078: 
                   2079:                found:
                   2080: 
                   2081:                        ;       /* everything is OK */
                   2082: 
                   2083:                }
                   2084: 
                   2085:        }
                   2086: 
                   2087:        return 0;
                   2088: 
                   2089: }
                   2090: 
                   2091: 
                   2092: 
                   2093: /*
                   2094: 
                   2095:  * denyaccess(XATTR *xattr, unsigned perm): checks to see if the access
                   2096: 
                   2097:  * specified by perm (which must be some combination of S_IROTH, S_IWOTH,
                   2098: 
                   2099:  * and S_IXOTH) should be granted to the current process
                   2100: 
                   2101:  * on a file with the given extended attributes. Returns 0 if access
                   2102: 
                   2103:  * by the current process is OK, 1 if not.
                   2104: 
                   2105:  */
                   2106: 
                   2107: 
                   2108: 
                   2109: int
                   2110: 
                   2111: denyaccess(xattr, perm)
                   2112: 
                   2113:        XATTR *xattr;
                   2114: 
                   2115:        unsigned perm;
                   2116: 
                   2117: {
                   2118: 
                   2119:        unsigned mode;
                   2120: 
                   2121: 
                   2122: 
                   2123: /* the super-user can do anything! */
                   2124: 
                   2125:        if (curproc->euid == 0)
                   2126: 
                   2127:                return 0;
                   2128: 
                   2129: 
                   2130: 
                   2131:        mode = xattr->mode;
                   2132: 
                   2133:        if (curproc->euid == xattr->uid)
                   2134: 
                   2135:                perm = perm << 6;
                   2136: 
                   2137:        else if (curproc->egid == xattr->gid)
                   2138: 
                   2139:                perm = perm << 3;
                   2140: 
                   2141:        if ((mode & perm) != perm) return 1;    /* access denied */
                   2142: 
                   2143:        return 0;
                   2144: 
                   2145: }
                   2146: 
                   2147: 
                   2148: 
                   2149: /*
                   2150: 
                   2151:  * Checks a lock against a list of locks to see if there is a conflict.
                   2152: 
                   2153:  * This is a utility to be used by file systems, somewhat like denyshare
                   2154: 
                   2155:  * above. Returns 0 if there is no conflict, or a pointer to the
                   2156: 
                   2157:  * conflicting LOCK structure if there is.
                   2158: 
                   2159:  *
                   2160: 
                   2161:  * Conflicts occur for overlapping locks if the process id's are
                   2162: 
                   2163:  * different and if at least one of the locks is a write lock.
                   2164: 
                   2165:  *
                   2166: 
                   2167:  * NOTE: we assume before being called that the locks have been converted
                   2168: 
                   2169:  * so that l_start is absolute. not relative to the current position or
                   2170: 
                   2171:  * end of file.
                   2172: 
                   2173:  */
                   2174: 
                   2175: 
                   2176: 
1.1.1.2   root     2177: LOCK * ARGS_ON_STACK 
1.1       root     2178: 
                   2179: denylock(list, lck)
                   2180: 
                   2181:        LOCK *list, *lck;
                   2182: 
                   2183: {
                   2184: 
                   2185:        LOCK *t;
                   2186: 
                   2187:        unsigned long tstart, tend;
                   2188: 
                   2189:        unsigned long lstart, lend;
                   2190: 
                   2191:        int pid = curproc->pid;
                   2192: 
                   2193:        int ltype;
                   2194: 
                   2195: 
                   2196: 
                   2197:        ltype = lck->l.l_type;
                   2198: 
                   2199:        lstart = lck->l.l_start;
                   2200: 
                   2201: 
                   2202: 
                   2203:        if (lck->l.l_len == 0)
                   2204: 
1.1.1.2   root     2205:                lend = 0xffffffffL;
1.1       root     2206: 
                   2207:        else
                   2208: 
1.1.1.3   root     2209:                lend = lstart + lck->l.l_len - 1;
1.1       root     2210: 
                   2211: 
                   2212: 
                   2213:        for (t = list; t; t = t->next) {
                   2214: 
                   2215:                tstart = t->l.l_start;
                   2216: 
                   2217:                if (t->l.l_len == 0)
                   2218: 
1.1.1.2   root     2219:                        tend = 0xffffffffL;
1.1       root     2220: 
                   2221:                else
                   2222: 
1.1.1.3   root     2223:                        tend = tstart + t->l.l_len - 1;
1.1       root     2224: 
                   2225: 
                   2226: 
                   2227:        /* look for overlapping locks */
                   2228: 
                   2229:                if (tstart <= lstart && tend >= lstart && t->l.l_pid != pid &&
                   2230: 
                   2231:                    (ltype == F_WRLCK || t->l.l_type == F_WRLCK))
                   2232: 
                   2233:                        break;
                   2234: 
                   2235:                if (lstart <= tstart && lend >= tstart && t->l.l_pid != pid &&
                   2236: 
                   2237:                    (ltype == F_WRLCK || t->l.l_type == F_WRLCK))
                   2238: 
                   2239:                        break;
                   2240: 
                   2241:        }
                   2242: 
                   2243:        return t;
                   2244: 
                   2245: }
                   2246: 
                   2247: 
                   2248: 
                   2249: /*
                   2250: 
                   2251:  * check to see that a file is a directory, and that write permission
                   2252: 
                   2253:  * is granted; return an error code, or 0 if everything is ok.
                   2254: 
                   2255:  */
                   2256: 
                   2257: long
                   2258: 
                   2259: dir_access(dir, perm)
                   2260: 
                   2261:        fcookie *dir;
                   2262: 
                   2263:        unsigned perm;
                   2264: 
                   2265: {
                   2266: 
                   2267:        XATTR xattr;
                   2268: 
                   2269:        long r;
                   2270: 
                   2271: 
                   2272: 
                   2273:        r = (*dir->fs->getxattr)(dir, &xattr);
                   2274: 
1.1.1.3   root     2275:        if (r) {
                   2276: 
                   2277:                DEBUG(("dir_access: file system returned %ld", r));
                   2278: 
                   2279:                return r;
                   2280: 
                   2281:        }
1.1       root     2282: 
                   2283:        if ( (xattr.mode & S_IFMT) != S_IFDIR ) {
                   2284: 
1.1.1.2   root     2285:                DEBUG(("file is not a directory"));
1.1       root     2286: 
                   2287:                return EPTHNF;
                   2288: 
                   2289:        }
                   2290: 
                   2291:        if (denyaccess(&xattr, perm)) {
                   2292: 
1.1.1.2   root     2293:                DEBUG(("no permission for directory"));
1.1       root     2294: 
                   2295:                return EACCDN;
                   2296: 
                   2297:        }
                   2298: 
                   2299:        return 0;
                   2300: 
                   2301: }
                   2302: 
                   2303: 
                   2304: 
                   2305: /*
                   2306: 
                   2307:  * returns 1 if the given name contains a wildcard character 
                   2308: 
                   2309:  */
                   2310: 
                   2311: 
                   2312: 
                   2313: int
                   2314: 
                   2315: has_wild(name)
                   2316: 
                   2317:        const char *name;
                   2318: 
                   2319: {
                   2320: 
                   2321:        char c;
                   2322: 
                   2323: 
                   2324: 
1.1.1.2   root     2325:        while ((c = *name++) != 0) {
1.1       root     2326: 
                   2327:                if (c == '*' || c == '?') return 1;
                   2328: 
                   2329:        }
                   2330: 
                   2331:        return 0;
                   2332: 
                   2333: }
                   2334: 
                   2335: 
                   2336: 
                   2337: /*
                   2338: 
                   2339:  * void copy8_3(dest, src): convert a file name (src) into DOS 8.3 format
                   2340: 
                   2341:  * (in dest). Note the following things:
                   2342: 
                   2343:  * if a field has less than the required number of characters, it is
                   2344: 
                   2345:  * padded with blanks
                   2346: 
                   2347:  * a '*' means to pad the rest of the field with '?' characters
                   2348: 
                   2349:  * special things to watch for:
                   2350: 
                   2351:  *     "." and ".." are more or less left alone
                   2352: 
                   2353:  *     "*.*" is recognized as a special pattern, for which dest is set
                   2354: 
                   2355:  *     to just "*"
                   2356: 
                   2357:  * Long names are truncated. Any extensions after the first one are
                   2358: 
                   2359:  * ignored, i.e. foo.bar.c -> foo.bar, foo.c.bar->foo.c.
                   2360: 
                   2361:  */
                   2362: 
                   2363: 
                   2364: 
                   2365: void
                   2366: 
                   2367: copy8_3(dest, src)
                   2368: 
                   2369:        char *dest;
                   2370: 
                   2371:        const char *src;
                   2372: 
                   2373: {
                   2374: 
                   2375:        char fill = ' ', c;
                   2376: 
                   2377:        int i;
                   2378: 
                   2379: 
                   2380: 
                   2381:        if (src[0] == '.') {
                   2382: 
                   2383:                if (src[1] == 0) {
                   2384: 
                   2385:                        strcpy(dest, ".       .   ");
                   2386: 
                   2387:                        return;
                   2388: 
                   2389:                }
                   2390: 
                   2391:                if (src[1] == '.' && src[2] == 0) {
                   2392: 
                   2393:                        strcpy(dest, "..      .   ");
                   2394: 
                   2395:                        return;
                   2396: 
                   2397:                }
                   2398: 
                   2399:        }
                   2400: 
                   2401:        if (src[0] == '*' && src[1] == '.' && src[2] == '*' && src[3] == 0) {
                   2402: 
                   2403:                dest[0] = '*';
                   2404: 
                   2405:                dest[1] = 0;
                   2406: 
                   2407:                return;
                   2408: 
                   2409:        }
                   2410: 
                   2411: 
                   2412: 
                   2413:        for (i = 0; i < 8; i++) {
                   2414: 
                   2415:                c = *src++;
                   2416: 
                   2417:                if (!c || c == '.') break;
                   2418: 
                   2419:                if (c == '*') {
                   2420: 
                   2421:                        fill = c = '?';
                   2422: 
                   2423:                }
                   2424: 
                   2425:                *dest++ = toupper(c);
                   2426: 
                   2427:        }
                   2428: 
                   2429:        while (i++ < 8) {
                   2430: 
                   2431:                *dest++ = fill;
                   2432: 
                   2433:        }
                   2434: 
                   2435:        *dest++ = '.';
                   2436: 
                   2437:        i = 0;
                   2438: 
                   2439:        fill = ' ';
                   2440: 
                   2441:        while (c && c != '.')
                   2442: 
                   2443:                c = *src++;
                   2444: 
                   2445: 
                   2446: 
                   2447:        if (c) {
                   2448: 
                   2449:                for( ;i < 3; i++) {
                   2450: 
                   2451:                        c = *src++;
                   2452: 
                   2453:                        if (!c || c == '.') break;
                   2454: 
                   2455:                        if (c == '*')
                   2456: 
                   2457:                                c = fill = '?';
                   2458: 
                   2459:                        *dest++ = toupper(c);
                   2460: 
                   2461:                }
                   2462: 
                   2463:        }
                   2464: 
                   2465:        while (i++ < 3)
                   2466: 
                   2467:                *dest++ = fill;
                   2468: 
1.1.1.2   root     2469:        *dest = 0;
1.1       root     2470: 
                   2471: }
                   2472: 
                   2473: 
                   2474: 
                   2475: /*
                   2476: 
                   2477:  * int pat_match(name, patrn): returns 1 if "name" matches the template in
                   2478: 
                   2479:  * "patrn", 0 if not. "patrn" is assumed to have been expanded in 8.3
                   2480: 
                   2481:  * format by copy8_3; "name" need not be. Any '?' characters in patrn
                   2482: 
                   2483:  * will match any character in name. Note that if "patrn" has a '*' as
                   2484: 
                   2485:  * the first character, it will always match; this will happen only if
                   2486: 
                   2487:  * the original pattern (before copy8_3 was applied) was "*.*".
                   2488: 
                   2489:  *
                   2490: 
                   2491:  * BUGS: acts a lot like the silly TOS pattern matcher.
                   2492: 
                   2493:  */
                   2494: 
                   2495: 
                   2496: 
                   2497: int
                   2498: 
                   2499: pat_match(name, template)
                   2500: 
                   2501:        const char *name, *template;
                   2502: 
                   2503: {
                   2504: 
                   2505:        register char *s, c;
                   2506: 
                   2507:        char expname[TOS_NAMELEN+1];
                   2508: 
                   2509: 
                   2510: 
                   2511:        if (*template == '*') return 1;
                   2512: 
                   2513:        copy8_3(expname, name);
                   2514: 
                   2515: 
                   2516: 
                   2517:        s = expname;
                   2518: 
1.1.1.2   root     2519:        while ((c = *template++) != 0) {
1.1       root     2520: 
                   2521:                if (c != *s && c != '?')
                   2522: 
                   2523:                        return 0;
                   2524: 
                   2525:                s++;
                   2526: 
                   2527:        }
                   2528: 
                   2529:        return 1;
                   2530: 
                   2531: }
                   2532: 
                   2533: 
                   2534: 
                   2535: /*
                   2536: 
                   2537:  * int samefile(fcookie *a, fcookie *b): returns 1 if the two cookies
                   2538: 
                   2539:  * refer to the same file or directory, 0 otherwise
                   2540: 
                   2541:  */
                   2542: 
                   2543: 
                   2544: 
                   2545: int
                   2546: 
                   2547: samefile(a, b)
                   2548: 
                   2549:        fcookie *a, *b;
                   2550: 
                   2551: {
                   2552: 
                   2553:        if (a->fs == b->fs && a->dev == b->dev && a->index == b->index)
                   2554: 
                   2555:                return 1;
                   2556: 
                   2557:        return 0;
                   2558: 
                   2559: }
                   2560: 

unix.superglobalmegacorp.com

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