Re: Talking to *n*x

From: Eric Sosman (Eric.Sosman_at_sun.com)
Date: 06/01/04


Date: Tue, 01 Jun 2004 15:15:11 -0400

robbie carlton wrote:
> reposted (after stern words :-) from comp.lang.c
>
> I understand that c was originally designed to write and communicate
> with the unix operating system. Until recently I ignored this, as I
> was mostly polaying with opengl. I am now interested in interacting
> more directly with the operating system (actually linux) and I was
> wondering if anyone has any advice/urls. Specifically, I've found the
> system() function, which almost does what I want. But, while I could
> do something like
>
> system("cd /me/home/somedir");
> system("mkdir somesubdir");

     You could certainly do that, but it would not do
what you probably desire unless your cwd is already
/me/home/somedir. If you're adept at writing shell
scripts (as is hinted below), you should understand
why: What happens to a parent shell's cwd when a script
in a sub-shell executes 'cd'?

> which is cool. But what if I want to get information /from/ the OS?
> say I just want to get the output of a ls command. Do I have to do it
> all with system() calls and environment variables? If so, I may as
> well write a shell script.

     As usual, the tool depends on the task. To get
output from or (not "and/or") feed input to a sub-process,
consider popen(). For finer control of sub-processes,
see fork() and the various flavors of exec(). To do
the job unaided in your own process, see the documentation
for the system calls and/or library functions that do
what you need -- for an 'ls' clone, opendir() and readdir()
seem a good start.

> Anyhoo, ta.

     Anyha, too.

-- 
Eric.Sosman@sun.com