/* returns ?? */ static int dos33_add_file(int fd, char dos_type, int file_type, int address, int length, char *filename, char *apple_filename) { int free_space,file_size,needed_sectors; struct stat file_info; int size_in_sectors=0; int initial_ts_list=0,ts_list=0,i,data_ts,x,bytes_read=0,old_ts_list; int catalog_track,catalog_sector,sectors_used=0; int input_fd; int result; int first_write=1; if (apple_filename[0]<64) { fprintf(stderr,"Error! First char of filename " "must be ASCII 64 or above!\n"); return ERROR_INVALID_FILENAME; } /* Check for comma in filename */ for(i=0;i<strlen(apple_filename);i++) { if (apple_filename[i]==',') { fprintf(stderr,"Error! " "Cannot have , in a filename!\n"); return ERROR_INVALID_FILENAME; } } /* FIXME */ /* check type */ /* and sanity check a/b filesize is set properly */ /* Determine size of file to upload */ if (stat(filename,&file_info)<0) { fprintf(stderr,"Error! %s not found!\n",filename); return ERROR_FILE_NOT_FOUND; } file_size=(int)file_info.st_size; if (debug) printf("Filesize: %d\n",file_size); if (file_type==ADD_BINARY) { if (debug) printf("Adding 4 bytes for size/offset\n"); if (length==0) length=file_size; file_size+=4; } /* We need to round up to nearest sector size */ /* Add an extra sector for the T/S list */ /* Then add extra sector for a T/S list every 122*256 bytes (~31k) */ needed_sectors=(file_size/BYTES_PER_SECTOR)+ /* round sectors */ ((file_size%BYTES_PER_SECTOR)!=0)+/* tail if needed */ 1+/* first T/S list */ (file_size/(122*BYTES_PER_SECTOR)); /* extra t/s lists */ /* Get free space on device */ free_space=dos33_free_space(fd); /* Check for free space */ if (needed_sectors*BYTES_PER_SECTOR>free_space) { fprintf(stderr,"Error! Not enough free space " "on disk image (need %d have %d)\n", needed_sectors*BYTES_PER_SECTOR,free_space); return ERROR_NO_SPACE; } /* plus one because we need a sector for the tail */ size_in_sectors=(file_size/BYTES_PER_SECTOR)+ ((file_size%BYTES_PER_SECTOR)!=0); if (debug) printf("Need to allocate %i data sectors\n",size_in_sectors); if (debug) printf("Need to allocate %i total sectors\n",needed_sectors); /* Open the local file */ input_fd=open(filename,O_RDONLY); if (input_fd<0) { fprintf(stderr,"Error! could not open %s\n",filename); return ERROR_IMAGE_NOT_FOUND; } i=0; while (i<size_in_sectors) { /* Create new T/S list if necessary */ if (i%TSL_MAX_NUMBER==0) { old_ts_list=ts_list; /* allocate a sector for the new list */ ts_list=dos33_allocate_sector(fd); sectors_used++; if (ts_list<0) return -1; /* clear the t/s sector */ for(x=0;x<BYTES_PER_SECTOR;x++) { sector_buffer[x]=0; } lseek(fd,DISK_OFFSET((ts_list>>8)&0xff,ts_list&0xff),SEEK_SET); result=write(fd,sector_buffer,BYTES_PER_SECTOR); if (i==0) { initial_ts_list=ts_list; } else { /* we aren't the first t/s list so do special stuff */ /* load in the old t/s list */ lseek(fd, DISK_OFFSET(get_high_byte(old_ts_list), get_low_byte(old_ts_list)), SEEK_SET); result=read(fd,§or_buffer,BYTES_PER_SECTOR); /* point from old ts list to new one we just made */ sector_buffer[TSL_NEXT_TRACK]=get_high_byte(ts_list); sector_buffer[TSL_NEXT_SECTOR]=get_low_byte(ts_list); /* set offset into file */ sector_buffer[TSL_OFFSET_H]=get_high_byte((i-122)*256); sector_buffer[TSL_OFFSET_L]=get_low_byte((i-122)*256); /* write out the old t/s list with updated info */ lseek(fd, DISK_OFFSET(get_high_byte(old_ts_list), get_low_byte(old_ts_list)), SEEK_SET); result=write(fd,sector_buffer,BYTES_PER_SECTOR); } } /* allocate a sector */ data_ts=dos33_allocate_sector(fd); sectors_used++; if (data_ts<0) return -1; /* clear sector */ for(x=0;x<BYTES_PER_SECTOR;x++) sector_buffer[x]=0; /* read from input */ if ((first_write) && (file_type==ADD_BINARY)) { first_write=0; sector_buffer[0]=address&0xff; sector_buffer[1]=(address>>8)&0xff; sector_buffer[2]=(length)&0xff; sector_buffer[3]=((length)>>8)&0xff; bytes_read=read(input_fd,sector_buffer+4, BYTES_PER_SECTOR-4); bytes_read+=4; }
/* returns ?? */ static int dos33_add_file(int fd,char type,char *filename, char *apple_filename) { int free_space,file_size,needed_sectors; struct stat file_info; int size_in_sectors=0; int initial_ts_list=0,ts_list=0,i,data_ts,x,bytes_read=0,old_ts_list; int catalog_track,catalog_sector,sectors_used=0; int input_fd; int result; if (apple_filename[0]<64) { fprintf(stderr,"Error! First char of filename must be ASCII 64 or above!\n"); exit(3); } { int i; for(i=0;i<strlen(apple_filename);i++) { if (apple_filename[i]==',') { fprintf(stderr,"Error! Cannot have , in a filename!\n"); exit(3); } } } /* FIXME */ /* check type */ /* and sanity check a/b filesize is set properly */ /* Determine size of file to upload */ if (stat(filename,&file_info)<0) { fprintf(stderr,"Error! %s not found!\n",filename); exit(3); } file_size=(int)file_info.st_size; /* We need to round up to nearest sector size */ /* Add an extra sector for the T/S list */ /* Then add extra sector for a T/S list every 122*256 bytes (~31k) */ needed_sectors=(file_size/BYTES_PER_SECTOR)+ /* round sectors */ ((file_size%BYTES_PER_SECTOR)!=0)+/* tail if needed */ 1+/* first T/S list */ (file_size/(122*BYTES_PER_SECTOR)); /* extra t/s lists */ /* Get free space on device */ free_space=dos33_free_space(fd); /* Check for free space */ if (needed_sectors*BYTES_PER_SECTOR>free_space) { fprintf(stderr,"Error! Not enough free space on disk image (need %d have %d)\n", needed_sectors*BYTES_PER_SECTOR,free_space); exit(4); } /* plus one because we need a sector for the tail */ size_in_sectors=(file_size/BYTES_PER_SECTOR)+ ((file_size%BYTES_PER_SECTOR)!=0); // printf("Need to allocate %i data sectors\n",size_in_sectors); // printf("Need to allocate %i total sectors\n",needed_sectors); /* Open the local file */ input_fd=open(filename,O_RDONLY); if (input_fd<0) { fprintf(stderr,"Error! could not open %s\n",filename); return -1; } i=0; while (i<size_in_sectors) { /* Create new T/S list if necessary */ if (i%TSL_MAX_NUMBER==0) { old_ts_list=ts_list; /* allocate a sector for the new list */ ts_list=dos33_allocate_sector(fd); sectors_used++; if (ts_list<0) return -1; /* clear the t/s sector */ for(x=0;x<BYTES_PER_SECTOR;x++) sector_buffer[x]=0; lseek(fd,DISK_OFFSET((ts_list>>8)&0xff,ts_list&0xff),SEEK_SET); result=write(fd,sector_buffer,BYTES_PER_SECTOR); if (i==0) initial_ts_list=ts_list; else { /* we aren't the first t/s list so do special stuff */ /* load in the old t/s list */ lseek(fd, DISK_OFFSET(get_high_byte(old_ts_list), get_low_byte(old_ts_list)), SEEK_SET); result=read(fd,§or_buffer,BYTES_PER_SECTOR); /* point from old ts list to new one we just made */ sector_buffer[TSL_NEXT_TRACK]=get_high_byte(ts_list); sector_buffer[TSL_NEXT_SECTOR]=get_low_byte(ts_list); /* set offset into file */ sector_buffer[TSL_OFFSET_H]=get_high_byte((i-122)*256); sector_buffer[TSL_OFFSET_L]=get_low_byte((i-122)*256); /* write out the old t/s list with updated info */ lseek(fd, DISK_OFFSET(get_high_byte(old_ts_list), get_low_byte(old_ts_list)), SEEK_SET); result=write(fd,sector_buffer,BYTES_PER_SECTOR); } } /* allocate a sector */ data_ts=dos33_allocate_sector(fd); sectors_used++; if (data_ts<0) return -1; /* clear sector */ for(x=0;x<BYTES_PER_SECTOR;x++) sector_buffer[x]=0; /* read from input */ bytes_read=read(input_fd,sector_buffer,BYTES_PER_SECTOR); if (bytes_read<0) fprintf(stderr,"Error reading bytes!\n"); /* write to disk image */ lseek(fd,DISK_OFFSET((data_ts>>8)&0xff,data_ts&0xff),SEEK_SET); result=write(fd,sector_buffer,BYTES_PER_SECTOR); // printf("Writing %i bytes to %i/%i\n",bytes_read,(data_ts>>8)&0xff, // data_ts&0xff); /* add to T/s table */ /* read in t/s list */ lseek(fd,DISK_OFFSET((ts_list>>8)&0xff,ts_list&0xff),SEEK_SET); result=read(fd,sector_buffer,BYTES_PER_SECTOR); /* point to new data sector */ sector_buffer[((i%TSL_MAX_NUMBER)*2)+TSL_LIST]=(data_ts>>8)&0xff; sector_buffer[((i%TSL_MAX_NUMBER)*2)+TSL_LIST+1]=(data_ts&0xff); /* write t/s list back out */ lseek(fd,DISK_OFFSET((ts_list>>8)&0xff,ts_list&0xff),SEEK_SET); result=write(fd,sector_buffer,BYTES_PER_SECTOR); i++; }