예제 #1
0
파일: ascii1.c 프로젝트: FuzzyHobbit/mordor
void write_all_ply()
{
	HFINDFILE	hff;
	char	filename[256];
	creature	*ply;
	FILE	*fp;

	fp = fopen( player_file, "w");
	if ( fp != NULL ) 
	{

		hff = find_first_file(get_player_path(), filename, 256);
		if ( hff )
		{
			do 
			{
				if ( filename[0] != '.')
				{
					if (load_ply_from_file(filename, &ply))
						printf("Unable to read binary player %s.\n", filename);
					else 
					{
						write_player(fp, filename, ply);
						free_crt(ply);
					}
				}
			} while( find_next_file( hff, filename, 256 ));

			close_find_file(hff);
		}
		fclose(fp);
	}
	
	return;
}
예제 #2
0
uint8 FSDrive::open_file(int channel, char *filename)
{
	char plainname[NAMEBUF_LENGTH];
	int filemode = FMODE_READ;
	int filetype = FTYPE_PRG;
	bool wildflag = false;
	char *mode = "rb";
	
	convert_filename(filename, plainname, &filemode, &filetype, &wildflag);

	// Channel 0 is READ PRG, channel 1 is WRITE PRG
	if (!channel) {
		filemode = FMODE_READ;
		filetype = FTYPE_PRG;
	}
	if (channel == 1) {
		filemode = FMODE_WRITE;
		filetype = FTYPE_PRG;
	}

	// Wildcards are only allowed on reading
	if (wildflag) {
		if (filemode != FMODE_READ) {
			set_error(ERR_SYNTAX33);
			return ST_OK;
		}
		find_first_file(plainname);
	}

	// Select fopen() mode according to file mode
	switch (filemode) {
		case FMODE_READ:
			mode = "rb";
			break;
		case FMODE_WRITE:
			mode = "wb";
			break;
		case FMODE_APPEND:
			mode = "ab";
			break;
	}

	// Open file
	if (chdir(dir_path))
		set_error(ERR_NOTREADY);
	else if ((file[channel] = fopen(plainname, mode)) != NULL) {
		if (filemode == FMODE_READ)	// Read and buffer first byte
			read_char[channel] = fgetc(file[channel]);
	} else
		set_error(ERR_FILENOTFOUND);
	chdir(AppDirPath);

	return ST_OK;
}
예제 #3
0
uint8 T64Drive::open_file(int channel, char *filename)
{
    char plainname[NAMEBUF_LENGTH];
    int filemode = FMODE_READ;
    int filetype = FTYPE_PRG;
    int num;

    convert_filename(filename, plainname, &filemode, &filetype);

    // Channel 0 is READ PRG, channel 1 is WRITE PRG
    if (!channel) {
        filemode = FMODE_READ;
        filetype = FTYPE_PRG;
    }
    if (channel == 1) {
        filemode = FMODE_WRITE;
        filetype = FTYPE_PRG;
    }

    // Allow only read accesses
    if (filemode != FMODE_READ) {
        set_error(ERR_WRITEPROTECT);
        return ST_OK;
    }

    // Find file
    if (find_first_file(plainname, filetype, &num)) {

        // Open temporary file
        if ((file[channel] = tmpfile()) != NULL) {

            // Write load address (.t64 only)
            if (!is_lynx) {
                fwrite(&file_info[num].sa_lo, 1, 1, file[channel]);
                fwrite(&file_info[num].sa_hi, 1, 1, file[channel]);
            }

            // Copy file contents from .t64 file to temp file
            uint8 *buf = new uint8[file_info[num].length];
            fseek(the_file, file_info[num].offset, SEEK_SET);
            fread(buf, file_info[num].length, 1, the_file);
            fwrite(buf, file_info[num].length, 1, file[channel]);
            rewind(file[channel]);
            delete[] buf;

            if (filemode == FMODE_READ)	// Read and buffer first byte
                read_char[channel] = fgetc(file[channel]);
        }
    } else
        set_error(ERR_FILENOTFOUND);

    return ST_OK;
}
예제 #4
0
파일: ascii1.c 프로젝트: FuzzyHobbit/mordor
void write_all_obj()
{
	HFINDFILE	hff;
	char	filename[256];
	object *obj;
	int	num;
	int	index;
	FILE	*fp;

	fp = fopen( object_file, "w");
	if ( fp != NULL ) 
	{

		hff = find_first_file(get_object_path(), filename, 256);
		if ( hff )
		{
			do 
			{
				if ( filename[0] != '.' && toupper(filename[0]) == 'O' && strlen(filename) == 3)
				{
					index = OFILESIZE * atoi(&filename[1]);

					for (num=0; num < OFILESIZE ; num++) 
					{
						if (load_obj_from_file(index + num, &obj ) == 0 )
						{
							write_object(fp, index+num,obj);
							free_obj(obj);
						}
					}

				}
			} while( find_next_file( hff, filename, 256 ));

			close_find_file(hff);
		}

		fclose(fp);
	}

	return;
	
}
예제 #5
0
파일: ascii1.c 프로젝트: FuzzyHobbit/mordor
void write_all_crt()
{
	HFINDFILE	hff;
	char	filename[256];
	creature	*crt;
	int	num;
	int	index;
	FILE	*fp;

	fp = fopen( creature_file, "w");
	if ( fp != NULL ) 
	{

		hff = find_first_file(get_monster_path(), filename, 256);
		if ( hff )
		{
			do 
			{
				if ( filename[0] != '.' && toupper(filename[0]) == 'M' && strlen(filename) == 3)
				{
					index = MFILESIZE * atoi(&filename[1]);

					for (num=0; num != MFILESIZE; num++) {
						if (load_crt_from_file(index + num, &crt ) == 0 )
						{
							write_creature(fp, index+num,crt);
							free_crt(crt);
						}
					}
				}
			} while( find_next_file( hff, filename, 256 ));

			close_find_file(hff);
		}

		fclose(fp);
	}

	return;
	
}
예제 #6
0
파일: ascii1.c 프로젝트: FuzzyHobbit/mordor
void write_all_rom()
{
	HFINDFILE	hff;
	char	filename[256];
	room	*rom;
	FILE	*fp;
	int n=0;

	fp = fopen( room_file, "w");
	if ( fp != NULL ) 
	{

		hff = find_first_file(get_room_path(), filename, 256);
		if ( hff )
		{
			do 
			{
				if ( filename[0] != '.' )
				{
					if (load_rom_from_file( atoi(&filename[1]), &rom))
						printf("Unable to read binary room %d.  Continuing.\n", atoi(&filename[1]) );
					else 
					{
						n++;
						write_room(fp, rom);
						free_rom(rom);
					}
				}
			} while( find_next_file( hff, filename, 256 ));

			close_find_file(hff);
		}

		fclose(fp);
	}

	
}
예제 #7
0
int ScanFile(HXCFLOPPYEMULATOR* floppycontext,struct Volume * adfvolume,char * folder,char * file)
{
	long hfindfile;
	filefoundinfo FindFileData;
	int bbool;
	int byte_written;
	FILE * ftemp;
	unsigned char  tempbuffer[512];
	struct File* adffile;
	unsigned char * fullpath;//,*fileimg;
	int size,filesize;
	RETCODE  rc;
	
	hfindfile=find_first_file(folder,file, &FindFileData); 
	if(hfindfile!=-1)
	{
		bbool=TRUE;
		while(hfindfile!=-1 && bbool)
		{
			if(FindFileData.isdirectory)
			{
				if(strcmp(".",FindFileData.filename)!=0 && strcmp("..",FindFileData.filename)!=0)
				{
					if(adfCountFreeBlocks(adfvolume)>4)
					{
						floppycontext->hxc_printf(MSG_INFO_1,"Adding directory %s",FindFileData.filename);
						rc=adfCreateDir(adfvolume,adfvolume->curDirPtr,FindFileData.filename); 
						if(rc==RC_OK)
						{
							floppycontext->hxc_printf(MSG_INFO_1,"entering directory %s",FindFileData.filename);
							rc=adfChangeDir(adfvolume, FindFileData.filename);
							if(rc==RC_OK)
							{
								
								fullpath=malloc(strlen(FindFileData.filename)+strlen(folder)+2);
								sprintf(fullpath,"%s\\%s",folder,FindFileData.filename);

								if(ScanFile(floppycontext,adfvolume,fullpath,file))
								{
									adfParentDir(adfvolume);
									free(fullpath);
									return 1;
								}
								floppycontext->hxc_printf(MSG_INFO_1,"Leaving directory %s",FindFileData.filename);
								free(fullpath);
								adfParentDir( adfvolume);
							}
							else
							{
								floppycontext->hxc_printf(MSG_ERROR,"Cannot enter to the directory %s !",FindFileData.filename);
								return 1;
							}
						}
						else
						{
							floppycontext->hxc_printf(MSG_ERROR,"Cannot Add the directory %s !",FindFileData.filename);
							return 1;
						}
					}
					else
					{
						floppycontext->hxc_printf(MSG_ERROR,"Cannot Add a directory ! : no more free block!!!");
					    return 1;
					}
					
				}
			}
			else
			{
				if(adfCountFreeBlocks(adfvolume)>4)
				{
						floppycontext->hxc_printf(MSG_INFO_1,"Adding file %s, %dB",FindFileData.filename,FindFileData.size);
						adffile = adfOpenFile(adfvolume, FindFileData.filename, "w");
						if(adffile)
						{
							if(FindFileData.size)
							{
								fullpath=malloc(strlen(FindFileData.filename)+strlen(folder)+2);
								sprintf(fullpath,"%s\\%s",folder,FindFileData.filename);

								ftemp=fopen(fullpath,"rb");
								if(ftemp)
								{
									fseek(ftemp,0,SEEK_END);
									filesize=ftell(ftemp);
									fseek(ftemp,0,SEEK_SET);
									do
									{					
										if(filesize>=512)
										{
											size=512;		
										}
										else
										{
                                            size=filesize;
										}
										fread(&tempbuffer,size,1,ftemp);

										byte_written=adfWriteFile(adffile, size, tempbuffer);
										if((byte_written!=size) || (adfCountFreeBlocks(adfvolume)<2) )
										{
											floppycontext->hxc_printf(MSG_ERROR,"Error while writting the file %s. No more free block ?",FindFileData.filename);
											adfCloseFile(adffile);
											fclose(ftemp);
											free(fullpath);
											return 1;
										}
										filesize=filesize-512;

									}while( (filesize>0) && (byte_written==size));
								

									/*fileimg=(unsigned char*)malloc(filesize);
									memset(fileimg,0,filesize);
									fread(fileimg,filesize,1,ftemp);
									adfWriteFile(adffile, filesize, fileimg);
									free(fileimg);*/

									adfCloseFile(adffile);
									fclose(ftemp);
									free(fullpath);
								}
								else
								{
										floppycontext->hxc_printf(MSG_ERROR,"Error : Cannot open %s !!!",fullpath);
										free(fullpath);
										return 1;
								}
							}
						}
						else
						{
                             floppycontext->hxc_printf(MSG_ERROR,"Error : Cannot create %s, %dB!!!",FindFileData.filename,FindFileData.size);
							 return 1;
						}
				}
				else
				{
					floppycontext->hxc_printf(MSG_ERROR,"Error : Cannot add a file : no more free block");
					return 1;
				}
			}
			bbool=find_next_file(hfindfile,folder,file,&FindFileData);
		}
		
	}
	else printf("Error FindFirstFile\n");

	find_close(hfindfile);
	return 0;
}
예제 #8
0
int ScanFileAndAddToFAT(HXCFLOPPYEMULATOR* floppycontext,char * folder,char * file, char * fattable,char *entriestable,char *datatable,int parentcluster,FATCONFIG * fatconfig,int numberofcluster)
{
	long hfindfile;
	filefoundinfo FindFileData;
	int bbool;
	int tii;
	FILE * ftemp;
	char * newentry;
	char * subnewentry;
	char tempstr[256];
	int lefttoread;
	int fatclusternb;
	unsigned char * fullpath;
	fat_directory_entry *entry;
	fat_directory_entry *subentry;
	int i,j;
	struct stat repstate;
	struct tm * ts;

	tii=0;
	hfindfile=find_first_file(folder,file, &FindFileData); 
	if(hfindfile!=-1)
	{
		bbool=1;
		while(hfindfile!=-1 && bbool)
		{
			if(FindFileData.isdirectory)
			{
				if(strcmp(".",FindFileData.filename)!=0 && strcmp("..",FindFileData.filename)!=0)
				{
					newentry=findfreeentry(entriestable);
					entry=(fat_directory_entry *)newentry;
					
					memset(entry->DIR_Name,0x20,8+3);
					sprintf((char*)tempstr,"%s",FindFileData.filename);
					
					floppycontext->hxc_printf(MSG_INFO_1,"Adding directory %s",FindFileData.filename);
					
					strupper(tempstr);
					if(strchr(tempstr,'.'))
					{
						memcpy(&entry->DIR_Name[8],strchr(tempstr,'.')+1,strlen(strchr(tempstr,'.')+1));
						*strchr(tempstr,'.')=0;
					}
					memcpy(entry->DIR_Name,tempstr,strlen(tempstr));
					entry->DIR_Attr=entry->DIR_Attr|0x10;
					entry->DIR_FileSize=FindFileData.size;


					fatclusternb=findfreecluster(fattable,numberofcluster);
					if(fatclusternb==-1)
					{
						floppycontext->hxc_printf(MSG_ERROR,"Cannot add this directory ! : No more cluster free !");
						find_close(hfindfile);
						return 1;
					}
					memset(&datatable[(fatclusternb-2)*fatconfig->sectorsize*fatconfig->clustersize],0,fatconfig->sectorsize*fatconfig->clustersize);
					
					entry->DIR_FstClusLO=fatclusternb;
					//*( (unsigned short*) &newentry[0x1A])=fatclusternb;
					setclusterptr(fattable,fatclusternb,0xFFF);
					
					subnewentry=findfreeentry(&datatable[(fatclusternb-2)*fatconfig->sectorsize*fatconfig->clustersize]);
					subentry=(fat_directory_entry *)subnewentry;

					sprintf(subentry->DIR_Name,".          ");

					//memcpy(subnewentry,".          ",strlen(".          "));
					subentry->DIR_Attr=0x10;
					subentry->DIR_FstClusLO=fatclusternb;
					
					subnewentry=findfreeentry(&datatable[(fatclusternb-2)*fatconfig->sectorsize*fatconfig->clustersize]);
					subentry=(fat_directory_entry *)subnewentry;
					sprintf(subentry->DIR_Name,"..         ");
					subentry->DIR_Attr=0x10;
					subentry->DIR_FstClusLO=parentcluster;
					//*( (unsigned short*) &subnewentry[0x1A])=parentcluster;
					
					floppycontext->hxc_printf(MSG_INFO_1,"Entering directory %s",FindFileData.filename);

					fullpath=malloc(strlen(FindFileData.filename)+strlen(folder)+2);
					sprintf(fullpath,"%s\\%s",folder,FindFileData.filename);

					if(ScanFileAndAddToFAT(floppycontext,fullpath,file,fattable,&datatable[(fatclusternb-2)*fatconfig->sectorsize*fatconfig->clustersize],datatable,fatclusternb,fatconfig,numberofcluster))
					{
						free(fullpath);
						find_close(hfindfile);
						return 1;
					}
					free(fullpath);

					floppycontext->hxc_printf(MSG_INFO_1,"Leaving directory %s",FindFileData.filename);
					
				}
			}
			else
			{
				floppycontext->hxc_printf(MSG_INFO_1,"Adding file %s, %dB",FindFileData.filename,FindFileData.size);

				sprintf(tempstr,"%s",FindFileData.filename);
				strupper(tempstr);

				newentry=findfreeentry(entriestable);
				entry=(fat_directory_entry *)newentry;
				memset(entry->DIR_Name,0x20,8+3);

				i=0;
				while(tempstr[i]  && (i<8) && tempstr[i]!='.')
				{

					if(tempstr[i]==' ')
					{
						newentry[i]='_';
					}
					else
					{
						newentry[i]=tempstr[i];
					}
					i++;
				}
					
				if(strchr(tempstr,'.'))
				{

					i=0;
					while(tempstr[i]!='.')
					{
						i++;
					}

					j=0;
					i++;
					while(tempstr[i]  && (j<3))
					{

						if(tempstr[i]==' ')
						{
							newentry[8+j]='_';
						}
						else
						{
							newentry[8+j]=tempstr[i];
						}
						i++;
						j++;
					}


					memcpy(newentry+8,strchr(tempstr,'.')+1,strlen(strchr(tempstr,'.')+1));
					*strchr(tempstr,'.')=0;
				}


				entry->DIR_FileSize=FindFileData.size;
				if(FindFileData.size)
				{
					lefttoread=FindFileData.size;
					fatclusternb=findfreecluster(fattable,numberofcluster);
					memset(&datatable[(fatclusternb-2)*fatconfig->sectorsize*fatconfig->clustersize],0,fatconfig->sectorsize*fatconfig->clustersize);
					
					if(fatclusternb==-1)
					{
						floppycontext->hxc_printf(MSG_ERROR,"Cannot add this file ! : No more cluster free !");
						find_close(hfindfile);
						return 1;
					}
					entry->DIR_FstClusLO=fatclusternb;
					
					if(file)
					{
						fullpath=malloc(strlen(FindFileData.filename)+strlen(folder)+2);
						sprintf(fullpath,"%s\\%s",folder,FindFileData.filename);
					}
					else
					{
						fullpath=malloc(strlen(folder)+1);
						sprintf(fullpath,"%s",folder);
					}


					
					stat(fullpath,&repstate);
					ts=localtime(&repstate.st_ctime);
					if(ts)
					{
						entry->DIR_CrtDate=  (((ts->tm_year-80) &0x7F)<<9) | ((ts->tm_mon+1 &0xF)<<5) | (ts->tm_mday &0x1F);
						entry->DIR_CrtTime= ((ts->tm_hour&0x1F)<<11) | ((ts->tm_min  &0x3F)<<5) | ((ts->tm_sec/2)&0x1F);
					}
					else
					{
						entry->DIR_CrtDate= 0;
						entry->DIR_CrtTime= 0;
					}

					stat(fullpath,&repstate);
					ts=localtime(&repstate.st_mtime);
					if(ts)
					{
						entry->DIR_WrtDate=  (((ts->tm_year-80) &0x7F)<<9) | ((ts->tm_mon+1 &0xF)<<5) | (ts->tm_mday &0x1F);
						entry->DIR_WrtTime= ((ts->tm_hour&0x1F)<<11) | ((ts->tm_min  &0x3F)<<5) | ((ts->tm_sec/2)&0x1F);
					}
					else
					{
						entry->DIR_WrtDate= 0;
						entry->DIR_WrtTime= 0;
					}

					ftemp=fopen(fullpath,"rb");
					if(ftemp)
					{
						do
						{
							fatclusternb=findfreecluster(fattable,numberofcluster);
							if(fatclusternb==-1)
							{
								floppycontext->hxc_printf(MSG_ERROR,"Error while adding this file ! : No more cluster free !");
								free(fullpath);
								find_close(hfindfile);
								fclose(ftemp);
								return 1;
							}
							memset(&datatable[(fatclusternb-2)*fatconfig->sectorsize*fatconfig->clustersize],0x00,fatconfig->sectorsize*fatconfig->clustersize);
							fread(&datatable[(fatclusternb-2)*fatconfig->sectorsize*fatconfig->clustersize],fatconfig->sectorsize*fatconfig->clustersize,1,ftemp);
							setclusterptr(fattable,fatclusternb,0xFFF);
							if(lefttoread>(fatconfig->sectorsize*fatconfig->clustersize))
							{
								setclusterptr(fattable,fatclusternb,findfreecluster(fattable,numberofcluster));
							}
							lefttoread=lefttoread-(fatconfig->sectorsize*fatconfig->clustersize);
						}while(lefttoread>0);
						
						fclose(ftemp);
					}
					else
					{
						floppycontext->hxc_printf(MSG_ERROR,"Error while adding this file ! : Access error !");
					}
					free(fullpath);
					
				}	
			}
			
			bbool=find_next_file(hfindfile,folder,file,&FindFileData);	
		}
		
	}
	else printf("Error FindFirstFile\n");
	
	find_close(hfindfile);
	
	return 0;
}
예제 #9
0
파일: plist.c 프로젝트: FuzzyHobbit/mordor
int main (int argc, char *argv[])
/* plist command provides a quick way to check a given players *
 * level, class, password and inventory without having to log  *
 * the player in or use the editor */
{
	int		i, adj;
	char	filename[256];
	char	flags = 0;
	HFINDFILE	hff;

	lvl=0;
	adj = 1;

 
    if (argc < 1) {
		usage();
        return(1);
    }
 
    for (i=1;i<argc;i++){
 
        if (argv[i][0] == '-'){
            switch(argv[i][1]){
                case 'i':
		    set_flag(flags,0);
                    continue;
	            break;
                case 'n':
		    set_flag(flags,1);
       	             continue;
                case 'p':
			set_flag(flags,2);
                    	continue;
		        break;  
            }
 
            if(i+1 < argc && isdigit(argv[i+1][0])){
                switch(argv[i][1]){
                    case 'l':
                        lvl = atoi(argv[i+1]);
                    break;
		    case 's':
                        Spellnum = atoi(argv[i+1]);
			spl=1;
                    break;
                    default:
                       set_flag(flags,3); 
                    break;
                }
                i++;
            }
            else
                set_flag(flags,3); 
        }
	else
	    break;
 
        if (is_fset(flags,3)){
            usage();
            return(1);
        }
            
    } 
	adj = i;

	if (adj > argc)
	{
       	usage();
		return(1);
	}
	else if ( adj == argc )
	{
		/* wants all players */
		hff = find_first_file(get_player_path(), filename, 256);
		if ( hff )
		{
			do 
			{
				if ( filename[0] != '.')
				{
					print_player( filename, flags );
				}

			} while( find_next_file( hff, filename, 256 ));

			close_find_file(hff);
		}
    }
	else
	{
		for(i= adj;i < argc; i++)
		{
			print_player( argv[i], flags );
		}
	}


	return(0);
}
예제 #10
0
uint8 FSDrive::open_file(int channel, const uint8 *name, int name_len)
{
	char plain_name[NAMEBUF_LENGTH];
	int plain_name_len;
	int mode = FMODE_READ;
	int type = FTYPE_PRG;
	int rec_len = 0;
	parse_file_name(name, name_len, (uint8 *)plain_name, plain_name_len, mode, type, rec_len, true);

	// Channel 0 is READ, channel 1 is WRITE
	if (channel == 0 || channel == 1) {
		mode = channel ? FMODE_WRITE : FMODE_READ;
		if (type == FTYPE_DEL)
			type = FTYPE_PRG;
	}

	bool writing = (mode == FMODE_WRITE || mode == FMODE_APPEND);

	// Expand wildcards (only allowed on reading)
	if (strchr(plain_name, '*') || strchr(plain_name, '?')) {
		if (writing) {
			set_error(ERR_SYNTAX33);
			return ST_OK;
		} else
			find_first_file(plain_name);
	}

	// Relative files are not supported
	if (type == FTYPE_REL) {
		set_error(ERR_UNIMPLEMENTED);
		return ST_OK;
	}

	// Select fopen() mode according to file mode
	const char *mode_str = "rb";
	switch (mode) {
		case FMODE_WRITE:
			mode_str = "wb";
			break;
		case FMODE_APPEND:
			mode_str = "ab";
			break;
	}

	// Open file
#ifndef __riscos__
	if (chdir(dir_path))
		set_error(ERR_NOTREADY);
	else if ((file[channel] = fopen(plain_name, mode_str)) != NULL) {
		if (mode == FMODE_READ || mode == FMODE_M)	// Read and buffer first byte
			read_char[channel] = fgetc(file[channel]);
	} else
		set_error(ERR_FILENOTFOUND);
#ifdef SPMP
	libgame_chdir_game();
#else
	chdir(AppDirPath);
#endif
#else
	{
	  char fullname[NAMEBUF_LENGTH];

  	  // On RISC OS make a full filename
	  sprintf(fullname,"%s.%s",dir_path,plain_name);
	  if ((file[channel] = fopen(fullname, mode)) != NULL)
	  {
	    if (mode == FMODE_READ || mode == FMODE_M)
	    {
	      read_char[channel] = fgetc(file[channel]);
	    }
	  }
	  else
	  {
	    set_error(ERR_FILENOTFOUND);
	  }
	}
#endif

	return ST_OK;
}
예제 #11
0
uint8 ArchDrive::open_file(int channel, const uint8 *name, int name_len)
{
	uint8 plain_name[NAMEBUF_LENGTH];
	int plain_name_len;
	int mode = FMODE_READ;
	int type = FTYPE_DEL;
	int rec_len = 0;
	parse_file_name(name, name_len, plain_name, plain_name_len, mode, type, rec_len);

	// Channel 0 is READ, channel 1 is WRITE
	if (channel == 0 || channel == 1) {
		mode = channel ? FMODE_WRITE : FMODE_READ;
		if (type == FTYPE_DEL)
			type = FTYPE_PRG;
	}

	bool writing = (mode == FMODE_WRITE || mode == FMODE_APPEND);

	// Wildcards are only allowed on reading
	if (writing && (strchr((const char *)plain_name, '*') || strchr((const char *)plain_name, '?'))) {
		set_error(ERR_SYNTAX33);
		return ST_OK;
	}

	// Allow only read accesses
	if (writing) {
		set_error(ERR_WRITEPROTECT);
		return ST_OK;
	}

	// Relative files are not supported
	if (type == FTYPE_REL) {
		set_error(ERR_UNIMPLEMENTED);
		return ST_OK;
	}

	// Find file
	int num;
	if (find_first_file(plain_name, plain_name_len, num)) {

		// Open temporary file
		if ((file[channel] = tmpfile()) != NULL) {

			// Write load address (.t64 only)
			if (archive_type == TYPE_T64) {
				fwrite(&file_info[num].sa_lo, 1, 1, file[channel]);
				fwrite(&file_info[num].sa_hi, 1, 1, file[channel]);
			}

			// Copy file contents from archive file to temp file
			uint8 *buf = new uint8[file_info[num].size];
			fseek(the_file, file_info[num].offset, SEEK_SET);
			fread(buf, file_info[num].size, 1, the_file);
			fwrite(buf, file_info[num].size, 1, file[channel]);
			rewind(file[channel]);
			delete[] buf;

			if (mode == FMODE_READ)	// Read and buffer first byte
				read_char[channel] = getc(file[channel]);
		}
	} else
		set_error(ERR_FILENOTFOUND);

	return ST_OK;
}