LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2004, 04:50 AM   #1
vrikers
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Rep: Reputation: 0
Question after redirecting stdin still need to read data from keyboard


hi, i'm a newbie !
please help me on doing the following :

a program (ipm) needs to receive data this way :
ps | ipm
(this command form is a MUST)

so the stdin in <b>ipm</b> will be ONLY what comes from "ps". how do i <b>recreate</b>(?) the stdin in order to read something from the keyboard ?
 
Old 10-31-2004, 04:54 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
What will be reading from the keyboard? ps doesn't need keyboard input and you say that ipm will ONLY read ps's output....so where would the keyboard input be going?
 
Old 10-31-2004, 05:00 AM   #3
vrikers
LQ Newbie
 
Registered: Oct 2004
Posts: 4

Original Poster
Rep: Reputation: 0
ipm needs to receive the process list.
after getting this list, the ipm program should wait for the user to specify a pid (by keyboard) and then kill it, or send some other signal to it.

and the only way that ipm can get the process list is by " ps | ipm " (it's a requirement).
how do i attach keyboard to stdin after this ?
 
Old 10-31-2004, 05:50 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I got this to work, but it will only work if you're logged in on pts/1

Code:
itsme@dreams:~/C$ cat inp.c
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void)
{
  char buf[4096];

  while(fgets(buf, sizeof(buf), stdin))
    printf("Received: %s", buf);

  if(!freopen("/dev/pts/1", "r", stdin))
  {
    printf("Error reattaching stdin: %s\n", strerror(errno));
    return 1;
  }
  printf("Type something: ");
  fflush(stdout);
  fgets(buf, sizeof(buf), stdin);
  printf("You typed: %s", buf);

  return 0;
}
Code:
itsme@dreams:~/C$ ps | ./inp
Received:   PID TTY          TIME CMD
Received:  1591 pts/1    00:00:00 bash
Received:  2374 pts/1    00:00:00 ps
Received:  2375 pts/1    00:00:00 inp
Type something: it works
You typed: it works
itsme@dreams:~/C$

Last edited by itsme86; 10-31-2004 at 05:51 AM.
 
Old 10-31-2004, 06:11 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Here's a solution I found google'ing:
http://<br /> http://www.lugod.org/...032.html<br />

Same concept, except they reattach to /dev/tty instead of /dev/pts/1. I'll leave the rest up to you I'm interested in what you find out though, so if you could post your findings that would be great.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting stdin in bash script artur Programming 3 12-09-2011 06:07 AM
redirecting /dev/ttyS0 -> stdin goestin Linux - Software 2 10-26-2011 11:34 AM
gzlib: stdin: invalid compressed data -crc error pashford Linux - General 4 04-29-2008 07:21 AM
redirecting stdin and stdout with pipes: convincing apps they're for a terminal? prell Programming 1 09-02-2004 06:38 AM
redirection data to stdin Majestros Programming 2 07-07-2004 09:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration