473,385 Members | 1,930 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,385 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 2023
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...

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.