Annotation of 42BSD/etc/newfs.c, revision 1.1

1.1     ! root        1: #ifndef lint
        !             2: static char sccsid[] = "@(#)newfs.c    4.12 8/28/83";
        !             3: #endif
        !             4: 
        !             5: /*
        !             6:  * newfs: friendly front end to mkfs
        !             7:  */
        !             8: #include <sys/param.h>
        !             9: #include <sys/stat.h>
        !            10: #include <sys/fs.h>
        !            11: #include <sys/dir.h>
        !            12: 
        !            13: #include <stdio.h>
        !            14: #include <disktab.h>
        !            15: 
        !            16: #define        BOOTDIR "/usr/mdec"     /* directory for boot blocks */
        !            17: 
        !            18: int    verbose;                /* show mkfs line before exec */
        !            19: int    noboot;                 /* do not fill boot blocks */
        !            20: int    fssize;                 /* file system size */
        !            21: int    fsize;                  /* fragment size */
        !            22: int    bsize;                  /* block size */
        !            23: int    ntracks;                /* # tracks/cylinder */
        !            24: int    nsectors;               /* # sectors/track */
        !            25: int    sectorsize;             /* bytes/sector */
        !            26: int    cpg;                    /* cylinders/cylinder group */
        !            27: int    minfree = -1;           /* free space threshold */
        !            28: int    rpm;                    /* revolutions/minute of drive */
        !            29: int    density;                /* number of bytes per inode */
        !            30: 
        !            31: char   *av[20];                /* argv array and buffers for exec */
        !            32: char   a2[20];
        !            33: char   a3[20];
        !            34: char   a4[20];
        !            35: char   a5[20];
        !            36: char   a6[20];
        !            37: char   a7[20];
        !            38: char   a8[20];
        !            39: char   a9[20];
        !            40: char   a10[20];
        !            41: char   device[MAXPATHLEN];
        !            42: char   cmd[BUFSIZ];
        !            43: 
        !            44: char   *index();
        !            45: char   *rindex();
        !            46: char   *sprintf();
        !            47: 
        !            48: main(argc, argv)
        !            49:        int argc;
        !            50:        char *argv[];
        !            51: {
        !            52:        char *cp, *special;
        !            53:        register struct disktab *dp;
        !            54:        register struct partition *pp;
        !            55:        struct stat st;
        !            56:        register int i;
        !            57:        int status;
        !            58: 
        !            59:        argc--, argv++;
        !            60:        while (argc > 0 && argv[0][0] == '-') {
        !            61:                for (cp = &argv[0][1]; *cp; cp++)
        !            62:                        switch (*cp) {
        !            63: 
        !            64:                        case 'v':
        !            65:                                verbose++;
        !            66:                                break;
        !            67: 
        !            68:                        case 'n':
        !            69:                                noboot++;
        !            70:                                break;
        !            71: 
        !            72:                        case 's':
        !            73:                                if (argc < 1)
        !            74:                                        fatal("-s: missing file system size");
        !            75:                                argc--, argv++;
        !            76:                                fssize = atoi(*argv);
        !            77:                                if (fssize < 0)
        !            78:                                        fatal("%s: bad file system size",
        !            79:                                                *argv);
        !            80:                                goto next;
        !            81: 
        !            82:                        case 't':
        !            83:                                if (argc < 1)
        !            84:                                        fatal("-t: missing track total");
        !            85:                                argc--, argv++;
        !            86:                                ntracks = atoi(*argv);
        !            87:                                if (ntracks < 0)
        !            88:                                        fatal("%s: bad total tracks", *argv);
        !            89:                                goto next;
        !            90: 
        !            91:                        case 'b':
        !            92:                                if (argc < 1)
        !            93:                                        fatal("-b: missing block size");
        !            94:                                argc--, argv++;
        !            95:                                bsize = atoi(*argv);
        !            96:                                if (bsize < 0 || bsize < MINBSIZE)
        !            97:                                        fatal("%s: bad block size", *argv);
        !            98:                                goto next;
        !            99: 
        !           100:                        case 'f':
        !           101:                                if (argc < 1)
        !           102:                                        fatal("-f: missing frag size");
        !           103:                                argc--, argv++;
        !           104:                                fsize = atoi(*argv);
        !           105:                                if (fsize < 0)
        !           106:                                        fatal("%s: bad frag size", *argv);
        !           107:                                goto next;
        !           108: 
        !           109:                        case 'S':
        !           110:                                if (argc < 1)
        !           111:                                        fatal("-S: missing sector size");
        !           112:                                argc--, argv++;
        !           113:                                sectorsize = atoi(*argv);
        !           114:                                if (sectorsize < 0)
        !           115:                                        fatal("%s: bad sector size", *argv);
        !           116:                                goto next;
        !           117: 
        !           118:                        case 'c':
        !           119:                                if (argc < 1)
        !           120:                                        fatal("-c: missing cylinders/group");
        !           121:                                argc--, argv++;
        !           122:                                cpg = atoi(*argv);
        !           123:                                if (cpg < 0)
        !           124:                                        fatal("%s: bad cylinders/group", *argv);
        !           125:                                goto next;
        !           126: 
        !           127:                        case 'm':
        !           128:                                if (argc < 1)
        !           129:                                        fatal("-m: missing free space %%\n");
        !           130:                                argc--, argv++;
        !           131:                                minfree = atoi(*argv);
        !           132:                                if (minfree < 0 || minfree > 99)
        !           133:                                        fatal("%s: bad free space %%\n",
        !           134:                                                *argv);
        !           135:                                goto next;
        !           136: 
        !           137:                        case 'r':
        !           138:                                if (argc < 1)
        !           139:                                        fatal("-r: missing revs/minute\n");
        !           140:                                argc--, argv++;
        !           141:                                rpm = atoi(*argv);
        !           142:                                if (rpm < 0)
        !           143:                                        fatal("%s: bad revs/minute\n", *argv);
        !           144:                                goto next;
        !           145: 
        !           146:                        case 'i':
        !           147:                                if (argc < 1)
        !           148:                                        fatal("-i: missing bytes per inode\n");
        !           149:                                argc--, argv++;
        !           150:                                density = atoi(*argv);
        !           151:                                if (density < 0)
        !           152:                                        fatal("%s: bad bytes per inode\n",
        !           153:                                                *argv);
        !           154:                                goto next;
        !           155: 
        !           156:                        default:
        !           157:                                fatal("-%c: unknown flag", cp);
        !           158:                        }
        !           159: next:
        !           160:                argc--, argv++;
        !           161:        }
        !           162:        if (argc < 2) {
        !           163:                fprintf(stderr, "usage: newfs [ -v ] [ mkfs-options ] %s\n",
        !           164:                        "special-device device-type");
        !           165:                fprintf(stderr, "where mkfs-options are:\n");
        !           166:                fprintf(stderr, "\t-s file system size (sectors)\n");
        !           167:                fprintf(stderr, "\t-b block size\n");
        !           168:                fprintf(stderr, "\t-f frag size\n");
        !           169:                fprintf(stderr, "\t-t tracks/cylinder\n");
        !           170:                fprintf(stderr, "\t-c cylinders/group\n");
        !           171:                fprintf(stderr, "\t-m minimum free space %%\n");
        !           172:                fprintf(stderr, "\t-r revolutions/minute\n");
        !           173:                fprintf(stderr, "\t-S sector size\n");
        !           174:                fprintf(stderr, "\t-i number of bytes per inode\n");
        !           175:                exit(1);
        !           176:        }
        !           177:        special = argv[0];
        !           178:        cp = rindex(special, '/');
        !           179:        if (cp != 0)
        !           180:                special = cp + 1;
        !           181:        if (*special == 'r' && special[1] != 'a' && special[1] != 'b')
        !           182:                special++;
        !           183:        special = sprintf(device, "/dev/r%s", special);
        !           184:        if (stat(special, &st) < 0) {
        !           185:                fprintf(stderr, "newfs: "); perror(special);
        !           186:                exit(2);
        !           187:        }
        !           188:        if ((st.st_mode & S_IFMT) != S_IFCHR)
        !           189:                fatal("%s: not a character device", special);
        !           190:        dp = getdiskbyname(argv[1]);
        !           191:        if (dp == 0)
        !           192:                fatal("%s: unknown disk type", argv[1]);
        !           193:        cp = index(argv[0], '\0') - 1;
        !           194:        if (cp == 0 || *cp < 'a' || *cp > 'h')
        !           195:                fatal("%s: can't figure out file system partition", argv[0]);
        !           196:        pp = &dp->d_partitions[*cp - 'a'];
        !           197:        if (fssize == 0) {
        !           198:                fssize = pp->p_size;
        !           199:                if (fssize < 0)
        !           200:                        fatal("%s: no default size for `%c' partition",
        !           201:                                argv[1], *cp);
        !           202:        }
        !           203:        if (nsectors == 0) {
        !           204:                nsectors = dp->d_nsectors;
        !           205:                if (nsectors < 0)
        !           206:                        fatal("%s: no default #sectors/track", argv[1]);
        !           207:        }
        !           208:        if (ntracks == 0) {
        !           209:                ntracks = dp->d_ntracks;
        !           210:                if (ntracks < 0)
        !           211:                        fatal("%s: no default #tracks", argv[1]);
        !           212:        }
        !           213:        if (sectorsize == 0) {
        !           214:                sectorsize = dp->d_secsize;
        !           215:                if (sectorsize < 0)
        !           216:                        fatal("%s: no default sector size", argv[1]);
        !           217:        }
        !           218:        if (bsize == 0) {
        !           219:                bsize = pp->p_bsize;
        !           220:                if (bsize < 0)
        !           221:                        fatal("%s: no default block size for `%c' partition",
        !           222:                                argv[1], *cp);
        !           223:        }
        !           224:        if (fsize == 0) {
        !           225:                fsize = pp->p_fsize;
        !           226:                if (fsize < 0)
        !           227:                        fatal("%s: no default frag size for `%c' partition",
        !           228:                                argv[1], *cp);
        !           229:        }
        !           230:        if (rpm == 0) {
        !           231:                rpm = dp->d_rpm;
        !           232:                if (rpm < 0)
        !           233:                        fatal("%s: no default revolutions/minute value",
        !           234:                                argv[1]);
        !           235:        }
        !           236:        if (density <= 0)
        !           237:                density = 2048;
        !           238:        if (minfree < 0)
        !           239:                minfree = 10;
        !           240:        if (cpg == 0)
        !           241:                cpg = 16;
        !           242:        i = 0;
        !           243:        av[i++] = sprintf(a2, "%d", fssize);
        !           244:        av[i++] = sprintf(a3, "%d", nsectors);
        !           245:        av[i++] = sprintf(a4, "%d", ntracks);
        !           246:        av[i++] = sprintf(a5, "%d", bsize);
        !           247:        av[i++] = sprintf(a6, "%d", fsize);
        !           248:        av[i++] = sprintf(a7, "%d", cpg);
        !           249:        av[i++] = sprintf(a8, "%d", minfree);
        !           250:        av[i++] = sprintf(a9, "%d", rpm / 60);
        !           251:        av[i++] = sprintf(a10, "%d", density);
        !           252:        av[i++] = 0;
        !           253:        sprintf(cmd, "/etc/mkfs %s", special);
        !           254:        for (i = 0; av[i] != 0; i++) {
        !           255:                strcat(cmd, " ");
        !           256:                strcat(cmd, av[i]);
        !           257:        }
        !           258:        if (verbose)
        !           259:                printf("%s\n", cmd);
        !           260:        if (status = system(cmd))
        !           261:                exit(status);
        !           262:        if (*cp == 'a' && !noboot) {
        !           263:                char type[3];
        !           264:                struct stat sb;
        !           265: 
        !           266:                cp = rindex(special, '/');
        !           267:                if (cp == NULL)
        !           268:                        fatal("%s: can't figure out disk type from name",
        !           269:                                special);
        !           270:                if (stat(special, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFCHR)
        !           271:                        cp++;
        !           272:                type[0] = *++cp;
        !           273:                type[1] = *++cp;
        !           274:                type[2] = '\0';
        !           275:                installboot(special, type);
        !           276:        }
        !           277:        exit(0);
        !           278: }
        !           279: 
        !           280: installboot(dev, type)
        !           281:        char *dev, *type;
        !           282: {
        !           283:        int fd;
        !           284:        char bootblock[MAXPATHLEN], standalonecode[MAXPATHLEN];
        !           285:        char bootimage[BBSIZE];
        !           286: 
        !           287:        sprintf(bootblock, "%s/%sboot", BOOTDIR, type);
        !           288:        sprintf(standalonecode, "%s/boot%s", BOOTDIR, type);
        !           289:        if (verbose) {
        !           290:                printf("installing boot code\n");
        !           291:                printf("sector 0 boot = %s\n", bootblock);
        !           292:                printf("1st level boot = %s\n", standalonecode);
        !           293:        }
        !           294:        fd = open(bootblock, 0);
        !           295:        if (fd < 0) {
        !           296:                fprintf(stderr, "newfs: "); perror(bootblock);
        !           297:                exit(1);
        !           298:        }
        !           299:        if (read(fd, bootimage, DEV_BSIZE) < 0) {
        !           300:                fprintf(stderr, "newfs: "); perror(bootblock);
        !           301:                exit(2);
        !           302:        }
        !           303:        close(fd);
        !           304:        fd = open(standalonecode, 0);
        !           305:        if (fd < 0) {
        !           306:                fprintf(stderr, "newfs: "); perror(standalonecode);
        !           307:                exit(1);
        !           308:        }
        !           309:        if (read(fd, &bootimage[DEV_BSIZE], BBSIZE - DEV_BSIZE) < 0) {
        !           310:                fprintf(stderr, "newfs: "); perror(standalonecode);
        !           311:                exit(2);
        !           312:        }
        !           313:        close(fd);
        !           314:        fd = open(dev, 1);
        !           315:        if (fd < 0) {
        !           316:                fprintf(stderr, "newfs: "); perror(dev);
        !           317:                exit(1);
        !           318:        }
        !           319:        if (write(fd, bootimage, BBSIZE) != BBSIZE) {
        !           320:                fprintf(stderr, "newfs: "); perror(dev);
        !           321:                exit(2);
        !           322:        }
        !           323:        close(fd);
        !           324: }
        !           325: 
        !           326: /*VARARGS*/
        !           327: fatal(fmt, arg1, arg2)
        !           328:        char *fmt;
        !           329: {
        !           330: 
        !           331:        fprintf(stderr, "newfs: ");
        !           332:        fprintf(stderr, fmt, arg1, arg2);
        !           333:        putc('\n', stderr);
        !           334:        exit(10);
        !           335: }

unix.superglobalmegacorp.com

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