1 /* #includes */ /*{{{C}}}*//*{{{*/
20 /* #defines */ /*{{{*/
23 /* Number of _used_ bits per int */
25 #define INTBITS ((int)(sizeof(int)*8))
27 /* There are four reserved entries: ., .., [passwd] and [label] */
29 #define RESERVED_ENTRIES 4
31 /* CP/M does not support any kind of inodes, so they are simulated.
32 Inode 0-(maxdir-1) are used by files which lowest extent is stored in
33 the respective directory entry. Inode maxdir is the root directory
34 and inode maxdir+1 is the passwd file, if any. */
36 #define RESERVED_INODES 3
38 #define PASSWD_RECLEN 24
41 extern char **environ;
43 static mode_t s_ifdir=1;
44 static mode_t s_ifreg=1;
46 /* memcpy7 -- Copy string, leaving 8th bit alone */ /*{{{*/
47 static void memcpy7(char *dest, const char *src, int count)
51 *dest = ((*dest) & 0x80) | ((*src) & 0x7F);
58 /* file name conversions */
59 /* splitFilename -- split file name into name and extension */ /*{{{*/
60 static int splitFilename(const char *fullname, int type, char *name, char *ext, int *user)
64 assert(fullname!=(const char*)0);
65 assert(name!=(char*)0);
66 assert(ext!=(char*)0);
67 assert(user!=(int*)0);
70 if (!isdigit(fullname[0]) || !isdigit(fullname[1]))
72 boo="illegal CP/M filename";
75 *user=10*(fullname[0]-'0')+(fullname[1]-'0');
77 if ((fullname[0]=='\0') || (type==CPMFS_DR22 && *user>=16) || (type==CPMFS_P2DOS && *user>=32))
79 boo="illegal CP/M filename";
82 for (i=0; i<8 && fullname[i] && fullname[i]!='.'; ++i) if (!ISFILECHAR(i,fullname[i]))
84 boo="illegal CP/M filename";
87 else name[i]=toupper(fullname[i]);
91 for (j=0; j<3 && fullname[i]; ++i,++j) if (!ISFILECHAR(1,fullname[i]))
93 boo="illegal CP/M filename";
96 else ext[j]=toupper(fullname[i]);
99 boo="illegal CP/M filename";
106 /* isMatching -- do two file names match? */ /*{{{*/
107 static int isMatching(int user1, const char *name1, const char *ext1, int user2, const char *name2, const char *ext2)
111 assert(name1!=(const char*)0);
112 assert(ext1!=(const char*)0);
113 assert(name2!=(const char*)0);
114 assert(ext2!=(const char*)0);
115 if (user1!=user2) return 0;
116 for (i=0; i<8; ++i) if ((name1[i]&0x7f)!=(name2[i]&0x7f)) return 0;
117 for (i=0; i<3; ++i) if ((ext1[i]&0x7f)!=(ext2[i]&0x7f)) return 0;
122 /* time conversions */
123 /* cpm2unix_time -- convert CP/M time to UTC */ /*{{{*/
124 static time_t cpm2unix_time(int days, int hour, int min)
126 /* CP/M stores timestamps in local time. We don't know which */
127 /* timezone was used and if DST was in effect. Assuming it was */
128 /* the current offset from UTC is most sensible, but not perfect. */
130 int year,days_per_year;
131 static int days_per_month[]={31,0,31,30,31,30,31,31,30,31,30,31};
133 static char gmt0[]="TZ=GMT0";
134 static char *gmt_env[]={ gmt0, (char*)0 };
146 tms.tm_min=((min>>4)&0xf)*10+(min&0xf);
147 tms.tm_hour=((hour>>4)&0xf)*10+(hour&0xf);
154 year=tms.tm_year+1900;
155 days_per_year=((year%4)==0 && ((year%100) || (year%400)==0)) ? 366 : 365;
156 if (days>days_per_year)
165 days_per_month[1]=(days_per_year==366) ? 29 : 28;
166 if (days>days_per_month[tms.tm_mon])
168 days-=days_per_month[tms.tm_mon];
173 t=mktime(&tms)+(days-1)*24*3600;
179 /* unix2cpm_time -- convert UTC to CP/M time */ /*{{{*/
180 static void unix2cpm_time(time_t now, int *days, int *hour, int *min)
186 *min=((tms->tm_min/10)<<4)|(tms->tm_min%10);
187 *hour=((tms->tm_hour/10)<<4)|(tms->tm_hour%10);
188 for (i=1978,*days=0; i<1900+tms->tm_year; ++i)
191 if (i%4==0 && (i%100!=0 || i%400==0)) ++*days;
193 *days += tms->tm_yday+1;
197 /* allocation vector bitmap functions */
198 /* alvInit -- init allocation vector */ /*{{{*/
199 static void alvInit(const struct cpmSuperBlock *d)
201 int i,j,offset,block;
203 assert(d!=(const struct cpmSuperBlock*)0);
204 /* clean bitmap */ /*{{{*/
205 memset(d->alv,0,d->alvSize*sizeof(int));
207 /* mark directory blocks as used */ /*{{{*/
208 *d->alv=(1<<((d->maxdir*32+d->blksiz-1)/d->blksiz))-1;
210 for (i=0; i<d->maxdir; ++i) /* mark file blocks as used */ /*{{{*/
212 if (d->dir[i].status>=0 && d->dir[i].status<=(d->type==CPMFS_P2DOS ? 31 : 15))
215 fprintf(stderr,"alvInit: allocate extent %d\n",i);
219 block=(unsigned char)d->dir[i].pointers[j];
220 if (d->size>=256) block+=(((unsigned char)d->dir[i].pointers[++j])<<8);
221 if (block && block<d->size)
224 fprintf(stderr,"alvInit: allocate block %d\n",block);
226 offset=block/INTBITS;
227 d->alv[offset]|=(1<<block%INTBITS);
235 /* allocBlock -- allocate a new disk block */ /*{{{*/
236 static int allocBlock(const struct cpmSuperBlock *drive)
240 assert(drive!=(const struct cpmSuperBlock*)0);
241 for (i=0; i<drive->alvSize; ++i)
243 for (j=0,bits=drive->alv[i]; j<INTBITS; ++j)
248 if (block>=drive->size)
253 drive->alv[i] |= (1<<j);
264 /* logical block I/O */
265 /* readBlock -- read a (partial) block */ /*{{{*/
266 static int readBlock(const struct cpmSuperBlock *d, int blockno, char *buffer, int start, int end)
268 int sect, track, counter;
271 assert(blockno<d->size);
272 assert(buffer!=(char*)0);
273 if (end<0) end=d->blksiz/d->secLength-1;
274 sect=(blockno*(d->blksiz/d->secLength)+ d->sectrk*d->boottrk)%d->sectrk;
275 track=(blockno*(d->blksiz/d->secLength)+ d->sectrk*d->boottrk)/d->sectrk;
276 for (counter=0; counter<=end; ++counter)
280 if (counter>=start && (err=Device_readSector(&d->dev,track,d->skewtab[sect],buffer+(d->secLength*counter))))
295 /* writeBlock -- write a (partial) block */ /*{{{*/
296 static int writeBlock(const struct cpmSuperBlock *d, int blockno, const char *buffer, int start, int end)
298 int sect, track, counter;
301 assert(blockno<d->size);
302 assert(buffer!=(const char*)0);
303 if (end < 0) end=d->blksiz/d->secLength-1;
304 sect = (blockno*(d->blksiz/d->secLength))%d->sectrk;
305 track = (blockno*(d->blksiz/d->secLength))/d->sectrk+d->boottrk;
306 for (counter = 0; counter<=end; ++counter)
310 if (counter>=start && (err=Device_writeSector(&d->dev,track,d->skewtab[sect],buffer+(d->secLength*counter))))
326 /* directory management */
327 /* readPhysDirectory -- read directory from drive */ /*{{{*/
328 static int readPhysDirectory(const struct cpmSuperBlock *drive)
332 blocks=(drive->maxdir*32+drive->blksiz-1)/drive->blksiz;
334 for (i=0; i<blocks; ++i)
336 if (readBlock(drive,i,(char*)(drive->dir+entry),0,-1)==-1) return -1;
337 entry+=(drive->blksiz/32);
342 /* writePhysDirectory -- write directory to drive */ /*{{{*/
343 static int writePhysDirectory(const struct cpmSuperBlock *drive)
347 blocks=(drive->maxdir*32+drive->blksiz-1)/drive->blksiz;
349 for (i=0; i<blocks; ++i)
351 if (writeBlock(drive,i,(char*)(drive->dir+entry),0,-1)==-1) return -1;
352 entry+=(drive->blksiz/32);
357 /* findFileExtent -- find first/next extent for a file */ /*{{{*/
358 static int findFileExtent(const struct cpmSuperBlock *sb, int user, const char *name, const char *ext, int start, int extno)
360 boo="file already exists";
361 for (; start<sb->maxdir; ++start)
365 ((unsigned char)sb->dir[start].status)<=(sb->type==CPMFS_P2DOS ? 31 : 15)
366 && (extno==-1 || (EXTENT(sb->dir[start].extnol,sb->dir[start].extnoh)/sb->extents)==(extno/sb->extents))
367 && isMatching(user,name,ext,sb->dir[start].status,sb->dir[start].name,sb->dir[start].ext)
370 boo="file not found";
374 /* findFreeExtent -- find first free extent */ /*{{{*/
375 static int findFreeExtent(const struct cpmSuperBlock *drive)
379 for (i=0; i<drive->maxdir; ++i) if (drive->dir[i].status==(char)0xe5) return (i);
380 boo="directory full";
384 /* updateTimeStamps -- convert time stamps to CP/M format */ /*{{{*/
385 static void updateTimeStamps(const struct cpmInode *ino, int extent)
387 struct PhysDirectoryEntry *date;
389 int ca_min,ca_hour,ca_days,u_min,u_hour,u_days;
391 if (!S_ISREG(ino->mode)) return;
393 fprintf(stderr,"CPMFS: updating time stamps for inode %d (%d)\n",extent,extent&3);
395 unix2cpm_time(ino->sb->cnotatime ? ino->ctime : ino->atime,&ca_days,&ca_hour,&ca_min);
396 unix2cpm_time(ino->mtime,&u_days,&u_hour,&u_min);
397 if ((ino->sb->type==CPMFS_P2DOS || ino->sb->type==CPMFS_DR3) && (date=ino->sb->dir+(extent|3))->status==0x21)
401 case 0: /* first entry */ /*{{{*/
403 date->name[0]=ca_days&0xff; date->name[1]=ca_days>>8;
404 date->name[2]=ca_hour;
405 date->name[3]=ca_min;
406 date->name[4]=u_days&0xff; date->name[5]=u_days>>8;
407 date->name[6]=u_hour;
412 case 1: /* second entry */ /*{{{*/
414 date->ext[2]=ca_days&0xff; date->extnol=ca_days>>8;
417 date->blkcnt=u_days&0xff; date->pointers[0]=u_days>>8;
418 date->pointers[1]=u_hour;
419 date->pointers[2]=u_min;
423 case 2: /* third entry */ /*{{{*/
425 date->pointers[5]=ca_days&0xff; date->pointers[6]=ca_days>>8;
426 date->pointers[7]=ca_hour;
427 date->pointers[8]=ca_min;
428 date->pointers[9]=u_days&0xff; date->pointers[10]=u_days>>8;
429 date->pointers[11]=u_hour;
430 date->pointers[12]=u_min;
439 /* diskdefReadSuper -- read super block from diskdefs file */ /*{{{*/
440 static int diskdefReadSuper(struct cpmSuperBlock *d, const char *format)
444 int insideDef=0,found=0;
446 if ((fp=fopen(DISKDEFS,"r"))==(FILE*)0 && (fp=fopen("diskdefs","r"))==(FILE*)0)
448 fprintf(stderr,"%s: Neither " DISKDEFS " nor diskdefs could be opened.\n",cmd);
451 while (fgets(line,sizeof(line),fp)!=(char*)0)
456 for (argc=0; argc<1 && (argv[argc]=strtok(argc ? (char*)0 : line," \t\n")); ++argc);
457 if ((argv[argc]=strtok((char*)0,"\n"))!=(char*)0) ++argc;
460 if (argc==1 && strcmp(argv[0],"end")==0)
463 d->size=(d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz;
464 if (d->extents==0) d->extents=((d->size>=256 ? 8 : 16)*d->blksiz)/16384;
465 if (d->extents==0) d->extents=1;
470 if (strcmp(argv[0],"seclen")==0) d->secLength=strtol(argv[1],(char**)0,0);
471 else if (strcmp(argv[0],"tracks")==0) d->tracks=strtol(argv[1],(char**)0,0);
472 else if (strcmp(argv[0],"sectrk")==0) d->sectrk=strtol(argv[1],(char**)0,0);
473 else if (strcmp(argv[0],"blocksize")==0) d->blksiz=strtol(argv[1],(char**)0,0);
474 else if (strcmp(argv[0],"maxdir")==0) d->maxdir=strtol(argv[1],(char**)0,0);
475 else if (strcmp(argv[0],"skew")==0) d->skew=strtol(argv[1],(char**)0,0);
476 else if (strcmp(argv[0],"boottrk")==0) d->boottrk=strtol(argv[1],(char**)0,0);
477 else if (strcmp(argv[0],"logicalextents")==0) d->extents=strtol(argv[1],(char**)0,0);
478 else if (strcmp(argv[0],"os")==0)
480 if (strcmp(argv[1],"2.2")==0) d->type=CPMFS_DR22;
481 else if (strcmp(argv[1],"3")==0) d->type=CPMFS_DR3;
482 else if (strcmp(argv[1],"p2dos")==0) d->type=CPMFS_P2DOS;
485 else if (argc>0 && argv[0][0]!='#')
487 fprintf(stderr,"%s: invalid keyword `%s'\n",cmd,argv[0]);
491 else if (argc==2 && strcmp(argv[0],"diskdef")==0)
497 if (strcmp(argv[1],format)==0) found=1;
503 fprintf(stderr,"%s: unknown format %s\n",cmd,format);
509 /* amsReadSuper -- read super block from amstrad disk */ /*{{{*/
510 static int amsReadSuper(struct cpmSuperBlock *d, const char *format)
512 unsigned char boot_sector[512], *boot_spec;
515 Device_setGeometry(&d->dev,512,9,40);
516 if ((err=Device_readSector(&d->dev, 0, 0, (char *)boot_sector)))
518 fprintf(stderr,"%s: Failed to read Amstrad superblock (%s)\n",cmd,err);
521 boot_spec=(boot_sector[0] == 0 || boot_sector[0] == 3)?boot_sector:(unsigned char*)0;
522 /* Check for JCE's extension to allow Amstrad and MSDOS superblocks
523 * in the same sector (for the PCW16)
527 (boot_sector[0] == 0xE9 || boot_sector[0] == 0xEB)
528 && !memcmp(boot_sector + 0x2B, "CP/M", 4)
529 && !memcmp(boot_sector + 0x33, "DSK", 3)
530 && !memcmp(boot_sector + 0x7C, "CP/M", 4)
531 ) boot_spec = boot_sector + 128;
532 if (boot_spec==(unsigned char*)0)
534 fprintf(stderr,"%s: Amstrad superblock not present\n",cmd);
537 /* boot_spec[0] = format number: 0 for SS SD, 3 for DS DD
538 [1] = single/double sided and density flags
539 [2] = cylinders per side
540 [3] = sectors per cylinder
541 [4] = Physical sector shift, 2 => 512
542 [5] = Reserved track count
544 [7] = No. of directory blocks
546 d->type = CPMFS_DR3; /* Amstrads are CP/M 3 systems */
547 d->secLength = 128 << boot_spec[4];
548 d->tracks = boot_spec[2];
549 if (boot_spec[1] & 3) d->tracks *= 2;
550 d->sectrk = boot_spec[3];
551 d->blksiz = 128 << boot_spec[6];
552 d->maxdir = (d->blksiz / 32) * boot_spec[7];
553 d->skew = 1; /* Amstrads skew at the controller level */
554 d->boottrk = boot_spec[5];
555 d->size = (d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz;
556 d->extents = ((d->size>=256 ? 8 : 16)*d->blksiz)/16384;
562 /* match -- match filename against a pattern */ /*{{{*/
563 static int recmatch(const char *a, const char *pattern)
573 if (*a=='.' && first) return 1;
575 while (*a) if (recmatch(a,pattern)) return 1; else ++a;
580 if (*a) { ++a; ++pattern; } else return 0;
583 default: if (tolower(*a)==tolower(*pattern)) { ++a; ++pattern; } else return 0;
587 return (*pattern=='\0' && *a=='\0');
590 int match(const char *a, const char *pattern)
595 assert(strlen(pattern)<255);
596 if (isdigit(*pattern) && *(pattern+1)==':') { user=(*pattern-'0'); pattern+=2; }
597 else if (isdigit(*pattern) && isdigit(*(pattern+1)) && *(pattern+2)==':') { user=(10*(*pattern-'0')+(*(pattern+1)-'0')); pattern+=3; }
599 if (user==-1) sprintf(pat,"??%s",pattern);
600 else sprintf(pat,"%02d%s",user,pattern);
601 return recmatch(a,pat);
605 /* cpmglob -- expand CP/M style wildcards */ /*{{{*/
606 void cpmglob(int optin, int argc, char * const argv[], struct cpmInode *root, int *gargc, char ***gargv)
609 int entries,dirsize=0;
610 struct cpmDirent *dirent=(struct cpmDirent*)0;
615 cpmOpendir(root,&dir);
618 dirent=malloc(sizeof(struct cpmDirent)*dirsize);
619 while (cpmReaddir(&dir,&dirent[entries]))
622 if (entries==dirsize) dirent=realloc(dirent,sizeof(struct cpmDirent)*(dirsize*=2));
624 for (i=optin; i<argc; ++i)
628 for (j=0,found=0; j<entries; ++j)
630 if (match(dirent[j].name,argv[i]))
632 if (*gargc==gargcap) *gargv=realloc(*gargv,sizeof(char*)*(gargcap ? (gargcap*=2) : (gargcap=16)));
633 (*gargv)[*gargc]=strcpy(malloc(strlen(dirent[j].name)+1),dirent[j].name);
641 char *pattern=argv[i];
644 if (isdigit(*pattern) && *(pattern+1)==':') { user=(*pattern-'0'); pattern+=2; }
645 else if (isdigit(*pattern) && isdigit(*(pattern+1)) && *(pattern+2)==':') { user=(10*(*pattern-'0')+(*(pattern+1)-'0')); pattern+=3; }
647 if (user==-1) sprintf(pat,"??%s",pattern);
648 else sprintf(pat,"%02d%s",user,pattern);
650 if (*gargc==gargcap) *gargv=realloc(*gargv,sizeof(char*)*(gargcap ? (gargcap*=2) : (gargcap=16)));
651 (*gargv)[*gargc]=strcpy(malloc(strlen(pat)+1),pat);
659 /* cpmReadSuper -- get DPB and init in-core data for drive */ /*{{{*/
660 int cpmReadSuper(struct cpmSuperBlock *d, struct cpmInode *root, const char *format)
662 while (s_ifdir && !S_ISDIR(s_ifdir)) s_ifdir<<=1;
664 while (s_ifreg && !S_ISREG(s_ifreg)) s_ifreg<<=1;
666 if (strcmp(format, "amstrad")==0) amsReadSuper(d,format);
667 else diskdefReadSuper(d,format);
668 Device_setGeometry(&d->dev,d->secLength,d->sectrk,d->tracks);
669 /* generate skew table */ /*{{{*/
670 if (( d->skewtab = malloc(d->sectrk*sizeof(int))) == (int*)0)
672 fprintf(stderr,"%s: can not allocate memory for skew sector table\n",cmd);
675 if (strcmp(format,"apple-do")==0)
677 static int skew[]={0,6,12,3,9,15,14,5,11,2,8,7,13,4,10,1};
678 memcpy(d->skewtab,skew,d->sectrk*sizeof(int));
680 else if (strcmp(format,"apple-po")==0)
682 static int skew[]={0,9,3,12,6,15,1,10,4,13,7,8,2,11,5,14};
683 memcpy(d->skewtab,skew,d->sectrk*sizeof(int));
689 for (i=j=0; i<d->sectrk; ++i,j=(j+d->skew)%d->sectrk)
693 for (k=0; k<i && d->skewtab[k]!=j; ++k);
694 if (k<i) j=(j+1)%d->sectrk;
701 /* initialise allocation vector bitmap */ /*{{{*/
703 d->alvSize=((d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz+INTBITS-1)/INTBITS;
704 if ((d->alv=malloc(d->alvSize*sizeof(int)))==(int*)0)
711 /* allocate directory buffer */ /*{{{*/
712 if ((d->dir=malloc(d->maxdir*32))==(struct PhysDirectoryEntry*)0)
718 if (d->dev.opened==0) memset(d->dir,0xe5,d->maxdir*32);
719 else if (readPhysDirectory(d)==-1) return -1;
721 if (d->type==CPMFS_DR3) /* read additional superblock information */ /*{{{*/
725 /* passwords */ /*{{{*/
729 for (i=0; i<d->maxdir; ++i) if (d->dir[i].status>=16 && d->dir[i].status<=31) ++passwords;
731 fprintf(stderr,"getformat: found %d passwords\n",passwords);
733 if ((d->passwdLength=passwords*PASSWD_RECLEN))
735 if ((d->passwd=malloc(d->passwdLength))==(char*)0)
740 for (i=0,passwords=0; i<d->maxdir; ++i) if (d->dir[i].status>=16 && d->dir[i].status<=31)
743 char *p=d->passwd+(passwords++*PASSWD_RECLEN);
745 p[0]='0'+(d->dir[i].status-16)/10;
746 p[1]='0'+(d->dir[i].status-16)%10;
747 for (j=0; j<8; ++j) p[2+j]=d->dir[i].name[j]&0x7f;
748 p[10]=(d->dir[i].ext[0]&0x7f)==' ' ? ' ' : '.';
749 for (j=0; j<3; ++j) p[11+j]=d->dir[i].ext[j]&0x7f;
751 pb=(unsigned char)d->dir[i].lrc;
752 for (j=0; j<8; ++j) p[15+j]=((unsigned char)d->dir[i].pointers[7-j])^pb;
755 fprintf(stderr,"getformat: %s\n",p);
762 /* disc label */ /*{{{*/
763 for (i=0; i<d->maxdir; ++i) if (d->dir[i].status==(char)0x20)
767 d->cnotatime=d->dir[i].extnol&0x10;
768 if (d->dir[i].extnol&0x1)
771 if ((d->label=malloc(d->labelLength))==(char*)0)
776 for (j=0; j<8; ++j) d->label[j]=d->dir[i].name[j]&0x7f;
777 for (j=0; j<3; ++j) d->label[8+j]=d->dir[i].ext[j]&0x7f;
803 root->mode=(s_ifdir|0777);
805 root->atime=root->mtime=root->ctime=0;
809 /* cpmNamei -- map name to inode */ /*{{{*/
810 int cpmNamei(const struct cpmInode *dir, const char *filename, struct cpmInode *i)
812 /* variables */ /*{{{*/
814 char name[8],extension[3];
815 struct PhysDirectoryEntry *date;
816 int highestExtno,highestExt=-1,lowestExtno,lowestExt=-1;
820 if (!S_ISDIR(dir->mode))
825 if (strcmp(filename,".")==0 || strcmp(filename,"..")==0) /* root directory */ /*{{{*/
831 else if (strcmp(filename,"[passwd]")==0 && dir->sb->passwdLength) /* access passwords */ /*{{{*/
834 i->ino=dir->sb->maxdir+1;
835 i->mode=s_ifreg|0444;
837 i->atime=i->mtime=i->ctime=0;
838 i->size=i->sb->passwdLength;
842 else if (strcmp(filename,"[label]")==0 && dir->sb->labelLength) /* access label */ /*{{{*/
845 i->ino=dir->sb->maxdir+2;
846 i->mode=s_ifreg|0444;
848 i->atime=i->mtime=i->ctime=0;
849 i->size=i->sb->labelLength;
853 if (splitFilename(filename,dir->sb->type,name,extension,&user)==-1) return -1;
854 /* find highest and lowest extent */ /*{{{*/
862 while ((extent=findFileExtent(dir->sb,user,name,extension,extent+1,-1))!=-1)
864 int extno=EXTENT(dir->sb->dir[extent].extnol,dir->sb->dir[extent].extnoh);
866 if (extno>highestExtno)
871 if (extno<lowestExtno)
879 if (highestExtno==-1) return -1;
880 /* calculate size */ /*{{{*/
884 i->size=highestExtno*16384;
885 if (dir->sb->size<256) for (block=15; block>=0; --block)
887 if (dir->sb->dir[highestExt].pointers[block]) break;
889 else for (block=7; block>=0; --block)
891 if (dir->sb->dir[highestExt].pointers[2*block] || dir->sb->dir[highestExt].pointers[2*block+1]) break;
893 if (dir->sb->dir[highestExt].blkcnt) i->size+=((dir->sb->dir[highestExt].blkcnt&0xff)-1)*128;
894 i->size+=dir->sb->dir[highestExt].lrc ? (dir->sb->dir[highestExt].lrc&0xff) : 128;
896 fprintf(stderr,"cpmNamei: size=%ld\n",(long)i->size);
903 /* set timestamps */ /*{{{*/
906 (dir->sb->type==CPMFS_P2DOS || dir->sb->type==CPMFS_DR3)
907 && (date=dir->sb->dir+(lowestExt|3))->status==0x21
910 /* variables */ /*{{{*/
911 int u_days=0,u_hour=0,u_min=0;
912 int ca_days=0,ca_hour=0,ca_min=0;
917 case 0: /* first entry of the four */ /*{{{*/
919 ca_days=((unsigned char)date->name[0])+(((unsigned char)date->name[1])<<8);
920 ca_hour=(unsigned char)date->name[2];
921 ca_min=(unsigned char)date->name[3];
922 u_days=((unsigned char)date->name[4])+(((unsigned char)date->name[5])<<8);
923 u_hour=(unsigned char)date->name[6];
924 u_min=(unsigned char)date->name[7];
925 protectMode=(unsigned char)date->name[8];
929 case 1: /* second entry */ /*{{{*/
931 ca_days=((unsigned char)date->ext[2])+(((unsigned char)date->extnol)<<8);
932 ca_hour=(unsigned char)date->lrc;
933 ca_min=(unsigned char)date->extnoh;
934 u_days=((unsigned char)date->blkcnt)+(((unsigned char)date->pointers[0])<<8);
935 u_hour=(unsigned char)date->pointers[1];
936 u_min=(unsigned char)date->pointers[2];
937 protectMode=(unsigned char)date->pointers[3];
941 case 2: /* third one */ /*{{{*/
943 ca_days=((unsigned char)date->pointers[5])+(((unsigned char)date->pointers[6])<<8);
944 ca_hour=(unsigned char)date->pointers[7];
945 ca_min=(unsigned char)date->pointers[8];
946 u_days=((unsigned char)date->pointers[9])+(((unsigned char)date->pointers[10])<<8);
947 u_hour=(unsigned char)date->pointers[11];
948 u_min=(unsigned char)date->pointers[12];
949 protectMode=(unsigned char)date->pointers[13];
954 if (i->sb->cnotatime)
956 i->ctime=cpm2unix_time(ca_days,ca_hour,ca_min);
962 i->atime=cpm2unix_time(ca_days,ca_hour,ca_min);
964 i->mtime=cpm2unix_time(u_days,u_hour,u_min);
966 else i->atime=i->mtime=i->ctime=0;
969 /* Determine the inode attributes */
971 if (dir->sb->dir[lowestExt].name[0]&0x80) i->attr |= CPM_ATTR_F1;
972 if (dir->sb->dir[lowestExt].name[1]&0x80) i->attr |= CPM_ATTR_F2;
973 if (dir->sb->dir[lowestExt].name[2]&0x80) i->attr |= CPM_ATTR_F3;
974 if (dir->sb->dir[lowestExt].name[3]&0x80) i->attr |= CPM_ATTR_F4;
975 if (dir->sb->dir[lowestExt].ext [0]&0x80) i->attr |= CPM_ATTR_RO;
976 if (dir->sb->dir[lowestExt].ext [1]&0x80) i->attr |= CPM_ATTR_SYS;
977 if (dir->sb->dir[lowestExt].ext [2]&0x80) i->attr |= CPM_ATTR_ARCV;
978 if (protectMode&0x20) i->attr |= CPM_ATTR_PWDEL;
979 if (protectMode&0x40) i->attr |= CPM_ATTR_PWWRITE;
980 if (protectMode&0x80) i->attr |= CPM_ATTR_PWREAD;
982 if (dir->sb->dir[lowestExt].ext[1]&0x80) i->mode|=01000;
984 if (!(dir->sb->dir[lowestExt].ext[0]&0x80)) i->mode|=0222;
985 if (extension[0]=='C' && extension[1]=='O' && extension[2]=='M') i->mode|=0111;
989 /* cpmStatFS -- statfs */ /*{{{*/
990 void cpmStatFS(const struct cpmInode *ino, struct cpmStatFS *buf)
993 struct cpmSuperBlock *d;
996 buf->f_bsize=d->blksiz;
997 buf->f_blocks=(d->tracks*d->sectrk*d->secLength)/d->blksiz;
999 buf->f_bused=-(d->maxdir*32+d->blksiz-1)/d->blksiz;
1000 for (i=0; i<d->alvSize; ++i)
1005 for (j=0; j<INTBITS; ++j)
1007 if (i*INTBITS+j < d->size)
1012 fprintf(stderr,"cpmStatFS: block %d allocated\n",(i*INTBITS+j));
1016 else ++buf->f_bfree;
1021 buf->f_bavail=buf->f_bfree;
1022 buf->f_files=d->maxdir;
1024 for (i=0; i<d->maxdir; ++i)
1026 if (d->dir[i].status==(char)0xe5) ++buf->f_ffree;
1031 /* cpmUnlink -- unlink */ /*{{{*/
1032 int cpmUnlink(const struct cpmInode *dir, const char *fname)
1035 char name[8],extension[3];
1037 struct cpmSuperBlock *drive;
1039 if (!S_ISDIR(dir->mode))
1045 if (splitFilename(fname,dir->sb->type,name,extension,&user)==-1) return -1;
1046 if ((extent=findFileExtent(drive,user,name,extension,0,-1))==-1) return -1;
1047 drive->dir[extent].status=(char)0xe5;
1050 drive->dir[extent].status=(char)0xe5;
1051 } while ((extent=findFileExtent(drive,user,name,extension,extent+1,-1))>=0);
1052 if (writePhysDirectory(drive)==-1) return -1;
1057 /* cpmRename -- rename */ /*{{{*/
1058 int cpmRename(const struct cpmInode *dir, const char *old, const char *new)
1060 struct cpmSuperBlock *drive;
1063 char oldname[8], oldext[3];
1065 char newname[8], newext[3];
1067 if (!S_ISDIR(dir->mode))
1073 if (splitFilename(old,dir->sb->type, oldname, oldext,&olduser)==-1) return -1;
1074 if (splitFilename(new,dir->sb->type, newname, newext,&newuser)==-1) return -1;
1075 if ((extent=findFileExtent(drive,olduser,oldname,oldext,0,-1))==-1) return -1;
1076 if (findFileExtent(drive,newuser,newname, newext,0,-1)!=-1)
1078 boo="file already exists";
1083 drive->dir[extent].status=newuser;
1084 memcpy7(drive->dir[extent].name, newname, 8);
1085 memcpy7(drive->dir[extent].ext, newext, 3);
1086 } while ((extent=findFileExtent(drive,olduser,oldname,oldext,extent+1,-1))!=-1);
1087 if (writePhysDirectory(drive)==-1) return -1;
1091 /* cpmOpendir -- opendir */ /*{{{*/
1092 int cpmOpendir(struct cpmInode *dir, struct cpmFile *dirp)
1094 if (!S_ISDIR(dir->mode))
1101 dirp->mode=O_RDONLY;
1105 /* cpmReaddir -- readdir */ /*{{{*/
1106 int cpmReaddir(struct cpmFile *dir, struct cpmDirent *ent)
1108 /* variables */ /*{{{*/
1109 struct PhysDirectoryEntry *cur=(struct PhysDirectoryEntry*)0;
1110 char buf[2+8+1+3+1]; /* 00foobarxy.zzy\0 */
1116 if (!(S_ISDIR(dir->ino->mode))) /* error: not a directory */ /*{{{*/
1118 boo="not a directory";
1124 if (dir->pos==0) /* first entry is . */ /*{{{*/
1126 ent->ino=dir->ino->sb->maxdir;
1128 strcpy(ent->name,".");
1134 else if (dir->pos==1) /* next entry is .. */ /*{{{*/
1136 ent->ino=dir->ino->sb->maxdir;
1138 strcpy(ent->name,"..");
1144 else if (dir->pos==2)
1146 if (dir->ino->sb->passwdLength) /* next entry is [passwd] */ /*{{{*/
1148 ent->ino=dir->ino->sb->maxdir+1;
1150 strcpy(ent->name,"[passwd]");
1157 else if (dir->pos==3)
1159 if (dir->ino->sb->labelLength) /* next entry is [label] */ /*{{{*/
1161 ent->ino=dir->ino->sb->maxdir+2;
1163 strcpy(ent->name,"[label]");
1170 else if (dir->pos>=RESERVED_ENTRIES && dir->pos<dir->ino->sb->maxdir+RESERVED_ENTRIES)
1172 int first=dir->pos-RESERVED_ENTRIES;
1174 if ((cur=dir->ino->sb->dir+(dir->pos-RESERVED_ENTRIES))->status>=0 && cur->status<=(dir->ino->sb->type==CPMFS_P2DOS ? 31 : 15))
1176 /* determine first extent for the current file */ /*{{{*/
1177 for (i=0; i<dir->ino->sb->maxdir; ++i) if (i!=(dir->pos-RESERVED_ENTRIES))
1179 if (isMatching(cur->status,cur->name,cur->ext,dir->ino->sb->dir[i].status,dir->ino->sb->dir[i].name,dir->ino->sb->dir[i].ext) && EXTENT(cur->extnol,cur->extnoh)>EXTENT(dir->ino->sb->dir[i].extnol,dir->ino->sb->dir[i].extnoh)) first=i;
1182 if (first==(dir->pos-RESERVED_ENTRIES))
1184 ent->ino=dir->pos-RESERVED_INODES;
1185 /* convert file name to UNIX style */ /*{{{*/
1186 buf[0]='0'+cur->status/10;
1187 buf[1]='0'+cur->status%10;
1188 for (bufp=buf+2,i=0; i<8 && (cur->name[i]&0x7f)!=' '; ++i) *bufp++=tolower(cur->name[i]&0x7f);
1189 for (hasext=0,i=0; i<3 && (cur->ext[i]&0x7f)!=' '; ++i)
1191 if (!hasext) { *bufp++='.'; hasext=1; }
1192 *bufp++=tolower(cur->ext[i]&0x7f);
1196 ent->reclen=strlen(buf);
1197 strcpy(ent->name,buf);
1209 /* cpmStat -- stat */ /*{{{*/
1210 void cpmStat(const struct cpmInode *ino, struct cpmStat *buf)
1213 buf->mode=ino->mode;
1214 buf->size=ino->size;
1215 buf->atime=ino->atime;
1216 buf->mtime=ino->mtime;
1217 buf->ctime=ino->ctime;
1220 /* cpmOpen -- open */ /*{{{*/
1221 int cpmOpen(struct cpmInode *ino, struct cpmFile *file, mode_t mode)
1223 if (S_ISREG(ino->mode))
1225 if ((mode&O_WRONLY) && (ino->mode&0222)==0)
1227 boo="permission denied";
1237 boo="not a regular file";
1242 /* cpmRead -- read */ /*{{{*/
1243 int cpmRead(struct cpmFile *file, char *buf, int count)
1245 int findext=1,findblock=1,extent=-1,block=-1,extentno=-1,got=0,nextblockpos=-1,nextextpos=-1;
1246 int blocksize=file->ino->sb->blksiz;
1249 extcap=(file->ino->sb->size<256 ? 16 : 8)*blocksize;
1250 if (extcap>16384) extcap=16384*file->ino->sb->extents;
1251 if (file->ino->ino==file->ino->sb->maxdir+1) /* [passwd] */ /*{{{*/
1253 if ((file->pos+count)>file->ino->size) count=file->ino->size-file->pos;
1254 if (count) memcpy(buf,file->ino->sb->passwd+file->pos,count);
1257 fprintf(stderr,"cpmRead passwd: read %d bytes, now at position %ld\n",count,(long)file->pos);
1262 else if (file->ino->ino==file->ino->sb->maxdir+2) /* [label] */ /*{{{*/
1264 if ((file->pos+count)>file->ino->size) count=file->ino->size-file->pos;
1265 if (count) memcpy(buf,file->ino->sb->label+file->pos,count);
1268 fprintf(stderr,"cpmRead label: read %d bytes, now at position %ld\n",count,(long)file->pos);
1273 else while (count>0 && file->pos<file->ino->size)
1279 extentno=file->pos/16384;
1280 extent=findFileExtent(file->ino->sb,file->ino->sb->dir[file->ino->ino].status,file->ino->sb->dir[file->ino->ino].name,file->ino->sb->dir[file->ino->ino].ext,0,extentno);
1281 nextextpos=(file->pos/extcap)*extcap+extcap;
1291 ptr=(file->pos%extcap)/blocksize;
1292 if (file->ino->sb->size>=256) ptr*=2;
1293 block=(unsigned char)file->ino->sb->dir[extent].pointers[ptr];
1294 if (file->ino->sb->size>=256) block+=((unsigned char)file->ino->sb->dir[extent].pointers[ptr+1])<<8;
1297 memset(buffer,0,blocksize);
1301 start=(file->pos%blocksize)/file->ino->sb->secLength;
1302 end=((file->pos%blocksize+count)>blocksize ? blocksize-1 : (file->pos%blocksize+count-1))/file->ino->sb->secLength;
1303 readBlock(file->ino->sb,block,buffer,start,end);
1306 nextblockpos=(file->pos/blocksize)*blocksize+blocksize;
1309 if (file->pos<nextblockpos)
1311 if (extent==-1) *buf++='\0'; else *buf++=buffer[file->pos%blocksize];
1316 else if (file->pos==nextextpos) findext=1; else findblock=1;
1319 fprintf(stderr,"cpmRead: read %d bytes, now at position %ld\n",got,(long)file->pos);
1324 /* cpmWrite -- write */ /*{{{*/
1325 int cpmWrite(struct cpmFile *file, const char *buf, int count)
1327 int findext=1,findblock=-1,extent=-1,extentno=-1,got=0,nextblockpos=-1,nextextpos=-1;
1328 int blocksize=file->ino->sb->blksiz;
1329 int extcap=(file->ino->sb->size<256 ? 16 : 8)*blocksize;
1330 int block=-1,start=-1,end=-1,ptr=-1,last=-1;
1335 if (findext) /*{{{*/
1337 extentno=file->pos/16384;
1338 extent=findFileExtent(file->ino->sb,file->ino->sb->dir[file->ino->ino].status,file->ino->sb->dir[file->ino->ino].name,file->ino->sb->dir[file->ino->ino].ext,0,extentno);
1339 nextextpos=(file->pos/extcap)*extcap+extcap;
1342 if ((extent=findFreeExtent(file->ino->sb))==-1) return (got==0 ? -1 : got);
1343 file->ino->sb->dir[extent]=file->ino->sb->dir[file->ino->ino];
1344 memset(file->ino->sb->dir[extent].pointers,0,16);
1345 file->ino->sb->dir[extent].extnol=EXTENTL(extentno);
1346 file->ino->sb->dir[extent].extnoh=EXTENTH(extentno);
1347 file->ino->sb->dir[extent].blkcnt=0;
1348 file->ino->sb->dir[extent].lrc=0;
1349 updateTimeStamps(file->ino,extent);
1355 if (findblock) /*{{{*/
1357 ptr=(file->pos%extcap)/blocksize;
1358 if (file->ino->sb->size>=256) ptr*=2;
1359 block=(unsigned char)file->ino->sb->dir[extent].pointers[ptr];
1360 if (file->ino->sb->size>=256) block+=((unsigned char)file->ino->sb->dir[extent].pointers[ptr+1])<<8;
1361 if (block==0) /* allocate new block, set start/end to cover it */ /*{{{*/
1363 if ((block=allocBlock(file->ino->sb))==-1) return (got==0 ? -1 : got);
1364 file->ino->sb->dir[extent].pointers[ptr]=block&0xff;
1365 if (file->ino->sb->size>=256) file->ino->sb->dir[extent].pointers[ptr+1]=(block>>8)&0xff;
1367 end=(blocksize-1)/file->ino->sb->secLength;
1368 memset(buffer,0,blocksize);
1371 else /* read existing block and set start/end to cover modified parts */ /*{{{*/
1373 start=(file->pos%blocksize)/file->ino->sb->secLength;
1374 end=((file->pos%blocksize+count)>blocksize ? blocksize-1 : (file->pos%blocksize+count-1))/file->ino->sb->secLength;
1375 if (file->pos%file->ino->sb->secLength) readBlock(file->ino->sb,block,buffer,start,start);
1376 if (end!=start && (file->pos+count-1)<blocksize) readBlock(file->ino->sb,block,buffer+end*file->ino->sb->secLength,end,end);
1379 nextblockpos=(file->pos/blocksize)*blocksize+blocksize;
1383 /* fill block and write it */ /*{{{*/
1384 while (file->pos!=nextblockpos && count)
1386 buffer[file->pos%blocksize]=*buf++;
1388 if (file->ino->size<file->pos) file->ino->size=file->pos;
1392 (void)writeBlock(file->ino->sb,block,buffer,start,end);
1393 if (file->ino->sb->size<256) for (last=15; last>=0; --last)
1395 if (file->ino->sb->dir[extent].pointers[last])
1400 else for (last=14; last>0; last-=2)
1402 if (file->ino->sb->dir[extent].pointers[last] || file->ino->sb->dir[extent].pointers[last+1])
1408 if (last>0) extentno+=(last*blocksize)/extcap;
1409 file->ino->sb->dir[extent].extnol=EXTENTL(extentno);
1410 file->ino->sb->dir[extent].extnoh=EXTENTH(extentno);
1411 file->ino->sb->dir[extent].blkcnt=((file->pos-1)%16384)/128+1;
1412 file->ino->sb->dir[extent].lrc=file->pos%128;
1413 updateTimeStamps(file->ino,extent);
1415 if (file->pos==nextextpos) findext=1;
1416 else if (file->pos==nextblockpos) findblock=1;
1418 writePhysDirectory(file->ino->sb);
1422 /* cpmClose -- close */ /*{{{*/
1423 int cpmClose(struct cpmFile *file)
1425 if (file->mode&O_WRONLY) return (writePhysDirectory(file->ino->sb));
1429 /* cpmCreat -- creat */ /*{{{*/
1430 int cpmCreat(struct cpmInode *dir, const char *fname, struct cpmInode *ino, mode_t mode)
1433 char name[8],extension[3];
1435 struct cpmSuperBlock *drive;
1436 struct PhysDirectoryEntry *ent;
1438 if (!S_ISDIR(dir->mode))
1440 boo="No such file or directory";
1443 if (splitFilename(fname,dir->sb->type,name,extension,&user)==-1) return -1;
1445 fprintf(stderr,"cpmCreat: %s -> %d:%-.8s.%-.3s\n",fname,user,name,extension);
1447 if (findFileExtent(dir->sb,user,name,extension,0,-1)!=-1) return -1;
1449 if ((extent=findFreeExtent(dir->sb))==-1) return -1;
1450 ent=dir->sb->dir+extent;
1453 memcpy(ent->name,name,8);
1454 memcpy(ent->ext,extension,3);
1456 ino->mode=s_ifreg|mode;
1462 updateTimeStamps(ino,extent);
1463 writePhysDirectory(dir->sb);
1467 /* cpmAttrGet -- get CP/M attributes */ /*{{{*/
1468 int cpmAttrGet(struct cpmInode *ino, cpm_attr_t *attrib)
1470 *attrib = ino->attr;
1474 /* cpmAttrSet -- set CP/M attributes */ /*{{{*/
1475 int cpmAttrSet(struct cpmInode *ino, cpm_attr_t attrib)
1477 struct cpmSuperBlock *drive;
1480 char name[8], extension[3];
1482 memset(name, 0, sizeof(name));
1483 memset(extension, 0, sizeof(extension));
1487 /* Strip off existing attribute bits */
1488 memcpy7(name, drive->dir[extent].name, 8);
1489 memcpy7(extension, drive->dir[extent].ext, 3);
1490 user = drive->dir[extent].status;
1492 /* And set new ones */
1493 if (attrib & CPM_ATTR_F1) name[0] |= 0x80;
1494 if (attrib & CPM_ATTR_F2) name[1] |= 0x80;
1495 if (attrib & CPM_ATTR_F3) name[2] |= 0x80;
1496 if (attrib & CPM_ATTR_F4) name[3] |= 0x80;
1497 if (attrib & CPM_ATTR_RO) extension[0] |= 0x80;
1498 if (attrib & CPM_ATTR_SYS) extension[1] |= 0x80;
1499 if (attrib & CPM_ATTR_ARCV) extension[2] |= 0x80;
1503 memcpy(drive->dir[extent].name, name, 8);
1504 memcpy(drive->dir[extent].ext, extension, 3);
1505 } while ((extent=findFileExtent(drive, user,name,extension,extent+1,-1))!=-1);
1506 if (writePhysDirectory(drive)==-1) return -1;
1508 /* Update the stored (inode) copies of the file attributes and mode */
1510 if (attrib&CPM_ATTR_RO) ino->mode&=~(S_IWUSR|S_IWGRP|S_IWOTH);
1511 else ino->mode|=(S_IWUSR|S_IWGRP|S_IWOTH);
1516 /* cpmChmod -- set CP/M r/o & sys */ /*{{{*/
1517 int cpmChmod(struct cpmInode *ino, mode_t mode)
1519 /* Convert the chmod() into a chattr() call that affects RO */
1520 int newatt = ino->attr & ~CPM_ATTR_RO;
1522 if (!(mode & (S_IWUSR|S_IWGRP|S_IWOTH))) newatt |= CPM_ATTR_RO;
1523 return cpmAttrSet(ino, newatt);
1526 /* cpmSync -- write directory back */ /*{{{*/
1527 int cpmSync(struct cpmSuperBlock *sb)
1529 return (writePhysDirectory(sb));
1532 /* cpmUmount -- free super block */ /*{{{*/
1533 void cpmUmount(struct cpmSuperBlock *sb)
1538 if (sb->passwdLength) free(sb->passwd);