Annotation of researchv9/sys/boot/stand/readfile.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * @(#)readfile.c 1.1 86/02/03 Copyright (c) 1985 by Sun Microsystems, Inc.
        !             3:  */
        !             4: 
        !             5: #include "saio.h"
        !             6: #include <a.out.h>
        !             7: 
        !             8: /*
        !             9:  * Read in a Unix executable file and return its entry point.
        !            10:  * Handle the various a.out formats correctly.
        !            11:  * "Io" is the standalone file descriptor to read from.
        !            12:  * Print informative little messages if "print" is on.
        !            13:  * Returns -1 for errors.
        !            14:  *
        !            15:  * The parameter <LOAD> must be set in the command line to cc, e.g.
        !            16:  *     cc -DLOAD=0x4000 -o readfile.c
        !            17:  * The makefile takes care of this.
        !            18:  */
        !            19: int (*
        !            20: readfile(io, print))()
        !            21:        register int io;
        !            22:        int print;
        !            23: {
        !            24:        struct exec x;
        !            25:        register int i;
        !            26:        register char *addr;
        !            27:        register int shared = 0;
        !            28:        register int loadaddr;
        !            29:        register int segsiz;
        !            30:        register int datasize;
        !            31: 
        !            32:        i = read(io, (char *)&x, sizeof x);
        !            33:        if (x.a_magic == ZMAGIC || x.a_magic == NMAGIC)
        !            34:                shared = 1;
        !            35:        if (i != sizeof x || (!shared && x.a_magic != OMAGIC)) {
        !            36:                printf("Not executable\n");
        !            37:                return (int (*)()) -1;
        !            38:        }
        !            39:        if (print)
        !            40:                printf("Size: %d", x.a_text);
        !            41:        datasize = x.a_data;
        !            42:        if (!shared) {
        !            43:                x.a_text = x.a_text + x.a_data;
        !            44:                x.a_data = 0;
        !            45:        }
        !            46:        if (lseek(io, N_TXTOFF(x), 0) == -1)
        !            47:                goto shread;
        !            48:        if (read(io, (char *)LOAD, (int)x.a_text) < x.a_text)
        !            49:                goto shread;
        !            50:        addr = (char *)(x.a_text + LOAD);
        !            51:        if (x.a_machtype == M_OLDSUN2)
        !            52:                segsiz = OLD_SEGSIZ;
        !            53:        else
        !            54:                segsiz = SEGSIZ;
        !            55:        if (shared)
        !            56:                while ((int)addr & (segsiz-1))
        !            57:                        *addr++ = 0;
        !            58:        if (print)
        !            59:                printf("+%d", datasize);
        !            60:        if (read(io, addr, (int)x.a_data) < x.a_data)
        !            61:                goto shread;
        !            62:        close(io);              /* Done with it. */
        !            63:        if (print)
        !            64:                printf("+%d", x.a_bss);
        !            65:        addr += x.a_data;
        !            66:        for (i = 0; i < x.a_bss; i++)
        !            67:                *addr++ = 0;
        !            68:        if (print)
        !            69:                printf(" bytes\n");
        !            70:        if (x.a_machtype != M_OLDSUN2 && x.a_magic == ZMAGIC)
        !            71:                loadaddr = LOAD + sizeof (struct exec);
        !            72:        else
        !            73:                loadaddr = LOAD;
        !            74:        return (int (*)()) (loadaddr);
        !            75: 
        !            76: shread:
        !            77:        printf("Truncated file\n");
        !            78:        return (int (*)()) -1;
        !            79: }

unix.superglobalmegacorp.com

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