home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / wizards / 4583 < prev    next >
Encoding:
Text File  |  1992-11-09  |  4.0 KB  |  92 lines

  1. Newsgroups: comp.unix.wizards
  2. Path: sparky!uunet!gumby!destroyer!mudos!mju
  3. From: mju@mudos.ann-arbor.mi.us (Marc Unangst)
  4. Subject: Re: Keyboard hit in C programs
  5. Message-ID: <BxHq3n.2G8@mudos.ann-arbor.mi.us>
  6. Date: Tue, 10 Nov 1992 07:57:22 GMT
  7. References: <1dms9iINNa6m@agate.berkeley.edu>
  8. Organization: The Programmer's Pit Stop, Ann Arbor MI
  9. Keywords: Keyboard hit, C programs, UNIX
  10. Lines: 80
  11.  
  12. In article <1dms9iINNa6m@agate.berkeley.edu> bernt@valois (Bernt Skottun) writes:
  13. >intervals will not do). In the DOS environment
  14. >I have been able to do this with the "kbhit()" function.
  15. >My question is: what is the equivalent function in the UNIX
  16. >environment? or how does one go about creating such a function?
  17.  
  18. Your question is answered in Part 4 of the comp.unix.questions FAQ
  19. list.  In the future, please check to see if your question is answered
  20. in one of the FAQ postings before posting it to the net.
  21.  
  22. 4.1)  How do I read characters from a terminal without requiring the user
  23.       to hit RETURN?
  24.  
  25.       Check out cbreak mode in BSD, ~ICANON mode in SysV.
  26.  
  27.       If you don't want to tackle setting the terminal parameters
  28.       yourself (using the "ioctl(2)" system call) you can let the stty
  29.       program do the work - but this is slow and inefficient, and you
  30.       should change the code to do it right some time:
  31.  
  32.       #include <stdio.h>
  33.       main()
  34.       {
  35.         int c;
  36.  
  37.         printf("Hit any character to continue\n");
  38.         /*
  39.          * ioctl() would be better here; only lazy
  40.          * programmers do it this way:
  41.          */
  42.         system("/bin/stty cbreak");        /* or "stty raw" */
  43.         c = getchar();
  44.         system("/bin/stty -cbreak");
  45.         printf("Thank you for typing %c.\n", c);
  46.  
  47.         exit(0);
  48.       }
  49.  
  50.       You might like to check out the documentation for the "curses"
  51.       library of portable screen functions.  Often if you're interested
  52.       in single-character I/O like this, you're also interested in
  53.       doing some sort of screen display control, and the curses library
  54.       provides various portable routines for both functions.
  55.  
  56. 4.2)  How do I check to see if there are characters to be read without
  57.       actually reading?
  58.  
  59.       Certain versions of UNIX provide ways to check whether characters
  60.       are currently available to be read from a file descriptor.  In
  61.       BSD, you can use select(2).  You can also use the FIONREAD ioctl
  62.       (see tty(4)), which returns the number of characters waiting to
  63.       be read, but only works on terminals, pipes and sockets.  In
  64.       System V Release 3, you can use poll(2), but that only works on
  65.       streams.  In Xenix - and therefore Unix SysV r3.2 and later - the
  66.       rdchk() system call reports whether a read() call on a given file
  67.       descriptor will block.
  68.  
  69.       There is no way to check whether characters are available to be
  70.       read from a FILE pointer.  (You could poke around inside stdio
  71.       data structures to see if the input buffer is nonempty, but that
  72.       wouldn't work since you'd have no way of knowing what will happen
  73.       the next time you try to fill the buffer.)
  74.  
  75.       Sometimes people ask this question with the intention of writing
  76.         if (characters available from fd)
  77.             read(fd, buf, sizeof buf);
  78.       in order to get the effect of a nonblocking read.  This is not
  79.       the best way to do this, because it is possible that characters
  80.       will be available when you test for availability, but will no
  81.       longer be available when you call read.  Instead, set the
  82.       O_NDELAY flag (which is also called FNDELAY under BSD) using the
  83.       F_SETFL option of fcntl(2).  Older systems (Version 7, 4.1 BSD)
  84.       don't have O_NDELAY; on these systems the closest you can get to
  85.       a nonblocking read is to use alarm(2) to time out the read.
  86.  
  87. -- 
  88. Marc Unangst, N8VRH         | "There are two ways to solve this problem:
  89. mju@mudos.ann-arbor.mi.us   | the hard way, and the easy way.  Let's start
  90.                             | with the hard way."
  91.                             |   - W. Scheider, from a Physics lecture
  92.