LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-30-2007, 11:46 AM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
openpty() in C


I am working on a project, for which I know I will need to use a pty, so that my program will be able to have two-way communication with an interactive text-based app (specifically mplayer and/or mpg321). I know that openpty() or forkpty() will be the functions I need, however, even after reading the manpage and some code samples, I'm not sure how I use the output of these functions to convert input and output to/from the external program into a stream I can read/write to.

Any suggestions or just a link to some more documentation?
 
Old 01-30-2007, 01:07 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
I use forkpty(), with no problems. Unfortunately, I can't remember what documents I used to figure out how to use it. But I can offer a relatively short program for your perusal. I use that program all the time to pick up mail landing in an Unix shell account I have with my ISP. Part of that involves using telnet in an automated fashion; since it's not as easy as it looks to modify the telnet client source code, I figured that using a pty was the best way to go. Part of the processing over the pty takes place with the telnet client, and part of it does not.

The program is here, and I'll leave it here through February of 2007:

http://www.panix.com/~wje/wiwohefi/getmale.c

It's fairly straightforward, with a few necessary wrinkles you may not have thought of. There are also a few unnecessary wrinkles, like checking the status code after pseudo-running a shell command, that you might find useful.

Start looking at it by reading the main() function, which is at the end. The other functions are in alphabetical order.

The program logs in thrice.

The first time is on my local machine. You may wish to examine this because you'll need to log in when you use a pty.

The second time is through telnet to my ISP. That code is similar, and you probably won't be interested in looking also at that. If you are, I run telnet by using a script of my own called "pnx", which you'll find that I invoke in main(). That script is simply a helping shortcut. You'd just use the telnet command.

The third time is through ftp to my ISP.

Another wrinkle of note is that when you want to send a <return> down the pty, \r works. I haven't tried \n, but \r is exactly what goes through your keyboard when you use the <Enter> key (or <Return> key, depending on your keyboard), so I'd stick with \r if I were you.

You will also be interested in examining the use of tcgetattr(), tcsetattr(), and select(), especially if you may not have used those functions yourself.

Anyway, take a look at the program and come back here if you have any questions.

Hope this helps.

Last edited by wjevans_7d1@yahoo.co; 01-30-2007 at 01:28 PM.
 
Old 01-30-2007, 02:14 PM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
thanks a bunch! I'm starting to read away.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



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

All times are GMT -5. The time now is 08:38 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