C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-12-2009, 06:46 AM   #1
Shalom
 
Drogin's Avatar
 
Join Date: Oct 2005
Location: Norway
Posts: 92
recieving pipes from bash

Hi!
I read that programs read input from pipes through stdin.
So that if I wrote this in bash:
Code:
echo "Hei" | cat
The cat-program finds the "Hei" text from reading stdin.


But how does for example the more-program work?
Because I can pipe things to more in bash, but if I write just "more" without anything else, it just says: " Illegal command blablab".

Shouldnt the "more"-program be waiting from input from stdin?

Or does it have some way of doing like this?
Code:
if(pipedInfoInStandardInput) {
 readFromStandardInput();
} else { // Normal programflow
 ...
}
Put simple: How can "more" know when to read stdin for piped info, and when not to?
(Because when I write "more" and nothing else, it doesent accept input from stdin...it's only when I use pipes that it reads stdin)

Last edited by Drogin; 04-12-2009 at 07:20 AM.
Drogin is offline   Reply With Quote
Old 04-12-2009, 07:46 AM   #2
Registered User
 
draugr's Avatar
 
Join Date: Mar 2009
Posts: 9
Hei Drogin

It's actually simple and you pretty much guessed it: the "magic function" is isatty(int filedes). The function can tell you if stdin (file descriptor 0) comes from an interactive terminal or not, so you can do different things depending on what it tells you.
draugr is offline   Reply With Quote
Old 04-12-2009, 09:05 AM   #3
Shalom
 
Drogin's Avatar
 
Join Date: Oct 2005
Location: Norway
Posts: 92
aah, thanks a lot!
So we could do something like this then?
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
  if(isatty(fileno(stdin)) != 1) {
    // Is it safe to assume that we can read info from stdin as if it was a piped, now?
  } else {
    // Programflow that does terminal things
  }
  return EXIT_SUCCESS;
}

Last edited by Drogin; 04-12-2009 at 09:10 AM.
Drogin is offline   Reply With Quote
Old 04-12-2009, 09:21 AM   #4
Registered User
 
draugr's Avatar
 
Join Date: Mar 2009
Posts: 9
Yes, that should work.
draugr is offline   Reply With Quote
Old 04-12-2009, 01:22 PM   #5
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
fileno(stdin) can be abbreviated as STDIN_FILENO (in unistd.h), which is defined by POSIX to be 0.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 04-12-2009, 04:07 PM   #6
Shalom
 
Drogin's Avatar
 
Join Date: Oct 2005
Location: Norway
Posts: 92
It can, but don't you think
isatty(fileno(stdin))
is more readable than a "magic" number 0?

EDIT:
Ah,nvm, understood what you meant now
Drogin is offline   Reply With Quote
Old 04-12-2009, 04:09 PM   #7
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
I find STDIN_FILENO to be more readable than fileno(stdin).

Whether 0 is more readable is a question of how much you work with POSIX systems, I suppose. I wouldn't have any problems with it, but I suppose people less used to it would have.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipes sometimes fail EVOEx Linux Programming 2 05-02-2009 01:47 PM
argument with space passed to bash script and further to executable lehe Linux Programming 3 04-10-2009 12:57 AM
Pipes Martin Kovac C Programming 1 03-31-2009 03:09 AM
bash scripting? Draco Linux Programming 1 01-08-2007 06:15 AM
Services and Pipes nickname_changed Windows Programming 0 07-16-2003 06:46 AM


All times are GMT -6. The time now is 04:37 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22