Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Direct I/O via /dev/tty; is this a shell limitation?

Status
Not open for further replies.

StickyBit

Technical User
Jan 4, 2002
264
CA
Hello,

I have a question regarding terminal I/O via /dev/tty. If a C program reads input directly from /dev/tty (not standard input) is it possible for the shell to redirect input to a C program? In other words, can the shell redirect input to /dev/tty as if the input was coming directly from the terminal?

I’m 99% sure this cannot be done, and I will write a small C program to prove it (or not). I’m just not sure at this point in time as to why the shell has this limitation. If anyone can shed light on this topic it would be greatly appreciated.

echo blabla > /dev/tty

/Stickybit
 
Sure you can.
Use select(), pipe()(or socketpair()). If the
processes aren't directly related use a named pipe
or unix sockets.
 
Yes, and no.
You can pipe it unix style:
Code:
echo 'blah blah blah' | program
Which isn't exsactly what your looking for. I suppose you could use another small program to write to any device or file that pretends to be a regular file in the same mannor, so that it LOOKS like the shell does.

What EXACTLY are you trying to do, because we can't help unless your a bit more spesific.

[plug=shameless]
[/plug]
 
Alright, I'll be specific.
You can read from this terminal 'device' with a
C program and then communicate with any other
process via S5 IPC or via immediate parent/child
ipc (pipe) relationships if this is the case with the C program or you can tell us what's going on.

There are also platform specific hacks available via
ioctl and root access that can redirect tty information
to any other source.
I don't know why you are 99% certain about anything.
 
I think the poster already solved his or her problem here: thread822-1041733

This is why double posting is frowned upon.
 
Sorry folks, I guess my question was a little vague. I was trying to script adding passwords to user accounts on a Solaris system via the passwd command. When I ran the script below, the script would echo the passwd to stdin but the passwd command would not read the input – I haven’t seen the source code for the Solaris passwd program but I believe it will only read input from /dev/tty not stdin (file descriptor 0).

#!/bin/bash
PASSWORD=4Fjkl456
echo ${PASSWORD} | gawk '{print $1}{system("sleep 1")}{print $1}' | passwd testuser

So my question still stands, if a C program will only read input from /dev/tty, can the shell redirect input to /dev/tty as if the input were coming directly from the terminal?

I was able to write a script to perform the above by using a pseudo-terminal i.e. telnet or ssh into the localhost then run the script. This makes sense if the program will only read from /dev/tty. I’m curious to know if this can be done another way via the shell without using expect or a 3rd generation language?

#!/bin/sh
host=127.0.0.1
port=23
login=user
passwd=password
newpass=12345
cmd1="passwd testuser"
echo open ${host} ${port}
sleep 1
echo ${login}
sleep 1
echo ${passwd}
sleep 1
echo ${cmd1}
sleep 1
echo ${newpass}
sleep 1
echo ${newpass}
sleep 2
echo exit

Usage: script | telnet



Note: The passwd command is implemented differently between various UNIX systems. The Redhat Linux passwd command will read from stdin, the Gentoo LINUX passwd command will read from /dev/tty, if not available, it will read from stdin, and the Solaris passwd command will only read from /dev/tty…?

Thank you for you time,

/Stickybit
 
Yes, I knew that jstreich. No harm no foul.
Just clarifying my initial suggestion.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top