Annotation of 43BSD/contrib/cpm/src/cpm.c, revision 1.1

1.1     ! root        1: /*     cpm.c   1.17    85/01/10        */
        !             2: /*
        !             3:        Copyright (c) 1982, 1983 by
        !             4:        Helge Skrivervik, UCB.
        !             5:        All rights reserved.
        !             6:        Permission granted for use by UNIX* licencees.
        !             7:        UNIX is a trademark of Bell Labs.
        !             8: 
        !             9: */
        !            10: 
        !            11: #include <stdio.h>
        !            12: #include "cpmio.h"
        !            13: #include "cpmfio.h"
        !            14: 
        !            15: #define BIG    2147483647
        !            16: 
        !            17: C_FILE c_iob[C_NFILE];
        !            18: int    fid;
        !            19: 
        !            20: 
        !            21: int    dflag, cflag, iflag, tflag;
        !            22: int    blksiz  =       1024;   /* default block size */
        !            23: int    tracks  =       77;     /* default number of tracks */
        !            24: int    maxdir  =       64;     /* default number of directory slots per disk */
        !            25: int    seclth  =       128;    /* default sector length [bytes] */
        !            26: int    sectrk  =       26;     /* default number of sectors per track */
        !            27: int    skew    =       6;      /* default sector skew factor */
        !            28: 
        !            29: int    *bitmap = 0;
        !            30: struct directory *dirbuf;
        !            31: 
        !            32: char *string;
        !            33: 
        !            34: main(argc, argv)
        !            35:        int     argc;
        !            36:        char    *argv[];
        !            37: {
        !            38: 
        !            39:        char *cpmname, *unixname, *malloc();
        !            40:        int xflag, stat=0, Cflag, Iflag, Bflag;
        !            41: 
        !            42:        if (argc == 1)
        !            43:                usage();
        !            44:        for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
        !            45:                switch (argv[1][1]) {
        !            46: 
        !            47:                case 0:
        !            48:                        break;
        !            49: 
        !            50:                case 'B':
        !            51:                        Bflag++;
        !            52:                        continue;
        !            53: 
        !            54:                case 'd':
        !            55:                        dflag++;
        !            56:                        continue;
        !            57: 
        !            58:                case 'c':
        !            59:                        if (Cflag)
        !            60:                                stat++;
        !            61:                        cpmname = argv[2];
        !            62:                        unixname = argv[3];
        !            63:                        argc -= 2;
        !            64:                        argv += 2;
        !            65:                        cflag++;
        !            66:                        continue;
        !            67: 
        !            68:                case 'i':
        !            69:                        if (isatty(0) && isatty(1)) iflag++;
        !            70:                        continue;
        !            71: 
        !            72:                case 'p':
        !            73:                        cpmname = argv[2];
        !            74:                        ++argv;
        !            75:                        --argc;
        !            76:                        tflag++;
        !            77:                        continue;
        !            78: 
        !            79:                case 's':
        !            80:                        string = argv[2];
        !            81:                        ++argv;
        !            82:                        skew = number(BIG);
        !            83:                        argc--;
        !            84:                        if ((skew < 1) || (skew > 10)) {
        !            85:                                fprintf(stderr, "cpm: skew factor out of range\n");
        !            86:                                exit(1);
        !            87:                        }
        !            88:                        continue;
        !            89: 
        !            90:                case 'b':
        !            91:                        string = argv[2];
        !            92:                        ++argv;
        !            93:                        blksiz = number(BIG);
        !            94:                        argc--;
        !            95:                        if (blksiz & 0xc3) {
        !            96:                                fprintf(stderr, "cpm: illegal block size: %d\n",blksiz);
        !            97:                                exit(1);
        !            98:                        }
        !            99:                        continue;
        !           100: 
        !           101:                case 'm':
        !           102:                        string = argv[2];
        !           103:                        ++argv;
        !           104:                        maxdir = number(BIG);
        !           105:                        argc--;
        !           106:                        if ((maxdir < 64) || (tracks >1024 )) {
        !           107:                                fprintf(stderr, "cpm: illegal value of m-flag: %d\n",maxdir);
        !           108:                                exit(1);
        !           109:                        }
        !           110:                        continue;
        !           111: 
        !           112:                case 'l':
        !           113:                        string = argv[2];
        !           114:                        ++argv;
        !           115:                        seclth = number(BIG);
        !           116:                        argc--;
        !           117:                        if ((seclth < 128) || (tracks >1024 )) {
        !           118:                                fprintf(stderr, "cpm: illegal sector length: %d\n",seclth);
        !           119:                                exit(1);
        !           120:                        }
        !           121:                        continue;
        !           122: 
        !           123:                case 'r':
        !           124:                        string = argv[2];
        !           125:                        ++argv;
        !           126:                        sectrk = number(BIG);
        !           127:                        argc--;
        !           128:                        if (sectrk > 48) {
        !           129:                                fprintf(stderr, "cpm: illegal r-flag: %d\n", sectrk);
        !           130:                                exit(1);
        !           131:                        }
        !           132:                        continue;
        !           133: 
        !           134:                case 'C':
        !           135:                        if (cflag)
        !           136:                                stat++;
        !           137:                        cpmname = argv[3];
        !           138:                        unixname = argv[2];
        !           139:                        argc -= 2;
        !           140:                        argv += 2;
        !           141:                        Cflag++;
        !           142:                        continue;
        !           143: 
        !           144:                case 'I':
        !           145:                        Iflag++;
        !           146:                        continue;
        !           147: 
        !           148: #ifdef DEBUG
        !           149:                case 'x':
        !           150:                        xflag++;
        !           151:                        continue;
        !           152: #endif
        !           153: 
        !           154:                default:
        !           155:                        fprintf(stderr, "cpm: unknown flag: -%c\n",argv[1][1]);
        !           156:                        stat++;
        !           157:                }
        !           158:                break;
        !           159:        }
        !           160:        if (stat > 0) {
        !           161:        }
        !           162:        if (argc <= 1 && iflag) {
        !           163:                fprintf(stderr, "cpm: can't copy from stdin in iteractive mode\n");
        !           164:                exit(1);
        !           165:        } else {
        !           166:                if (argc <= 1)
        !           167:                        fid = fileno(stdin);
        !           168:                else {
        !           169:                        int ic;
        !           170:                        if ((fid = open(*++argv,2)) == -1 ) {
        !           171:                                /*
        !           172:                                 * The specified input file does not exist,
        !           173:                                 * does the user want to initialize a new
        !           174:                                 * file?
        !           175:                                 */
        !           176: 
        !           177:                                printf("Input file does not exist.\n");
        !           178:                                printf("Initialize new floppy file? (y/n)?");
        !           179:                                if ((ic = getchar()) != 'y' && ic != 'Y')
        !           180:                                        exit(1);
        !           181:                                fid = initcpm(*argv);
        !           182:                                ic = getchar();         /* get <eol> */
        !           183:                        } else {
        !           184:                                if (Iflag) {
        !           185:                                     printf("Initialize floppy file? (y/n)?");
        !           186:                                     if ((ic = getchar()) != 'y' && ic != 'Y')
        !           187:                                        exit(1);
        !           188:                                     fid = initcpm(*argv);
        !           189:                                     ic = getchar();            /* get <eol> */
        !           190:                                }
        !           191:                        }
        !           192:                }
        !           193:        }
        !           194:        /* allocate memory for the directory buffer */
        !           195:        if ((dirbuf = (struct directory *) malloc(maxdir*32)) == NULL ) {
        !           196:                fprintf(stderr, "cpm: can't allocate memory for directory buffer\n");
        !           197:                exit(1);
        !           198:        }
        !           199:        gen_sktab();
        !           200:        getdir();
        !           201:        build_bmap();
        !           202: #ifdef DEBUG
        !           203:        if (xflag > 0) {
        !           204:                int i;
        !           205:                char ch;
        !           206: 
        !           207:                dbmap("current bitmap:\n"); 
        !           208:                for (i = (int)dirbuf; i< (int)dirbuf+maxdir*32; i++) {
        !           209:                        ch = *(char *)i;
        !           210:                        putchar((int)ch);
        !           211:                }
        !           212:        }
        !           213: #endif
        !           214:        if (iflag > 0) {
        !           215:                interact();
        !           216:                printf("\n");
        !           217:                exit(0);
        !           218:        }
        !           219:        if (dflag > 0) 
        !           220:                dispdir();
        !           221:        if (cflag > 0) {
        !           222:                copy(cpmname, unixname, Bflag);
        !           223:                exit(0);
        !           224:        }
        !           225:        if (Cflag > 0) {
        !           226:                pipc(unixname, cpmname, Bflag);
        !           227:                exit(0);
        !           228:        }
        !           229:        if (tflag > 0) {
        !           230:                copy(cpmname, stdout, 0);
        !           231:                exit(0);
        !           232:        }
        !           233: }
        !           234: 
        !           235: 
        !           236: number(big)
        !           237: {
        !           238:        register char *cs;
        !           239:        long n;
        !           240: 
        !           241:        cs = string;
        !           242:        n = 0;
        !           243:        while(*cs >= '0' && *cs <= '9')
        !           244:                n = n*10 + *cs++ - '0';
        !           245:        for(;;)
        !           246:        switch(*cs++) {
        !           247: 
        !           248:        case 'k':
        !           249:                n *= 1024;
        !           250:                continue;
        !           251: 
        !           252:        case 'w':
        !           253:                n *= sizeof(int);
        !           254:                continue;
        !           255: 
        !           256:        case 'b':
        !           257:                n *= 512;
        !           258:                continue;
        !           259: 
        !           260:        case '*':
        !           261:        case 'x':
        !           262:                string = cs;
        !           263:                n *= number(BIG);
        !           264: 
        !           265:        case '\0':
        !           266:                if (n >= big || n < 0) {
        !           267:                        fprintf(stderr,"cpm: number: argument %d out of range\n", n);
        !           268:                        exit(1);
        !           269:                }
        !           270:                return (n);
        !           271:        }
        !           272: }
        !           273: 
        !           274: usage()
        !           275: {
        !           276:        printf("Usage: cpm [-i][-d][-p name][-c|C name1 name2] file-name\n");
        !           277:        exit(1);
        !           278: }

unix.superglobalmegacorp.com

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