ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
There's an easy way and a hard way The easy way is to just have a command line argument that says to turn off the prompt, e.g.
int main(int argc, char** argv)
int prompt = 1;
if (argc > 1 && strncmp(argv, "-q", 2) == 0)
prompt = 0;
So now you can say "echo 1,1 | ./a -q" and it doesn't print the prompt. Ok, so that's a little ugly. If you want exactly the results you're looking for, you need to check if stdin comes from a terminal. There is probably a right way to do this which I don't know. The wrong way, which would work, is to use the readlink() function (see its man page) on /proc/$$/fd/0, where $$ is your pid which you can get with getpid(), and see if the link points toward something beginning with "/dev/pts/". If so, you're reading from a terminal so you should present the prompt.
Other systems might have different naming conventions, too.
I'm assuming you're on linux. If you're on *bsd you might have to do something special as root to even get /proc. If you're on something else, well, sorry
Oh yeah, another horribly ugly solution would be to select() on stdin for a short time, and if it fails to find waiting input assume you're reading from a terminal. That's reaaaly ugly though.