Blame manual/examples/select.c

Packit 6c4009
/* Waiting for Input or Output
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/time.h>
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
int
Packit 6c4009
input_timeout (int filedes, unsigned int seconds)
Packit 6c4009
{
Packit 6c4009
  fd_set set;
Packit 6c4009
  struct timeval timeout;
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
  /* Initialize the file descriptor set. */
Packit 6c4009
  FD_ZERO (&set);
Packit 6c4009
  FD_SET (filedes, &set);
Packit 6c4009
Packit 6c4009
  /* Initialize the timeout data structure. */
Packit 6c4009
  timeout.tv_sec = seconds;
Packit 6c4009
  timeout.tv_usec = 0;
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
  /* @code{select} returns 0 if timeout, 1 if input available, -1 if error. */
Packit 6c4009
  return TEMP_FAILURE_RETRY (select (FD_SETSIZE,
Packit 6c4009
				     &set, NULL, NULL,
Packit 6c4009
				     &timeout));
Packit 6c4009
}
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  fprintf (stderr, "select returned %d.\n",
Packit 6c4009
	   input_timeout (STDIN_FILENO, 5));
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
/*@end group*/