1 /* #includes */ /*{{{C}}}*//*{{{*/
17 /* #defines */ /*{{{*/
23 /* mkfs -- make file system */ /*{{{*/
24 static int mkfs(struct cpmSuperBlock *drive, const char *name, const char *label, char *bootTracks)
26 /* variables */ /*{{{*/
35 /* open image file */ /*{{{*/
36 if ((fd = open(name, O_BINARY|O_CREAT|O_WRONLY, 0666)) < 0)
42 /* write system tracks */ /*{{{*/
43 /* this initialises only whole tracks, so it skew is not an issue */
44 trkbytes=drive->secLength*drive->sectrk;
45 for (i=0; i<trkbytes*drive->boottrk; i+=drive->secLength) if (write(fd, bootTracks+i, drive->secLength)!=drive->secLength)
52 /* write directory */ /*{{{*/
54 bytes=drive->maxdir*32;
55 if (bytes%trkbytes) bytes=((bytes+trkbytes)/trkbytes)*trkbytes;
56 if (drive->type==CPMFS_P2DOS || drive->type==CPMFS_DR3) buf[3*32]=0x21;
57 memcpy(firstbuf,buf,128);
58 if (drive->type==CPMFS_DR3)
65 for (i=0; i<11 && *label; ++i,++label) firstbuf[1+i]=toupper(*label&0x7f);
66 while (i<11) firstbuf[1+i++]=' ';
67 firstbuf[12]=0x11; /* label set and first time stamp is creation date */
68 memset(&firstbuf[13],0,1+2+8);
71 min=((t->tm_min/10)<<4)|(t->tm_min%10);
72 hour=((t->tm_hour/10)<<4)|(t->tm_hour%10);
73 for (i=1978,days=0; i < 1900 + t->tm_year; ++i)
76 if (i%4==0 && (i%100!=0 || i%400==0)) ++days;
78 days += t->tm_yday + 1;
79 firstbuf[24]=firstbuf[28]=days&0xff; firstbuf[25]=firstbuf[29]=days>>8;
80 firstbuf[26]=firstbuf[30]=hour;
81 firstbuf[27]=firstbuf[31]=min;
83 for (i=0; i < bytes; i += 128) if (write(fd, i==0 ? firstbuf : buf, 128)!=128)
90 /* close image file */ /*{{{*/
101 const char cmd[]="mkfs.cpm";
103 int main(int argc, char *argv[]) /*{{{*/
106 const char *format=FORMAT;
108 struct cpmSuperBlock drive;
109 struct cpmInode root;
110 const char *label="unlabeled";
111 size_t bootTrackSize,used;
113 const char *boot[4]={(const char*)0,(const char*)0,(const char*)0,(const char*)0};
115 while ((c=getopt(argc,argv,"b:f:L:h?"))!=EOF) switch(c)
119 if (boot[0]==(const char*)0) boot[0]=optarg;
120 else if (boot[1]==(const char*)0) boot[1]=optarg;
121 else if (boot[2]==(const char*)0) boot[2]=optarg;
122 else if (boot[3]==(const char*)0) boot[3]=optarg;
126 case 'f': format=optarg; break;
127 case 'L': label=optarg; break;
129 case '?': usage=1; break;
132 if (optind!=(argc-1)) usage=1;
133 else image=argv[optind++];
137 fprintf(stderr,"Usage: %s [-f format] [-b boot] [-L label] image\n",cmd);
141 cpmReadSuper(&drive,&root,format);
142 bootTrackSize=drive.boottrk*drive.secLength*drive.sectrk;
143 if ((bootTracks=malloc(bootTrackSize))==(void*)0)
145 fprintf(stderr,"%s: can not allocate boot track buffer: %s\n",cmd,strerror(errno));
148 memset(bootTracks,0xe5,bootTrackSize);
150 for (c=0; c<4 && boot[c]; ++c)
155 if ((fd=open(boot[c],O_BINARY|O_RDONLY))==-1)
157 fprintf(stderr,"%s: can not open %s: %s\n",cmd,boot[c],strerror(errno));
160 size=read(fd,bootTracks+used,bootTrackSize-used);
162 fprintf(stderr,"%d %04x %s\n",c,used+0x800,boot[c]);
164 if (size%drive.secLength) size=(size|(drive.secLength-1))+1;
168 if (mkfs(&drive,image,label,bootTracks)==-1)
170 fprintf(stderr,"%s: can not make new file system: %s\n",cmd,boo);