* ser-unix.c (wait_for): Initialize the FD_SET before every select
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
index 919c9fd90d9a6768032f59c6e5cb3ac3ff120b59..b75e3387e40e44b7c48b19851b08980fbf1a72ef 100644 (file)
@@ -435,37 +435,37 @@ static int
 wait_for (serial_t scb, int timeout)
 {
 #ifdef HAVE_SGTTY
-  {
-    struct timeval tv;
-    fd_set readfds;
+  while (1)
+    {
+      struct timeval tv;
+      fd_set readfds;
+      int numfds;
 
-    FD_ZERO (&readfds);
+      /* NOTE: Some OS's can scramble the READFDS when the select()
+         call fails (ex the kernel with Red Hat 5.2).  Initialize all
+         arguments before each call. */
 
-    tv.tv_sec = timeout;
-    tv.tv_usec = 0;
+      tv.tv_sec = timeout;
+      tv.tv_usec = 0;
 
-    FD_SET (scb->fd, &readfds);
+      FD_ZERO (&readfds);
+      FD_SET (scb->fd, &readfds);
 
-    while (1)
-      {
-       int numfds;
+      if (timeout >= 0)
+       numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
+      else
+       numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
 
-       if (timeout >= 0)
-         numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
+      if (numfds <= 0)
+       if (numfds == 0)
+         return SERIAL_TIMEOUT;
+       else if (errno == EINTR)
+         continue;
        else
-         numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
-
-       if (numfds <= 0)
-         if (numfds == 0)
-           return SERIAL_TIMEOUT;
-         else if (errno == EINTR)
-           continue;
-         else
-           return SERIAL_ERROR;        /* Got an error from select or poll */
+         return SERIAL_ERROR;  /* Got an error from select or poll */
 
-       return 0;
-      }
-  }
+      return 0;
+    }
 #endif /* HAVE_SGTTY */
 
 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
@@ -778,9 +778,7 @@ hardwire_setbaudrate (serial_t scb, int rate)
 }
 
 static int
-hardwire_setstopbits (scb, num)
-     serial_t scb;
-     int num;
+hardwire_setstopbits (serial_t scb, int num)
 {
   struct hardwire_ttystate state;
   int newbit;
@@ -860,21 +858,24 @@ ser_unix_nop_raw (serial_t scb)
 int
 ser_unix_wait_for (serial_t scb, int timeout)
 {
-  int numfds;
-  struct timeval tv;
-  fd_set readfds, exceptfds;
+  while (1)
+    {
+      int numfds;
+      struct timeval tv;
+      fd_set readfds, exceptfds;
 
-  FD_ZERO (&readfds);
-  FD_ZERO (&exceptfds);
+      /* NOTE: Some OS's can scramble the READFDS when the select()
+         call fails (ex the kernel with Red Hat 5.2).  Initialize all
+         arguments before each call. */
 
-  tv.tv_sec = timeout;
-  tv.tv_usec = 0;
+      tv.tv_sec = timeout;
+      tv.tv_usec = 0;
 
-  FD_SET (scb->fd, &readfds);
-  FD_SET (scb->fd, &exceptfds);
+      FD_ZERO (&readfds);
+      FD_ZERO (&exceptfds);
+      FD_SET (scb->fd, &readfds);
+      FD_SET (scb->fd, &exceptfds);
 
-  while (1)
-    {
       if (timeout >= 0)
        numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
       else
@@ -929,7 +930,8 @@ do_unix_readchar (serial_t scb, int timeout)
        }
 
       status = ser_unix_wait_for (scb, delta);
-      timeout -= delta;
+      if (timeout > 0)
+        timeout -= delta;
 
       /* If we got a character or an error back from wait_for, then we can 
          break from the loop before the timeout is completed. */
@@ -1278,7 +1280,7 @@ ser_unix_async (serial_t scb,
       if (SERIAL_DEBUG_P (scb))
        fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
                            scb->fd);
-      /* De-schedule what ever tasks are currently scheduled. */
+      /* De-schedule whatever tasks are currently scheduled. */
       switch (scb->async_state)
        {
        case FD_SCHEDULED:
This page took 0.024817 seconds and 4 git commands to generate.