Blame gl/tests/test-select-stdin.c

Packit aea12f
/* Test of select() substitute, reading from stdin.
Packit Service 991b93
   Copyright (C) 2008-2020 Free Software Foundation, Inc.
Packit aea12f
Packit aea12f
   This program is free software: you can redistribute it and/or modify
Packit aea12f
   it under the terms of the GNU General Public License as published by
Packit aea12f
   the Free Software Foundation; either version 3 of the License, or
Packit aea12f
   (at your option) any later version.
Packit aea12f
Packit aea12f
   This program is distributed in the hope that it will be useful,
Packit aea12f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit aea12f
   GNU General Public License for more details.
Packit aea12f
Packit aea12f
   You should have received a copy of the GNU General Public License
Packit aea12f
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit aea12f
Packit aea12f
/* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
Packit aea12f
#include <stdio.h>
Packit aea12f
#include <stdlib.h>
Packit aea12f
#include <sys/select.h>
Packit aea12f
#include <sys/time.h>
Packit aea12f
#include <unistd.h>
Packit aea12f
Packit aea12f
#include "macros.h"
Packit aea12f
Packit aea12f
int
Packit aea12f
main (void)
Packit aea12f
{
Packit aea12f
  printf ("Applying select() from standard input. Press Ctrl-C to abort.\n");
Packit aea12f
  for (;;)
Packit aea12f
    {
Packit aea12f
      struct timeval before;
Packit aea12f
      struct timeval after;
Packit aea12f
      unsigned long spent_usec;
Packit aea12f
      fd_set readfds;
Packit aea12f
      struct timeval timeout;
Packit aea12f
      int ret;
Packit aea12f
Packit aea12f
      gettimeofday (&before, NULL);
Packit aea12f
Packit aea12f
      FD_ZERO (&readfds);
Packit aea12f
      FD_SET (0, &readfds);
Packit aea12f
      timeout.tv_sec = 0;
Packit aea12f
      timeout.tv_usec = 500000;
Packit aea12f
      ret = select (1, &readfds, NULL, NULL, &timeout);
Packit aea12f
Packit aea12f
      gettimeofday (&after, NULL);
Packit aea12f
      spent_usec = (after.tv_sec - before.tv_sec) * 1000000
Packit aea12f
                   + after.tv_usec - before.tv_usec;
Packit aea12f
Packit aea12f
      if (ret < 0)
Packit aea12f
        {
Packit aea12f
          perror ("select failed");
Packit aea12f
          exit (1);
Packit aea12f
        }
Packit aea12f
      if ((ret == 0) != ! FD_ISSET (0, &readfds))
Packit aea12f
        {
Packit aea12f
          fprintf (stderr, "incorrect return value\n");
Packit aea12f
          exit (1);
Packit aea12f
        }
Packit aea12f
      if (ret == 0)
Packit aea12f
        {
Packit aea12f
          if (spent_usec < 250000)
Packit aea12f
            {
Packit aea12f
              fprintf (stderr, "returned too early\n");
Packit aea12f
              exit (1);
Packit aea12f
            }
Packit aea12f
          /* Timeout */
Packit aea12f
          printf (".");
Packit aea12f
          ASSERT (fflush (stdout) == 0);
Packit aea12f
        }
Packit aea12f
      else
Packit aea12f
        {
Packit aea12f
          char c;
Packit aea12f
Packit aea12f
          printf ("Input available! Trying to read 1 byte...\n");
Packit aea12f
          ASSERT (read (0, &c, 1) == 1);
Packit aea12f
        }
Packit aea12f
    }
Packit aea12f
}