Bug List: (This bug is not in your last search results)   Show last search results      Search page      Enter new bug
Bug#: 12722
Product:  
Component:  
Status: RESOLVED
Resolution: INVALID
Assigned To: Not yet assigned to anyone <unassigned@gcc.gnu.org>
Host:
Reported against  
Priority:  
Severity:  
Target Milestone:  
 
 
Target:
Reporter: Scott Weathers <sweathers@toptech.com>
Add CC:
CC:
Remove selected CCs
Build:
URL:
Summary:
Keywords:
Known to work:
Known to fail:

Attachment Description Type Created Size Actions
test-ioctl.c Example of ioctl( fd, FIONREAD, &nchar ) call text/plain 2003-10-22 12:30 1.05 KB Edit
test-ioctl.c Example of ioctl( fd, FIONREAD, &nchar ) call text/plain 2003-10-22 12:31 1.05 KB Edit
Create a New Attachment (proposed patch, testcase, etc.) View All

Bug 12722 depends on: Show dependency tree
Show dependency graph
Bug 12722 blocks:

Additional Comments:






View Bug Activity   |   Format For Printing   |   Clone This Bug


Description:   Last confirmed: Opened: 2003-10-22 12:27
Description of problem:
It is my understanding that the FIONREAD should return the number of bytes 
in /dev/ttyS0.  I am try to do an intelligent read for the number of bytes in 
serial port buffer so I don't have to wait for a time out to occur.  When I 
use ioctl( comm_fd, FIONREAD, &nchar ) it does not return me the number of 
bytes in the serial port buffer.

Any in site would be of great help.  If this is the wrong place to ask this 
question please direction to the correct place.


Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/ioctl.h>


char    	comm_log_buf[5000];
int 		wcomm_fd,
			comm_fd;

extern unsigned old_mode;

int	caught_alrm = 0;

//-----------------------------------------------------------------------------
int tty_open (char *line, int flg)
{
	int fd;
	char devname[100];

	strcpy(devname, "/dev/");
	strcat(devname, line);

    fd = open (devname, flg);
    if (fd == -1)
    {
		printf("error (%d) opening %s - [%s]\n", errno, devname, 
strerror(errno));
		return(errno);
	}

	if(isatty(fd) == 0)
	{
		fprintf(stderr, "%s is not a tty\n", devname);
		return(-1);
	}
	
	return(fd);
}
//-----------------------------------------------------------------------------
static void sig_alrm(int signo)
{
	caught_alrm = 1;
	return;
}
//-----------------------------------------------------------------------------
size_t my_get_com_byte(int fd, char *buf)
{
	for ( ; ; )
	{
		if(caught_alrm)
		{
			fprintf(stderr, "\nAlarm Read Timeout...\n");
			return(-1);
		}
		if(read(fd, buf, 1) == 1)
		{
			//fprintf(stderr, "Read more than one byte...\n");
			return (1);
		}
		if(errno == EINTR && caught_alrm)
		{
			fprintf(stderr, "\nRead Timeout after read...\n");
			return(-1);
		}
	}
}
//-----------------------------------------------------------------------------
int dev_str(int fd, char *ptr)
{
	int nchar,
		i;
	

	if(signal(SIGALRM, sig_alrm) == SIG_ERR)
	{
		fprintf(stderr, "signal SIGLARM error in dev_str");
		return(-1);
	}
	caught_alrm = 0;
	alarm(1);

	if(	(i = ioctl( fd, FIONREAD, &nchar )) == -1)
		return(-1);
	fprintf(stdout, "Found %d %d bytes in port buffer\n",i, nchar);
	for(i = 1; i <= 80; i++)
	{
		if (my_get_com_byte(fd, &ptr[i]) == -1)
			break;
    	if(ptr[i]<32 || ptr[i]>126)
       		sprintf(comm_log_buf,"[%x]",ptr[i]);
    	else
      		sprintf(comm_log_buf,"%c",ptr[i]);
      	fprintf(stdout,"%s ",comm_log_buf);
	}	

	return(i);
}
//-----------------------------------------------------------------------------
int main (int argc, char **argv)
{
    long    n,
            c,
            len,
            done,
            bad,
            mismatch,
			cnt = 0,
            spurious;
    char    buf [500],
			test_pattern[256],
            line [500];
	int		err,
			i,
			nchar;

    strcpy (test_pattern, "0123456789");


    wcomm_fd = comm_fd = open ("/dev/ttyS0", O_RDWR | O_NONBLOCK);
    if (comm_fd == -1)
    {
		printf("error (%d) opening  [%s]\n", errno, strerror(errno));
		return(errno);
	}

   	/* send the data */
   	err = write(wcomm_fd,test_pattern,strlen(test_pattern));
	fprintf(stdout, "\nWrite %d bytes \n", err);
	
	if(	(err = ioctl( comm_fd, FIONREAD, &nchar )) == -1)
	{
		fprintf(stderr, "ioctl - error (%d) [%s]\n", errno, strerror
(errno));
		return(-1);
	}
	fprintf(stdout, "Found %d %d bytes in port buffer\n",err, nchar);
	dev_str(comm_fd,buf);


}


Version-Release number of selected component (if applicable):


How reproducible:


Steps to Reproduce:
1.compile
2.execute test code
3.
    
Actual results:
Write 10 bytes
Found 0 0 bytes in port buffer
Found 0 0 bytes in port buffer

Alarm Read Timeout...
0 1 2 3 4 5 6 7 8 9 

Expected results:
Write 10 bytes
Found 0 10 bytes in port buffer
Found 0 10 bytes in port buffer

Alarm Read Timeout...
0 1 2 3 4 5 6 7 8 9 

Additional info:

GCC version:
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-
checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)

------- Comment #1 From Scott Weathers 2003-10-22 12:30 -------
Created an attachment (id=4979) [edit]
Example of  ioctl( fd, FIONREAD, &nchar ) call

------- Comment #2 From Scott Weathers 2003-10-22 12:31 -------
Created an attachment (id=4980) [edit]
Example of  ioctl( fd, FIONREAD, &nchar ) call

------- Comment #3 From Andrew Pinski 2003-10-22 21:09 -------
Not a bug in GCC but most likely a bug in glibc, report it to them.

Bug List: (This bug is not in your last search results)   Show last search results      Search page      Enter new bug