Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Talking to *n*x

Reply
Thread Tools

Talking to *n*x

 
 
robbie carlton
Guest
Posts: n/a
 
      06-01-2004
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.
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      06-01-2004
robbie carlton <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
Case
Guest
Posts: n/a
 
      06-01-2004
robbie carlton wrote:
> 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>

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      06-01-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

robbie carlton wrote:

> 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-----
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      06-01-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (robbie carlton) writes:

> 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


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      06-01-2004
# 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
OOOOOOOOOO! NAVY SEALS!
 
Reply With Quote
 
Kieran Simkin
Guest
Posts: n/a
 
      06-01-2004
"robbie carlton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.

> Anyhoo, ta.



 
Reply With Quote
 
those who know me have no need of my name
Guest
Posts: n/a
 
      06-01-2004
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.

--
a signature
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-02-2004
In <40bc7cb8$0$33920$(E-Mail Removed)4all.nl> Case <(E-Mail Removed)> writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
nobody
Guest
Posts: n/a
 
      06-07-2004
"Dan Pop" <(E-Mail Removed)> wrote in message
news:c9kuna$sfh$(E-Mail Removed)...
> In <40bc7cb8$0$33920$(E-Mail Removed)4all.nl> Case <(E-Mail Removed)> writes:
>
> >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.
>

[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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[META] Talking about talking about C. Seebs C Programming 113 11-24-2010 08:57 AM
4 computers 2 not talking to each other Carol Wireless Networking 8 12-30-2005 06:11 PM
Netscreen-Remote client talking to Cisco VPN 3005? Road Rage Cisco 0 05-11-2005 03:26 PM
ATM links not talking polifemo Cisco 1 10-20-2003 02:28 PM
where can I find book/resources talking about DSP design using VHDL? walala VHDL 1 09-01-2003 09:54 AM



Advertisments