aboutsummaryrefslogtreecommitdiff
path: root/sample.c
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2005-10-25 23:02:25 +0000
committerAris Adamantiadis <aris@0xbadc0de.be>2005-10-25 23:02:25 +0000
commitd86f0017545f0076a1136b5c6f8b0a7a58366342 (patch)
tree6b28fc5766ad23b3c8c9dbb1ffa2bbfae4cbc808 /sample.c
parent5f7c84f900b81e3bbff55378f8170ddf150daf9c (diff)
downloadlibssh-d86f0017545f0076a1136b5c6f8b0a7a58366342.tar.gz
libssh-d86f0017545f0076a1136b5c6f8b0a7a58366342.tar.xz
libssh-d86f0017545f0076a1136b5c6f8b0a7a58366342.zip
channel_select(). this function rocks !
I adapted the sample.c file. the select_loop function is bloated and fails to demonstrate how libssh is simple to handle... it looks to run at first try. git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@39 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'sample.c')
-rw-r--r--sample.c60
1 files changed, 39 insertions, 21 deletions
diff --git a/sample.c b/sample.c
index d25ecf58..bf77f8d2 100644
--- a/sample.c
+++ b/sample.c
@@ -122,10 +122,10 @@ void select_loop(SSH_SESSION *session,CHANNEL *channel){
struct timeval timeout;
char buffer[10];
BUFFER *readbuf=buffer_new();
- CHANNEL *channels[]={channel,NULL};
- CHANNEL *outchannel[2];
+ CHANNEL *channels[2];
int lus;
int eof=0;
+ int maxfd;
int ret;
while(channel){
/* when a signal is caught, ssh_select will return
@@ -141,23 +141,40 @@ void select_loop(SSH_SESSION *session,CHANNEL *channel){
FD_SET(0,&fds);
timeout.tv_sec=30;
timeout.tv_usec=0;
- ret=ssh_select(channels,outchannel,0+1,&fds,&timeout);
- if(signal_delayed)
- sizechanged();
- } while (ret==SSH_EINTR);
- if(FD_ISSET(0,&fds)){
- lus=read(0,buffer,10);
- if(lus){
- channel_write(channel,buffer,lus);
+ FD_SET(ssh_get_fd(session),&fds);
+ maxfd=ssh_get_fd(session)+1;
+ ret=select(maxfd,&fds,NULL,NULL,&timeout);
+ if(ret==EINTR)
+ continue;
+ if(FD_ISSET(0,&fds)){
+ lus=read(0,buffer,10);
+ if(lus)
+ channel_write(channel,buffer,lus);
+ else {
+ eof=1;
+ channel_send_eof(channel);
+ }
}
- else{
- eof=1;
- channel_send_eof(channel);
+ if(FD_ISSET(ssh_get_fd(session),&fds)){
+ ssh_set_fd_toread(session);
}
+ channels[0]=channel; // set the first channel we want to read from
+ channels[1]=NULL;
+ ret=channel_select(channels,NULL,NULL,NULL); // no specific timeout - just poll
+ if(signal_delayed)
+ sizechanged();
+ } while (ret==EINTR || ret==SSH_EINTR);
+
+ // we already looked for input from stdin. Now, we are looking for input from the channel
+
+ if(channel && channel_is_closed(channel)){
+ channel_free(channel);
+ channel=NULL;
+ channels[0]=NULL;
}
- if(outchannel[0]){
- while(channel && channel_poll(outchannel[0],0)){
- lus=channel_read(outchannel[0],readbuf,0,0);
+ if(channels[0]){
+ while(channel && channel_is_open(channel) && channel_poll(channel,0)){
+ lus=channel_read(channel,readbuf,0,0);
if(lus==-1){
ssh_say(0,"error reading channel : %s\n",ssh_get_error(session));
return;
@@ -165,12 +182,12 @@ void select_loop(SSH_SESSION *session,CHANNEL *channel){
if(lus==0){
ssh_say(1,"EOF received\n");
channel_free(channel);
- channel=NULL;
+ channel=channels[0]=NULL;
} else
write(1,buffer_get(readbuf),lus);
}
- while(channel && channel_poll(outchannel[0],1)){ /* stderr */
- lus=channel_read(outchannel[0],readbuf,0,1);
+ while(channel && channel_is_open(channel) && channel_poll(channel,1)){ /* stderr */
+ lus=channel_read(channel,readbuf,0,1);
if(lus==-1){
ssh_say(0,"error reading channel : %s\n",ssh_get_error(session));
return;
@@ -178,12 +195,12 @@ void select_loop(SSH_SESSION *session,CHANNEL *channel){
if(lus==0){
ssh_say(1,"EOF received\n");
channel_free(channel);
- channel=NULL;
+ channel=channels[0]=NULL;
} else
write(2,buffer_get(readbuf),lus);
}
}
- if(channel && !channel_is_open(channel)){
+ if(channel && channel_is_closed(channel)){
channel_free(channel);
channel=NULL;
}
@@ -191,6 +208,7 @@ void select_loop(SSH_SESSION *session,CHANNEL *channel){
buffer_free(readbuf);
}
+
void shell(SSH_SESSION *session){
CHANNEL *channel;
struct termios terminal_local;