Annotation of 42BSD/old/1kfix.c, revision 1.1

1.1     ! root        1: static char *sccsid = "@(#)1kfix.c     4.1 (Berkeley) 10/16/80";
        !             2: #include <stdio.h>
        !             3: #include <a.out.h>
        !             4: #include <sys/param.h>
        !             5: #include <sys/vm.h>
        !             6: char   *malloc();
        !             7: 
        !             8: #define        round(a,b) (((a)+((b)-1))&~(b-1))
        !             9: 
        !            10: main(argc, argv)
        !            11:        int argc;
        !            12:        char *argv[];
        !            13: {
        !            14:        char *tp, *dp, *sp;
        !            15:        struct exec x, y;
        !            16:        int io;
        !            17:        char zeroes[NBPG];
        !            18: 
        !            19:        --argc;
        !            20:        ++argv;
        !            21:        if (argc == 0) {
        !            22:                fprintf(stderr, "usage: 1kfix file ...\n");
        !            23:                exit(1);
        !            24:        }
        !            25:        do {
        !            26:                io = open(argv[0], 2);
        !            27:                if (io < 0) {
        !            28:                        perror(argv[0]);
        !            29:                        argc--, argv++;
        !            30:                        continue;
        !            31:                }
        !            32:                if (read(io, &x, sizeof x) != sizeof x)
        !            33:                        goto format;
        !            34: 
        !            35:                switch (x.a_magic) {
        !            36: 
        !            37:                case OMAGIC:
        !            38:                case NMAGIC:
        !            39:                        if ((round(x.a_text,NBPG) & CLOFSET) == 0) {
        !            40:                                fprintf(stderr, "%s: wins as is\n", argv[0]);
        !            41:                                goto skip;
        !            42:                        }
        !            43:                        break;
        !            44: 
        !            45:                case ZMAGIC:
        !            46:                        lseek(io, NBPG, 0);
        !            47:                        break;
        !            48: 
        !            49:                default:
        !            50: format:
        !            51:                        printf("%s: not object file\n", argv[0]);
        !            52:                        goto skip;
        !            53:                }
        !            54: 
        !            55:                tp = malloc(x.a_text);
        !            56:                dp = malloc(x.a_data);
        !            57:                sp = malloc(x.a_syms);
        !            58:                if (read(io, tp, x.a_text) != x.a_text ||
        !            59:                    read(io, dp, x.a_data) != x.a_data ||
        !            60:                    read(io, sp, x.a_syms) != x.a_syms) {
        !            61:                        fprintf(stderr, "%s: short read\n", argv[0]);
        !            62:                        goto skip;
        !            63:                }
        !            64:                close(io);
        !            65:                io = creat(argv[0], 0755);
        !            66:                if (io < 0) {
        !            67:                        perror(argv[0]);
        !            68:                        goto skip;
        !            69:                }
        !            70: 
        !            71:                y = x;
        !            72:                switch (x.a_magic) {
        !            73: 
        !            74:                case ZMAGIC: {
        !            75:                        int i;
        !            76:                        for (i = 0; i < 512; i++)
        !            77:                                if (tp[i] != 0)
        !            78:                                        break;
        !            79:                        if (i == 512)
        !            80:                                printf("%s: already fixed\n", argv[0]);
        !            81:                        if (x.a_text & CLOFSET) {
        !            82:                                y.a_text -= NBPG;
        !            83:                                y.a_data += NBPG;
        !            84:                        }
        !            85:                        }
        !            86:                        break;
        !            87: 
        !            88:                case OMAGIC:
        !            89:                case NMAGIC:
        !            90:                        y.a_text = round(x.a_text, NBPG) - NBPG;
        !            91:                        y.a_data += NBPG;
        !            92:                        if (y.a_text == 0) {
        !            93:                                fprintf(stderr, "%s: text size would be 0\n", argv[0]);
        !            94:                                goto skip;
        !            95:                        }
        !            96:                }
        !            97:                y.a_trsize = y.a_drsize = 0;
        !            98:                write(io, (char *)&y, sizeof y);
        !            99:                if (x.a_magic == ZMAGIC)
        !           100:                        lseek(io, BSIZE, 0);
        !           101:                write(io, tp, x.a_text);
        !           102:                if (x.a_magic != ZMAGIC)
        !           103:                        write(io, zeroes, round(x.a_text, NBPG) - x.a_text);
        !           104:                write(io, dp, x.a_data);
        !           105:                write(io, sp, x.a_syms);
        !           106:                free(tp);
        !           107:                free(dp);
        !           108:                free(sp);
        !           109: skip:
        !           110:                argc--, argv++;
        !           111:                close(io);
        !           112:        } while (argc > 0);
        !           113:        exit(0);
        !           114: }

unix.superglobalmegacorp.com

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