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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.