Annotation of MiNT/src/dosfile.c, revision 1.1.1.4

1.1       root        1: /*
                      2: 
1.1.1.3   root        3: Copyright 1990,1991,1992 Eric R. Smith.
                      4: 
1.1.1.4 ! root        5: Copyright 1992,1993 Atari Corporation.
1.1.1.3   root        6: 
                      7: All rights reserved.
1.1       root        8: 
                      9: */
                     10: 
                     11: 
                     12: 
                     13: /* DOS file handling routines */
                     14: 
                     15: 
                     16: 
                     17: #include "mint.h"
                     18: 
                     19: 
                     20: 
                     21: static long do_dup P_((int,int));
                     22: 
                     23: static void unselectme P_((PROC *));
                     24: 
                     25: 
                     26: 
                     27: /*
                     28: 
                     29:  * first, some utility routines
                     30: 
                     31:  */
                     32: 
                     33: 
                     34: 
                     35: FILEPTR *
                     36: 
                     37: do_open(name, rwmode, attr, x)
                     38: 
                     39:        const char *name;       /* file name */
                     40: 
                     41:        int rwmode;     /* file access mode */
                     42: 
                     43:        int attr;       /* TOS attributes for created files (if applicable) */
                     44: 
                     45:        XATTR *x;       /* filled in with attributes of opened file */
                     46: 
                     47: {
                     48: 
                     49:        struct tty *tty;
                     50: 
                     51:        fcookie dir, fc;
                     52: 
                     53:        long devsp;
                     54: 
                     55:        FILEPTR *f;
                     56: 
                     57:        DEVDRV *dev;
                     58: 
                     59:        long r;
                     60: 
                     61:        XATTR xattr;
                     62: 
                     63:        unsigned perm;
                     64: 
                     65:        int creating;
                     66: 
1.1.1.4 ! root       67:        char temp1[PATH_MAX];
        !            68: 
1.1.1.3   root       69:        extern FILESYS proc_filesys;
                     70: 
1.1       root       71: 
                     72: 
                     73: /* for special BIOS "fake" devices */
                     74: 
                     75:        extern DEVDRV fakedev;
                     76: 
                     77: 
                     78: 
1.1.1.2   root       79:        TRACE(("do_open(%s)", name));
1.1       root       80: 
                     81: 
                     82: 
                     83: /*
                     84: 
                     85:  * first step: get a cookie for the directory
                     86: 
                     87:  */
                     88: 
                     89: 
                     90: 
                     91:        r = path2cookie(name, temp1, &dir);
                     92: 
                     93:        if (r) {
                     94: 
1.1.1.2   root       95:                mint_errno = (int)r;
1.1       root       96: 
1.1.1.2   root       97:                DEBUG(("do_open(%s): error %ld", name, r));
1.1       root       98: 
                     99:                return NULL;
                    100: 
                    101:        }
                    102: 
                    103: 
                    104: 
                    105: /*
                    106: 
                    107:  * second step: try to locate the file itself
                    108: 
                    109:  */
                    110: 
                    111:        r = relpath2cookie(&dir, temp1, follow_links, &fc, 0);
                    112: 
                    113: 
                    114: 
                    115: /*
                    116: 
                    117:  * file found: this is an error if (O_CREAT|O_EXCL) are set
                    118: 
                    119:  */
                    120: 
1.1.1.3   root      121: 
                    122: 
1.1       root      123:        if ( (r == 0) && ( (rwmode & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) ) ) {
                    124: 
1.1.1.2   root      125:                DEBUG(("do_open(%s): file already exists",name));
1.1       root      126: 
                    127:                mint_errno = EACCDN;
                    128: 
1.1.1.3   root      129:                release_cookie(&fc);
                    130: 
                    131:                release_cookie(&dir);
                    132: 
1.1       root      133:                return NULL;
                    134: 
                    135:        }
                    136: 
                    137: /*
                    138: 
                    139:  * file not found: maybe we should create it
                    140: 
1.1.1.3   root      141:  * note that if r != 0, the fc cookie is invalid (so we don't need to
                    142: 
                    143:  * release it)
                    144: 
1.1       root      145:  */
                    146: 
                    147:        if (r == EFILNF && (rwmode & O_CREAT)) {
                    148: 
                    149:        /* check first for write permission in the directory */
                    150: 
                    151:                r = (*dir.fs->getxattr)(&dir, &xattr);
                    152: 
                    153:                if (r == 0) {
                    154: 
                    155:                        if (denyaccess(&xattr, S_IWOTH))
                    156: 
                    157:                                r = EACCDN;
                    158: 
                    159:                }
                    160: 
                    161:                if (r) {
                    162: 
1.1.1.2   root      163:                        DEBUG(("do_open(%s): couldn't get "
1.1       root      164: 
1.1.1.2   root      165:                              "write permission on directory",name));
1.1       root      166: 
1.1.1.2   root      167:                        mint_errno = (int)r;
1.1       root      168: 
1.1.1.3   root      169:                        release_cookie(&dir);
                    170: 
1.1       root      171:                        return NULL;
                    172: 
                    173:                }
                    174: 
                    175:                r = (*dir.fs->creat)(&dir, temp1,
                    176: 
                    177:                        (S_IFREG|DEFAULT_MODE) & (~curproc->umask), attr, &fc);
                    178: 
                    179:                if (r) {
                    180: 
1.1.1.2   root      181:                        DEBUG(("do_open(%s): error %ld while creating file",
1.1       root      182: 
1.1.1.2   root      183:                                name, r));
1.1       root      184: 
1.1.1.2   root      185:                        mint_errno = (int)r;
1.1       root      186: 
1.1.1.3   root      187:                        release_cookie(&dir);
                    188: 
1.1       root      189:                        return NULL;
                    190: 
                    191:                }
                    192: 
                    193:                creating = 1;
                    194: 
                    195:        } else if (r) {
                    196: 
1.1.1.2   root      197:                DEBUG(("do_open(%s): error %ld while searching for file",
1.1       root      198: 
1.1.1.2   root      199:                        name, r));
1.1       root      200: 
1.1.1.2   root      201:                mint_errno = (int)r;
1.1       root      202: 
1.1.1.3   root      203:                release_cookie(&dir);
                    204: 
1.1       root      205:                return NULL;
                    206: 
                    207:        } else {
                    208: 
                    209:                creating = 0;
                    210: 
                    211:        }
                    212: 
                    213: 
                    214: 
                    215: /*
                    216: 
                    217:  * check now for permission to actually access the file
                    218: 
                    219:  */
                    220: 
                    221:        r = (*fc.fs->getxattr)(&fc, &xattr);
                    222: 
                    223:        if (r) {
                    224: 
1.1.1.2   root      225:                DEBUG(("do_open(%s): couldn't get file attributes",name));
1.1       root      226: 
1.1.1.2   root      227:                mint_errno = (int)r;
1.1       root      228: 
1.1.1.3   root      229:                release_cookie(&dir);
                    230: 
                    231:                release_cookie(&fc);
                    232: 
1.1       root      233:                return NULL;
                    234: 
                    235:        }
                    236: 
                    237: /*
                    238: 
                    239:  * we don't do directories
                    240: 
                    241:  */
                    242: 
                    243:        if ( (xattr.mode & S_IFMT) == S_IFDIR ) {
                    244: 
1.1.1.2   root      245:                DEBUG(("do_open(%s): file is a directory",name));
1.1       root      246: 
1.1.1.3   root      247:                release_cookie(&dir);
                    248: 
                    249:                release_cookie(&fc);
                    250: 
1.1       root      251:                mint_errno = EFILNF;
                    252: 
                    253:                return NULL;
                    254: 
                    255:        }
                    256: 
                    257: 
                    258: 
                    259:        switch (rwmode & O_RWMODE) {
                    260: 
                    261:        case O_WRONLY:
                    262: 
                    263:                perm = S_IWOTH;
                    264: 
                    265:                break;
                    266: 
                    267:        case O_RDWR:
                    268: 
                    269:                perm = S_IROTH|S_IWOTH;
                    270: 
                    271:                break;
                    272: 
                    273:        case O_EXEC:
                    274: 
                    275:                perm = (fc.fs->fsflags & FS_NOXBIT) ? S_IROTH : S_IXOTH;
                    276: 
                    277:                break;
                    278: 
                    279:        case O_RDONLY:
                    280: 
                    281:                perm = S_IROTH;
                    282: 
                    283:                break;
                    284: 
                    285:        default:
                    286: 
                    287:                perm = 0;
                    288: 
                    289:                ALERT("do_open: bad file access mode: %x", rwmode);
                    290: 
                    291:        }
                    292: 
                    293:        if (!creating && denyaccess(&xattr, perm)) {
                    294: 
1.1.1.2   root      295:                DEBUG(("do_open(%s): access to file denied",name));
1.1       root      296: 
1.1.1.3   root      297:                release_cookie(&dir);
                    298: 
                    299:                release_cookie(&fc);
                    300: 
1.1       root      301:                mint_errno = EACCDN;
                    302: 
                    303:                return NULL;
                    304: 
                    305:        }
                    306: 
                    307: 
                    308: 
                    309: /*
                    310: 
                    311:  * an extra check for write access -- even the superuser shouldn't
                    312: 
                    313:  * write to files with the FA_RDONLY attribute bit set (unless,
                    314: 
1.1.1.3   root      315:  * we just created the file, or unless the file is on the proc
                    316: 
                    317:  * file system and hence FA_RDONLY has a different meaning)
1.1       root      318: 
                    319:  */
                    320: 
1.1.1.3   root      321:        if ( !creating && (xattr.attr & FA_RDONLY) && fc.fs != &proc_filesys) {
1.1       root      322: 
                    323:                if ( (rwmode & O_RWMODE) == O_RDWR ||
                    324: 
                    325:                     (rwmode & O_RWMODE) == O_WRONLY ) {
                    326: 
1.1.1.2   root      327:                        DEBUG(("do_open(%s): can't write a read-only file",
1.1       root      328: 
1.1.1.2   root      329:                                name));
1.1       root      330: 
1.1.1.3   root      331:                        release_cookie(&dir);
                    332: 
                    333:                        release_cookie(&fc);
                    334: 
1.1       root      335:                        mint_errno = EACCDN;
                    336: 
                    337:                        return NULL;
                    338: 
                    339:                }
                    340: 
                    341:        }
                    342: 
                    343: 
                    344: 
                    345: /*
                    346: 
                    347:  * if writing to a setuid or setgid file, clear those bits
                    348: 
                    349:  */
                    350: 
                    351:        if ( (perm & S_IWOTH) && (xattr.mode & (S_ISUID|S_ISGID)) ) {
                    352: 
                    353:                xattr.mode &= ~(S_ISUID|S_ISGID);
                    354: 
                    355:                (*fc.fs->chmode)(&fc, (xattr.mode & ~S_IFMT));
                    356: 
                    357:        }
                    358: 
                    359: /*
                    360: 
                    361:  * If the caller asked for the attributes of the opened file, copy them over.
                    362: 
                    363:  */
                    364: 
                    365:        if (x) *x = xattr;
                    366: 
                    367: 
                    368: 
                    369: /*
                    370: 
                    371:  * So far, so good. Let's get the device driver now, and try to
                    372: 
                    373:  * actually open the file.
                    374: 
                    375:  */
                    376: 
                    377:        dev = (*fc.fs->getdev)(&fc, &devsp);
                    378: 
                    379:        if (!dev) {
                    380: 
1.1.1.2   root      381:                mint_errno = (int)devsp;
1.1       root      382: 
1.1.1.2   root      383:                DEBUG(("do_open(%s): device driver not found",name));
1.1       root      384: 
1.1.1.3   root      385:                release_cookie(&dir);
                    386: 
                    387:                release_cookie(&fc);
                    388: 
1.1       root      389:                return NULL;
                    390: 
                    391:        }
                    392: 
                    393: 
                    394: 
                    395:        if (dev == &fakedev) {          /* fake BIOS devices */
                    396: 
                    397:                f = curproc->handle[devsp];
                    398: 
                    399:                if (!f) {
                    400: 
                    401:                        mint_errno = EIHNDL;
                    402: 
                    403:                        return 0;
                    404: 
                    405:                }
                    406: 
                    407:                f->links++;
                    408: 
1.1.1.3   root      409:                release_cookie(&dir);
                    410: 
                    411:                release_cookie(&fc);
                    412: 
1.1       root      413:                return f;
                    414: 
                    415:        }
                    416: 
1.1.1.2   root      417:        if (0 == (f = new_fileptr())) {
1.1       root      418: 
1.1.1.3   root      419:                release_cookie(&dir);
                    420: 
                    421:                release_cookie(&fc);
                    422: 
1.1       root      423:                mint_errno = ENSMEM;
                    424: 
                    425:                return NULL;
                    426: 
                    427:        }
                    428: 
                    429:        f->links = 1;
                    430: 
                    431:        f->flags = rwmode;
                    432: 
                    433:        f->pos = 0;
                    434: 
                    435:        f->devinfo = devsp;
                    436: 
                    437:        f->fc = fc;
                    438: 
                    439:        f->dev = dev;
                    440: 
1.1.1.3   root      441:        release_cookie(&dir);
                    442: 
1.1       root      443: 
                    444: 
                    445:        r = (*dev->open)(f);
                    446: 
                    447:        if (r < 0) {
                    448: 
1.1.1.2   root      449:                DEBUG(("do_open(%s): device open failed with error %ld",
1.1       root      450: 
1.1.1.2   root      451:                        name, r));
1.1       root      452: 
1.1.1.2   root      453:                mint_errno = (int)r;
1.1       root      454: 
                    455:                f->links = 0;
                    456: 
1.1.1.3   root      457:                release_cookie(&fc);
                    458: 
1.1       root      459:                dispose_fileptr(f);
                    460: 
                    461:                return NULL;
                    462: 
                    463:        }
                    464: 
                    465: 
                    466: 
                    467: /* special code for opening a tty */
                    468: 
                    469:        if (is_terminal(f)) {
                    470: 
                    471:                extern struct tty default_tty;  /* in tty.c */
                    472: 
                    473: 
                    474: 
                    475:                tty = (struct tty *)f->devinfo;
                    476: 
                    477:                if (tty->use_cnt == 0) {     /* first open for this device? */
                    478: 
                    479:                        *tty = default_tty;
                    480: 
                    481:                }
                    482: 
                    483:                tty->use_cnt++;
                    484: 
                    485:        }
                    486: 
                    487:        return f;
                    488: 
                    489: }
                    490: 
                    491: 
                    492: 
                    493: /*
                    494: 
                    495:  * helper function for do_close: this closes the indicated file pointer which
                    496: 
                    497:  * is assumed to be associated with process p. The extra parameter is necessary
                    498: 
                    499:  * because f_midipipe mucks with file pointers of other processes, so
                    500: 
                    501:  * sometimes p != curproc.
                    502: 
                    503:  *
                    504: 
                    505:  * Note that the function changedrv() in filesys.c can call this routine.
                    506: 
                    507:  * in that case, f->dev will be 0 to represent an invalid device, and
                    508: 
                    509:  * we cannot call the device close routine.
                    510: 
                    511:  */
                    512: 
                    513: 
                    514: 
                    515: long
                    516: 
                    517: do_pclose(p, f)
                    518: 
                    519:        PROC *p;
                    520: 
                    521:        FILEPTR *f;
                    522: 
                    523: {
                    524: 
                    525:        long r = 0;
                    526: 
                    527: 
                    528: 
                    529:        if (!f) return EIHNDL;
                    530: 
                    531: 
                    532: 
                    533: /* if this file is "select'd" by this process, unselect it
                    534: 
1.1.1.2   root      535:  * (this is just in case we were killed by a signal)
1.1       root      536: 
                    537:  */
                    538: 
                    539: 
                    540: 
                    541: /* BUG? Feature? If media change is detected while we're doing the select,
                    542: 
                    543:  * we'll never unselect (since f->dev is set to NULL by changedrv())
                    544: 
                    545:  */
                    546: 
                    547:        if (f->dev) {
                    548: 
                    549:                (*f->dev->unselect)(f, (long)p, O_RDONLY);
                    550: 
                    551:                (*f->dev->unselect)(f, (long)p, O_WRONLY);
                    552: 
                    553:        }
                    554: 
                    555: 
                    556: 
                    557:        f->links--;
                    558: 
                    559: 
                    560: 
                    561: /* TTY manipulation must be done *before* calling the device close routine,
                    562: 
                    563:  * since afterwards the TTY structure may no longer exist
                    564: 
                    565:  */
                    566: 
                    567:        if (is_terminal(f) && f->links <= 0) {
                    568: 
                    569:                struct tty *tty = (struct tty *)f->devinfo;
                    570: 
                    571:                tty->use_cnt--;
                    572: 
1.1.1.3   root      573:                if (tty->use_cnt <= 0)
                    574: 
                    575:                        tty->pgrp = 0;
                    576: 
1.1       root      577:                if (tty->use_cnt <= 0 && tty->xkey) {
                    578: 
                    579:                        kfree(tty->xkey);
                    580: 
                    581:                        tty->xkey = 0;
                    582: 
                    583:                }
                    584: 
                    585:        }
                    586: 
                    587: 
                    588: 
                    589:        if (f->dev) {
                    590: 
                    591:                r = (*f->dev->close)(f, p->pid);
                    592: 
                    593:                if (r) {
                    594: 
1.1.1.2   root      595:                        DEBUG(("close: device close failed"));
1.1       root      596: 
                    597:                }
                    598: 
                    599:        }
                    600: 
                    601:        if (f->links <= 0) {
                    602: 
1.1.1.3   root      603:                release_cookie(&f->fc);
                    604: 
1.1       root      605:                dispose_fileptr(f);
                    606: 
                    607:        }
                    608: 
                    609:        return  r;
                    610: 
                    611: }
                    612: 
                    613: 
                    614: 
                    615: long
                    616: 
                    617: do_close(f)
                    618: 
                    619:        FILEPTR *f;
                    620: 
                    621: {
                    622: 
                    623:        return do_pclose(curproc, f);
                    624: 
                    625: }
                    626: 
                    627: 
                    628: 
1.1.1.2   root      629: long ARGS_ON_STACK
1.1       root      630: 
                    631: f_open(name, mode)
                    632: 
                    633:        const char *name;
                    634: 
                    635:        int mode;
                    636: 
                    637: {
                    638: 
                    639:        int i;
                    640: 
                    641:        FILEPTR *f;
                    642: 
                    643:        PROC *proc;
                    644: 
                    645: 
                    646: 
1.1.1.2   root      647:        TRACE(("Fopen(%s, %x)", name, mode));
1.1       root      648: 
                    649: #if O_GLOBAL
                    650: 
                    651:        if (mode & O_GLOBAL) {
                    652: 
                    653:            /* oh, boy! user wants us to open a global handle! */
                    654: 
                    655:            proc = rootproc;
                    656: 
                    657:        }
                    658: 
                    659:        else
                    660: 
                    661: #endif
                    662: 
                    663:            proc = curproc;
                    664: 
                    665: 
                    666: 
                    667:        for (i = MIN_OPEN; i < MAX_OPEN; i++) {
                    668: 
                    669:                if (!proc->handle[i])
                    670: 
                    671:                        goto found_for_open;
                    672: 
                    673:        }
                    674: 
1.1.1.2   root      675:        DEBUG(("Fopen(%s): process out of handles",name));
1.1       root      676: 
                    677:        return ENHNDL;          /* no more handles */
                    678: 
                    679: 
                    680: 
                    681: found_for_open:
                    682: 
                    683:        mode &= O_USER;         /* make sure the mode is legal */
                    684: 
                    685: 
                    686: 
                    687: /* note: file mode 3 is reserved for the kernel; for users, transmogrify it
                    688: 
                    689:  * into O_RDWR (mode 2)
                    690: 
                    691:  */
                    692: 
                    693:        if ( (mode & O_RWMODE) == O_EXEC ) {
                    694: 
                    695:                mode = (mode & ~O_RWMODE) | O_RDWR;
                    696: 
                    697:        }
                    698: 
                    699: 
                    700: 
                    701:        f = do_open(name, mode, 0, (XATTR *)0);
                    702: 
                    703: 
                    704: 
                    705: 
                    706: 
                    707:        if (!f) {
                    708: 
                    709:                return mint_errno;
                    710: 
                    711:        }
                    712: 
                    713:        proc->handle[i] = f;
                    714: 
                    715: /* default is to close non-standard files on exec */
                    716: 
                    717:        proc->fdflags[i] = FD_CLOEXEC;
                    718: 
                    719: 
                    720: 
                    721: #if O_GLOBAL
                    722: 
                    723:        if (proc != curproc) {
                    724: 
                    725:            /* we just opened a global handle */
                    726: 
                    727:            i += 100;
                    728: 
                    729:        }
                    730: 
                    731: #endif
                    732: 
                    733: 
                    734: 
1.1.1.2   root      735:        TRACE(("Fopen: returning %d", i));
1.1       root      736: 
                    737:        return i;
                    738: 
                    739: }
                    740: 
                    741: 
                    742: 
1.1.1.2   root      743: long ARGS_ON_STACK
1.1       root      744: 
                    745: f_create(name, attrib)
                    746: 
                    747:        const char *name;
                    748: 
                    749:        int attrib;
                    750: 
                    751: {
                    752: 
                    753:        fcookie dir;
                    754: 
                    755:        int i;
                    756: 
                    757:        FILEPTR *f;
                    758: 
                    759:        long r;
                    760: 
                    761:        PROC *proc;
                    762: 
                    763:        int offset = 0;
                    764: 
1.1.1.4 ! root      765:        char temp1[PATH_MAX];
        !           766: 
1.1       root      767: 
                    768: 
1.1.1.2   root      769:        TRACE(("Fcreate(%s, %x)", name, attrib));
1.1       root      770: 
                    771: #if O_GLOBAL
                    772: 
                    773:        if (attrib & O_GLOBAL) {
                    774: 
                    775:                proc = rootproc;
                    776: 
                    777:                offset = 100;
                    778: 
                    779:                attrib &= ~O_GLOBAL;
                    780: 
                    781:        }
                    782: 
                    783:        else
                    784: 
                    785: #endif
                    786: 
                    787:                proc = curproc;
                    788: 
                    789: 
                    790: 
                    791:        for (i = MIN_OPEN; i < MAX_OPEN; i++) {
                    792: 
                    793:                if (!proc->handle[i])
                    794: 
                    795:                        goto found_for_create;
                    796: 
                    797:        }
                    798: 
1.1.1.2   root      799:        DEBUG(("Fcreate(%s): process out of handles",name));
1.1       root      800: 
                    801:        return ENHNDL;          /* no more handles */
                    802: 
                    803: 
                    804: 
                    805: found_for_create:
                    806: 
                    807:        if (attrib == FA_LABEL) {
                    808: 
                    809:                r = path2cookie(name, temp1, &dir);
                    810: 
                    811:                if (r) return r;
                    812: 
                    813:                r = (*dir.fs->writelabel)(&dir, temp1);
                    814: 
1.1.1.3   root      815:                release_cookie(&dir);
                    816: 
1.1       root      817:                if (r) return r;
                    818: 
                    819: /*
                    820: 
                    821:  * just in case the caller tries to do something with this handle,
                    822: 
                    823:  * make it point to u:\dev\null
                    824: 
                    825:  */
                    826: 
                    827:                f = do_open("u:\\dev\\null", O_RDWR|O_CREAT|O_TRUNC, 0,
                    828: 
                    829:                             (XATTR *)0);
                    830: 
                    831:                proc->handle[i] = f;
                    832: 
1.1.1.4 ! root      833:                proc->fdflags[i] = FD_CLOEXEC;
        !           834: 
1.1       root      835:                return i+offset;
                    836: 
                    837:        }
                    838: 
                    839:        if (attrib & (FA_LABEL|FA_DIR)) {
                    840: 
1.1.1.2   root      841:                DEBUG(("Fcreate(%s,%x): illegal attributes",name,attrib));
1.1       root      842: 
                    843:                return EACCDN;
                    844: 
                    845:        }
                    846: 
                    847: 
                    848: 
                    849:        f = do_open(name, O_RDWR|O_CREAT|O_TRUNC, attrib, (XATTR *)0);
                    850: 
                    851: 
                    852: 
                    853:        if (!f) {
                    854: 
1.1.1.2   root      855:                DEBUG(("Fcreate(%s) failed, error %d", name, mint_errno));
1.1       root      856: 
                    857:                return mint_errno;
                    858: 
                    859:        }
                    860: 
                    861:        proc->handle[i] = f;
                    862: 
1.1.1.4 ! root      863:        proc->fdflags[i] = FD_CLOEXEC;
        !           864: 
1.1       root      865:        i += offset;
                    866: 
1.1.1.2   root      867:        TRACE(("Fcreate: returning %d", i));
1.1       root      868: 
                    869:        return i;
                    870: 
                    871: }
                    872: 
                    873: 
                    874: 
1.1.1.2   root      875: long ARGS_ON_STACK
1.1       root      876: 
                    877: f_close(fh)
                    878: 
                    879:        int fh;
                    880: 
                    881: {
                    882: 
                    883:        FILEPTR *f;
                    884: 
                    885:        long r;
                    886: 
                    887:        PROC *proc;
                    888: 
                    889: 
                    890: 
1.1.1.2   root      891:        TRACE(("Fclose: %d", fh));
1.1       root      892: 
                    893: #if O_GLOBAL
                    894: 
                    895:        if (fh >= 100) {
                    896: 
                    897:            fh -= 100;
                    898: 
                    899:            proc = rootproc;
                    900: 
                    901:        }
                    902: 
                    903:        else
                    904: 
                    905: #endif
                    906: 
                    907:            proc = curproc;
                    908: 
                    909: 
                    910: 
1.1.1.2   root      911:        if (fh < 0 || fh >= MAX_OPEN || 0 == (f = proc->handle[fh])) {
1.1       root      912: 
                    913:                return EIHNDL;
                    914: 
                    915:        }
                    916: 
1.1.1.3   root      917:        r = do_pclose(proc, f);
1.1       root      918: 
                    919: 
                    920: 
                    921: /* standard handles should be restored to default values */
                    922: 
                    923: /* do this for TOS domain only! */
                    924: 
                    925:        if (proc->domain == DOM_TOS) {
                    926: 
                    927:                if (fh == 0 || fh == 1)
                    928: 
                    929:                        f = proc->handle[-1];
                    930: 
                    931:                else if (fh == 2 || fh == 3)
                    932: 
                    933:                        f = proc->handle[-fh];
                    934: 
                    935:                else
                    936: 
                    937:                        f = 0;
                    938: 
                    939:        } else
                    940: 
                    941:                f = 0;
                    942: 
                    943: 
                    944: 
1.1.1.4 ! root      945:        if (f) {
        !           946: 
        !           947:                f->links++;
        !           948: 
        !           949:                proc->fdflags[fh] = 0;
        !           950: 
        !           951:        }
1.1       root      952: 
                    953:        proc->handle[fh] = f;
                    954: 
                    955:        return r;
                    956: 
                    957: }
                    958: 
                    959: 
                    960: 
1.1.1.2   root      961: long ARGS_ON_STACK
1.1       root      962: 
                    963: f_read(fh, count, buf)
                    964: 
                    965:        int fh;
                    966: 
                    967:        long count;
                    968: 
                    969:        char *buf;
                    970: 
                    971: {
                    972: 
                    973:        FILEPTR *f;
                    974: 
                    975: 
                    976: 
                    977:        PROC *proc;
                    978: 
                    979: 
                    980: 
                    981: #if O_GLOBAL
                    982: 
                    983:        if (fh >= 100) {
                    984: 
                    985:            fh -= 100;
                    986: 
                    987:            proc = rootproc;
                    988: 
                    989:        }
                    990: 
                    991:        else
                    992: 
                    993: #endif
                    994: 
                    995:            proc = curproc;
                    996: 
                    997: 
                    998: 
1.1.1.2   root      999:        if (fh < MIN_HANDLE || fh >= MAX_OPEN || 0 == (f = proc->handle[fh])) {
1.1       root     1000: 
1.1.1.2   root     1001:                DEBUG(("Fread: invalid handle: %d", fh));
1.1       root     1002: 
                   1003:                return EIHNDL;
                   1004: 
                   1005:        }
                   1006: 
                   1007:        if ( (f->flags & O_RWMODE) == O_WRONLY ) {
                   1008: 
1.1.1.2   root     1009:                DEBUG(("Fread: read on a write-only handle"));
1.1       root     1010: 
                   1011:                return EACCDN;
                   1012: 
                   1013:        }
                   1014: 
                   1015:        if (is_terminal(f))
                   1016: 
                   1017:                return tty_read(f, buf, count);
                   1018: 
                   1019: 
                   1020: 
1.1.1.3   root     1021:        TRACELOW(("Fread: %ld bytes from handle %d to %lx", count, fh, buf));
1.1       root     1022: 
                   1023:        return (*f->dev->read)(f, buf, count);
                   1024: 
                   1025: }
                   1026: 
                   1027: 
                   1028: 
1.1.1.2   root     1029: long ARGS_ON_STACK
1.1       root     1030: 
                   1031: f_write(fh, count, buf)
                   1032: 
                   1033:        int fh;
                   1034: 
                   1035:        long count;
                   1036: 
                   1037:        const char *buf;
                   1038: 
                   1039: {
                   1040: 
                   1041:        FILEPTR *f;
                   1042: 
                   1043:        PROC *proc;
                   1044: 
                   1045:        long r;
                   1046: 
                   1047: 
                   1048: 
                   1049: #if O_GLOBAL
                   1050: 
                   1051:        if (fh >= 100) {
                   1052: 
                   1053:            fh -= 100;
                   1054: 
                   1055:            proc = rootproc;
                   1056: 
                   1057:        }
                   1058: 
                   1059:        else
                   1060: 
                   1061: #endif
                   1062: 
                   1063:            proc = curproc;
                   1064: 
                   1065: 
                   1066: 
1.1.1.2   root     1067:        if (fh < MIN_HANDLE || fh >= MAX_OPEN || 0 == (f = proc->handle[fh])) {
1.1       root     1068: 
1.1.1.2   root     1069:                DEBUG(("Fwrite: bad handle: %d", fh));
1.1       root     1070: 
                   1071:                return EIHNDL;
                   1072: 
                   1073:        }
                   1074: 
                   1075:        if ( (f->flags & O_RWMODE) == O_RDONLY ) {
                   1076: 
1.1.1.2   root     1077:                DEBUG(("Fwrite: write on a read-only handle"));
1.1       root     1078: 
                   1079:                return EACCDN;
                   1080: 
                   1081:        }
                   1082: 
                   1083:        if (is_terminal(f))
                   1084: 
                   1085:                return tty_write(f, buf, count);
                   1086: 
                   1087: 
                   1088: 
                   1089:        /* it would be faster to do this in the device driver, but this
                   1090: 
                   1091:         * way the drivers are easier to write
                   1092: 
                   1093:         */
                   1094: 
1.1.1.3   root     1095:        if (f->flags & O_APPEND) {
1.1       root     1096: 
                   1097:                r = (*f->dev->lseek)(f, 0L, SEEK_END);
                   1098: 
1.1.1.3   root     1099:                /* ignore errors from unseekable files (e.g. pipes) */
                   1100: 
                   1101:                if (r == EACCDN)
                   1102: 
                   1103:                        r = 0;
                   1104: 
                   1105:        } else
1.1       root     1106: 
                   1107:                r = 0;
                   1108: 
                   1109:        if (r >= 0) {
                   1110: 
1.1.1.3   root     1111:                TRACELOW(("Fwrite: %ld bytes to handle %d", count, fh));
1.1       root     1112: 
                   1113:                r = (*f->dev->write)(f, buf, count);
                   1114: 
                   1115:        }
                   1116: 
                   1117:        if (r < 0) {
                   1118: 
1.1.1.2   root     1119:                DEBUG(("Fwrite: error %ld", r));
1.1       root     1120: 
                   1121:        }
                   1122: 
                   1123:        return r;
                   1124: 
                   1125: }
                   1126: 
                   1127: 
                   1128: 
1.1.1.2   root     1129: long ARGS_ON_STACK
1.1       root     1130: 
                   1131: f_seek(place, fh, how)
                   1132: 
                   1133:        long place;
                   1134: 
                   1135:        int fh;
                   1136: 
                   1137:        int how;
                   1138: 
                   1139: {
                   1140: 
                   1141:        FILEPTR *f;
                   1142: 
                   1143:        PROC *proc;
                   1144: 
                   1145: 
                   1146: 
1.1.1.2   root     1147:        TRACE(("Fseek(%ld, %d) on handle %d", place, how, fh));
1.1       root     1148: 
                   1149: #if O_GLOBAL
                   1150: 
                   1151:        if (fh >= 100) {
                   1152: 
                   1153:            fh -= 100;
                   1154: 
                   1155:            proc = rootproc;
                   1156: 
                   1157:        }
                   1158: 
                   1159:        else
                   1160: 
                   1161: #endif
                   1162: 
                   1163:            proc = curproc;
                   1164: 
                   1165: 
                   1166: 
1.1.1.2   root     1167:        if (fh < MIN_HANDLE || fh >= MAX_OPEN || 0 == (f = proc->handle[fh])) {
1.1       root     1168: 
1.1.1.2   root     1169:                DEBUG(("Fseek: bad handle: %d", fh));
1.1       root     1170: 
                   1171:                return EIHNDL;
                   1172: 
                   1173:        }
                   1174: 
                   1175:        if (is_terminal(f)) {
                   1176: 
                   1177:                return 0;
                   1178: 
                   1179:        }
                   1180: 
                   1181:        return (*f->dev->lseek)(f, place, how);
                   1182: 
                   1183: }
                   1184: 
                   1185: 
                   1186: 
                   1187: /* duplicate file pointer fh; returns a new file pointer >= min, if
                   1188: 
                   1189:    one exists, or ENHNDL if not. called by f_dup and f_cntl
                   1190: 
                   1191:  */
                   1192: 
                   1193: 
                   1194: 
                   1195: static long do_dup(fh, min)
                   1196: 
                   1197:        int fh, min;
                   1198: 
                   1199: {
                   1200: 
                   1201:        FILEPTR *f;
                   1202: 
                   1203:        int i;
                   1204: 
                   1205:        PROC *proc;
                   1206: 
                   1207: 
                   1208: 
                   1209:        for (i = min; i < MAX_OPEN; i++) {
                   1210: 
                   1211:                if (!curproc->handle[i])
                   1212: 
                   1213:                        goto found;
                   1214: 
                   1215:        }
                   1216: 
                   1217:        return ENHNDL;          /* no more handles */
                   1218: 
                   1219: found:
                   1220: 
                   1221: #if O_GLOBAL
                   1222: 
                   1223:        if (fh >= 100) {
                   1224: 
                   1225:            fh -= 100;
                   1226: 
                   1227:            proc = rootproc;
                   1228: 
                   1229:        } else
                   1230: 
                   1231: #endif
                   1232: 
                   1233:            proc = curproc;
                   1234: 
                   1235: 
                   1236: 
1.1.1.2   root     1237:        if (fh < MIN_HANDLE || fh >= MAX_OPEN || 0 == (f = proc->handle[fh]))
1.1       root     1238: 
                   1239:                return EIHNDL;
                   1240: 
                   1241: 
                   1242: 
                   1243:        curproc->handle[i] = f;
                   1244: 
                   1245: 
                   1246: 
                   1247: /* set default file descriptor flags */
                   1248: 
                   1249:        if (i >= 0) {
                   1250: 
                   1251:                if (i >= MIN_OPEN)
                   1252: 
                   1253:                        curproc->fdflags[i] = FD_CLOEXEC;
                   1254: 
                   1255:                else
                   1256: 
                   1257:                        curproc->fdflags[i] = 0;
                   1258: 
                   1259:        }
                   1260: 
                   1261:        f->links++;
                   1262: 
                   1263:        return i;
                   1264: 
                   1265: }
                   1266: 
                   1267: 
                   1268: 
1.1.1.2   root     1269: long ARGS_ON_STACK
1.1       root     1270: 
                   1271: f_dup(fh)
                   1272: 
                   1273:        int fh;
                   1274: 
                   1275: {
                   1276: 
                   1277:        long r;
                   1278: 
                   1279:        r = do_dup(fh, MIN_OPEN);
                   1280: 
1.1.1.2   root     1281:        TRACE(("Fdup(%d) -> %ld", fh, r));
1.1       root     1282: 
                   1283:        return r;
                   1284: 
                   1285: }
                   1286: 
                   1287: 
                   1288: 
1.1.1.2   root     1289: long ARGS_ON_STACK
1.1       root     1290: 
                   1291: f_force(newh, oldh)
                   1292: 
                   1293:        int newh;
                   1294: 
                   1295:        int oldh;
                   1296: 
                   1297: {
                   1298: 
                   1299:        FILEPTR *f;
                   1300: 
                   1301:        PROC *proc;
                   1302: 
                   1303: 
                   1304: 
1.1.1.2   root     1305:        TRACE(("Fforce(%d, %d)", newh, oldh));
1.1       root     1306: 
                   1307: 
                   1308: 
                   1309: #if O_GLOBAL
                   1310: 
                   1311:        if (oldh >= 100) {
                   1312: 
                   1313:            oldh -= 100;
                   1314: 
                   1315:            proc = rootproc;
                   1316: 
                   1317:        } else
                   1318: 
                   1319: #endif
                   1320: 
                   1321:            proc = curproc;
                   1322: 
                   1323: 
                   1324: 
                   1325:        if (oldh < MIN_HANDLE || oldh >= MAX_OPEN ||
                   1326: 
1.1.1.2   root     1327:            0 == (f = proc->handle[oldh])) {
1.1       root     1328: 
1.1.1.2   root     1329:                DEBUG(("Fforce: old handle invalid"));
1.1       root     1330: 
                   1331:                return EIHNDL;
                   1332: 
                   1333:        }
                   1334: 
                   1335: 
                   1336: 
                   1337:        if (newh < MIN_HANDLE || newh >= MAX_OPEN) {
                   1338: 
1.1.1.2   root     1339:                DEBUG(("Fforce: new handle out of range"));
1.1       root     1340: 
                   1341:                return EIHNDL;
                   1342: 
                   1343:        }
                   1344: 
                   1345: 
                   1346: 
                   1347:        (void)do_close(curproc->handle[newh]);
                   1348: 
                   1349:        curproc->handle[newh] = f;
                   1350: 
1.1.1.4 ! root     1351:        /* set default file descriptor flags */
        !          1352: 
        !          1353:        if (newh >= 0)
        !          1354: 
        !          1355:                curproc->fdflags[newh] = (newh >= MIN_OPEN) ? FD_CLOEXEC : 0;
        !          1356: 
1.1       root     1357:        f->links++;
                   1358: 
                   1359: /*
                   1360: 
                   1361:  * special: for a tty, if this is becoming a control terminal and the
                   1362: 
                   1363:  * tty doesn't have a pgrp yet, make it have the pgrp of the process
                   1364: 
                   1365:  * doing the Fforce
                   1366: 
                   1367:  */
                   1368: 
                   1369:        if (is_terminal(f) && newh == -1) {
                   1370: 
                   1371:                struct tty *tty = (struct tty *)f->devinfo;
                   1372: 
                   1373: 
                   1374: 
                   1375:                if (!tty->pgrp)
                   1376: 
                   1377:                        tty->pgrp = curproc->pgrp;
                   1378: 
                   1379:        }
                   1380: 
1.1.1.4 ! root     1381:        return 0;
1.1       root     1382: 
                   1383: }
                   1384: 
                   1385: 
                   1386: 
1.1.1.2   root     1387: long ARGS_ON_STACK
1.1       root     1388: 
                   1389: f_datime(timeptr, fh, rwflag)
                   1390: 
                   1391:        short *timeptr;
                   1392: 
                   1393:        int fh;
                   1394: 
                   1395:        int rwflag;
                   1396: 
                   1397: {
                   1398: 
                   1399:        FILEPTR *f;
                   1400: 
                   1401:        PROC *proc;
                   1402: 
                   1403: 
                   1404: 
1.1.1.2   root     1405:        TRACE(("Fdatime(%d)", fh));
1.1       root     1406: 
                   1407: #if O_GLOBAL
                   1408: 
                   1409:        if (fh >= 100) {
                   1410: 
                   1411:            fh -= 100;
                   1412: 
                   1413:            proc = rootproc;
                   1414: 
                   1415:        }
                   1416: 
                   1417:        else
                   1418: 
                   1419: #endif
                   1420: 
                   1421:            proc = curproc;
                   1422: 
                   1423: 
                   1424: 
1.1.1.2   root     1425:        if (fh < MIN_HANDLE || fh >= MAX_OPEN || 0 == (f = proc->handle[fh])) {
1.1       root     1426: 
1.1.1.2   root     1427:                DEBUG(("Fdatime: invalid handle"));
1.1       root     1428: 
                   1429:                return EIHNDL;
                   1430: 
                   1431:        }
                   1432: 
                   1433: 
                   1434: 
                   1435: /* some programs use Fdatime to test for TTY devices */
                   1436: 
                   1437:        if (is_terminal(f))
                   1438: 
                   1439:                return EACCDN;
                   1440: 
                   1441: 
                   1442: 
                   1443:        return (*f->dev->datime)(f, timeptr, rwflag);
                   1444: 
                   1445: }
                   1446: 
                   1447: 
                   1448: 
1.1.1.2   root     1449: long ARGS_ON_STACK
1.1       root     1450: 
                   1451: f_lock(fh, mode, start, length)
                   1452: 
                   1453:        int fh, mode;
                   1454: 
                   1455:        long start, length;
                   1456: 
                   1457: {
                   1458: 
                   1459:        FILEPTR *f;
                   1460: 
                   1461:        struct flock lock;
                   1462: 
                   1463:        PROC *proc;
                   1464: 
                   1465: 
                   1466: 
                   1467: #if O_GLOBAL
                   1468: 
                   1469:        if (fh >= 100) {
                   1470: 
                   1471:            fh -= 100;
                   1472: 
                   1473:            proc = rootproc;
                   1474: 
                   1475:        }
                   1476: 
                   1477:        else
                   1478: 
                   1479: #endif
                   1480: 
                   1481:            proc = curproc;
                   1482: 
                   1483: 
                   1484: 
1.1.1.2   root     1485:        if (fh < MIN_HANDLE || fh >= MAX_OPEN || 0 == (f = proc->handle[fh])) {
1.1       root     1486: 
1.1.1.2   root     1487:                DEBUG(("Flock: invalid handle"));
1.1       root     1488: 
                   1489:                return EIHNDL;
                   1490: 
                   1491:        }
                   1492: 
1.1.1.2   root     1493:        TRACE(("Flock(%d,%d,%ld,%ld)", fh, mode, start, length));
1.1       root     1494: 
                   1495:        lock.l_whence = SEEK_SET;
                   1496: 
                   1497:        lock.l_start = start;
                   1498: 
                   1499:        lock.l_len = length;
                   1500: 
                   1501: 
                   1502: 
                   1503:        if (mode == 0)          /* create a lock */
                   1504: 
                   1505:                lock.l_type = F_WRLCK;
                   1506: 
                   1507:        else if (mode == 1)     /* unlock region */
                   1508: 
                   1509:                lock.l_type = F_UNLCK;
                   1510: 
                   1511:        else
                   1512: 
                   1513:                return EINVFN;
                   1514: 
                   1515: 
                   1516: 
                   1517:        return (*f->dev->ioctl)(f, F_SETLK, &lock);
                   1518: 
                   1519: }
                   1520: 
                   1521: 
                   1522: 
                   1523: /*
                   1524: 
                   1525:  * extensions to GEMDOS:
                   1526: 
                   1527:  */
                   1528: 
                   1529: 
                   1530: 
                   1531: /*
                   1532: 
                   1533:  * Fpipe(int *handles): opens a pipe. if successful, returns 0, and
                   1534: 
                   1535:  * sets handles[0] to a file descriptor for the read end of the pipe
                   1536: 
                   1537:  * and handles[1] to one for the write end.
                   1538: 
                   1539:  */
                   1540: 
                   1541: 
                   1542: 
1.1.1.2   root     1543: long ARGS_ON_STACK
1.1       root     1544: 
                   1545: f_pipe(usrh)
                   1546: 
                   1547:        short *usrh;
                   1548: 
                   1549: {
                   1550: 
                   1551:        FILEPTR *in, *out;
                   1552: 
                   1553:        static int pipeno = 0;
                   1554: 
                   1555:        int i, j;
                   1556: 
                   1557:        char pipename[32]; /* MAGIC: 32 >= strlen "u:\pipe\sys$pipe.000\0" */
                   1558: 
                   1559: 
                   1560: 
1.1.1.2   root     1561:        TRACE(("Fpipe"));
1.1       root     1562: 
                   1563: 
                   1564: 
                   1565: /* BUG: more than 999 open pipes hangs the system */
                   1566: 
                   1567:        do {
                   1568: 
                   1569:                ksprintf(pipename, "u:\\pipe\\sys$pipe.%03d", pipeno);
                   1570: 
                   1571:                pipeno++; if (pipeno > 999) pipeno = 0;
                   1572: 
1.1.1.2   root     1573:                out = do_open(pipename, O_WRONLY|O_CREAT|O_EXCL, FA_RDONLY|FA_HIDDEN|FA_CHANGED,
                   1574: 
                   1575:                                 (XATTR *)0);
1.1       root     1576: 
                   1577:                        /* read-only attribute means unidirectional fifo */
                   1578: 
                   1579:                        /* hidden attribute means check for broken pipes */
                   1580: 
1.1.1.2   root     1581:                        /* changed attribute means act like Unix fifos */
                   1582: 
1.1       root     1583:        } while (out == 0 && mint_errno == EACCDN);
                   1584: 
                   1585: 
                   1586: 
                   1587:        if (!out) {
                   1588: 
1.1.1.2   root     1589:                DEBUG(("Fpipe: error %d", mint_errno));
1.1       root     1590: 
                   1591:                return mint_errno;
                   1592: 
                   1593:        }
                   1594: 
                   1595: 
                   1596: 
                   1597:        in = do_open(pipename, O_RDONLY, 0, (XATTR *)0);
                   1598: 
                   1599:        if (!in) {
                   1600: 
1.1.1.2   root     1601:                DEBUG(("Fpipe: in side of pipe not opened (error %d)",
1.1       root     1602: 
1.1.1.2   root     1603:                        mint_errno));
1.1       root     1604: 
                   1605:                (void)do_close(out);
                   1606: 
                   1607:                return mint_errno;
                   1608: 
                   1609:        }
                   1610: 
                   1611: 
                   1612: 
                   1613:        for (i = MIN_OPEN; i < MAX_OPEN; i++) {
                   1614: 
                   1615:                if (curproc->handle[i] == 0)
                   1616: 
                   1617:                        break;
                   1618: 
                   1619:        }
                   1620: 
                   1621: 
                   1622: 
                   1623:        for (j = i+1; j < MAX_OPEN; j++) {
                   1624: 
                   1625:                if (curproc->handle[j] == 0)
                   1626: 
                   1627:                        break;
                   1628: 
                   1629:        }
                   1630: 
                   1631: 
                   1632: 
                   1633:        if (j >= MAX_OPEN) {
                   1634: 
1.1.1.2   root     1635:                DEBUG(("Fpipe: not enough handles left"));
1.1       root     1636: 
                   1637:                (void) do_close(in);
                   1638: 
                   1639:                (void) do_close(out);
                   1640: 
                   1641:                return ENHNDL;
                   1642: 
                   1643:        }
                   1644: 
                   1645:        curproc->handle[i] = in; curproc->handle[j] = out;
                   1646: 
                   1647: /* leave pipes open across Pexec */
                   1648: 
                   1649:        curproc->fdflags[i] = 0;
                   1650: 
                   1651:        curproc->fdflags[j] = 0;
                   1652: 
                   1653: 
                   1654: 
                   1655:        usrh[0] = i;
                   1656: 
                   1657:        usrh[1] = j;
                   1658: 
1.1.1.2   root     1659:        TRACE(("Fpipe: returning 0: input %d output %d",i,j));
1.1       root     1660: 
                   1661:        return 0;
                   1662: 
                   1663: }
                   1664: 
                   1665: 
                   1666: 
                   1667: /*
                   1668: 
                   1669:  * f_cntl: a combination "ioctl" and "fcntl". Some functions are
                   1670: 
                   1671:  * handled here, if they apply to the file descriptors directly
                   1672: 
                   1673:  * (e.g. F_DUPFD) or if they're easily translated into file system
                   1674: 
                   1675:  * functions (e.g. FSTAT). Others are passed on to the device driver
                   1676: 
                   1677:  * via dev->ioctl.
                   1678: 
                   1679:  */
                   1680: 
                   1681: 
                   1682: 
1.1.1.2   root     1683: long ARGS_ON_STACK
1.1       root     1684: 
                   1685: f_cntl(fh, arg, cmd)
                   1686: 
                   1687:        int fh;
                   1688: 
                   1689:        long arg;
                   1690: 
                   1691:        int cmd;
                   1692: 
                   1693: {
                   1694: 
                   1695:        FILEPTR *f;
                   1696: 
                   1697:        PROC *proc;
                   1698: 
                   1699:        struct flock *fl;
                   1700: 
                   1701:        long r;
                   1702: 
                   1703: 
                   1704: 
1.1.1.2   root     1705:        TRACE(("Fcntl(%d, cmd=0x%x)", fh, cmd));
1.1       root     1706: 
                   1707: #if O_GLOBAL
                   1708: 
                   1709:        if (fh >= 100) {
                   1710: 
                   1711:            fh -= 100;
                   1712: 
                   1713:            proc = rootproc;
                   1714: 
                   1715:        }
                   1716: 
                   1717:        else
                   1718: 
                   1719: #endif
                   1720: 
                   1721:            proc = curproc;
                   1722: 
                   1723: 
                   1724: 
                   1725:        if (fh < MIN_HANDLE || fh >= MAX_OPEN) {
                   1726: 
1.1.1.2   root     1727:                DEBUG(("Fcntl: bad file handle"));
1.1       root     1728: 
                   1729:                return EIHNDL;
                   1730: 
                   1731:        }
                   1732: 
                   1733: 
                   1734: 
                   1735:        if (cmd == F_DUPFD) {
                   1736: 
                   1737: #if O_GLOBAL
                   1738: 
                   1739:                if (proc != curproc) fh += 100;
                   1740: 
                   1741: #endif
                   1742: 
                   1743:                return do_dup(fh, (int)arg);
                   1744: 
                   1745:        }
                   1746: 
                   1747: 
                   1748: 
                   1749:        f = proc->handle[fh];
                   1750: 
                   1751:        if (!f) return EIHNDL;
                   1752: 
                   1753: 
                   1754: 
                   1755:        switch(cmd) {
                   1756: 
                   1757:        case F_GETFD:
                   1758: 
1.1.1.2   root     1759:                TRACE(("Fcntl F_GETFD"));
1.1       root     1760: 
                   1761:                if (fh < 0) return EIHNDL;
                   1762: 
                   1763:                return proc->fdflags[fh];
                   1764: 
                   1765:        case F_SETFD:
                   1766: 
1.1.1.2   root     1767:                TRACE(("Fcntl F_SETFD"));
1.1       root     1768: 
                   1769:                if (fh < 0) return EIHNDL;
                   1770: 
                   1771:                proc->fdflags[fh] = arg;
                   1772: 
                   1773:                return 0;
                   1774: 
                   1775:        case F_GETFL:
                   1776: 
1.1.1.2   root     1777:                TRACE(("Fcntl F_GETFL"));
1.1       root     1778: 
                   1779:                return f->flags & O_USER;
                   1780: 
                   1781:        case F_SETFL:
                   1782: 
1.1.1.2   root     1783:                TRACE(("Fcntl F_SETFL"));
1.1       root     1784: 
                   1785:                arg &= O_USER;          /* make sure only user bits set */
                   1786: 
1.1.1.3   root     1787: #if 0
                   1788: 
1.1       root     1789:        /* COMPATIBILITY WITH OLD VERSIONS ONLY */
                   1790: 
                   1791:        /* THIS CODE WILL GO AWAY. REALLY! */
                   1792: 
                   1793:                if (arg & 4) {
                   1794: 
                   1795:                        arg |= O_NDELAY;
                   1796: 
                   1797:                        arg &= ~4;
                   1798: 
                   1799:                }
                   1800: 
1.1.1.3   root     1801: #endif
                   1802: 
1.1       root     1803: 
                   1804: 
                   1805:        /* make sure the file access and sharing modes are not changed */
                   1806: 
                   1807:                arg &= ~(O_RWMODE|O_SHMODE);
                   1808: 
                   1809:                arg |= f->flags & (O_RWMODE|O_SHMODE);
                   1810: 
                   1811:                f->flags &= ~O_USER;    /* set user bits to arg */
                   1812: 
                   1813:                f->flags |= arg;
                   1814: 
                   1815:                return 0;
                   1816: 
                   1817:        case FSTAT:
                   1818: 
                   1819:                return (*f->fc.fs->getxattr)(&f->fc, (XATTR *)arg);
                   1820: 
                   1821:        case F_SETLK:
                   1822: 
1.1.1.2   root     1823:        case F_SETLKW:
                   1824: 
1.1       root     1825:        /* make sure that the file was opened with appropriate permissions */
                   1826: 
                   1827:                fl = (struct flock *)arg;
                   1828: 
                   1829:                if (fl->l_type == F_RDLCK) {
                   1830: 
                   1831:                        if ( (f->flags & O_RWMODE) == O_WRONLY )
                   1832: 
                   1833:                                return EACCDN;
                   1834: 
                   1835:                } else {
                   1836: 
                   1837:                        if ( (f->flags & O_RWMODE) == O_RDONLY )
                   1838: 
                   1839:                                return EACCDN;
                   1840: 
                   1841:                }
                   1842: 
                   1843:                /* fall through to device ioctl */
                   1844: 
                   1845:        default:
                   1846: 
1.1.1.2   root     1847:                TRACE(("Fcntl mode %x: calling ioctl",cmd));
1.1       root     1848: 
                   1849:                r = (*f->dev->ioctl)(f, cmd, (void *)arg);
                   1850: 
                   1851:                if (r == EINVFN && is_terminal(f)) {
                   1852: 
                   1853:                        r = tty_ioctl(f, cmd, (void *)arg);
                   1854: 
                   1855:                }
                   1856: 
                   1857:                return r;
                   1858: 
                   1859:        }
                   1860: 
                   1861: }
                   1862: 
                   1863: 
                   1864: 
                   1865: /*
                   1866: 
                   1867:  * fselect(timeout, rfd, wfd, xfd)
                   1868: 
                   1869:  * timeout is an (unsigned) 16 bit integer giving the maximum number
                   1870: 
                   1871:  * of milliseconds to wait; rfd, wfd, and xfd are pointers to 32 bit
                   1872: 
                   1873:  * integers containing bitmasks that describe which file descriptors
                   1874: 
                   1875:  * we're interested in. These masks are changed to represent which
                   1876: 
                   1877:  * file descriptors actually have data waiting (rfd), are ready to
                   1878: 
                   1879:  * output (wfd), or have exceptional conditions (xfd -- currently
                   1880: 
                   1881:  * ignored). If timeout is 0, fselect blocks until some file descriptor
                   1882: 
                   1883:  * is ready; otherwise, it waits only "timeout" milliseconds.
                   1884: 
                   1885:  * Return value: number of file descriptors that are available for
                   1886: 
                   1887:  * reading/writing; or a negative error number.
                   1888: 
                   1889:  */
                   1890: 
                   1891: 
                   1892: 
                   1893: /* helper function for time outs */
                   1894: 
                   1895: static void
                   1896: 
                   1897: unselectme(p)
                   1898: 
                   1899:        PROC *p;
                   1900: 
                   1901: {
                   1902: 
                   1903:        wakeselect((long)p);
                   1904: 
                   1905: }
                   1906: 
                   1907: 
                   1908: 
1.1.1.2   root     1909: long ARGS_ON_STACK
1.1       root     1910: 
                   1911: f_select(timeout, rfdp, wfdp, xfdp)
                   1912: 
                   1913:        unsigned timeout;
                   1914: 
                   1915:        long *rfdp, *wfdp, *xfdp;
                   1916: 
                   1917: {
                   1918: 
                   1919:        long rfd, wfd;
                   1920: 
                   1921:        long mask, bytes;
                   1922: 
                   1923:        int i, count;
                   1924: 
                   1925:        FILEPTR *f;
                   1926: 
                   1927:        PROC *p;
                   1928: 
                   1929:        TIMEOUT *t;
                   1930: 
1.1.1.3   root     1931:        short sr;
                   1932: 
1.1       root     1933: 
                   1934: 
1.1.1.3   root     1935:        if (xfdp)
                   1936: 
                   1937:                *xfdp = 0;
1.1.1.2   root     1938: 
                   1939: 
                   1940: 
1.1       root     1941:        if (rfdp) {
                   1942: 
                   1943:                rfd = *rfdp; *rfdp = 0;
                   1944: 
                   1945:        }
                   1946: 
                   1947:        else
                   1948: 
                   1949:                rfd = 0;
                   1950: 
                   1951:        if (wfdp) {
                   1952: 
                   1953:                wfd = *wfdp; *wfdp = 0;
                   1954: 
                   1955:        }
                   1956: 
                   1957:        else
                   1958: 
                   1959:                wfd = 0;
                   1960: 
                   1961: 
                   1962: 
1.1.1.2   root     1963:        TRACE(("Fselect(%u, %lx, %lx)", timeout, rfd, wfd));
1.1       root     1964: 
                   1965:        p = curproc;                    /* help the optimizer out */
                   1966: 
                   1967: 
                   1968: 
                   1969:        /* first, validate the masks */
                   1970: 
                   1971:        mask = 1L;
                   1972: 
                   1973:        for (i = 0; i < MAX_OPEN; i++) {
                   1974: 
                   1975:                if ( ((rfd & mask) || (wfd & mask)) && !(p->handle[i]) ) {
                   1976: 
1.1.1.4 ! root     1977:                        DEBUG(("Fselect: invalid handle: %d", i));
1.1       root     1978: 
                   1979:                        return EIHNDL;
                   1980: 
                   1981:                }
                   1982: 
                   1983:                mask = mask << 1L;
                   1984: 
                   1985:        }
                   1986: 
                   1987: 
                   1988: 
                   1989: /* now, loop through the file descriptors, setting up the select process */
                   1990: 
                   1991: /* NOTE: wakeselect will set p->wait_cond to 0 if data arrives during the
                   1992: 
                   1993:  * selection
                   1994: 
                   1995:  * Also note: because of the validation above, we may assume that the
                   1996: 
                   1997:  * file handles are valid here. However, this assumption may no longer
                   1998: 
                   1999:  * be true after we've gone to sleep, since a signal handler may have
                   2000: 
                   2001:  * closed one of the handles.
                   2002: 
                   2003:  */
                   2004: 
                   2005: 
                   2006: 
                   2007:        mask = 1L;
                   2008: 
                   2009:        count = 0;
                   2010: 
1.1.1.2   root     2011:        curproc->wait_cond = (long)wakeselect;          /* flag */
1.1       root     2012: 
                   2013: 
                   2014: 
                   2015:        for (i = 0; i < MAX_OPEN; i++) {
                   2016: 
                   2017:                if (rfd & mask) {
                   2018: 
                   2019:                        f = p->handle[i];
                   2020: 
                   2021:                        if ((*f->dev->select)(f, (long)p, O_RDONLY)) {
                   2022: 
                   2023:                                count++;
                   2024: 
                   2025:                                *rfdp |= mask;
                   2026: 
                   2027:                        }
                   2028: 
                   2029:                }
                   2030: 
                   2031:                if (wfd & mask) {
                   2032: 
                   2033:                        f = p->handle[i];
                   2034: 
                   2035:                        if ((*f->dev->select)(f, (long)p, O_WRONLY)) {
                   2036: 
                   2037:                                count++;
                   2038: 
                   2039:                                *wfdp |= mask;
                   2040: 
                   2041:                        }
                   2042: 
                   2043:                }
                   2044: 
                   2045:                mask = mask << 1L;
                   2046: 
                   2047:        }
                   2048: 
                   2049: 
                   2050: 
                   2051:        if (count == 0) {
                   2052: 
                   2053:                /* OK, now let's set a timeout */
                   2054: 
                   2055: 
                   2056: 
                   2057:                if (timeout) {
                   2058: 
                   2059:                        t = addtimeout((long)timeout, unselectme);
                   2060: 
                   2061:                } else {
                   2062: 
                   2063:                        t = 0;
                   2064: 
                   2065:                }
                   2066: 
                   2067: 
                   2068: 
1.1.1.3   root     2069:                sr = spl7();
                   2070: 
                   2071: 
                   2072: 
1.1       root     2073:        /* curproc->wait_cond changes when data arrives or the timeout happens */
                   2074: 
1.1.1.2   root     2075:                while (curproc->wait_cond == (long)wakeselect) {
1.1       root     2076: 
1.1.1.2   root     2077:                        TRACE(("sleeping in Fselect"));
1.1       root     2078: 
1.1.1.2   root     2079:                        sleep(SELECT_Q, (long)wakeselect);
1.1       root     2080: 
                   2081:                }
                   2082: 
1.1.1.3   root     2083:                spl(sr);
                   2084: 
1.1       root     2085: 
                   2086: 
                   2087:        /* we can cancel the time out now (if it hasn't already happened) */
                   2088: 
                   2089:                if (t) canceltimeout(t);
                   2090: 
                   2091: 
                   2092: 
                   2093:        /* OK, let's see what data arrived (if any) */
                   2094: 
                   2095:                mask = 1L;
                   2096: 
                   2097:                for (i = 0; i < MAX_OPEN; i++) {
                   2098: 
                   2099:                        if (rfd & mask) {
                   2100: 
                   2101:                                f = p->handle[i];
                   2102: 
                   2103:                                if (f) {
                   2104: 
                   2105:                                    bytes = 1L;
                   2106: 
                   2107:                                    (void)(*f->dev->ioctl)(f, FIONREAD,&bytes);
                   2108: 
                   2109:                                    if (bytes > 0) {
                   2110: 
                   2111:                                        *rfdp |= mask;
                   2112: 
                   2113:                                        count++;
                   2114: 
                   2115:                                    }
                   2116: 
                   2117:                                }
                   2118: 
                   2119:                        }
                   2120: 
                   2121:                        if (wfd & mask) {
                   2122: 
                   2123:                                f = p->handle[i];
                   2124: 
                   2125:                                if (f) {
                   2126: 
                   2127:                                    bytes = 1L;
                   2128: 
                   2129:                                    (void)(*f->dev->ioctl)(f, FIONWRITE,&bytes);
                   2130: 
                   2131:                                    if (bytes > 0) {
                   2132: 
                   2133:                                        *wfdp |= mask;
                   2134: 
                   2135:                                        count++;
                   2136: 
                   2137:                                    }
                   2138: 
                   2139:                                }
                   2140: 
                   2141:                        }
                   2142: 
                   2143:                        mask = mask << 1L;
                   2144: 
                   2145:                }
                   2146: 
                   2147:        }
                   2148: 
                   2149: 
                   2150: 
                   2151:        /* at this point, we either have data or a time out */
                   2152: 
                   2153:        /* cancel all the selects */
                   2154: 
                   2155:        mask = 1L;
                   2156: 
                   2157: 
                   2158: 
                   2159:        for (i = 0; i < MAX_OPEN; i++) {
                   2160: 
                   2161:                if (rfd & mask) {
                   2162: 
                   2163:                        f = p->handle[i];
                   2164: 
                   2165:                        if (f)
                   2166: 
                   2167:                                (*f->dev->unselect)(f, (long)p, O_RDONLY);
                   2168: 
                   2169:                }
                   2170: 
                   2171:                if (wfd & mask) {
                   2172: 
                   2173:                        f = p->handle[i];
                   2174: 
                   2175:                        if (f)
                   2176: 
                   2177:                                (*f->dev->unselect)(f, (long)p, O_WRONLY);
                   2178: 
                   2179:                }
                   2180: 
                   2181:                mask = mask << 1L;
                   2182: 
                   2183:        }
                   2184: 
                   2185: 
                   2186: 
1.1.1.2   root     2187:        TRACE(("Fselect: returning %d", count));
1.1       root     2188: 
                   2189:        return count;
                   2190: 
                   2191: }
                   2192: 
                   2193: 
                   2194: 
                   2195: 
                   2196: 
                   2197: /*
                   2198: 
                   2199:  * GEMDOS extension: Fmidipipe
                   2200: 
                   2201:  * Fmidipipe(pid, in, out) manipultes the MIDI file handles (handles -4 and -5)
                   2202: 
                   2203:  * of process "pid" so that they now point to the files with handles "in" and
                   2204: 
                   2205:  * "out" in the calling process
                   2206: 
                   2207:  */
                   2208: 
                   2209: 
                   2210: 
1.1.1.2   root     2211: long ARGS_ON_STACK
1.1       root     2212: 
                   2213: f_midipipe(pid, in, out)
                   2214: 
                   2215:        int pid, in, out;
                   2216: 
                   2217: {
                   2218: 
                   2219:        PROC *p;
                   2220: 
                   2221:        FILEPTR *fin, *fout;
                   2222: 
                   2223: 
                   2224: 
                   2225: /* first, find the process */
                   2226: 
                   2227: 
                   2228: 
                   2229:        if (pid == 0)
                   2230: 
                   2231:                p = curproc;
                   2232: 
                   2233:        else {
                   2234: 
                   2235:                p = pid2proc(pid);
                   2236: 
                   2237:                if (!p)
                   2238: 
                   2239:                        return EFILNF;
                   2240: 
                   2241:        }
                   2242: 
                   2243:  
                   2244: 
                   2245: /* next, validate the input and output file handles */
                   2246: 
1.1.1.2   root     2247:        if (in < MIN_HANDLE || in >= MAX_OPEN || (0==(fin = curproc->handle[in])))
1.1       root     2248: 
                   2249:                return EIHNDL;
                   2250: 
                   2251:        if ( (fin->flags & O_RWMODE) == O_WRONLY ) {
                   2252: 
1.1.1.2   root     2253:                DEBUG(("Fmidipipe: input side is write only"));
1.1       root     2254: 
                   2255:                return EACCDN;
                   2256: 
                   2257:        }
                   2258: 
1.1.1.2   root     2259:        if (out < MIN_HANDLE || out >= MAX_OPEN || (0==(fout = curproc->handle[out])))
1.1       root     2260: 
                   2261:                return EIHNDL;
                   2262: 
                   2263:        if ( (fout->flags & O_RWMODE) == O_RDONLY ) {
                   2264: 
1.1.1.2   root     2265:                DEBUG(("Fmidipipe: output side is read only"));
1.1       root     2266: 
                   2267:                return EACCDN;
                   2268: 
                   2269:        }
                   2270: 
                   2271: 
                   2272: 
                   2273: /* OK, duplicate the handles and put them in the new process */
                   2274: 
                   2275:        fin->links++; fout->links++;
                   2276: 
                   2277:        (void)do_pclose(p, p->midiin);
                   2278: 
                   2279:        (void)do_pclose(p, p->midiout);
                   2280: 
                   2281:        p->midiin = fin; p->midiout = fout;
                   2282: 
                   2283:        return 0;
                   2284: 
                   2285: }
                   2286: 

unix.superglobalmegacorp.com

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