예제 #1
0
int main(int argc, char **argv)
{
	int ret;

	process_options(argc,argv);

	if ((in_fd == -1) || (out_fd == -1)) {
		if(in_fd != -1)
			close(in_fd);
		if(out_fd != -1)
			close(out_fd);
		fprintf(stderr,helptext);
		error_msg_and_die("You must specify input and output files!\n");
	}

	init_buffers();
	init_sumlist();

	while ((ret = load_next_block())) {
		create_summed_image(ret);
	}

	flush_buffers();
	clean_buffers();
	clean_sumlist();

	if (in_fd != -1)
		close(in_fd);
	if (out_fd != -1)
		close(out_fd);

	return 0;
}
예제 #2
0
void myinterrupt(int dd)
{
  interrupted = True;
  flush_buffers();
  set_async(False);
  STInfo.lock_mode=False;
}
예제 #3
0
/*
 * Write a page to NAND.
 */
static void mxs_nand_ecc_write_page(struct mtd_info *mtd,
				struct nand_chip *nand, const uint8_t *buf)
{
	struct mxs_nand_info *nand_info = nand->priv;
	struct mxs_dma_desc *d;
	uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
	int ret;

	memcpy(nand_info->data_buf, buf, mtd->writesize);
	memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);

	/* Handle block mark swapping. */
	mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);

	/* Compile the DMA descriptor - write data. */
	d = mxs_nand_get_dma_desc(nand_info);
	d->cmd.data =
		MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
		MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
		(6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);

	d->cmd.address = 0;

	d->cmd.pio_words[0] =
		GPMI_CTRL0_COMMAND_MODE_WRITE |
		GPMI_CTRL0_WORD_LENGTH |
		(nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
		GPMI_CTRL0_ADDRESS_NAND_DATA;
	d->cmd.pio_words[1] = 0;
	d->cmd.pio_words[2] =
		GPMI_ECCCTRL_ENABLE_ECC |
		GPMI_ECCCTRL_ECC_CMD_ENCODE |
		GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
	d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
	d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
	d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;

	flush_buffers(mtd, nand_info);

	mxs_dma_desc_append(channel, d);

	/* Flush caches */
	mxs_nand_flush_data_buf(nand_info);

	/* Execute the DMA chain. */
	ret = mxs_dma_go(channel);
	if (ret) {
		printf("%s: DMA write error\n", __func__);
		goto rtn;
	}

	ret = mxs_nand_wait_for_bch_complete();
	if (ret) {
		printf("%s: BCH write timeout\n", __func__);
		goto rtn;
	}

rtn:
	mxs_nand_return_dma_descs(nand_info);
}
예제 #4
0
파일: file.c 프로젝트: HarryR/sanos
int dfs_fsync(struct file *filp) {
  int rc;
  struct inode *inode = (struct inode *) filp->data;

  // Flush and sync buffer cache for entire file system
  rc = flush_buffers(inode->fs->cache, 0);
  if (rc < 0) return rc;

  rc = sync_buffers(inode->fs->cache, 0);
  if (rc < 0) return rc;

  return 0;
}
예제 #5
0
void	init_preview(void)
{
	fprintf(stderr, "initializing preview\n");
	create_component(PREVIEW, &(g_data.preview), "preview");
	configure_preview_input();
	set_preview_input_port();
	create_pool_on_port(
			&(g_data.preview_input_port_pool),
			g_data.preview_input_port,
			preview_buffer_callback,
			"preview");
	flush_buffers();
}
예제 #6
0
static int start_mouse_transfer(libusb_device_handle * handle,
        unsigned int ep, libusb_transfer_cb_fn cb, void * data,
        unsigned int timeout)
{
    int iso_packets = 0, usb_err, i, num_urbs_in_flight = 8;

    num_urbs_in_flight = 1;	//JM

    struct libusb_transfer * trans[num_urbs_in_flight];
    unsigned char * buf = NULL;


    for(i = 0; i < num_urbs_in_flight; ++i){
        trans[i] = libusb_alloc_transfer(iso_packets);
        if(trans[i] == NULL){
            for(--i; i >= 0; --i){
                libusb_free_transfer(trans[i]);
            }
            return LIBUSB_ERROR_NO_MEM;
        }
        buf  = calloc(g_packet_size, sizeof(unsigned char));
        if(buf == NULL){
            for(; i>= 0; --i)
                libusb_free_transfer(trans[i]);
            return LIBUSB_ERROR_NO_MEM;
        }

        libusb_fill_interrupt_transfer(trans[i], handle, ep, buf, g_packet_size, cb, data, timeout);

        trans[i]->flags = LIBUSB_TRANSFER_FREE_BUFFER;
        usb_err = libusb_submit_transfer(trans[i]);
        if(usb_err != 0){

            printf_tagged_message(FOURCC('L', 'O', 'G', 'S'), "\n JM  error ");
            flush_buffers();

            for(--i; i >=0; --i){
                libusb_cancel_transfer(trans[i]);
                //todo: handle error besides LIBUSB_ERROR_NOT_FOUND for cancel
                libusb_free_transfer(trans[i]);
            }
            return usb_err;
        }
    }
    return 0;
}
예제 #7
0
파일: fatdir.c 프로젝트: TijmenW/FreeDOS
VOID dir_close(REG f_node_ptr fnp)
{
  /* Test for invalid f_nodes                                     */
  if (fnp == NULL || !(fnp->f_flags & F_DDIR))
    return;

#ifndef IPL
  /* Write out the entry                                          */
  dir_write(fnp);

#endif
  /* Clear buffers after release                                  */
  /* hazard: no error checking! */
  flush_buffers(fnp->f_dpb->dpb_unit);

  /* and release this instance of the fnode                       */
  release_f_node(fnp);
}
예제 #8
0
파일: fatdir.c 프로젝트: TijmenW/FreeDOS
VOID dir_close(REG f_node_ptr fnp)
{
  REG COUNT disk = fnp->f_dpb->dpb_unit;

  /* Test for invalid f_nodes                                     */
  if (fnp == NULL)
    return;

#ifndef IPL
  /* Write out the entry                                          */
  dir_write(fnp);

#endif
  /* Clear buffers after release                                  */
  flush_buffers(disk);

  /* and release this instance of the fnode                       */
  release_f_node(fnp);
}
예제 #9
0
파일: super.c 프로젝트: HarryR/sanos
void close_filesystem(struct filsys *fs)
{
  unsigned int i;

  // Release all group descriptors
  for (i = 0; i < fs->groupdesc_blocks; i++) release_buffer(fs->cache, fs->groupdesc_buffers[i]);
  free(fs->groupdesc_buffers);
  free(fs->groups);

  // Write super block
  if (fs->super_dirty) dev_write(fs->devno, fs->super, SECTORSIZE, 1);
  free(fs->super);

  // Flush buffer cache
  flush_buffers(fs->cache);

  // Free cache
  free_buffer_pool(fs->cache);

  // Deallocate file system
  free(fs);
}
예제 #10
0
main()
{
   const char *device = "/dev/spidev1.0"; 
    fd = open(device, O_RDWR);
    int4 i;
  int4 status;
  float f;
  char *verb, *qual;
  int4 iverb;
  char host_name[80];
  int4 port;
  char table_name[80];
  char *theString;

      printf  (" \n");
      TERM_BLUE_COLOR;
      printf  (" ***************************************************** \n");
      printf  (" ***************************************************** \n");
      printf  (" **                                                 ** \n");
      printf  (" **            Welcome to CRISTAL:                  ** \n");
      printf  (" **       ContRol In Sum-Trigger ALgorithm          ** \n");
      printf  (" **                                                 ** \n");
      printf  (" **          by: Jezabel R. Garcia                  ** \n");
      printf  (" **                                                 ** \n");
      printf  (" ***************************************************** \n");
      printf  (" ***************************************************** \n");
      TERM_RESET_COLOR;
      printf  (" \n");

  //  signal(SIGINT, myinterrupt);
  signal(SIGPIPE,brokenpipe);
  signal(SIGTERM,myabort);
  signal(SIGBUS,mybuserror);

  theString=(char *)malloc(sizeof(char)*256);
  STInfo.hostname[0]=(char*)malloc(sizeof(char)*80);
  STInfo.hostname[1]=(char*)malloc(sizeof(char)*80);
  
  /*
   * Set the initial values for status/times
   */
  time(&STInfo.startime);// mirar los usos del tiempo
  time(&STInfo.writetime);//
  STInfo.istatus=ST_STATUS_BUSY; 
  STInfo.threshold_control_flag=0;  //by default off
  STInfo.Global_Target_rate = 500; //Hz
  STInfo.telescope=1;
  STInfo.temp_control=0;
  STInfo.total_time=3600;
  STInfo.time_interval=900;   
  
  //initializes the  BBmenu package
  menu_init();
  
  //allow simulate_input()
  AllowInput=1;
  
  define_file("cristal_define.uic");
  
  simulate_input("@cristal_init");
  
  AllowInput=0;
  /*
   * Launch threads for service programs
   */
 // usleep(100);//DBG
  STInfo.exit = False;
  printf("init threads \n");
  STInfo.monexit=true; //monitoring thread not running

  //SERVER THREAD (para oir a cc)
    pthread_attr_init(&serv_attr);
    pthread_create(&serv_tid, &serv_attr, (void * (*)(void *)) server_thread, NULL);
  printf("server_thread initiated \n");
  
  //CLIENT THREAD ( para hablar a cc)
    pthread_attr_init(&client_attr);
    pthread_create(&client_tid, &client_attr,  (void * (*)(void *)) client_thread, NULL);
  printf("client_thread initiated \n");
 //Add a Thread for L3 
  
  
  //Report thread 
  pthread_attr_init(&Report_attr);
  pthread_create(&Report_tid, &Report_attr, (void * (*)(void *)) Report_thread, NULL);
  printf("Report_thread initiated \n");
  


  while(True)
    {
      
      verb=action_menu( "ST_Main_Menu", &iverb);
      if(interrupted==True)
	{
	  interrupted=False;
	  flush_buffers();
	}
      
      //////////////////////
      if(strcmp(verb,"STOP!")==0) 
	{             
	  transfer(0xA4, 0, 0, 0);
	  printf("Stopping gloabl triggers.");
	}
      
      //////////////////////
      if(strcmp(verb,"START")==0)
	{                        
	  transfer(0xA3, 0, 0, 0);
          printf("Starting gloabl triggers.");
	}

      //////////////////////                                                                  
      if(strcmp(verb,"SETTARGETRATE")==0)
        {
	  get_integer("Enter Global Target Trigger Rate", 
		      &STInfo.Global_Target_rate, 0, 0);
        }



      //////////////////////                                                                          
      if(strcmp(verb,"MONSTOP!")==0)  //monitoring stop
        {
	  STInfo.monexit=true; //stop monitpring thread
        }

      //////////////////////                                                                          
      if(strcmp(verb,"MONSTART")==0)  //monitoring start
        {
	  pthread_attr_init(&STmon_attr);
	  pthread_create(&STmon_tid, &STmon_attr, (void * (*)(void *)) STmon_thread, NULL);
	  //STInfo.monexit=0; // keep monitoring threat running
        }

      
      //////////////////////
      if(strcmp(verb,"REPORT")==0) 
	{                                                
	  error_logger(__FILE__,ERINFO,"Reports ON!");                               
	  if(!timer_on)start_timer(imon_sec);                                        
	}
      
      //////////////////////
      if(strcmp(verb,"REP!")==0) 
	{                                                
	  stop_timer();                                                              
	  error_logger(__FILE__,ERINFO,"Reports OFF");                               
	}                           
      
      //////////////////////
      if(strcmp(verb,"LOCK!")==0) 
	{
	//switch to remote control
	  error_logger(__FILE__,ERINFO,"Readout ready to be engaged!");
	  set_async(True);//aqui es cuando cierra la conexion, usando getsring
	  STInfo.lock_mode=True;
	}
    
      //////////////////////
      if(strcmp(verb,"ULOCK")==0) 
	{
	  //	if(client_conn) {
	  //	    client_close(); client_conn=False; }
	  error_logger(__FILE__,ERINFO,"Readout disengaged from CC");
	  set_async(False);
	  STInfo.lock_mode=False;
	}
      
      //////////////////////
      if(strcmp(verb,"HOSTNAME")==0) 
	{
	  int ihost;
	  for(ihost=0;ihost<2;ihost++)
	    {
	      get_text("Enter Host Name",(char *)STInfo.hostname[ihost]);
	     printf(" DBG HOSTNAME, %s", STInfo.hostname[ihost]);
	      //if(STInfo.debug==True)
	      // printf("Host Name defined: %s\n", STInfo.hostname[ihost]);
	    }
	}
      
      //////////////////////
      if(strcmp(verb,"HOSTNO")==0) 
	{
	  int ihost;
	  for(ihost=0;ihost<2;ihost++)
	    {
	      get_integer("Enter Host Port Number",&STInfo.hostno[ihost],0,0);
	      //  if(STInfo.debug==True)
	          printf("Host Port Number defined: %ld\n", STInfo.hostno[ihost]);
	    }
	}
      
      //////////////////////
      	if(strcmp(verb,"CRISTAL")==0) 
	{
	  printf("\n");
	  TERM_PURPLE_COLOR;
	  printf("CIRSTAL:Control in Sum-Trigger Algorithm\n");
	  printf("Author: Jezabel R. Garcia\n");
    	  TERM_RESET_COLOR;
	  printf("This progam  was developed following the concept of MIR .\n");
	  printf("(by Riccardo Paoeletti and Daniel Mazin)\n");
	  printf("The Mir space station was built by connecting\n");
	  printf("several modules, each launched into orbit separately\n");
	  printf("One of the main module of the Mir was Cristal\n");
	  printf("that performed the astronomical observations and \n");
	  printf("extended the scientific possibilities of Mir.\n");
	  printf  (" \n");
	}
      
      //////////////////////
      	if(strcmp(verb,"TELESCOPE")==0) 
	{
	  get_integer("Who I am?",&STInfo.telescope,1,2);
	  printf("This cristal version is running on M%i\n",
	  STInfo.telescope);
	}
      
      //////////////////////
      if(strcmp(verb,"PORTNO")==0) 
	{
	  get_integer("Enter Local Port Number",&STInfo.portno,0,0);
	  // if(STInfo.debug==True)
	     printf("Local Port Number defined: %ld\n", STInfo.portno);
	}
      
      //////////////////////
      if(strcmp(verb,"TOOLS")==0) 	tools_menu();
      
      //////////////////////
      if(strcmp(verb,"DUMMY")==0) 	dummy_menu();
      
      //////////////////////
      if(strcmp(verb,"ASTRO")==0) 	astro_menu();
      
      //////////////////////
      if(strcmp(verb,"CLIP")==0) 	clip_menu();
      
      //////////////////////
      if(strcmp(verb,"SUM")==0) 	sum_menu();
      
      //////////////////////
      if(strcmp(verb,"POWER")==0) power_menu();
      
      //////////////////////
      if(strcmp(verb,"MESSAGE")==0) {
	get_text("Enter command", theString);
	puts(theString);
	error_logger(__FILE__,ERINFO, theString);
      }
      
      //////////////////////
      if(strcmp(verb,"TIME_READ")==0)
	{
	  int data4=0;
	  get_hexadecimal("time? ",(long unsigned int*) &data4,0,0);
	  
	  int secs=15-data4&0xF;
	  int sect=15-(data4>>4)&0x7;
	  int mins=15-(data4>>7)&0xF;
	  int mint=15-(data4>>11)&0x7;
	  int hout=15-(data4>>14)&0x2;
	  
	  int hour=15-(data4>>16)&0xF;
        int tickWrong=(data4>>24)&0xFF;
        int tickRight = 0;
        int tickc = 0;
        for (tickc=0;tickc<8;tickc++)
        {
            tickRight += ((tickWrong>>(7-tickc))&0x1)*pow(2,tickc);
            //				  printf(" right %x wrong  %x \n ",tickRight, (tickWrong>>(7-tickc))&0x1);
        }
        int tick = tickRight;

        //super second:
        //				printf("R %X ",data4&0x0000FFFF);
        //super sub second:
        printf(" %X ",(data4&0xFFFF0000)>>16);

        //subsecond:
        get_hexadecimal("time? ",(long unsigned int*)&data4,0,0);

        printf("time: %d.%d h %d%d m %d%d %lf %d\n ",hout,hour,mint,mins,sect,secs,1.677721600-(data4&0x00007FFF)*51200e-9, tickRight);

    }
    
    //////////////////////
    if(strcmp(verb,"SYSTEM")==0)
    {
      get_text("Enter command", theString);
      system(theString);
    }
    
    /////////////////////:
    if(strcmp(verb,"READY")==0)
    {
	  STInfo.istatus=ST_STATUS_BUSY; 
      TERM_BLUE_COLOR;
      printf  (" ***************************************************** \n");
      printf  (" ***************************************************** \n");
      printf  (" **                                                 ** \n");
      printf  (" **         CRISTAL IS NOW READY TO OPERATE         ** \n");
      printf  (" **                                                 ** \n");
      printf  (" **                 ENJOY IT!                       ** \n");
      printf  (" **                                                 ** \n");
      printf  (" ***************************************************** \n");
      printf  (" ***************************************************** \n");
      TERM_RESET_COLOR;
      printf  (" \n");
      printf  ("Press ? for more info. \n");
    }
    
    if(strcmp(verb,"EXIT")==0)
    {
        stop_timer();
      break;
    }
    usleep(250);
  }
예제 #11
0
/* flush all changes made on a filesystem */
void reiserfs_flush (reiserfs_filsys_t fs)
{
    flush_buffers ();
}
예제 #12
0
/*
 * Read a page from NAND.
 */
static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
					uint8_t *buf, int page)
{
	struct mxs_nand_info *nand_info = nand->priv;
	struct mxs_dma_desc *d;
	uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
	uint32_t corrected = 0, failed = 0;
	uint8_t	*status;
	int i, ret;

	/* Compile the DMA descriptor - wait for ready. */
	d = mxs_nand_get_dma_desc(nand_info);
	d->cmd.data =
		MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
		MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
		(1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);

	d->cmd.address = 0;

	d->cmd.pio_words[0] =
		GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
		GPMI_CTRL0_WORD_LENGTH |
		(nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
		GPMI_CTRL0_ADDRESS_NAND_DATA;

	mxs_dma_desc_append(channel, d);

	/* Compile the DMA descriptor - enable the BCH block and read. */
	d = mxs_nand_get_dma_desc(nand_info);
	d->cmd.data =
		MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
		MXS_DMA_DESC_WAIT4END |	(6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);

	d->cmd.address = 0;

	d->cmd.pio_words[0] =
		GPMI_CTRL0_COMMAND_MODE_READ |
		GPMI_CTRL0_WORD_LENGTH |
		(nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
		GPMI_CTRL0_ADDRESS_NAND_DATA |
		(mtd->writesize + mtd->oobsize);
	d->cmd.pio_words[1] = 0;
	d->cmd.pio_words[2] =
		GPMI_ECCCTRL_ENABLE_ECC |
		GPMI_ECCCTRL_ECC_CMD_DECODE |
		GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
	d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
	d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
	d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;

	flush_buffers(mtd, nand_info);

	mxs_dma_desc_append(channel, d);

	/* Compile the DMA descriptor - disable the BCH block. */
	d = mxs_nand_get_dma_desc(nand_info);
	d->cmd.data =
		MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
		MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
		(3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);

	d->cmd.address = 0;

	d->cmd.pio_words[0] =
		GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
		GPMI_CTRL0_WORD_LENGTH |
		(nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
		GPMI_CTRL0_ADDRESS_NAND_DATA |
		(mtd->writesize + mtd->oobsize);
	d->cmd.pio_words[1] = 0;
	d->cmd.pio_words[2] = 0;

	mxs_dma_desc_append(channel, d);

	/* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
	d = mxs_nand_get_dma_desc(nand_info);
	d->cmd.data =
		MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
		MXS_DMA_DESC_DEC_SEM;

	d->cmd.address = 0;

	mxs_dma_desc_append(channel, d);

	/* Execute the DMA chain. */
	ret = mxs_dma_go(channel);
	if (ret) {
		printf("%s: DMA read error\n", __func__);
		goto rtn;
	}

	ret = mxs_nand_wait_for_bch_complete();
	if (ret) {
		printf("MXS NAND: BCH read timeout\n");
		goto rtn;
	}

	/* Invalidate caches */
	mxs_nand_inval_data_buf(nand_info);

	/* Read DMA completed, now do the mark swapping. */
	mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);

	/* Loop over status bytes, accumulating ECC status. */
	status = nand_info->oob_buf + mxs_nand_aux_status_offset();
	for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd); i++) {
		if (status[i] == 0x00)
			continue;

		if (status[i] == 0xff)
			continue;

		if (status[i] == 0xfe) {
			failed++;
			continue;
		}

		corrected += status[i];
	}

	/* Propagate ECC status to the owning MTD. */
	mtd->ecc_stats.failed += failed;
	mtd->ecc_stats.corrected += corrected;

	/*
	 * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
	 * details about our policy for delivering the OOB.
	 *
	 * We fill the caller's buffer with set bits, and then copy the block
	 * mark to the caller's buffer. Note that, if block mark swapping was
	 * necessary, it has already been done, so we can rely on the first
	 * byte of the auxiliary buffer to contain the block mark.
	 */
	memset(nand->oob_poi, 0xff, mtd->oobsize);

	nand->oob_poi[0] = nand_info->oob_buf[0];

	memcpy(buf, nand_info->data_buf, mtd->writesize);

rtn:
	mxs_nand_return_dma_descs(nand_info);

	return ret;
}
예제 #13
0
/**************************************************************
 * function: main()
 * args: none
 * returns: none
 * side effects: coalesces all existing checkpoint files into a 
 *               single map and a single data file.
 * called from: ckpt_recover(), take_ckpt()
 *************************************************************/
void main(int argc, char ** argv)
{
  int i=0, j, no_files;
  long no_chunks, total_chunks;
  int map_fd, data_fd;
  char mapfile[256], datafile[256], maptemp[256], datatemp[256];
  char * addr;
  caddr_t stopaddr, topstack;
  long size, pn;
  off_t seekptr;
  CLIST list;
  int page1; char buf[BUFSIZE];

  if(argc != 5){
    fprintf(stderr, "Error: Coalesce called with wrong number of args\n");
    exit(1);
  }

  FILEAREA = (caddr_t)(uintptr_t)atoi(argv[1]);
  filename = strdup(argv[2]);
  verbose = (BOOL)atoi(argv[3]);
  DEBUG = atoi(argv[4]);

  /*-- checking for the number of checkpoint files --*/
  while(1){
    sprintf(datafile, "%s.%s.%d", filename, "data", i);
    data_fd = OPEN(datafile, O_RDONLY, 0644);

    sprintf(mapfile, "%s.%s.%d", filename, "map", i);
    map_fd = OPEN(mapfile, O_RDONLY, 0644);

    if(data_fd == -1 || map_fd == -1){
      CLOSE(data_fd); CLOSE(map_fd);
      break;
    }
    CLOSE(data_fd); CLOSE(map_fd);
    i++;
  }

  no_files = i;

  if(no_files == 0){
    fprintf(stderr, "Error: Recovery specified, but no checkpoint files found\n");
    exit(-1);
  }
  else if(no_files == 1){
    return;
  }

  /*-- reset buffer counts --*/
  datanochars=0;
  mapnochars=0;

  if(verbose)
    fprintf(stderr, "CKPT: coalescing %d sets of checkpoint files\n", no_files);

  sprintf(datatemp, "%s.%s.temp", filename, "data");
  remove(datatemp);
  datatemp_fd = OPEN(datatemp, O_WRONLY | O_CREAT, 0644);
 
  sprintf(maptemp, "%s.%s.temp", filename, "map");
  remove(maptemp);
  maptemp_fd = OPEN(maptemp, O_WRONLY | O_CREAT, 0644);

  /*-- transfer data to the relevant temp file (most recent to least recent) --*/
  for(i=no_files-1; i>=0; i--){
    sprintf(mapfile, "%s.%s.%d", filename, "map", i);
    map_fd = OPEN(mapfile, O_RDONLY, 0644);

    if(i==no_files-1){                  /*-- read this info from last file only --*/
      read(map_fd, (char *)&topstack, POINTERSIZE);
      read(map_fd, (char *)&DATASTART, POINTERSIZE);
      read(map_fd, (char *)&stopaddr, POINTERSIZE);
      read(map_fd, (char *)&no_chunks, LONGSIZE);
      write(maptemp_fd, (char *)&topstack, POINTERSIZE);
      write(maptemp_fd, (char *)&DATASTART, POINTERSIZE);
      write(maptemp_fd, (char *)&stopaddr, POINTERSIZE);
      write(maptemp_fd, (char *)&no_chunks, POINTERSIZE);
      list = make_clist(stopaddr);
    }
    else{
      lseek(map_fd, POINTERSIZE*3, SEEK_SET);
      read(map_fd, (char *)&no_chunks, LONGSIZE);
    }

    no_chunks--;  /*-- exclude the stack's chunk --*/
 
    /*-- exclude filearea, if present --*/
    if(FILEAREA != 0){
      no_chunks--;
    }
 
    for(j=0; j<no_chunks; j++){
      read(map_fd, (char *)&addr, POINTERSIZE);
      read(map_fd, (char *)&size, LONGSIZE);
      read(map_fd, (char *)&seekptr, LONGSIZE);

      clist_insertchunk(list, addr, size, i, seekptr);
    }

    CLOSE(map_fd);
  }

  total_chunks = coa_transfer_chunks(list);
  destroy_clist(list);

  sprintf(datafile, "%s.%s.%d", filename, "data", no_files-1);
  data_fd = OPEN(datafile, O_RDONLY, 0644);

  sprintf(mapfile, "%s.%s.%d", filename, "map", no_files-1);
  map_fd = OPEN(mapfile, O_RDONLY, 0644);

  /*-- We must advance the seekptr in the map file appropriately --*/
  lseek(map_fd, POINTERSIZE*3, SEEK_SET);
  read(map_fd, (char *)&no_chunks, LONGSIZE);
  no_chunks--;         /*-- exclude the stack's chunk --*/
  if(FILEAREA != 0){   /*-- exclude filearea, if present --*/
    no_chunks--;
  }
  lseek(map_fd, no_chunks*MAPCHUNKSIZE, SEEK_CUR);


  if(FILEAREA != 0){
    read(map_fd, (char *)&addr, POINTERSIZE);
    read(map_fd, (char *)&size, POINTERSIZE);
    read(map_fd, (char *)&seekptr, POINTERSIZE);
    coa_write_chunk(addr, size, data_fd, seekptr);

    debug(stderr, "DEBUG: writing 0x%x - Ox%x from ckptfile %d\n", addr, 
                                          addr+size, no_files-1);

    total_chunks++;
  }

  read(map_fd, (char *)&addr, POINTERSIZE);
  read(map_fd, (char *)&size, POINTERSIZE);
  read(map_fd, (char *)&seekptr, POINTERSIZE);
  coa_write_chunk(addr, size, data_fd, seekptr);

  debug(stderr, "DEBUG: writing 0x%x - Ox%x from ckptfile %d\n", addr, 
                                          addr+size, no_files-1);

  total_chunks++;
  
  CLOSE(map_fd);
  CLOSE(data_fd);

  flush_buffers();
  lseek(maptemp_fd, POINTERSIZE*3, SEEK_SET);
  write(maptemp_fd, (char *)&total_chunks, LONGSIZE);
  CLOSE(maptemp_fd);
  CLOSE(datatemp_fd);

  sprintf(datafile, "%s.%s.0", filename, "data");
  sprintf(mapfile, "%s.%s.0", filename, "map");
  rename(datatemp, datafile);
  rename(maptemp, mapfile);

  for(i=no_files-1; i>0; i--){
    sprintf(datafile, "%s.%s.%d", filename, "data", i);
    sprintf(mapfile, "%s.%s.%d", filename, "map", i);
    remove(datafile);
    remove(mapfile);
  }

  if(verbose)
    fprintf(stderr, "CKPT: coalescing complete\n");
}