1 /* #includes */ /*{{{C}}}*//*{{{*/
20 /* #defines */ /*{{{*/
23 /* Number of _used_ bits per int */
25 #define INTBITS ((int)(sizeof(int)*8))
27 /* Convert BCD datestamp digits to binary */
29 #define BCD2BIN(x) ((((x)>>4)&0xf)*10 + ((x)&0xf))
31 #define BIN2BCD(x) (((((x)/10)&0xf)<<4) + (((x)%10)&0xf))
33 /* There are four reserved directory entries: ., .., [passwd] and [label].
34 The first two of them refer to the same inode. */
36 #define RESERVED_ENTRIES 4
38 /* CP/M does not support any kind of inodes, so they are simulated.
39 Inode 0-(maxdir-1) correlate to the lowest extent number (not the first
40 extent of the file in the directory) of a file. Inode maxdir is the
41 root directory, inode maxdir+1 is the optional passwd file and inode
42 maxdir+2 the optional disk label. */
44 #define RESERVED_INODES 3
46 #define PASSWD_RECLEN 24
49 extern char **environ;
51 static mode_t s_ifdir=1;
52 static mode_t s_ifreg=1;
54 /* memcpy7 -- Copy string, leaving 8th bit alone */ /*{{{*/
55 static void memcpy7(char *dest, const char *src, int count)
59 *dest = ((*dest) & 0x80) | ((*src) & 0x7F);
66 /* file name conversions */
67 /* splitFilename -- split file name into name and extension */ /*{{{*/
68 static int splitFilename(const char *fullname, int type, char *name, char *ext, int *user)
72 assert(fullname!=(const char*)0);
73 assert(name!=(char*)0);
74 assert(ext!=(char*)0);
75 assert(user!=(int*)0);
78 if (!isdigit(fullname[0]) || !isdigit(fullname[1]))
80 boo="illegal CP/M filename";
83 *user=10*(fullname[0]-'0')+(fullname[1]-'0');
85 if ((fullname[0]=='\0') || *user>=((type&CPMFS_HI_USER) ? 32 : 16))
87 boo="illegal CP/M filename";
90 for (i=0; i<8 && fullname[i] && fullname[i]!='.'; ++i) if (!ISFILECHAR(i,fullname[i]))
92 boo="illegal CP/M filename";
95 else name[i]=toupper(fullname[i]);
99 for (j=0; j<3 && fullname[i]; ++i,++j) if (!ISFILECHAR(1,fullname[i]))
101 boo="illegal CP/M filename";
104 else ext[j]=toupper(fullname[i]);
107 boo="illegal CP/M filename";
114 /* isMatching -- do two file names match? */ /*{{{*/
115 static int isMatching(int user1, const char *name1, const char *ext1, int user2, const char *name2, const char *ext2)
119 assert(name1!=(const char*)0);
120 assert(ext1!=(const char*)0);
121 assert(name2!=(const char*)0);
122 assert(ext2!=(const char*)0);
123 if (user1!=user2) return 0;
124 for (i=0; i<8; ++i) if ((name1[i]&0x7f)!=(name2[i]&0x7f)) return 0;
125 for (i=0; i<3; ++i) if ((ext1[i]&0x7f)!=(ext2[i]&0x7f)) return 0;
130 /* time conversions */
131 /* cpm2unix_time -- convert CP/M time to UTC */ /*{{{*/
132 static time_t cpm2unix_time(int days, int hour, int min)
134 /* CP/M stores timestamps in local time. We don't know which */
135 /* timezone was used and if DST was in effect. Assuming it was */
136 /* the current offset from UTC is most sensible, but not perfect. */
138 int year,days_per_year;
139 static int days_per_month[]={31,0,31,30,31,30,31,31,30,31,30,31};
141 static char gmt0[]="TZ=GMT0";
142 static char *gmt_env[]={ gmt0, (char*)0 };
154 tms.tm_min=((min>>4)&0xf)*10+(min&0xf);
155 tms.tm_hour=((hour>>4)&0xf)*10+(hour&0xf);
162 year=tms.tm_year+1900;
163 days_per_year=((year%4)==0 && ((year%100) || (year%400)==0)) ? 366 : 365;
164 if (days>days_per_year)
173 days_per_month[1]=(days_per_year==366) ? 29 : 28;
174 if (days>days_per_month[tms.tm_mon])
176 days-=days_per_month[tms.tm_mon];
181 t=mktime(&tms)+(days-1)*24*3600;
187 /* unix2cpm_time -- convert UTC to CP/M time */ /*{{{*/
188 static void unix2cpm_time(time_t now, int *days, int *hour, int *min)
194 *min=((tms->tm_min/10)<<4)|(tms->tm_min%10);
195 *hour=((tms->tm_hour/10)<<4)|(tms->tm_hour%10);
196 for (i=1978,*days=0; i<1900+tms->tm_year; ++i)
199 if (i%4==0 && (i%100!=0 || i%400==0)) ++*days;
201 *days += tms->tm_yday+1;
204 /* ds2unix_time -- convert DS to Unix time */ /*{{{*/
205 static time_t ds2unix_time(const struct dsEntry *entry)
210 if (entry->minute==0 &&
214 entry->year==0) return 0;
218 tms.tm_min = BCD2BIN( entry->minute );
219 tms.tm_hour = BCD2BIN( entry->hour );
220 tms.tm_mday = BCD2BIN( entry->day );
221 tms.tm_mon = BCD2BIN( entry->month ) - 1;
223 yr = BCD2BIN(entry->year);
230 /* unix2ds_time -- convert Unix to DS time */ /*{{{*/
231 static void unix2ds_time(time_t now, struct dsEntry *entry)
238 entry->minute=entry->hour=entry->day=entry->month=entry->year = 0;
243 entry->minute = BIN2BCD( tms->tm_min );
244 entry->hour = BIN2BCD( tms->tm_hour );
245 entry->day = BIN2BCD( tms->tm_mday );
246 entry->month = BIN2BCD( tms->tm_mon + 1 );
249 if ( yr>100 ) yr -= 100;
250 entry->year = BIN2BCD( yr );
255 /* allocation vector bitmap functions */
256 /* alvInit -- init allocation vector */ /*{{{*/
257 static void alvInit(const struct cpmSuperBlock *d)
259 int i,j,offset,block;
261 assert(d!=(const struct cpmSuperBlock*)0);
262 /* clean bitmap */ /*{{{*/
263 memset(d->alv,0,d->alvSize*sizeof(int));
265 /* mark directory blocks as used */ /*{{{*/
266 *d->alv=(1<<((d->maxdir*32+d->blksiz-1)/d->blksiz))-1;
268 for (i=0; i<d->maxdir; ++i) /* mark file blocks as used */ /*{{{*/
270 if (d->dir[i].status>=0 && d->dir[i].status<=(d->type&CPMFS_HI_USER ? 31 : 15))
273 fprintf(stderr,"alvInit: allocate extent %d\n",i);
277 block=(unsigned char)d->dir[i].pointers[j];
278 if (d->size>=256) block+=(((unsigned char)d->dir[i].pointers[++j])<<8);
279 if (block && block<d->size)
282 fprintf(stderr,"alvInit: allocate block %d\n",block);
284 offset=block/INTBITS;
285 d->alv[offset]|=(1<<block%INTBITS);
293 /* allocBlock -- allocate a new disk block */ /*{{{*/
294 static int allocBlock(const struct cpmSuperBlock *drive)
298 assert(drive!=(const struct cpmSuperBlock*)0);
299 for (i=0; i<drive->alvSize; ++i)
301 for (j=0,bits=drive->alv[i]; j<INTBITS; ++j)
306 if (block>=drive->size)
311 drive->alv[i] |= (1<<j);
322 /* logical block I/O */
323 /* readBlock -- read a (partial) block */ /*{{{*/
324 static int readBlock(const struct cpmSuperBlock *d, int blockno, char *buffer, int start, int end)
326 int sect, track, counter;
329 assert(blockno<d->size);
330 assert(buffer!=(char*)0);
331 if (end<0) end=d->blksiz/d->secLength-1;
332 sect=(blockno*(d->blksiz/d->secLength)+ d->sectrk*d->boottrk)%d->sectrk;
333 track=(blockno*(d->blksiz/d->secLength)+ d->sectrk*d->boottrk)/d->sectrk;
334 for (counter=0; counter<=end; ++counter)
338 if (counter>=start && (err=Device_readSector(&d->dev,track,d->skewtab[sect],buffer+(d->secLength*counter))))
353 /* writeBlock -- write a (partial) block */ /*{{{*/
354 static int writeBlock(const struct cpmSuperBlock *d, int blockno, const char *buffer, int start, int end)
356 int sect, track, counter;
359 assert(blockno<d->size);
360 assert(buffer!=(const char*)0);
361 if (end < 0) end=d->blksiz/d->secLength-1;
362 sect = (blockno*(d->blksiz/d->secLength))%d->sectrk;
363 track = (blockno*(d->blksiz/d->secLength))/d->sectrk+d->boottrk;
364 for (counter = 0; counter<=end; ++counter)
368 if (counter>=start && (err=Device_writeSector(&d->dev,track,d->skewtab[sect],buffer+(d->secLength*counter))))
384 /* directory management */
385 /* findFileExtent -- find first/next extent for a file */ /*{{{*/
386 static int findFileExtent(const struct cpmSuperBlock *sb, int user, const char *name, const char *ext, int start, int extno)
388 boo="file already exists";
389 for (; start<sb->maxdir; ++start)
393 ((unsigned char)sb->dir[start].status)<=(sb->type&CPMFS_HI_USER ? 31 : 15)
394 && (extno==-1 || (EXTENT(sb->dir[start].extnol,sb->dir[start].extnoh)/sb->extents)==(extno/sb->extents))
395 && isMatching(user,name,ext,sb->dir[start].status,sb->dir[start].name,sb->dir[start].ext)
398 boo="file not found";
402 /* findFreeExtent -- find first free extent */ /*{{{*/
403 static int findFreeExtent(const struct cpmSuperBlock *drive)
407 for (i=0; i<drive->maxdir; ++i) if (drive->dir[i].status==(char)0xe5) return (i);
408 boo="directory full";
412 /* updateTimeStamps -- convert time stamps to CP/M format */ /*{{{*/
413 static void updateTimeStamps(const struct cpmInode *ino, int extent)
415 struct PhysDirectoryEntry *date;
417 int ca_min,ca_hour,ca_days,u_min,u_hour,u_days;
419 if (!S_ISREG(ino->mode)) return;
421 fprintf(stderr,"CPMFS: updating time stamps for inode %d (%d)\n",extent,extent&3);
423 unix2cpm_time(ino->sb->cnotatime ? ino->ctime : ino->atime,&ca_days,&ca_hour,&ca_min);
424 unix2cpm_time(ino->mtime,&u_days,&u_hour,&u_min);
425 if ((ino->sb->type&CPMFS_CPM3_DATES) && (date=ino->sb->dir+(extent|3))->status==0x21)
427 ino->sb->dirtyDirectory=1;
430 case 0: /* first entry */ /*{{{*/
432 date->name[0]=ca_days&0xff; date->name[1]=ca_days>>8;
433 date->name[2]=ca_hour;
434 date->name[3]=ca_min;
435 date->name[4]=u_days&0xff; date->name[5]=u_days>>8;
436 date->name[6]=u_hour;
441 case 1: /* second entry */ /*{{{*/
443 date->ext[2]=ca_days&0xff; date->extnol=ca_days>>8;
446 date->blkcnt=u_days&0xff; date->pointers[0]=u_days>>8;
447 date->pointers[1]=u_hour;
448 date->pointers[2]=u_min;
452 case 2: /* third entry */ /*{{{*/
454 date->pointers[5]=ca_days&0xff; date->pointers[6]=ca_days>>8;
455 date->pointers[7]=ca_hour;
456 date->pointers[8]=ca_min;
457 date->pointers[9]=u_days&0xff; date->pointers[10]=u_days>>8;
458 date->pointers[11]=u_hour;
459 date->pointers[12]=u_min;
467 /* updateDsStamps -- set time in datestamper file */ /*{{{*/
468 static void updateDsStamps(const struct cpmInode *ino, int extent)
472 struct dsDate *stamp;
474 if (!S_ISREG(ino->mode)) return;
475 if ( !(ino->sb->type&CPMFS_DS_DATES) ) return;
478 fprintf(stderr,"CPMFS: updating ds stamps for inode %d (%d)\n",extent,extent&3);
481 /* Get datestamp struct */
482 stamp = ino->sb->ds+extent;
484 unix2ds_time( ino->mtime, &stamp->modify );
485 unix2ds_time( ino->ctime, &stamp->create );
486 unix2ds_time( ino->atime, &stamp->access );
488 ino->sb->dirtyDs = 1;
491 /* readTimeStamps -- read CP/M time stamp */ /*{{{*/
492 static int readTimeStamps(struct cpmInode *i, int lowestExt)
494 /* variables */ /*{{{*/
495 struct PhysDirectoryEntry *date;
496 int u_days=0,u_hour=0,u_min=0;
497 int ca_days=0,ca_hour=0,ca_min=0;
501 if ( (i->sb->type&CPMFS_CPM3_DATES) && (date=i->sb->dir+(lowestExt|3))->status==0x21 )
505 case 0: /* first entry of the four */ /*{{{*/
507 ca_days=((unsigned char)date->name[0])+(((unsigned char)date->name[1])<<8);
508 ca_hour=(unsigned char)date->name[2];
509 ca_min=(unsigned char)date->name[3];
510 u_days=((unsigned char)date->name[4])+(((unsigned char)date->name[5])<<8);
511 u_hour=(unsigned char)date->name[6];
512 u_min=(unsigned char)date->name[7];
513 protectMode=(unsigned char)date->ext[0];
517 case 1: /* second entry */ /*{{{*/
519 ca_days=((unsigned char)date->ext[2])+(((unsigned char)date->extnol)<<8);
520 ca_hour=(unsigned char)date->lrc;
521 ca_min=(unsigned char)date->extnoh;
522 u_days=((unsigned char)date->blkcnt)+(((unsigned char)date->pointers[0])<<8);
523 u_hour=(unsigned char)date->pointers[1];
524 u_min=(unsigned char)date->pointers[2];
525 protectMode=(unsigned char)date->pointers[3];
529 case 2: /* third one */ /*{{{*/
531 ca_days=((unsigned char)date->pointers[5])+(((unsigned char)date->pointers[6])<<8);
532 ca_hour=(unsigned char)date->pointers[7];
533 ca_min=(unsigned char)date->pointers[8];
534 u_days=((unsigned char)date->pointers[9])+(((unsigned char)date->pointers[10])<<8);
535 u_hour=(unsigned char)date->pointers[11];
536 u_min=(unsigned char)date->pointers[12];
537 protectMode=(unsigned char)date->pointers[13];
542 if (i->sb->cnotatime)
544 i->ctime=cpm2unix_time(ca_days,ca_hour,ca_min);
550 i->atime=cpm2unix_time(ca_days,ca_hour,ca_min);
552 i->mtime=cpm2unix_time(u_days,u_hour,u_min);
556 i->atime=i->mtime=i->ctime=0;
563 /* readDsStamps -- read datestamper time stamp */ /*{{{*/
564 static void readDsStamps(struct cpmInode *i, int lowestExt)
566 struct dsDate *stamp;
568 if ( !(i->sb->type&CPMFS_DS_DATES) ) return;
571 stamp = i->sb->ds+lowestExt;
573 i->mtime = ds2unix_time(&stamp->modify);
574 i->ctime = ds2unix_time(&stamp->create);
575 i->atime = ds2unix_time(&stamp->access);
579 /* match -- match filename against a pattern */ /*{{{*/
580 static int recmatch(const char *a, const char *pattern)
590 if (*a=='.' && first) return 1;
592 while (*a) if (recmatch(a,pattern)) return 1; else ++a;
597 if (*a) { ++a; ++pattern; } else return 0;
600 default: if (tolower(*a)==tolower(*pattern)) { ++a; ++pattern; } else return 0;
604 return (*pattern=='\0' && *a=='\0');
607 int match(const char *a, const char *pattern)
612 assert(strlen(pattern)<255);
613 if (isdigit(*pattern) && *(pattern+1)==':') { user=(*pattern-'0'); pattern+=2; }
614 else if (isdigit(*pattern) && isdigit(*(pattern+1)) && *(pattern+2)==':') { user=(10*(*pattern-'0')+(*(pattern+1)-'0')); pattern+=3; }
616 if (user==-1) sprintf(pat,"??%s",pattern);
617 else sprintf(pat,"%02d%s",user,pattern);
618 return recmatch(a,pat);
622 /* cpmglob -- expand CP/M style wildcards */ /*{{{*/
623 void cpmglob(int optin, int argc, char * const argv[], struct cpmInode *root, int *gargc, char ***gargv)
626 int entries,dirsize=0;
627 struct cpmDirent *dirent=(struct cpmDirent*)0;
632 cpmOpendir(root,&dir);
635 dirent=malloc(sizeof(struct cpmDirent)*dirsize);
636 while (cpmReaddir(&dir,&dirent[entries]))
639 if (entries==dirsize) dirent=realloc(dirent,sizeof(struct cpmDirent)*(dirsize*=2));
641 for (i=optin; i<argc; ++i)
645 for (j=0,found=0; j<entries; ++j)
647 if (match(dirent[j].name,argv[i]))
649 if (*gargc==gargcap) *gargv=realloc(*gargv,sizeof(char*)*(gargcap ? (gargcap*=2) : (gargcap=16)));
650 (*gargv)[*gargc]=strcpy(malloc(strlen(dirent[j].name)+1),dirent[j].name);
660 /* superblock management */
661 /* diskdefReadSuper -- read super block from diskdefs file */ /*{{{*/
662 static int diskdefReadSuper(struct cpmSuperBlock *d, const char *format)
666 int insideDef=0,found=0;
669 if ((fp=fopen("diskdefs","r"))==(FILE*)0 && (fp=fopen(DISKDEFS,"r"))==(FILE*)0)
671 fprintf(stderr,"%s: Neither `diskdefs' nor `" DISKDEFS "' could be opened.\n",cmd);
674 while (fgets(line,sizeof(line),fp)!=(char*)0)
679 for (argc=0; argc<1 && (argv[argc]=strtok(argc ? (char*)0 : line," \t\n")); ++argc);
680 if ((argv[argc]=strtok((char*)0,"\n"))!=(char*)0) ++argc;
683 if (argc==1 && strcmp(argv[0],"end")==0)
686 d->size=(d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz;
687 if (d->extents==0) d->extents=((d->size>=256 ? 8 : 16)*d->blksiz)/16384;
688 if (d->extents==0) d->extents=1;
693 if (strcmp(argv[0],"seclen")==0) d->secLength=strtol(argv[1],(char**)0,0);
694 else if (strcmp(argv[0],"tracks")==0) d->tracks=strtol(argv[1],(char**)0,0);
695 else if (strcmp(argv[0],"sectrk")==0) d->sectrk=strtol(argv[1],(char**)0,0);
696 else if (strcmp(argv[0],"blocksize")==0) d->blksiz=strtol(argv[1],(char**)0,0);
697 else if (strcmp(argv[0],"maxdir")==0) d->maxdir=strtol(argv[1],(char**)0,0);
698 else if (strcmp(argv[0],"skew")==0) d->skew=strtol(argv[1],(char**)0,0);
699 else if (strcmp(argv[0],"skewtab")==0)
703 for (pass=0; pass<2; ++pass)
708 for (s=argv[1]; *s; )
713 phys=strtol(s,&end,10);
714 if (pass==1) d->skewtab[sectors]=phys;
717 fprintf(stderr,"%s: invalid skewtab `%s' at `%s'\n",cmd,argv[1],s);
724 if (pass==0) d->skewtab=malloc(sizeof(int)*sectors);
727 else if (strcmp(argv[0],"boottrk")==0) d->boottrk=strtol(argv[1],(char**)0,0);
728 else if (strcmp(argv[0],"offset")==0)
731 unsigned int multiplier;
736 val = strtoul(argv[1],&endptr,10);
737 if ((errno==ERANGE && val==ULONG_MAX)||(errno!=0 && val==0))
739 fprintf(stderr,"%s: invalid offset value \"%s\" - %s\n",cmd,argv[1],strerror(errno));
744 fprintf(stderr,"%s: offset value \"%s\" is not a number\n",cmd,argv[1]);
749 /* Have a unit specifier */
750 switch (toupper(*endptr))
756 multiplier=1024*1024;
759 if (d->sectrk<0||d->tracks<0||d->secLength<0)
761 fprintf(stderr,"%s: offset must be specified after sectrk, tracks and secLength\n",cmd);
764 multiplier=d->sectrk*d->secLength;
767 if (d->sectrk<0||d->tracks<0||d->secLength<0)
769 fprintf(stderr,"%s: offset must be specified after sectrk, tracks and secLength\n",cmd);
772 multiplier=d->secLength;
775 fprintf(stderr,"%s: unknown unit specifier \"%c\"\n",cmd,*endptr);
779 if (val*multiplier>INT_MAX)
781 fprintf(stderr,"%s: effective offset is out of range\n",cmd);
784 d->offset=val*multiplier;
786 else if (strcmp(argv[0],"logicalextents")==0) d->extents=strtol(argv[1],(char**)0,0);
787 else if (strcmp(argv[0],"os")==0)
789 if (strcmp(argv[1],"2.2" )==0) d->type|=CPMFS_DR22;
790 else if (strcmp(argv[1],"3" )==0) d->type|=CPMFS_DR3;
791 else if (strcmp(argv[1],"isx" )==0) d->type|=CPMFS_ISX;
792 else if (strcmp(argv[1],"p2dos")==0) d->type|=CPMFS_P2DOS;
793 else if (strcmp(argv[1],"zsys" )==0) d->type|=CPMFS_ZSYS;
796 fprintf(stderr, "%s: invalid OS type `%s'\n", cmd, argv[1]);
801 else if (argc>0 && argv[0][0]!='#')
803 fprintf(stderr,"%s: invalid keyword `%s'\n",cmd,argv[0]);
807 else if (argc==2 && strcmp(argv[0],"diskdef")==0)
815 d->boottrk=d->secLength=d->sectrk=d->tracks=-1;
816 if (strcmp(argv[1],format)==0) found=1;
822 fprintf(stderr,"%s: unknown format %s\n",cmd,format);
827 fprintf(stderr, "%s: boottrk parameter invalid or missing from diskdef\n",cmd);
832 fprintf(stderr, "%s: secLength parameter invalid or missing from diskdef\n",cmd);
837 fprintf(stderr, "%s: sectrk parameter invalid or missing from diskdef\n",cmd);
842 fprintf(stderr, "%s: tracks parameter invalid or missing from diskdef\n",cmd);
848 /* amsReadSuper -- read super block from amstrad disk */ /*{{{*/
849 static int amsReadSuper(struct cpmSuperBlock *d, const char *format)
851 unsigned char boot_sector[512], *boot_spec;
854 Device_setGeometry(&d->dev,512,9,40,0);
855 if ((err=Device_readSector(&d->dev, 0, 0, (char *)boot_sector)))
857 fprintf(stderr,"%s: Failed to read Amstrad superblock (%s)\n",cmd,err);
860 boot_spec=(boot_sector[0] == 0 || boot_sector[0] == 3)?boot_sector:(unsigned char*)0;
861 /* Check for JCE's extension to allow Amstrad and MSDOS superblocks
862 * in the same sector (for the PCW16)
866 (boot_sector[0] == 0xE9 || boot_sector[0] == 0xEB)
867 && !memcmp(boot_sector + 0x2B, "CP/M", 4)
868 && !memcmp(boot_sector + 0x33, "DSK", 3)
869 && !memcmp(boot_sector + 0x7C, "CP/M", 4)
870 ) boot_spec = boot_sector + 128;
871 if (boot_spec==(unsigned char*)0)
873 fprintf(stderr,"%s: Amstrad superblock not present\n",cmd);
876 /* boot_spec[0] = format number: 0 for SS SD, 3 for DS DD
877 [1] = single/double sided and density flags
878 [2] = cylinders per side
879 [3] = sectors per cylinder
880 [4] = Physical sector shift, 2 => 512
881 [5] = Reserved track count
883 [7] = No. of directory blocks
886 d->type |= CPMFS_DR3; /* Amstrads are CP/M 3 systems */
887 d->secLength = 128 << boot_spec[4];
888 d->tracks = boot_spec[2];
889 if (boot_spec[1] & 3) d->tracks *= 2;
890 d->sectrk = boot_spec[3];
891 d->blksiz = 128 << boot_spec[6];
892 d->maxdir = (d->blksiz / 32) * boot_spec[7];
893 d->skew = 1; /* Amstrads skew at the controller level */
894 d->skewtab = (int*)0;
895 d->boottrk = boot_spec[5];
897 d->size = (d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz;
898 d->extents = ((d->size>=256 ? 8 : 16)*d->blksiz)/16384;
903 /* cpmCheckDs -- read all datestamper timestamps */ /*{{{*/
904 int cpmCheckDs(struct cpmSuperBlock *sb)
906 int dsoffset, dsblks, dsrecs, off, i;
909 if (!isMatching(0,"!!!TIME&","DAT",sb->dir->status,sb->dir->name,sb->dir->ext)) return -1;
911 /* Offset to ds file in alloc blocks */
912 dsoffset=(sb->maxdir*32+(sb->blksiz-1))/sb->blksiz;
914 dsrecs=(sb->maxdir+7)/8;
915 dsblks=(dsrecs*128+(sb->blksiz-1))/sb->blksiz;
917 /* Allocate buffer */
918 sb->ds=malloc(dsblks*sb->blksiz);
920 /* Read ds file in its entirety */
922 for (i=dsoffset; i<dsoffset+dsblks; i++)
924 if (readBlock(sb,i,((char*)sb->ds)+off,0,-1)==-1) return -1;
928 /* Verify checksums */
929 buf = (unsigned char *)sb->ds;
930 for (i=0; i<dsrecs; i++)
934 for (j=0; j<127; j++) cksum += buf[j];
935 if (buf[j]!=(cksum&0xff))
938 fprintf( stderr, "!!!TIME&.DAT file failed cksum at record %i\n", i );
941 sb->ds = (struct dsDate *)0;
949 /* cpmReadSuper -- get DPB and init in-core data for drive */ /*{{{*/
950 int cpmReadSuper(struct cpmSuperBlock *d, struct cpmInode *root, const char *format)
952 while (s_ifdir && !S_ISDIR(s_ifdir)) s_ifdir<<=1;
954 while (s_ifreg && !S_ISREG(s_ifreg)) s_ifreg<<=1;
956 if (strcmp(format, "amstrad")==0) amsReadSuper(d,format);
957 else diskdefReadSuper(d,format);
958 Device_setGeometry(&d->dev,d->secLength,d->sectrk,d->tracks,d->offset);
959 if (d->skewtab==(int*)0) /* generate skew table */ /*{{{*/
963 if (( d->skewtab = malloc(d->sectrk*sizeof(int))) == (int*)0)
965 fprintf(stderr,"%s: can not allocate memory for skew sector table\n",cmd);
968 memset(d->skewtab,0,d->sectrk*sizeof(int));
969 for (i=j=0; i<d->sectrk; ++i,j=(j+d->skew)%d->sectrk)
973 for (k=0; k<i && d->skewtab[k]!=j; ++k);
974 if (k<i) j=(j+1)%d->sectrk;
981 /* initialise allocation vector bitmap */ /*{{{*/
983 d->alvSize=((d->secLength*d->sectrk*(d->tracks-d->boottrk))/d->blksiz+INTBITS-1)/INTBITS;
984 if ((d->alv=malloc(d->alvSize*sizeof(int)))==(int*)0)
991 /* allocate directory buffer */ /*{{{*/
992 if ((d->dir=malloc(d->maxdir*32))==(struct PhysDirectoryEntry*)0)
998 if (d->dev.opened==0) /* create empty directory in core */ /*{{{*/
1000 memset(d->dir,0xe5,d->maxdir*32);
1003 else /* read directory in core */ /*{{{*/
1007 blocks=(d->maxdir*32+d->blksiz-1)/d->blksiz;
1009 for (i=0; i<blocks; ++i)
1011 if (readBlock(d,i,(char*)(d->dir+entry),0,-1)==-1) return -1;
1012 entry+=(d->blksiz/32);
1017 if (d->type&CPMFS_CPM3_OTHER) /* read additional superblock information */ /*{{{*/
1021 /* passwords */ /*{{{*/
1025 for (i=0; i<d->maxdir; ++i) if (d->dir[i].status>=16 && d->dir[i].status<=31) ++passwords;
1027 fprintf(stderr,"getformat: found %d passwords\n",passwords);
1029 if ((d->passwdLength=passwords*PASSWD_RECLEN))
1031 if ((d->passwd=malloc(d->passwdLength))==(char*)0)
1033 boo="out of memory";
1036 for (i=0,passwords=0; i<d->maxdir; ++i) if (d->dir[i].status>=16 && d->dir[i].status<=31)
1039 char *p=d->passwd+(passwords++*PASSWD_RECLEN);
1041 p[0]='0'+(d->dir[i].status-16)/10;
1042 p[1]='0'+(d->dir[i].status-16)%10;
1043 for (j=0; j<8; ++j) p[2+j]=d->dir[i].name[j]&0x7f;
1044 p[10]=(d->dir[i].ext[0]&0x7f)==' ' ? ' ' : '.';
1045 for (j=0; j<3; ++j) p[11+j]=d->dir[i].ext[j]&0x7f;
1047 pb=(unsigned char)d->dir[i].lrc;
1048 for (j=0; j<8; ++j) p[15+j]=((unsigned char)d->dir[i].pointers[7-j])^pb;
1051 fprintf(stderr,"getformat: %s\n",p);
1058 /* disc label */ /*{{{*/
1059 for (i=0; i<d->maxdir; ++i) if (d->dir[i].status==(char)0x20)
1063 d->cnotatime=d->dir[i].extnol&0x10;
1064 if (d->dir[i].extnol&0x1)
1067 if ((d->label=malloc(d->labelLength))==(char*)0)
1069 boo="out of memory";
1072 for (j=0; j<8; ++j) d->label[j]=d->dir[i].name[j]&0x7f;
1073 for (j=0; j<3; ++j) d->label[8+j]=d->dir[i].ext[j]&0x7f;
1097 d->dirtyDirectory = 0;
1098 root->ino=d->maxdir;
1100 root->mode=(s_ifdir|0777);
1102 root->atime=root->mtime=root->ctime=0;
1105 if (cpmCheckDs(d)==0) d->type|=CPMFS_DS_DATES;
1106 else d->ds=(struct dsDate*)0;
1111 /* syncDs -- write all datestamper timestamps */ /*{{{*/
1112 static int syncDs(const struct cpmSuperBlock *sb)
1116 int dsoffset, dsblks, dsrecs, off, i;
1119 dsrecs=(sb->maxdir+7)/8;
1121 /* Re-calculate checksums */
1122 buf = (unsigned char *)sb->ds;
1123 for ( i=0; i<dsrecs; i++ )
1127 for ( j=0; j<127; j++ ) cksum += buf[j];
1128 buf[j] = cksum & 0xff;
1131 dsoffset=(sb->maxdir*32+(sb->blksiz-1))/sb->blksiz;
1132 dsblks=(dsrecs*128+(sb->blksiz-1))/sb->blksiz;
1135 for (i=dsoffset; i<dsoffset+dsblks; i++)
1137 if (writeBlock(sb,i,((char*)(sb->ds))+off,0,-1)==-1) return -1;
1144 /* cpmSync -- write directory back */ /*{{{*/
1145 int cpmSync(struct cpmSuperBlock *sb)
1147 if (sb->dirtyDirectory)
1151 blocks=(sb->maxdir*32+sb->blksiz-1)/sb->blksiz;
1153 for (i=0; i<blocks; ++i)
1155 if (writeBlock(sb,i,(char*)(sb->dir+entry),0,-1)==-1) return -1;
1156 entry+=(sb->blksiz/32);
1158 sb->dirtyDirectory=0;
1160 if (sb->type&CPMFS_DS_DATES) syncDs(sb);
1164 /* cpmUmount -- free super block */ /*{{{*/
1165 void cpmUmount(struct cpmSuperBlock *sb)
1168 if (sb->type&CPMFS_DS_DATES) free(sb->ds);
1172 if (sb->passwdLength) free(sb->passwd);
1176 /* cpmNamei -- map name to inode */ /*{{{*/
1177 int cpmNamei(const struct cpmInode *dir, const char *filename, struct cpmInode *i)
1179 /* variables */ /*{{{*/
1181 char name[8],extension[3];
1182 int highestExtno,highestExt=-1,lowestExtno,lowestExt=-1;
1186 if (!S_ISDIR(dir->mode))
1191 if (strcmp(filename,".")==0 || strcmp(filename,"..")==0) /* root directory */ /*{{{*/
1197 else if (strcmp(filename,"[passwd]")==0 && dir->sb->passwdLength) /* access passwords */ /*{{{*/
1200 i->ino=dir->sb->maxdir+1;
1201 i->mode=s_ifreg|0444;
1203 i->atime=i->mtime=i->ctime=0;
1204 i->size=i->sb->passwdLength;
1208 else if (strcmp(filename,"[label]")==0 && dir->sb->labelLength) /* access label */ /*{{{*/
1211 i->ino=dir->sb->maxdir+2;
1212 i->mode=s_ifreg|0444;
1214 i->atime=i->mtime=i->ctime=0;
1215 i->size=i->sb->labelLength;
1219 if (splitFilename(filename,dir->sb->type,name,extension,&user)==-1) return -1;
1220 /* find highest and lowest extent */ /*{{{*/
1228 while ((extent=findFileExtent(dir->sb,user,name,extension,extent+1,-1))!=-1)
1230 int extno=EXTENT(dir->sb->dir[extent].extnol,dir->sb->dir[extent].extnoh);
1232 if (extno>highestExtno)
1237 if (extno<lowestExtno)
1245 if (highestExtno==-1) return -1;
1246 /* calculate size */ /*{{{*/
1250 i->size=highestExtno*16384;
1251 if (dir->sb->size<256) for (block=15; block>=0; --block)
1253 if (dir->sb->dir[highestExt].pointers[block]) break;
1255 else for (block=7; block>=0; --block)
1257 if (dir->sb->dir[highestExt].pointers[2*block] || dir->sb->dir[highestExt].pointers[2*block+1]) break;
1259 if (dir->sb->dir[highestExt].blkcnt) i->size+=((dir->sb->dir[highestExt].blkcnt&0xff)-1)*128;
1260 if (dir->sb->type & CPMFS_ISX)
1262 i->size += (128 - dir->sb->dir[highestExt].lrc);
1266 i->size+=dir->sb->dir[highestExt].lrc ? (dir->sb->dir[highestExt].lrc&0xff) : 128;
1269 fprintf(stderr,"cpmNamei: size=%ld\n",(long)i->size);
1277 /* read timestamps */ /*{{{*/
1278 protectMode = readTimeStamps(i,lowestExt);
1281 /* Determine the inode attributes */
1283 if (dir->sb->dir[lowestExt].name[0]&0x80) i->attr |= CPM_ATTR_F1;
1284 if (dir->sb->dir[lowestExt].name[1]&0x80) i->attr |= CPM_ATTR_F2;
1285 if (dir->sb->dir[lowestExt].name[2]&0x80) i->attr |= CPM_ATTR_F3;
1286 if (dir->sb->dir[lowestExt].name[3]&0x80) i->attr |= CPM_ATTR_F4;
1287 if (dir->sb->dir[lowestExt].ext [0]&0x80) i->attr |= CPM_ATTR_RO;
1288 if (dir->sb->dir[lowestExt].ext [1]&0x80) i->attr |= CPM_ATTR_SYS;
1289 if (dir->sb->dir[lowestExt].ext [2]&0x80) i->attr |= CPM_ATTR_ARCV;
1290 if (protectMode&0x20) i->attr |= CPM_ATTR_PWDEL;
1291 if (protectMode&0x40) i->attr |= CPM_ATTR_PWWRITE;
1292 if (protectMode&0x80) i->attr |= CPM_ATTR_PWREAD;
1294 if (dir->sb->dir[lowestExt].ext[1]&0x80) i->mode|=01000;
1296 if (!(dir->sb->dir[lowestExt].ext[0]&0x80)) i->mode|=0222;
1297 if (extension[0]=='C' && extension[1]=='O' && extension[2]=='M') i->mode|=0111;
1299 readDsStamps(i,lowestExt);
1304 /* cpmStatFS -- statfs */ /*{{{*/
1305 void cpmStatFS(const struct cpmInode *ino, struct cpmStatFS *buf)
1308 struct cpmSuperBlock *d;
1311 buf->f_bsize=d->blksiz;
1312 buf->f_blocks=d->size;
1314 buf->f_bused=-(d->maxdir*32+d->blksiz-1)/d->blksiz;
1315 for (i=0; i<d->alvSize; ++i)
1320 for (j=0; j<INTBITS; ++j)
1322 if (i*INTBITS+j < d->size)
1327 fprintf(stderr,"cpmStatFS: block %d allocated\n",(i*INTBITS+j));
1331 else ++buf->f_bfree;
1336 buf->f_bavail=buf->f_bfree;
1337 buf->f_files=d->maxdir;
1339 for (i=0; i<d->maxdir; ++i)
1341 if (d->dir[i].status==(char)0xe5) ++buf->f_ffree;
1346 /* cpmUnlink -- unlink */ /*{{{*/
1347 int cpmUnlink(const struct cpmInode *dir, const char *fname)
1350 char name[8],extension[3];
1352 struct cpmSuperBlock *drive;
1354 if (!S_ISDIR(dir->mode))
1360 if (splitFilename(fname,dir->sb->type,name,extension,&user)==-1) return -1;
1361 if ((extent=findFileExtent(drive,user,name,extension,0,-1))==-1) return -1;
1362 drive->dirtyDirectory=1;
1363 drive->dir[extent].status=(char)0xe5;
1366 drive->dir[extent].status=(char)0xe5;
1367 } while ((extent=findFileExtent(drive,user,name,extension,extent+1,-1))>=0);
1372 /* cpmRename -- rename */ /*{{{*/
1373 int cpmRename(const struct cpmInode *dir, const char *old, const char *new)
1375 struct cpmSuperBlock *drive;
1378 char oldname[8], oldext[3];
1380 char newname[8], newext[3];
1382 if (!S_ISDIR(dir->mode))
1388 if (splitFilename(old,dir->sb->type, oldname, oldext,&olduser)==-1) return -1;
1389 if (splitFilename(new,dir->sb->type, newname, newext,&newuser)==-1) return -1;
1390 if ((extent=findFileExtent(drive,olduser,oldname,oldext,0,-1))==-1) return -1;
1391 if (findFileExtent(drive,newuser,newname, newext,0,-1)!=-1)
1393 boo="file already exists";
1398 drive->dirtyDirectory=1;
1399 drive->dir[extent].status=newuser;
1400 memcpy7(drive->dir[extent].name, newname, 8);
1401 memcpy7(drive->dir[extent].ext, newext, 3);
1402 } while ((extent=findFileExtent(drive,olduser,oldname,oldext,extent+1,-1))!=-1);
1406 /* cpmOpendir -- opendir */ /*{{{*/
1407 int cpmOpendir(struct cpmInode *dir, struct cpmFile *dirp)
1409 if (!S_ISDIR(dir->mode))
1416 dirp->mode=O_RDONLY;
1420 /* cpmReaddir -- readdir */ /*{{{*/
1421 int cpmReaddir(struct cpmFile *dir, struct cpmDirent *ent)
1423 /* variables */ /*{{{*/
1424 struct PhysDirectoryEntry *cur=(struct PhysDirectoryEntry*)0;
1425 char buf[2+8+1+3+1]; /* 00foobarxy.zzy\0 */
1431 if (!(S_ISDIR(dir->ino->mode))) /* error: not a directory */ /*{{{*/
1433 boo="not a directory";
1439 if (dir->pos==0) /* first entry is . */ /*{{{*/
1441 ent->ino=dir->ino->sb->maxdir;
1443 strcpy(ent->name,".");
1449 else if (dir->pos==1) /* next entry is .. */ /*{{{*/
1451 ent->ino=dir->ino->sb->maxdir;
1453 strcpy(ent->name,"..");
1459 else if (dir->pos==2)
1461 if (dir->ino->sb->passwdLength) /* next entry is [passwd] */ /*{{{*/
1463 ent->ino=dir->ino->sb->maxdir+1;
1465 strcpy(ent->name,"[passwd]");
1472 else if (dir->pos==3)
1474 if (dir->ino->sb->labelLength) /* next entry is [label] */ /*{{{*/
1476 ent->ino=dir->ino->sb->maxdir+2;
1478 strcpy(ent->name,"[label]");
1485 else if (dir->pos>=RESERVED_ENTRIES && dir->pos<dir->ino->sb->maxdir+RESERVED_ENTRIES)
1487 int first=dir->pos-RESERVED_ENTRIES;
1489 if ((cur=dir->ino->sb->dir+(dir->pos-RESERVED_ENTRIES))->status>=0 && cur->status<=(dir->ino->sb->type&CPMFS_HI_USER ? 31 : 15))
1491 /* determine first extent for the current file */ /*{{{*/
1492 for (i=0; i<dir->ino->sb->maxdir; ++i) if (i!=(dir->pos-RESERVED_ENTRIES))
1494 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;
1497 if (first==(dir->pos-RESERVED_ENTRIES))
1499 ent->ino=dir->pos-RESERVED_INODES;
1500 /* convert file name to UNIX style */ /*{{{*/
1501 buf[0]='0'+cur->status/10;
1502 buf[1]='0'+cur->status%10;
1503 for (bufp=buf+2,i=0; i<8 && (cur->name[i]&0x7f)!=' '; ++i) *bufp++=tolower(cur->name[i]&0x7f);
1504 for (hasext=0,i=0; i<3 && (cur->ext[i]&0x7f)!=' '; ++i)
1506 if (!hasext) { *bufp++='.'; hasext=1; }
1507 *bufp++=tolower(cur->ext[i]&0x7f);
1511 assert(bufp<=buf+sizeof(buf));
1512 ent->reclen=bufp-buf;
1513 strcpy(ent->name,buf);
1525 /* cpmStat -- stat */ /*{{{*/
1526 void cpmStat(const struct cpmInode *ino, struct cpmStat *buf)
1529 buf->mode=ino->mode;
1530 buf->size=ino->size;
1531 buf->atime=ino->atime;
1532 buf->mtime=ino->mtime;
1533 buf->ctime=ino->ctime;
1536 /* cpmOpen -- open */ /*{{{*/
1537 int cpmOpen(struct cpmInode *ino, struct cpmFile *file, mode_t mode)
1539 if (S_ISREG(ino->mode))
1541 if ((mode&O_WRONLY) && (ino->mode&0222)==0)
1543 boo="permission denied";
1553 boo="not a regular file";
1558 /* cpmRead -- read */ /*{{{*/
1559 int cpmRead(struct cpmFile *file, char *buf, int count)
1561 int findext=1,findblock=1,extent=-1,block=-1,extentno=-1,got=0,nextblockpos=-1,nextextpos=-1;
1562 int blocksize=file->ino->sb->blksiz;
1565 extcap=(file->ino->sb->size<256 ? 16 : 8)*blocksize;
1566 if (extcap>16384) extcap=16384*file->ino->sb->extents;
1567 if (file->ino->ino==file->ino->sb->maxdir+1) /* [passwd] */ /*{{{*/
1569 if ((file->pos+count)>file->ino->size) count=file->ino->size-file->pos;
1570 if (count) memcpy(buf,file->ino->sb->passwd+file->pos,count);
1573 fprintf(stderr,"cpmRead passwd: read %d bytes, now at position %ld\n",count,(long)file->pos);
1578 else if (file->ino->ino==file->ino->sb->maxdir+2) /* [label] */ /*{{{*/
1580 if ((file->pos+count)>file->ino->size) count=file->ino->size-file->pos;
1581 if (count) memcpy(buf,file->ino->sb->label+file->pos,count);
1584 fprintf(stderr,"cpmRead label: read %d bytes, now at position %ld\n",count,(long)file->pos);
1589 else while (count>0 && file->pos<file->ino->size)
1595 extentno=file->pos/16384;
1596 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);
1597 nextextpos=(file->pos/extcap)*extcap+extcap;
1607 ptr=(file->pos%extcap)/blocksize;
1608 if (file->ino->sb->size>=256) ptr*=2;
1609 block=(unsigned char)file->ino->sb->dir[extent].pointers[ptr];
1610 if (file->ino->sb->size>=256) block+=((unsigned char)file->ino->sb->dir[extent].pointers[ptr+1])<<8;
1613 memset(buffer,0,blocksize);
1617 start=(file->pos%blocksize)/file->ino->sb->secLength;
1618 end=((file->pos%blocksize+count)>blocksize ? blocksize-1 : (file->pos%blocksize+count-1))/file->ino->sb->secLength;
1619 readBlock(file->ino->sb,block,buffer,start,end);
1622 nextblockpos=(file->pos/blocksize)*blocksize+blocksize;
1625 if (file->pos<nextblockpos)
1627 if (extent==-1) *buf++='\0'; else *buf++=buffer[file->pos%blocksize];
1632 else if (file->pos==nextextpos) findext=1; else findblock=1;
1635 fprintf(stderr,"cpmRead: read %d bytes, now at position %ld\n",got,(long)file->pos);
1640 /* cpmWrite -- write */ /*{{{*/
1641 int cpmWrite(struct cpmFile *file, const char *buf, int count)
1643 int findext=1,findblock=-1,extent=-1,extentno=-1,got=0,nextblockpos=-1,nextextpos=-1;
1644 int blocksize=file->ino->sb->blksiz;
1645 int extcap=(file->ino->sb->size<256 ? 16 : 8)*blocksize;
1646 int block=-1,start=-1,end=-1,ptr=-1,last=-1;
1651 if (findext) /*{{{*/
1653 extentno=file->pos/16384;
1654 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);
1655 nextextpos=(file->pos/extcap)*extcap+extcap;
1658 if ((extent=findFreeExtent(file->ino->sb))==-1) return (got==0 ? -1 : got);
1659 file->ino->sb->dir[extent]=file->ino->sb->dir[file->ino->ino];
1660 memset(file->ino->sb->dir[extent].pointers,0,16);
1661 file->ino->sb->dir[extent].extnol=EXTENTL(extentno);
1662 file->ino->sb->dir[extent].extnoh=EXTENTH(extentno);
1663 file->ino->sb->dir[extent].blkcnt=0;
1664 file->ino->sb->dir[extent].lrc=0;
1665 time(&file->ino->ctime);
1666 updateTimeStamps(file->ino,extent);
1667 updateDsStamps(file->ino,extent);
1673 if (findblock) /*{{{*/
1675 ptr=(file->pos%extcap)/blocksize;
1676 if (file->ino->sb->size>=256) ptr*=2;
1677 block=(unsigned char)file->ino->sb->dir[extent].pointers[ptr];
1678 if (file->ino->sb->size>=256) block+=((unsigned char)file->ino->sb->dir[extent].pointers[ptr+1])<<8;
1679 if (block==0) /* allocate new block, set start/end to cover it */ /*{{{*/
1681 if ((block=allocBlock(file->ino->sb))==-1) return (got==0 ? -1 : got);
1682 file->ino->sb->dir[extent].pointers[ptr]=block&0xff;
1683 if (file->ino->sb->size>=256) file->ino->sb->dir[extent].pointers[ptr+1]=(block>>8)&0xff;
1685 end=(blocksize-1)/file->ino->sb->secLength;
1686 memset(buffer,0,blocksize);
1687 time(&file->ino->ctime);
1688 updateTimeStamps(file->ino,extent);
1689 updateDsStamps(file->ino,extent);
1692 else /* read existing block and set start/end to cover modified parts */ /*{{{*/
1694 start=(file->pos%blocksize)/file->ino->sb->secLength;
1695 end=((file->pos%blocksize+count)>blocksize ? blocksize-1 : (file->pos%blocksize+count-1))/file->ino->sb->secLength;
1696 if (file->pos%file->ino->sb->secLength) readBlock(file->ino->sb,block,buffer,start,start);
1697 if (end!=start && (file->pos+count-1)<blocksize) readBlock(file->ino->sb,block,buffer+end*file->ino->sb->secLength,end,end);
1700 nextblockpos=(file->pos/blocksize)*blocksize+blocksize;
1704 /* fill block and write it */ /*{{{*/
1705 file->ino->sb->dirtyDirectory=1;
1706 while (file->pos!=nextblockpos && count)
1708 buffer[file->pos%blocksize]=*buf++;
1710 if (file->ino->size<file->pos) file->ino->size=file->pos;
1714 (void)writeBlock(file->ino->sb,block,buffer,start,end);
1715 time(&file->ino->mtime);
1716 if (file->ino->sb->size<256) for (last=15; last>=0; --last)
1718 if (file->ino->sb->dir[extent].pointers[last])
1723 else for (last=14; last>0; last-=2)
1725 if (file->ino->sb->dir[extent].pointers[last] || file->ino->sb->dir[extent].pointers[last+1])
1731 if (last>0) extentno+=(last*blocksize)/extcap;
1732 file->ino->sb->dir[extent].extnol=EXTENTL(extentno);
1733 file->ino->sb->dir[extent].extnoh=EXTENTH(extentno);
1734 file->ino->sb->dir[extent].blkcnt=((file->pos-1)%16384)/128+1;
1735 if (file->ino->sb->type & CPMFS_EXACT_SIZE)
1737 file->ino->sb->dir[extent].lrc = (128 - (file->pos%128)) & 0x7F;
1741 file->ino->sb->dir[extent].lrc=file->pos%128;
1743 updateTimeStamps(file->ino,extent);
1744 updateDsStamps(file->ino,extent);
1746 if (file->pos==nextextpos) findext=1;
1747 else if (file->pos==nextblockpos) findblock=1;
1752 /* cpmClose -- close */ /*{{{*/
1753 int cpmClose(struct cpmFile *file)
1758 /* cpmCreat -- creat */ /*{{{*/
1759 int cpmCreat(struct cpmInode *dir, const char *fname, struct cpmInode *ino, mode_t mode)
1762 char name[8],extension[3];
1764 struct cpmSuperBlock *drive;
1765 struct PhysDirectoryEntry *ent;
1767 if (!S_ISDIR(dir->mode))
1769 boo="No such file or directory";
1772 if (splitFilename(fname,dir->sb->type,name,extension,&user)==-1) return -1;
1774 fprintf(stderr,"cpmCreat: %s -> %d:%-.8s.%-.3s\n",fname,user,name,extension);
1776 if (findFileExtent(dir->sb,user,name,extension,0,-1)!=-1) return -1;
1778 if ((extent=findFreeExtent(dir->sb))==-1) return -1;
1779 ent=dir->sb->dir+extent;
1780 drive->dirtyDirectory=1;
1783 memcpy(ent->name,name,8);
1784 memcpy(ent->ext,extension,3);
1786 ino->mode=s_ifreg|mode;
1793 updateTimeStamps(ino,extent);
1794 updateDsStamps(ino,extent);
1799 /* cpmAttrGet -- get CP/M attributes */ /*{{{*/
1800 int cpmAttrGet(struct cpmInode *ino, cpm_attr_t *attrib)
1802 *attrib = ino->attr;
1806 /* cpmAttrSet -- set CP/M attributes */ /*{{{*/
1807 int cpmAttrSet(struct cpmInode *ino, cpm_attr_t attrib)
1809 struct cpmSuperBlock *drive;
1812 char name[8], extension[3];
1814 memset(name, 0, sizeof(name));
1815 memset(extension, 0, sizeof(extension));
1819 drive->dirtyDirectory=1;
1820 /* Strip off existing attribute bits */
1821 memcpy7(name, drive->dir[extent].name, 8);
1822 memcpy7(extension, drive->dir[extent].ext, 3);
1823 user = drive->dir[extent].status;
1825 /* And set new ones */
1826 if (attrib & CPM_ATTR_F1) name[0] |= 0x80;
1827 if (attrib & CPM_ATTR_F2) name[1] |= 0x80;
1828 if (attrib & CPM_ATTR_F3) name[2] |= 0x80;
1829 if (attrib & CPM_ATTR_F4) name[3] |= 0x80;
1830 if (attrib & CPM_ATTR_RO) extension[0] |= 0x80;
1831 if (attrib & CPM_ATTR_SYS) extension[1] |= 0x80;
1832 if (attrib & CPM_ATTR_ARCV) extension[2] |= 0x80;
1836 memcpy(drive->dir[extent].name, name, 8);
1837 memcpy(drive->dir[extent].ext, extension, 3);
1838 } while ((extent=findFileExtent(drive, user,name,extension,extent+1,-1))!=-1);
1840 /* Update the stored (inode) copies of the file attributes and mode */
1842 if (attrib&CPM_ATTR_RO) ino->mode&=~(S_IWUSR|S_IWGRP|S_IWOTH);
1843 else ino->mode|=(S_IWUSR|S_IWGRP|S_IWOTH);
1848 /* cpmChmod -- set CP/M r/o & sys */ /*{{{*/
1849 int cpmChmod(struct cpmInode *ino, mode_t mode)
1851 /* Convert the chmod() into a chattr() call that affects RO */
1852 int newatt = ino->attr & ~CPM_ATTR_RO;
1854 if (!(mode & (S_IWUSR|S_IWGRP|S_IWOTH))) newatt |= CPM_ATTR_RO;
1855 return cpmAttrSet(ino, newatt);
1858 /* cpmUtime -- set timestamps */ /*{{{*/
1859 void cpmUtime(struct cpmInode *ino, struct utimbuf *times)
1861 ino->atime = times->actime;
1862 ino->mtime = times->modtime;
1864 updateTimeStamps(ino,ino->ino);
1865 updateDsStamps(ino,ino->ino);