/* 2001 March 27 ^D flushes the input stream [read() returns]. There is no EOF character. EOF is indicated by a read that returns no characters. */ #include main() { int c; FILE *fp; printf("Program to illustrate ^D and EOF handling by the terminal driver.\n"); printf("There is no EOF character in UNIX.\n"); printf("^D merely flushes the input stream - causes read to return.\n"); printf("An Enter/Carriage-return, appends newline to the current line\n"); printf(" and then causes the read to return.\n"); printf("A read that returns no characters is interpreted as an EOF.\n\n"); printf("It follows that you can read through EOF's and that an\n"); printf(" EOF is obtained by ^D at beginning of line, or ^D^D in midline.\n"); printf(" (the first ^D flushes the line, the next flushes with no characters)\n"); printf("Try it:\n"); if(0 != system("PS1= xterm -C &")) fprintf(stderr,"Warning: cannot open terminal for /dev/console\n"); fp = fopen("/dev/console","w"); if (fp == NULL) { fprintf(stderr,"cannot open /dev/console\n"); exit(1); } for(;;) switch(c = getchar()) { case '\n': fprintf(fp,"Saw: newline (hexdecimal code: %02x)\n",c,c); break; case EOF: fprintf(fp,"Saw EOF: read of zero bytes\n"); break; default: fprintf(fp,"Saw: %c (hexdecimal-code: %02x)\n",c,c); } }