Exemplo n.º 1
0
static bool
sip (struct file_data *current, bool skip_test)
{
  /* If we have a nonexistent file at this stage, treat it as empty.  */
  if (current->desc < 0)
    {
      /* Leave room for a sentinel.  */
      current->bufsize = sizeof (word);
      current->buffer = xmalloc (current->bufsize);
    }
  else
    {
      current->bufsize = buffer_lcm (sizeof (word),
				     STAT_BLOCKSIZE (current->stat),
				     PTRDIFF_MAX - 2 * sizeof (word));
      current->buffer = xmalloc (current->bufsize);

      if (! skip_test)
	{
	  /* Check first part of file to see if it's a binary file.  */

	  /* FIXME: if O_BINARY, this should revert to text mode
	     if the file is not binary.  */

	  file_block_read (current, current->bufsize);
	  return binary_file_p (current->buffer, current->buffered);
	}
    }

  current->buffered = 0;
  current->eof = false;
  return false;
}
Exemplo n.º 2
0
static bool
sip (struct file_data *current, bool skip_test)
{
  /* If we have a nonexistent file at this stage, treat it as empty.  */
  if (current->desc < 0)
    {
      /* Leave room for a sentinel.  */
      current->bufsize = sizeof (word);
      current->buffer = xmalloc (current->bufsize);
    }
  else
    {
      current->bufsize = buffer_lcm (sizeof (word),
				     STAT_BLOCKSIZE (current->stat),
				     PTRDIFF_MAX - 2 * sizeof (word));
      current->buffer = xmalloc (current->bufsize);

#ifdef __KLIBC__
      /* Skip test if seek is not possible */
      skip_test = skip_test
		  || (lseek (current->desc, 0, SEEK_CUR) < 0
		      && errno == ESPIPE);
#endif

      if (! skip_test)
	{
	  /* Check first part of file to see if it's a binary file.  */

	  int prev_mode = set_binary_mode (current->desc, O_BINARY);
	  off_t buffered;
	  file_block_read (current, current->bufsize);
	  buffered = current->buffered;

	  if (prev_mode != O_BINARY)
	    {
	      /* Revert to text mode and seek back to the start to reread
		 the file.  Use relative seek, since file descriptors
		 like stdin might not start at offset zero.  */
	      if (lseek (current->desc, - buffered, SEEK_CUR) < 0)
		pfatal_with_name (current->name);
	      set_binary_mode (current->desc, prev_mode);
	      current->buffered = 0;
	      current->eof = false;
	    }

	  return binary_file_p (current->buffer, buffered);
	}
    }

  current->buffered = 0;
  current->eof = false;
  return false;
}
Exemplo n.º 3
0
static bool
sip (struct file_data *current, bool skip_test)
{
  /* If we have a nonexistent file at this stage, treat it as empty.  */
  if (current->desc < 0)
    {
      /* Leave room for a sentinel.  */
      current->bufsize = sizeof (word);
      current->buffer = xmalloc (current->bufsize);
    }
  else
    {
      current->bufsize = buffer_lcm (sizeof (word),
				     STAT_BLOCKSIZE (current->stat),
				     PTRDIFF_MAX - 2 * sizeof (word));
      current->buffer = xmalloc (current->bufsize);

      if (! skip_test)
	{
	  /* Check first part of file to see if it's a binary file.  */

	  bool was_binary = set_binary_mode (current->desc, true);
	  off_t buffered;
	  file_block_read (current, current->bufsize);
	  buffered = current->buffered;

	  if (! was_binary)
	    {
	      /* Revert to text mode and seek back to the beginning to
		 reread the file.  Use relative seek, since file
		 descriptors like stdin might not start at offset
		 zero.  */

	      if (lseek (current->desc, - buffered, SEEK_CUR) == -1)
		pfatal_with_name (current->name);
	      set_binary_mode (current->desc, false);
	      current->buffered = 0;
	      current->eof = false;
	    }

	  return binary_file_p (current->buffer, buffered);
	}
    }

  current->buffered = 0;
  current->eof = false;
  return false;
}
Exemplo n.º 4
0
/* read_hrecs_file's job is to read a history file and fill in new "hrec"
 * (history record) array elements with the ones we need to print.
 *
 * Logic:
 * - Read a block from the file. 
 * - Walk through the block parsing line into hr records. 
 * - if the hr isn't used, free its strings, if it is, bump the hrec counter
 * - at the end of a block, copy the end of the current block to the start 
 * of space for the next block, then read in the next block.  If we get less
 * than the whole block, we're done. 
 */
static int
read_hrecs_file (Node *p, void *closure)
{
    char *cpstart, *cpend, *cp, *nl;
    char *hrline;
    int i;
    int fd;
    struct stat st_buf;
    const char *fname = p->key;

    if ((fd = CVS_OPEN (fname, O_RDONLY | OPEN_BINARY)) < 0)
    {
	error (0, errno, "cannot open history file `%s'", fname);
	return 0;
    }

    if (fstat (fd, &st_buf) < 0)
    {
	error (0, errno, "can't stat history file `%s'", fname);
	return 0;
    }

    if (!(st_buf.st_size))
    {
	error (0, 0, "history file `%s' is empty", fname);
	return 0;
    }

    cpstart = xnmalloc (2, STAT_BLOCKSIZE (st_buf));
    cpstart[0] = '\0';
    cp = cpend = cpstart;

    for (;;)
    {
	for (nl = cp; nl < cpend && *nl != '\n'; nl++)
	    if (!isprint (*nl)) *nl = ' ';

	if (nl >= cpend)
	{
	    if (nl - cp >= STAT_BLOCKSIZE (st_buf))
	    {
		error(1, 0, "history line %ld too long (> %lu)", hrec_idx + 1,
		      (unsigned long) STAT_BLOCKSIZE(st_buf));
	    }
	    if (nl > cp)
		memmove (cpstart, cp, nl - cp);
	    nl = cpstart + (nl - cp);
	    cp = cpstart;
	    i = read (fd, nl, STAT_BLOCKSIZE(st_buf));
	    if (i > 0)
	    {
		cpend = nl + i;
		*cpend = '\0';
		continue;
	    }
	    if (i < 0)
	    {
		error (0, errno, "error reading history file `%s'", fname);
		return 0;
	    }
	    if (nl == cp) break;
	    error (0, 0, "warning: no newline at end of history file `%s'",
		   fname);
	}
	*nl = '\0';

	if (hrec_count == hrec_max)
	{
	    struct hrec *old_head = hrec_head;

	    hrec_max += HREC_INCREMENT;
	    hrec_head = xnrealloc (hrec_head, hrec_max, sizeof (struct hrec));
	    if (last_since_tag)
		last_since_tag = hrec_head + (last_since_tag - old_head);
	    if (last_backto)
		last_backto = hrec_head + (last_backto - old_head);
	}

	/* fill_hrec dates from when history read the entire 
	   history file in one chunk, and then records were pulled out
	   by pointing to the various parts of this big chunk.  This is
	   why there are ugly hacks here:  I don't want to completely
	   re-write the whole history stuff right now.  */

	hrline = xstrdup (cp);
	fill_hrec (hrline, &hrec_head[hrec_count]);
	if (select_hrec (&hrec_head[hrec_count]))
	    hrec_count++;
	else 
	    free (hrline);

	cp = nl + 1;
    }
    free (cpstart);
    close (fd);
    return 1;
}