Function cmd_getc in command.c contains this code (2.7.3):

    retval = select(num_fds, &readfds, NULL, NULL,
                   (quick_timeout ? &value : NULL));

 /* See if we can read from the application */
    if (FD_ISSET(cmd_fd, &readfds)) {
        int  count, n;

        cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
        for (count = BUFSIZ; count; count -= n, cmdbuf_endp += n)
            if ((n = read(cmd_fd, cmdbuf_endp, count)) <= 0)
                break;
        if (count != BUFSIZ)    /* some characters read in */
            return (*cmdbuf_ptr++);
    }
 /* select statement timed out - we're not hard and fast scrolling */
    if (retval == 0) {
        refresh_count = 0;
        refresh_limit = 1;
    }

I'm definitely NO expert on the select function, but going by
'info libc 'Waiting for I/O'' there is a more efficient code.

    if (select(num_fds, &readfds, NULL, NULL,
              (quick_timeout ? &value : NULL)) == 0) {
 /* select statement timed out - we're not hard and fast scrolling */
        refresh_count = 0;
        refresh_limit = 1;
    }
 /* See if we can read from the application */
    else if (FD_ISSET(cmd_fd, &readfds)) {
        int  count, n;

        cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
        for (count = BUFSIZ; count; count -= n, cmdbuf_endp += n)
            if ((n = read(cmd_fd, cmdbuf_endp, count)) <= 0)
                break;
        if (count != BUFSIZ)    /* some characters read in */
            return (*cmdbuf_ptr++);
    }

libc.info states:
    "If select returns because the timeout period expires,
     it returns a value of zero."
So if select returns zero, there is no point in testing
FD_ISSET(cmd_fd, &readfds).
Since select will return zero most of the time (it only
returns non-zero in case of "input"), skipping the
    if FD_ISSET(cmd_fd, &readfds)
part in case select returns zero makes sense.

The code proposed also does away with the 'retval' variable.

Now I probably miss out on some tricky detail, in which case
I would be happy to hear (perhaps from the person who coded
the cmd_getc function) why the retval value is not used
straightaway.

I have another question pertaining to cmd_getc, which I
will post in a separate message shortly.

Cheers,

Tijs

Reply via email to