1
1

libssh2_poll() no longer relies on C99 features but instead uses alloca()

on systems that have it - and uses a fixed-size array on those that don't.
session.c was also cleaned from a large amount of trailing whitespace.
Этот коммит содержится в:
Daniel Stenberg 2007-07-23 21:18:43 +00:00
родитель 2b7856ad32
Коммит 209d06d6c9
2 изменённых файлов: 113 добавлений и 94 удалений

Просмотреть файл

@ -248,6 +248,8 @@ AC_CHECK_HEADERS([sys/select.h sys/socket.h sys/ioctl.h sys/time.h])
AC_CHECK_HEADERS([arpa/inet.h netinet/in.h]) AC_CHECK_HEADERS([arpa/inet.h netinet/in.h])
AC_CHECK_FUNCS(poll gettimeofday select) AC_CHECK_FUNCS(poll gettimeofday select)
AC_FUNC_ALLOCA
# Checks for typedefs, structures, and compiler characteristics. # Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST AC_C_CONST
AC_C_INLINE AC_C_INLINE

Просмотреть файл

@ -46,6 +46,9 @@
#ifdef HAVE_GETTIMEOFDAY #ifdef HAVE_GETTIMEOFDAY
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
/* {{{ libssh2_default_alloc /* {{{ libssh2_default_alloc
*/ */
@ -432,8 +435,12 @@ LIBSSH2_API LIBSSH2_SESSION *libssh2_session_init_ex(
/* {{{ libssh2_session_callback_set /* {{{ libssh2_session_callback_set
* Set (or reset) a callback function * Set (or reset) a callback function
* Returns the prior address * Returns the prior address
*
* FIXME: this function relies on that we can typecast function pointers
* to void pointers, which isn't allowed in ISO C!
*/ */
LIBSSH2_API void* libssh2_session_callback_set(LIBSSH2_SESSION *session, int cbtype, void *callback) LIBSSH2_API void* libssh2_session_callback_set(LIBSSH2_SESSION *session,
int cbtype, void *callback)
{ {
void *oldcb; void *oldcb;
@ -1142,12 +1149,16 @@ LIBSSH2_API int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeou
unsigned int i, active_fds; unsigned int i, active_fds;
#ifdef HAVE_POLL #ifdef HAVE_POLL
LIBSSH2_SESSION *session = NULL; LIBSSH2_SESSION *session = NULL;
struct pollfd sockets[nfds]; #ifdef HAVE_ALLOCA
/* FIXME: (dast) this is not C89 code! However, the prototype for this struct pollfd *sockets = alloca(sizeof(struct pollfd) * nfds);
function doesn't provide a session struct so we can't easily use #else
the user-provided malloc replacement here... I suggest we modify struct pollfd sockets[256];
the proto to make it possible. */
if(nfds > 256)
/* systems without alloca use a fixed-size array, this can be fixed
if we really want to, at least if the compiler is a C99 capable one */
return -1;
#endif
/* Setup sockets for polling */ /* Setup sockets for polling */
for(i = 0; i < nfds; i++) { for(i = 0; i < nfds; i++) {
fds[i].revents = 0; fds[i].revents = 0;
@ -1192,29 +1203,35 @@ LIBSSH2_API int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeou
case LIBSSH2_POLLFD_SOCKET: case LIBSSH2_POLLFD_SOCKET:
if (fds[i].events & LIBSSH2_POLLFD_POLLIN) { if (fds[i].events & LIBSSH2_POLLFD_POLLIN) {
FD_SET(fds[i].fd.socket, &rfds); FD_SET(fds[i].fd.socket, &rfds);
if (fds[i].fd.socket > maxfd) maxfd = fds[i].fd.socket; if (fds[i].fd.socket > maxfd)
maxfd = fds[i].fd.socket;
} }
if (fds[i].events & LIBSSH2_POLLFD_POLLOUT) { if (fds[i].events & LIBSSH2_POLLFD_POLLOUT) {
FD_SET(fds[i].fd.socket, &wfds); FD_SET(fds[i].fd.socket, &wfds);
if (fds[i].fd.socket > maxfd) maxfd = fds[i].fd.socket; if (fds[i].fd.socket > maxfd)
maxfd = fds[i].fd.socket;
} }
break; break;
case LIBSSH2_POLLFD_CHANNEL: case LIBSSH2_POLLFD_CHANNEL:
FD_SET(fds[i].fd.channel->session->socket_fd, &rfds); FD_SET(fds[i].fd.channel->session->socket_fd, &rfds);
if (fds[i].fd.channel->session->socket_fd > maxfd) maxfd = fds[i].fd.channel->session->socket_fd; if (fds[i].fd.channel->session->socket_fd > maxfd)
maxfd = fds[i].fd.channel->session->socket_fd;
if (!session) session = fds[i].fd.channel->session; if (!session) session = fds[i].fd.channel->session;
break; break;
case LIBSSH2_POLLFD_LISTENER: case LIBSSH2_POLLFD_LISTENER:
FD_SET(fds[i].fd.listener->session->socket_fd, &rfds); FD_SET(fds[i].fd.listener->session->socket_fd, &rfds);
if (fds[i].fd.listener->session->socket_fd > maxfd) maxfd = fds[i].fd.listener->session->socket_fd; if (fds[i].fd.listener->session->socket_fd > maxfd)
maxfd = fds[i].fd.listener->session->socket_fd;
if (!session) session = fds[i].fd.listener->session; if (!session) session = fds[i].fd.listener->session;
break; break;
default: default:
if (session) libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE, if (session)
"Invalid descriptor passed to libssh2_poll()", 0); libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE,
"Invalid descriptor passed to libssh2_poll()",
0);
return -1; return -1;
} }
} }