In particular, I'm writing an interpreter, and I'd like it to run source code from STDIN if no source file is given.
So I wrote a tiny program that is basically just a loop that, upon each iteration, reads a line from STDIN and adds it to a linked list, then prints the list when it's done. When I pipe a file to this program, i.e. cat my_awesome_file | ./tiny_program
, it does in fact print the list and subsequently exit. How does it know to do this? Does cat send a sigint
to tiny_program
? Is there some sentinel byte (maybe the null byte) that conveys "hey, I'm done sending you stuff now"?
So in summary, my program is working fine, but I don't really know why it's working fine, and I'd very much like to.
Here's my tiny program, in rust. The exact code is probably not relevant, but maybe someone will find it helpful.
fn main() {
let mut ll: LinkedList<String> = LinkedList::new();
for line in std::io::stdin().lines() {
ll.push_back(line.unwrap());
}
println!("{:?}", ll);
}