Impatiently wait for input

bash, 38 bytes

read -t10||a=no;echo $a input received

This uses the -t (timeout) option to bash's read, which causes it to fail and return a nonzero exit code if no input is given in the specified number of seconds.


Haskell, 97 89 bytes

import System.Timeout
timeout(10^7)getChar>>=putStr.(++"input received").maybe"no "mempty

If timeout times out it returns Nothing and Just Char (Char, because we're using getChar) otherwise. This return value is converted to "no " or "" by function maybe "no " mempty. Append "input received" and print.

Edit: @BMO suggested maybe and saved some bytes.


POSIX C99, 71 63 bytes

main(){puts("no input received"+3*poll((int[]){0,1},1,10000));}

Ungolfed:

#include <unistd.h>
#include <poll.h>
#include <stdio.h>
int main()
{
  struct pollfd pfd; 
  pfd.fd = STDIN_FILENO; 
  pfd.events = POLLIN;  
  puts("no input received"+3*poll(&pfd,1,10000));
}

Since poll will return 1 in case of success, we multiply the result by 3 and shift the string accordingly. Then, we use the fact that struct pollfd has the following layout:

     struct pollfd {
     int    fd;       /* file descriptor */
     short  events;   /* events to look for */
     short  revents;  /* events returned */
 };

and that STDIN_FILENO is 0, POLLIN is 1 to replace pfd with int pfd[] = {0,1}, which we finally make a compound litteral (as allowed by C99).

Tags:

Code Golf