ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/06/29/12:58:37

Date: Thu, 29 Jun 1995 18:58:34 +0300
From: eliz AT is DOT elta DOT co DOT il (Eli Zaretskii)
To: kenny AT Freak DOT ORG
Subject: Re: read()?
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

> > >     while ((count = read(0, buffer, sizeof(buffer))) > 0) {
> >  
> > You are reading from handle 0, which is standard input, probably
> > the keyboard.  If you don't type anything, this indeed *should*
> > hang until such time as you type sizeof(buffer) characters.  Could
> > this be the problem?
> > 
>
> I type things and it doesn't echo anything and eventually beeps at me 
> like I overflowed the keyboard buffer.  It's very frustrating.

In the original message you've said that you ``use DJGPP with maint5''.
I wrote a simplified version of your program (below) and tested it under
1.12maint4 and beta release of DJGPP v2.0 (I don't know what is maint5).
Here is what I found:

	1) Under 1.12m4, you must close() handle 0 and open the console again
in O_BINARY mode.  Then your code will work as you'd expect.  See the example
code below for details.

	2) Under v2.0beta1, you should call setmode(0, O_BINARY) before entering
the loop.  This forces the reads from standard input to work in raw mode, without
which the read() call waits for a CR before it returns (that's a DOS (mis)feature).
Currently, v2.0 library has a bug (IMHO) whereas open("con", O_BINARY) doesn't set
the handle to raw mode, so the solution in 1) above won't work unless you also
explicitly call setmode() after open().  I reported this to v2.0 bug-tracking
system, so it will most probably be fixed in the final v2.0 release.

------------------ Cut here --------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <conio.h>
#include <io.h>
 
int
main(void)
{
  int count;
  char buf[10];
#if USE_DJGPP_V_1
  int ttyin;
  close(0);
  ttyin = open("con", O_RDONLY | O_BINARY);
 
  if (ttyin < 0)
    {
      perror("/dev/con");
      exit(1);
    }
  setmode(ttyin, O_BINARY);
  while ((count = read(ttyin, buf, 1)) > 0 && buf[0] != '0')
#else  /* using DJGPP V2.0 beta */
  setmode(0, O_BINARY);
  while ((count = read(0, buf, 1)) > 0 && buf[0] != '0')
#endif
 
    {
      buf[1] = count + '0';
      buf[2] = '\0';
      gotoxy(40, 10);
      cputs(buf);
      gotoxy(1, 10);
    }
 
  if (count == -1)
    perror("/dev/con");
 
  return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019