⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 input.c

📁 android-w.song.android.widget
💻 C
📖 第 1 页 / 共 2 页
字号:
  if (fd1 == fd2)    return 0;  m = max (fd1, fd2);  ALLOCATE_BUFFERS (m);  /* If FD2 is the file descriptor bash is currently using for shell input,     we need to do some extra work to make sure that the buffered stream     actually exists (it might not if fd1 was not active, and the copy     didn't actually do anything). */  is_bash_input = (bash_input.type == st_bstream) &&		  (bash_input.location.buffered_fd == fd2);  if (buffers[fd2])    {      /* If the two objects share the same b_buffer, don't free it. */      if (buffers[fd1] && buffers[fd1]->b_buffer && buffers[fd1]->b_buffer == buffers[fd2]->b_buffer)	buffers[fd2] = (BUFFERED_STREAM *)NULL;      else	free_buffered_stream (buffers[fd2]);    }  buffers[fd2] = copy_buffered_stream (buffers[fd1]);  if (buffers[fd2])    buffers[fd2]->b_fd = fd2;  if (is_bash_input)    {      if (!buffers[fd2])	fd_to_buffered_stream (fd2);      buffers[fd2]->b_flag |= B_WASBASHINPUT;    }  return (fd2);}/* Return 1 if a seek on FD will succeed. */#define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)/* Take FD, a file descriptor, and create and return a buffered stream   corresponding to it.  If something is wrong and the file descriptor   is invalid, return a NULL stream. */BUFFERED_STREAM *fd_to_buffered_stream (fd)     int fd;{  char *buffer;  size_t size;  struct stat sb;  if (fstat (fd, &sb) < 0)    {      close (fd);      return ((BUFFERED_STREAM *)NULL);    }  size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;  if (size == 0)    size = 1;  buffer = (char *)xmalloc (size);  return (make_buffered_stream (fd, buffer, size));}/* Return a buffered stream corresponding to FILE, a file name. */BUFFERED_STREAM *open_buffered_stream (file)     char *file;{  int fd;  fd = open (file, O_RDONLY);  return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);}/* Deallocate a buffered stream and free up its resources.  Make sure we   zero out the slot in BUFFERS that points to BP. */voidfree_buffered_stream (bp)     BUFFERED_STREAM *bp;{  int n;  if (!bp)    return;  n = bp->b_fd;  if (bp->b_buffer)    free (bp->b_buffer);  free (bp);  buffers[n] = (BUFFERED_STREAM *)NULL;}/* Close the file descriptor associated with BP, a buffered stream, and free   up the stream.  Return the status of closing BP's file descriptor. */intclose_buffered_stream (bp)     BUFFERED_STREAM *bp;{  int fd;  if (!bp)    return (0);  fd = bp->b_fd;  free_buffered_stream (bp);  return (close (fd));}/* Deallocate the buffered stream associated with file descriptor FD, and   close FD.  Return the status of the close on FD. */intclose_buffered_fd (fd)     int fd;{  if (fd < 0)    {      errno = EBADF;      return -1;    }  if (fd >= nbuffers || !buffers || !buffers[fd])    return (close (fd));  return (close_buffered_stream (buffers[fd]));}/* Make the BUFFERED_STREAM associcated with buffers[FD] be BP, and return   the old BUFFERED_STREAM. */BUFFERED_STREAM *set_buffered_stream (fd, bp)     int fd;     BUFFERED_STREAM *bp;{  BUFFERED_STREAM *ret;  ret = buffers[fd];  buffers[fd] = bp;  return ret;}/* Read a buffer full of characters from BP, a buffered stream. */static intb_fill_buffer (bp)     BUFFERED_STREAM *bp;{  ssize_t nr;  off_t o;  CHECK_TERMSIG;  /* In an environment where text and binary files are treated differently,     compensate for lseek() on text files returning an offset different from     the count of characters read() returns.  Text-mode streams have to be     treated as unbuffered. */  if ((bp->b_flag & (B_TEXT | B_UNBUFF)) == B_TEXT)    {      o = lseek (bp->b_fd, 0, SEEK_CUR);      nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);      if (nr > 0 && nr < lseek (bp->b_fd, 0, SEEK_CUR) - o)	{	  lseek (bp->b_fd, o, SEEK_SET);	  bp->b_flag |= B_UNBUFF;	  bp->b_size = 1;	  nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);	}    }  else    nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);  if (nr <= 0)    {      bp->b_used = 0;      bp->b_buffer[0] = 0;      if (nr == 0)	bp->b_flag |= B_EOF;      else	bp->b_flag |= B_ERROR;      return (EOF);    }  bp->b_used = nr;  bp->b_inputp = 0;  return (bp->b_buffer[bp->b_inputp++] & 0xFF);}/* Get a character from buffered stream BP. */#define bufstream_getc(bp) \  (bp->b_inputp == bp->b_used || !bp->b_used) \  		? b_fill_buffer (bp) \		: bp->b_buffer[bp->b_inputp++] & 0xFF/* Push C back onto buffered stream BP. */static intbufstream_ungetc(c, bp)     int c;     BUFFERED_STREAM *bp;{  if (c == EOF || bp->b_inputp == 0)    return (EOF);  bp->b_buffer[--bp->b_inputp] = c;  return (c);}/* Seek backwards on file BFD to synchronize what we've read so far   with the underlying file pointer. */intsync_buffered_stream (bfd)     int bfd;{  BUFFERED_STREAM *bp;  off_t chars_left;  if (buffers == 0 || (bp = buffers[bfd]) == 0)    return (-1);  chars_left = bp->b_used - bp->b_inputp;  if (chars_left)    lseek (bp->b_fd, -chars_left, SEEK_CUR);  bp->b_used = bp->b_inputp = 0;  return (0);}intbuffered_getchar (){  CHECK_TERMSIG;#if !defined (DJGPP)  return (bufstream_getc (buffers[bash_input.location.buffered_fd]));#else  /* On DJGPP, ignore \r. */  int ch;  while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')    ;  return ch;#endif}intbuffered_ungetchar (c)     int c;{  return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));}/* Make input come from file descriptor BFD through a buffered stream. */voidwith_input_from_buffered_stream (bfd, name)     int bfd;     char *name;{  INPUT_STREAM location;  BUFFERED_STREAM *bp;  location.buffered_fd = bfd;  /* Make sure the buffered stream exists. */  bp = fd_to_buffered_stream (bfd);  init_yy_io (bp == 0 ? return_EOF : buffered_getchar,	      buffered_ungetchar, st_bstream, name, location);}#if defined (TEST)void *xmalloc(s)int s;{	return (malloc (s));}void *xrealloc(s, size)char	*s;int	size;{	if (!s)		return(malloc (size));	else		return(realloc (s, size));}voidinit_yy_io (){}process(bp)BUFFERED_STREAM *bp;{	int c;	while ((c = bufstream_getc(bp)) != EOF)		putchar(c);}BASH_INPUT bash_input;struct stat dsb;		/* can be used from gdb *//* imitate /bin/cat */main(argc, argv)int	argc;char	**argv;{	register int i;	BUFFERED_STREAM *bp;	if (argc == 1) {		bp = fd_to_buffered_stream (0);		process(bp);		exit(0);	}	for (i = 1; i < argc; i++) {		if (argv[i][0] == '-' && argv[i][1] == '\0') {			bp = fd_to_buffered_stream (0);			if (!bp)				continue;			process(bp);			free_buffered_stream (bp);		} else {			bp = open_buffered_stream (argv[i]);			if (!bp)				continue;			process(bp);			close_buffered_stream (bp);		}	}	exit(0);}#endif /* TEST */#endif /* BUFFERED_INPUT */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -