Talking to *n*x

R

robbie carlton

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");

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.
Anyhoo, ta.
 
J

Jens.Toerring

robbie carlton said:
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");
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.

With anything beyond system() you leave the realm of standard C.
So better ask this kind of questions in an appropriate newsgroup
like comp.unix.programmer or comp.os.linux.development.apps.

<OT>
Have a look at the popen(3) function, but opendir(3), readdir(3)
and closedir(3) may also be interesting for you.
</OT>
Regards, Jens
 
C

Case

robbie said:
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");

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.
Anyhoo, ta.

<OT>
There's a thick book, "Advanced Programming in the UNIX Environment"
written by the late Richard Stevens about this subject. The function
system() is interesting, but in many cases not the (most efficient)
way to go.

All the best,

Case
</OT>
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

robbie said:
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 realize, of course, that these two system() calls are independant of
each other, and the second call will /not/ create
/me/home/somedir/somesubdir ?

[snip]
- --

Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAvIZhagVFX4UWr64RAhEjAJ9g8g/YsDkHi2CV1P77EoCf445pSwCgubLw
b1GMXvOo4fLz02gM5EWxS8A=
=qaMa
-----END PGP SIGNATURE-----
 
M

Martin Dickopp

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.

The newsgroup comp.unix.programmer has advice and URLs (e.g. their FAQ
will be interesting for you). However, Unix and the system-specific
parts of its interface to C are off-topic here, so please ask in
comp.unix.programmer.
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");

The first function call has no effect on the calling program under Unix.
The second is likely to work, but very inefficient.
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?

No. Please ask in comp.unix.programmer for details.

Martin
 
S

SM Ryan

# system("cd /me/home/somedir");

This doesn't do what you perhaps you think it does.

# which is cool. But what if I want to get information /from/ the OS?

You can construct commands that write to temporary files, and then
read the temporary files.

cd /tmp
cat <<':eof' >t.c
#include <stdio.h>
#include <stdlib.h>
int main(int N,char **P) {
printf("--------------A\n");
system("uname > xyz");
printf("--------------B\n");
FILE *f = fopen("xyz","r"); int c;
printf("--------------C\n");
while ((c=fgetc(f))!=EOF) fputc(c,stdout);
printf("--------------D\n");
system("rm xyz");
return 0;
}
:eof
cc t.c
a.out
--------------A
--------------B
--------------C
Darwin
--------------D

By keeping the commands passed to system() in a per-OS include, you can
keep to ANSI C and still do system specific work.

# 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.

It's not either C or scripting. Anything you can do in a script, you can
do in C. The question is where is it easiest to do it, and often it's a
combination of C and scripting.
 
K

Kieran Simkin

robbie carlton said:
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");

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.

You've missed one fundamental thing - most of the commandline tools like ls,
cd and mkdir are written in C and are simply wrappers for C functions. Why
call an external program to do something which you can do yourself in pure C
code? Try `man 2 mkdir` for example.

This is all off topic of course. But I can't help but point out that if
you're running system() on `cd` and `mkdir`, you're generally doing
something wrong.
 
T

those who know me have no need of my name

in comp.lang.c i read:
# what if I want to get information /from/ the OS?

typically an implementation and/or o/s provides functions and libraries by
which you can gain that information -- and it's really no less portable
than doing things via system(), in that what you pass to system and what
happens is undefined (by the c standard), and so is using an implementation
provided function. in fact the implementation function may be better,
e.g., posix/sus defines the behavior of uname() but the behavior of calling
system() remains undefined!
system("uname > xyz");

this is the tricky bit -- not all o/s' do redirection, or do but not with
this syntax. a per-o/s set of includes still presumes it can all be done
in the same sorts of ways, probably with a single call to system. and
that's not a given. (also consider what happens if two such programs are
run at nearly the same time -- reading from a truncated file doesn't
provide the answer you wanted.) instead write an abstract interface for
the *purpose*, then for each o/s on which your program should run you write
the necessary code -- i.e., you `port it' -- including using whatever build
management tools are available to `use' the proper chunk of code.
while ((c=fgetc(f))!=EOF) fputc(c,stdout);
printf("--------------D\n");
system("rm xyz");

as an aside: the file should be closed first. on some systems (unix-ish)
it doesn't matter, but on other systems that use the same sort of syntax
(windows with pwb tools) it does (the rm will fail). i suggest fclose(f);
remove("xyz"); instead.
 
D

Dan Pop

In said:
There's a thick book, "Advanced Programming in the UNIX Environment"
written by the late Richard Stevens about this subject. The function
system() is interesting, but in many cases not the (most efficient)
way to go.

These days, unless you have system() in the performance critical path,
its efficiency is a non-issue.

fangorn:~/tmp 21> cat test.c
#include <stdlib.h>

int main(void)
{
system("date");
return 0;
}
fangorn:~/tmp 22> gcc test.c
fangorn:~/tmp 23> time ./a.out
Wed Jun 2 18:18:04 CEST 2004
0.000u 0.000s 0:00.01 0.0% 0+0k 0+0io 515pf+0w

Dan
 
N

nobody

Dan Pop said:
These days, unless you have system() in the performance critical path,
its efficiency is a non-issue.
[snip]

He didn't necessarilly mean execution-time efficiency:

7.20.4.5 The system function
Synopsis
1 #include <stdlib.h>
int system(const char *string);
Description
2 If string is a null pointer, the system function determines whether the
host
environment has a command processor. If string is not a null pointer, the
system
function passes the string pointed to by string to that command processor to
be
executed in a manner which the implementation shall document; this might
then cause the

^^^^^^^^^^^^^^^^^^^^
program calling system to behave in a non-conforming manner or to terminate.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
D

Dan Pop

In said:
Dan Pop said:
^^^^^^^^^
These days, unless you have system() in the performance critical path,
its efficiency is a non-issue.
[snip]

He didn't necessarilly mean execution-time efficiency:

About what other kind of efficiency could he be talking??? The text you
quote below has exactly zilch to do with efficiency issues.
7.20.4.5 The system function
Synopsis
1 #include <stdlib.h>
int system(const char *string);
Description
2 If string is a null pointer, the system function determines whether the
host
environment has a command processor. If string is not a null pointer, the
system
function passes the string pointed to by string to that command processor to
be
executed in a manner which the implementation shall document; this might
then cause the

^^^^^^^^^^^^^^^^^^^^
program calling system to behave in a non-conforming manner or to terminate.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You're reading impaired again. Compare the text you have underlined with
"but in many cases not the (most efficient) way to go" and explain any
connection you can find. Keep in mind that the context is Unix.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top