ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So, I know that one can use the system(); command in C to run an external command. My question is, is there any way to interact with that program from within your C code?
I'm trying to piece together an extremely lightweight but relatively full-featured set of apps for an ancient machine, and I'd love a media player with a nicer playlist-type interface similar to rhythmbox, but in gtk+2 only and very memory concious. I'm trying to write it myself and I'm wondering if there's anyway to leverage MPlayer's existing interactive console controls or if I'll have to delve into the MPlayer (or perhaps MPlayerXP) code.
You can open a pipe to the program, and send data to it that way (the program reading it from stdin). This is suitable for programs which read commands from stdin, e.g. bc.
Got it, I suppose my inexperience at programming is showing, as I have no clue how to open a pipe, but I suppose now is a good time to rtfm! It's good for me :-)
popen is probably the call you want to read up on. popen will start a process and depending on the type you select, you can read from the pipe, or write to it. Note that you cannot do both reading and writing. If you need to both write to and read from a process, you can do it using pty. Here's some example code snippets: http://www.codase.com/search/call?name=openpty
It would appear that pty's are exactly what I'm looking for, but I'm a bit unsure of the manpage for openpty(). It makes constant reference to int values master and slave? The way I'm understanding it, I give it master, and a pointer to slave, and it changes the contents of slave. But then how do I go from having master and slave to having read/write access to a stream?
Quote:
int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp);
The openpty() function finds an available pseudo-tty and returns file
descriptors for the master and slave in amaster and aslave. If name is
non-null, the filename of the slave is returned in name. If termp is
non-null, the terminal parameters of the slave will be set to the values
in termp. If winp is non-null, the window size of the slave will be set
to the values in winp.
by "file descriptor" do they mean that amaster is an integer representation of a null-terminaled string containing the filesystem location, like /dev/ptyp2 for example?
I'm having a remarkably hard time finding any documentation on openpty() or forkpty(). The little documentation I have found, just the man page, is what leads me to believe that I want forkpty(), but can anyone point me to some more documentation? I can't figure out how to use these. Thanks!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.