473,497 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

system call as variabe

i have written a code::
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sys;
int i686;
sys=system("/bin/uname -m");
//printf ("%d",sys);
if (sys==i686){
printf("the syst. 32 bit");
}
else
printf("64but");
}
where i want the output of "system("/bin/uname -m")" as the variable
sys.....to find the machine structure.this procedure is not quite
working. can you people suggest me some way?
Jul 9 '08 #1
5 2035
rudra said:
i have written a code::
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sys;
int i686;
sys=system("/bin/uname -m");
//printf ("%d",sys);
if (sys==i686){
Since the defined-but-not-initialised i686 int object doesn't have a value,
how can this comparison be meaningful? I think what you're really trying
to do is find out whether the output of uname is the string "i686", but
the return value of the system() function won't tell you this.

One possibility for achieving your objective is to redirect the output of
the command to a file, and then open and read the file. This would keep
the code reasonably portable. An alternative that you might wish to
explore if you're into system-specific stuff is a pipe (and
comp.unix.programmer knows all about pipes).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 9 '08 #2
Richard Heathfield <rj*@see.sig.invalidwrites:
rudra said:
>i have written a code::
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sys;
int i686;
sys=system("/bin/uname -m");
//printf ("%d",sys);
if (sys==i686){

Since the defined-but-not-initialised i686 int object doesn't have a value,
how can this comparison be meaningful? I think what you're really trying
to do is find out whether the output of uname is the string "i686", but
the return value of the system() function won't tell you this.

One possibility for achieving your objective is to redirect the output of
the command to a file, and then open and read the file. This would keep
the code reasonably portable. An alternative that you might wish to
explore if you're into system-specific stuff is a pipe (and
comp.unix.programmer knows all about pipes).
Portability is a tricky thing. The call system("/bin/uname -m") is
portable in the sense that it uses a function defined by the C
standard, but it will fail unless the string "/bin/uname -m" happens
to be meaningful as a command.

As it happens, any system on which "/bin/uname -m" is a valid command
will have other methods to achieve the same result, using non-portable
functions. <OT>Using a pipe should work, but invoking the underlying
uname() function directly is cleaner and really no less portable.</OT>

Let me repeat the excellent advice to post to comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 9 '08 #3
Keith Thompson said:
Richard Heathfield <rj*@see.sig.invalidwrites:
<snip>
>>
One possibility for achieving your objective is to redirect the output
of the command to a file, and then open and read the file. This would
keep the code reasonably portable. An alternative that you might wish to
explore if you're into system-specific stuff is a pipe (and
comp.unix.programmer knows all about pipes).

Portability is a tricky thing. The call system("/bin/uname -m") is
portable in the sense that it uses a function defined by the C
standard, but it will fail unless the string "/bin/uname -m" happens
to be meaningful as a command.
Yes, which is why I said "reasonably portable" rather than "maximally
portable". One obvious improvement is to have the command string read in
at runtime, allowing the user to provide a command that is relevant to the
host system. This presupposes, alas, that the user knows what he's doing,
and also trusts that he is not a malicious exploiter.

<snip>
Let me repeat the excellent advice to post to comp.unix.programmer.
By all means.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 9 '08 #4
On 9 Jul 2008 at 21:50, Keith Thompson wrote:
As it happens, any system on which "/bin/uname -m" is a valid command
will have other methods to achieve the same result, using non-portable
functions. Using a pipe should work, but invoking the underlying
uname() function directly is cleaner and really no less portable.
Cleaner and safer.

Here's a simple example:

#include <stdio.h>
#include <sys/utsname.h>

int main(void)
{
struct utsname buf;
if(uname(&buf)==0)
printf("sysname: %s\nnodename: %s\nrelease: %s\nversion: %s\nmachine: %s\n",
buf.sysname, buf.nodename, buf.release, buf.version, buf.machine);
return 0;
}

Jul 9 '08 #5
On 9 Jul 2008 at 22:00, Richard Heathfield wrote:
One obvious improvement is to have the command string read in at
runtime, allowing the user to provide a command that is relevant to
the host system.
An improvement, eh? Let's all pray we never have to rely on the security
of your software. Do you also like your programs to run with root
privileges for good measure?

Jul 9 '08 #6

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

Similar topics

5
14386
by: Ayesha Ahsan | last post by:
Hi, I use Runtime.getRuntime().exec(command) to make my system call. For Windows based Dos, i add "cmd /c" before I type in my system call. So for example make the system call "dir": String...
1
2678
by: Erik Johnson | last post by:
Hi, I am trying to spawn a daemon type program to go off on its own and do some work (asynchoronously) from within an HTTPServer, but I am running into a problem where the web browser (actually...
7
6935
by: rahul8143 | last post by:
hello, what is difference between system call and library function call? Does library function call can have context switch to kernel mode? regards, rahul
8
1430
by: Just Me | last post by:
With VB6 I wanted to be consistent in how I named variables so I developed the following doc. The last column is what I named the variable. I tried to be consistent wit the Win32 document but it...
3
4962
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
6
2333
by: leoman730 | last post by:
This is one of the interview question this morning, hope someone can help out with this. Thanks. What is the different between System call and library call?
21
2423
by: omkar pangarkar | last post by:
Hi all, I have two simple hello world programs one using printf() and other using write() --prog 1-- #include<stdio.h> #include<stdlib.h> int main() { printf("Hello"); /* up to here...
2
207
by: rudra | last post by:
i have written a code:: #include <stdio.h> #include <stdlib.h> int main() { int sys; int i686; sys=system("/bin/uname -m"); //printf ("%d",sys); if (sys==i686){
0
11242
ashitpro
by: ashitpro | last post by:
Writing System Call Wrappers in User Space. Recently I saw a post in Linux/Unix section to know the user who deleted the file in linux. Currently there is nothing in linux which could achieve...
0
6991
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...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.