Hi
I am trying to read from different pts's by select. The pts's are owned by me (that's i use them). But the program below only reads from the current pts it is running!
What is wrong with the code!?
Code:
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/select.h>
int main()
{
int fd;
int fdmax;
int i,j;
size_t bytes_read;
DIR *dp;
struct dirent *dir_ent;
fd_set master_set;
fd_set myset;
char tmp[16];
char buffer[256];
// open directory
if ((dp=opendir("/dev/pts/"))==NULL)
{
perror("opendir");
exit(1);
}
// clearing the file descriptor sets
FD_ZERO(&master_set);
FD_ZERO(&myset);
// getting the open 'pts's and adding them to fd_set
while ((dir_ent=readdir(dp)))
{
if (strlen(dir_ent->d_name)>6)
{
printf("Our array cannot hold the name of path!\n");
printf("Avoiding a buffer overflow. Exiting...\n");
exit(0);
}
// eleminating "." and ".."
if (strcmp(dir_ent->d_name,".") && strcmp(dir_ent->d_name,".."))
{
sprintf(tmp,"/dev/pts/%s",dir_ent->d_name);
// opening the device
if ((fd=open(tmp,O_RDONLY))==-1)
{
perror("open");
exit(1);
}
// setting the device option to nonblocking
if (fcntl(fd,F_SETFL,O_NONBLOCK)==-1)
{
perror("fcntl");
exit(1);
}
// Adding the file desciptor to master set
FD_SET(fd,&master_set);
printf("%s is added to fd_set.\n",tmp);
}
} // while
fdmax=fd;
for (;;)
{
myset=master_set;
if (select(fdmax+1,&myset,NULL,NULL,NULL)==-1)
{
perror("select");
exit(1);
}
// Tracing the possible fd's
for (i=0;i<=fdmax;i++)
{
if (FD_ISSET(i,&myset))
{
if ((bytes_read=read(i,buffer,sizeof(buffer)))==-1)
{
perror("read");
exit(1);
}
if (bytes_read>0)
{
printf("\n");
for (j=0;j<bytes_read;j++)
{
printf("%c",buffer[j]);
} // for
printf("\n");
}
} // if
}
} // for
close(fd);
closedir(dp);
return 0;
}