Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signal EOF in mac osx terminal

I am stumped by the 1.5.2 question in K&R. I googled for sometime and found out that I have to supply the EOF input after entering the characters.

long nc = 0;  while (getchar() != EOF)     ++nc; printf("%ld\n", nc);  return 0; 

I tried both command-D and control-D as EOF inputs but nothing worked. Any idea how to supply the EOF for Mac OS X?

like image 472
Morpheus Avatar asked Jan 26 '14 14:01

Morpheus


People also ask

How do you EOF in Terminal Mac?

You have to press Ctrl + D + D . Hold the control down and press D twice.

How do you signal EOF in Terminal?

the “end-of-file” (EOF) key combination can be used to quickly log out of any terminal. CTRL-D is also used in programs such as “at” to signal that you have finished typing your commands (the EOF command). key combination is used to stop a process. It can be used to put something in the background temporarily.


2 Answers

By default, macOS (formerly OS X and Mac OS X) software recognizes EOF when Control-D is pressed at the beginning of a line. (I believe this behavior is similar for other versions of Unix as well.)

In detail, the actual operation is that, when Control-D is pressed, all bytes in the terminal’s input buffer are sent to the running process using the terminal. At the start of a line, no bytes are in the buffer, so the process is told there are zero bytes available, and this acts as an EOF indicator.

This procedure doubles as a method of delivering input to the process before the end of a line: The user may type some characters and press Control-D, and the characters will be sent to the process immediately, without the usual wait for enter/return to be pressed. After this “send all buffered bytes immediately” operation is performed, no bytes are left in the buffer. So, when Control-D is pressed a second time, it is the same as the beginning of a line (no bytes are sent, and the process is given zero bytes), and it acts like an EOF.

You can learn more about terminal behavior by using the command “man 4 tty” in Terminal. The default line discipline is termios. You can learn more about the termios line discipline by using the command man termios.

like image 188
Eric Postpischil Avatar answered Oct 02 '22 15:10

Eric Postpischil


If you want to see what EOF is set as in your terminal, you can type

stty all 

on my mac, this gives the output -

speed 9600 baud; 24 rows; 80 columns; lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl     -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo -extproc iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8 -ignbrk brkint -inpck -ignpar -parmrk oflags: opost onlcr -oxtabs -onocr -onlret cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow -dtrflow -mdmbuf discard dsusp   eof     eol     eol2    erase   intr    kill    lnext    ^O      ^Y      ^D      <undef> <undef> ^?      ^C      ^U      ^V       min     quit    reprint start   status  stop    susp    time    werase    1       ^\      ^R      ^Q      ^T      ^S      ^Z      0       ^W      

You can see four lines up from the bottom, three cells in eof is ^D.

There's a fuller description here which is where I found the information.

like image 28
dan Avatar answered Oct 02 '22 15:10

dan