Ejemplo n.º 1
0
int save_rom_to_file(int num, room *rom_ptr)
{
	int fd;
	long n; 
	char file[256];
	char str[512];


	get_room_filename( num, file );

	unlink(file);
	fd = open(file, O_RDWR | O_CREAT | O_BINARY, ACC);
	if(fd < 0)
	{
		sprintf(str, "Could not create room file: %s", file);
		elog(str);
		return(-1);
	}

	n = write_rom(fd, rom_ptr, 0);
	if(n < 0) {
		sprintf(str, "write_rom() failed on saving file: %s", file);
		elog(str);
		close(fd);
		return(-1);
	}
	close(fd);

	return(0);
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
	struct code_rom rom;
	FILE *in, *out;

	memset(&rom, 0, sizeof(rom));

	if ( argc != 3 ) {
		fprintf(stderr, "%s: Usage: %s [in] [out]\n", argv[0], argv[0]);
		return EXIT_FAILURE;
	}

	if (strcmp(argv[1], "-")) {
		rom.fn = argv[1];
		in = fopen(argv[1], "r");
	}else{
		in = stdin;
		rom.fn = "(stdin)";
	}

	if ( NULL == in ) {
		fprintf(stderr, "%s: %s: %s\n",
			argv[0], rom.fn, strerror(errno));
		return EXIT_FAILURE;
	}

	if ( !assemble(&rom, in) )
		return EXIT_FAILURE;

	if (strcmp(argv[2], "-")) {
		rom.fn = argv[2];
		out = fopen(argv[2], "w");
	}else{
		out = stdout;
		rom.fn = "(stdout)";
	}

	if ( !write_rom(&rom, out) )
		return EXIT_FAILURE;

	fclose(out);
	return EXIT_SUCCESS;
}
Ejemplo n.º 3
0
int fixup_room(int fd, room *rom_ptr )
{
	/* assume the worst */
	int nReturn = 0;
	int	num;
	int	size;
	int	fd_bad;

	char	filename[256];
	char	filebad[256];
	char	str[128];
	char	*buffer;
	int		ndx;

	ASSERTLOG( rom_ptr );

	num = rom_ptr->rom_num;
	get_room_filename( num, filename );
	
	sprintf(str, "Attempting to fix corrupt room number %d", num );
	elog( str );

	/* find a name that doesn't exist */
	sprintf( filebad, "%s.bad", filename );

	ndx = 0;
	while ( file_exists( filebad ) )
	{
		sprintf( filebad, "%s.ba%d", filename, ndx );
		ndx++;
	}

	sprintf(str, "Copying file %s to %s", filename, filebad);
	elog( str );

	size = lseek(fd, 0, SEEK_END);
	buffer = malloc( size );
	if ( buffer )
	{
		/* rewind */
		if ( lseek( fd, 0, SEEK_SET ) == 0)
		{
			if ( size != read(fd, buffer, size ) )
			{
				elog("Unable to read room to make a backup of room" );
				free(buffer );
				return(0);
			}

			fd_bad = open(filebad, O_CREAT | O_RDWR | O_BINARY );
			if(fd_bad < 0)
			{
				elog("Unable to open backup of room" );
				free(buffer );
				return(0);
			}

			if ( size != write(fd_bad, buffer, size ) )
			{
				elog("Unable to write backup of room" );
				free(buffer );
				close( fd_bad );
				return(0);
			}

			/* done with backup */
			close( fd_bad );
		}
		else
		{
			elog("Unable to rewind file.");
			free(buffer);
			return(0);
		}

		free(buffer);
	}


	/* now write out a good copy to the original file handle */
	
	/* start back at the beginning */
	if ( lseek(fd, 0, SEEK_SET) == 0 )
	{
		if ( write_rom(fd, rom_ptr, 1 ) >= 0 )
		{
			/* it worked */
			nReturn = 1;
			sprintf(str, "Saved a good copy of file %s.", filename);
			elog( str );
		}
	}
	else
	{
		elog("Unable to rewind file.");
		return(0);
	}



	return(nReturn);
}