473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Talking to *n*x

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.
Nov 14 '05 #1
10 1404
robbie carlton <ro************@hotmail.com> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
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>

Nov 14 '05 #3
ro************@hotmail.com (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. \_/
Nov 14 '05 #4
-----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-----
Nov 14 '05 #5
# 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!
Nov 14 '05 #6
"robbie carlton" <ro************@hotmail.com> wrote in message
news:32**************************@posting.google.c 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.

Nov 14 '05 #7
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
Nov 14 '05 #8
In <40***********************@news.xs4all.nl> Case <no@no.no> 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: Da*****@ifh.de
Nov 14 '05 #9
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9***********@sunnews.cern.ch...
In <40***********************@news.xs4all.nl> Case <no@no.no> 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.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
Nov 14 '05 #10
In <x2******************@twister01.bloor.is.net.cable .rogers.com> "nobody" <no****@nowhere.non> writes:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9***********@sunnews.cern.ch...
In <40***********************@news.xs4all.nl> Case <no@no.no> 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:


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jane Austine | last post by:
I need to control a command line program via python. I first tried popen2 and 3 but I couldn't find a way to talk to the subprocess interactively; that is, read some and then write some, and...
6
by: Don | last post by:
Environment: VB6 under W98 I have a USB hardware device I wish to program directly. The original software installed a low level driver and the hardware is both registered and operational. Now...
11
by: seattleboatguy | last post by:
I am trying to send a message from a visual c++ user-thread to the main window, so the main window can update text on the window to reflect what the user-thread is doing. I have tried 2 different...
2
by: Simon T. | last post by:
Hello All, I am new to C# and the .NET framework, so please forgive and pity me if the question and understand reveals massive ignorance. I am trying to get SerialPort talking to a "Standard...
3
by: pamelafluente | last post by:
I am new to asp.net. I have an asp page with a submit button which sends out some information. Instead of having IIS to respond and deal with this information, I would like to have a .NET...
1
by: Daniel | last post by:
what is best way to talk to https webpages within .net? any good librarys for talking to https websites?
20
by: raylopez99 | last post by:
Dvorak is always interesting, albeit speculative. What hidden gem has he found in Vista that helps developers? It can't be .NET/CLI, that's been out forever. RL Vista rollout hides reality...
1
by: Pumpkin Carver | last post by:
I have a form that has a listview on it and a serious of strings in the listiew. When i doubel click on the listview item it opens a new form and displays the text that i pass to the constructor....
20
by: Steven D'Aprano | last post by:
Am I the only one who finds that I'm writing more documentation than code? I recently needed to write a function to generate a rank table from a list. That is, a list of ranks, where the rank of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.