473,398 Members | 2,404 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,398 software developers and data experts.

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 2026
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.