LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to send CTRL-D to child process via a pipe? (http://www.linuxquestions.org/questions/programming-9/how-to-send-ctrl-d-to-child-process-via-a-pipe-363670/)

neo_in_matrix 09-15-2005 02:55 PM

How to send CTRL-D to child process via a pipe?
 
I am successful at spawning a child process and connect its stdin/stdout to two pipes. The program (gdb) needs input. I tried fputc('q', f); fputc('\r', f); and fputs("q", f), but none worked.

Matir 09-15-2005 03:03 PM

Did you try fputs("q\n",f);? If you really want to send CTRL-D, send 0x04.

neo_in_matrix 09-15-2005 03:06 PM

Tried fputs("q\n",f) - did not work. :-(

jlliagre 09-15-2005 03:27 PM

Can you issue other commands to gdb successfully and get their output ?

neo_in_matrix 09-15-2005 03:39 PM

I found that if I do fflush(f); after fputs, then it works.

And also setting setvbuf(f, NULL, _IOLBF, 0); also works (fflush not needed).

It's strange I have to send fputs("h\n", f) - I thought it should be the CR key - \r.

Matir 09-15-2005 05:29 PM

\r by itself is a mac thing. \n is the unix way. :) (Newline).

deiussum 09-15-2005 05:56 PM

Quote:

Originally posted by Matir
\r by itself is a mac thing. \n is the unix way. :) (Newline).
And Windows is a combination of CRLF. (e.g. \r\n.) :)

Matir 09-15-2005 06:14 PM

Quote:

Originally posted by deiussum
And Windows is a combination of CRLF. (e.g. \r\n.) :)
Yep, sure is. Why they thought they needed TWO characters for the same thing that is accomplished by one in every other operating system is beyond me. :)

deiussum 09-15-2005 07:34 PM

Technically, they aren't the same, though. CR returns the cursor to the beginning of the line, and LF moves the cursor down a line, (but doesn't necessarily return it to the beginning of the line.) In Windows, though, when writing a \n it is typically translated underneath to \r\n when writing to text-mode files.

Matir 09-15-2005 08:00 PM

Yes, that is true. But I have honestly never seen any good use of one by itself in windows. Have you? :)

jlliagre 09-15-2005 08:09 PM

Quote:

Why they thought they needed TWO characters for the same thing that is accomplished by one in every other operating system is beyond me.
This is wrong, many of the non Unix/Unix like O/Ses use CR-LF, and not exactly the problem.

Every terminal or terminal emulator need both CR and LF to go to the next line, including GNU/Linux and Unix.
Before Unix and C were invented, that convention was used too for representing an end of line in text files on all operating systems using ASCII. That was easing the display of a file on a screen (or a teletype) as no processing was required to "cat" a file.

C created the "\n" convention (newline) to represent the end of lines in text files, regardless of the underlying O/S (of course at the beginning the only C underlying O/S was Unix).

That was saving a little storage space, and easing all programs dealing with files (a single character is faster to locate that two) and improve response time of interactive programs at a time when sending one byte could cost a noticeable amount of time ...

However that added some complexity to the terminal drivers, which had (and still have) to convert this delimitor in what the terminal expects/sends.

See the numerous stty options to deal with that mess: inlcr igncr icrnl onlcr ocrnl onocr onlret echonl


All times are GMT -5. The time now is 12:17 PM.