1 /* #includes */ /*{{{C}}}*//*{{{*/
14 /* #defines */ /*{{{*/
20 /* mkfs -- make file system */ /*{{{*/
21 static int mkfs(struct cpmSuperBlock *drive, const char *name, const char *format, const char *label, char *bootTracks, int timeStamps, int uppercase)
23 /* variables */ /*{{{*/
29 unsigned int trkbytes;
32 /* open image file */ /*{{{*/
33 if ((fd = open(name, O_BINARY|O_CREAT|O_WRONLY, 0666)) < 0)
39 /* write system tracks */ /*{{{*/
40 /* this initialises only whole tracks, so it skew is not an issue */
41 trkbytes=drive->secLength*drive->sectrk;
42 for (i=0; i<trkbytes*drive->boottrk; i+=drive->secLength) if (write(fd, bootTracks+i, drive->secLength)!=(ssize_t)drive->secLength)
49 /* write directory */ /*{{{*/
51 bytes=drive->maxdir*32;
52 if (bytes%trkbytes) bytes=((bytes+trkbytes)/trkbytes)*trkbytes;
53 if (timeStamps && (drive->type==CPMFS_P2DOS || drive->type==CPMFS_DR3)) buf[3*32]=0x21;
54 memcpy(firstbuf,buf,128);
55 if (drive->type==CPMFS_DR3)
62 for (i=0; i<11 && *label; ++i,++label) firstbuf[1+i]=toupper(*label&0x7f);
63 while (i<11) firstbuf[1+i++]=' ';
64 firstbuf[12]=timeStamps ? 0x11 : 0x01; /* label set and first time stamp is creation date */
65 memset(&firstbuf[13],0,1+2+8);
73 min=((t->tm_min/10)<<4)|(t->tm_min%10);
74 hour=((t->tm_hour/10)<<4)|(t->tm_hour%10);
75 for (year=1978,days=0; year<1900+t->tm_year; ++year)
78 if (year%4==0 && (year%100!=0 || year%400==0)) ++days;
80 days += t->tm_yday + 1;
81 firstbuf[24]=firstbuf[28]=days&0xff; firstbuf[25]=firstbuf[29]=days>>8;
82 firstbuf[26]=firstbuf[30]=hour;
83 firstbuf[27]=firstbuf[31]=min;
86 for (i=0; i<bytes; i+=128) if (write(fd, i==0 ? firstbuf : buf, 128)!=128)
93 /* close image file */ /*{{{*/
100 if (timeStamps && !(drive->type==CPMFS_P2DOS || drive->type==CPMFS_DR3)) /*{{{*/
103 struct cpmInode ino, root;
104 static const char sig[] = "!!!TIME";
105 unsigned int records;
107 struct cpmSuperBlock super;
110 if ((err=Device_open(&super.dev,name,O_RDWR,NULL)))
112 fprintf(stderr,"%s: can not open %s (%s)\n",cmd,name,err);
115 cpmReadSuper(&super,&root,format,uppercase);
117 records=root.sb->maxdir/8;
118 if (!(ds=malloc(records*128)))
123 memset(ds,0,records*128);
125 for (i=0; i<records; i++)
127 for (j=0; j<7; j++,offset+=16)
129 *((char*)ds+offset) = sig[j];
131 /* skip checksum byte */
135 /* Set things up so cpmSync will generate checksums and write the
138 if (cpmCreat(&root,"00!!!TIME&.DAT",&ino,0)==-1)
140 fprintf(stderr,"%s: Unable to create DateStamper file: %s\n",cmd,boo);
153 const char cmd[]="mkfs.cpm";
155 int main(int argc, char *argv[]) /*{{{*/
161 struct cpmSuperBlock drive;
162 struct cpmInode root;
163 const char *label="unlabeled";
165 size_t bootTrackSize,used;
167 const char *boot[4]={(const char*)0,(const char*)0,(const char*)0,(const char*)0};
169 if (!(format=getenv("CPMTOOLSFMT"))) format=FORMAT;
170 while ((c=getopt(argc,argv,"b:f:L:tuh?"))!=EOF) switch(c)
174 if (boot[0]==(const char*)0) boot[0]=optarg;
175 else if (boot[1]==(const char*)0) boot[1]=optarg;
176 else if (boot[2]==(const char*)0) boot[2]=optarg;
177 else if (boot[3]==(const char*)0) boot[3]=optarg;
181 case 'f': format=optarg; break;
182 case 'L': label=optarg; break;
183 case 't': timeStamps=1; break;
184 case 'u': uppercase=1; break;
186 case '?': usage=1; break;
189 if (optind!=(argc-1)) usage=1;
190 else image=argv[optind++];
194 fprintf(stderr,"Usage: %s [-f format] [-b boot] [-L label] [-t] [-u] image\n",cmd);
198 cpmReadSuper(&drive,&root,format,uppercase);
199 bootTrackSize=drive.boottrk*drive.secLength*drive.sectrk;
200 if ((bootTracks=malloc(bootTrackSize))==(void*)0)
202 fprintf(stderr,"%s: can not allocate boot track buffer: %s\n",cmd,strerror(errno));
205 memset(bootTracks,0xe5,bootTrackSize);
207 for (c=0; c<4 && boot[c]; ++c)
212 if ((fd=open(boot[c],O_BINARY|O_RDONLY))==-1)
214 fprintf(stderr,"%s: can not open %s: %s\n",cmd,boot[c],strerror(errno));
217 size=read(fd,bootTracks+used,bootTrackSize-used);
219 fprintf(stderr,"%d %04x %s\n",c,used+0x800,boot[c]);
221 if (size%drive.secLength) size=(size|(drive.secLength-1))+1;
225 if (mkfs(&drive,image,format,label,bootTracks,timeStamps,uppercase)==-1)
227 fprintf(stderr,"%s: can not make new file system: %s\n",cmd,boo);