LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2008, 01:02 AM   #1
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Rep: Reputation: 23
system call as variable


i want to use system bit as a input of my c program. i wrote a code:
Quote:
#include <stdio.h>
//#include <stdlib.h>
main()
{
unsigned sys;
unsigned i686;
// int i386;
sys=system("/bin/uname -m");
printf ("%u",sys);
if (sys = i686) printf("the syst. 32 bit\n");
else printf("64bit\n");
}
but its not quite working.
can you people plz help me?
 
Old 07-09-2008, 01:23 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
I take it that you don't want the i686 result to appear on the standard console, the way it is doing?

You want to pipe the output from uname into your program. Instead of using the system call, use popen.

man popen will get you going, but it looks like this:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{

FILE *stream;
char sys[5],*sysptr;
sysptr = &sys[0];
sys[4] = 0x0;
stream=popen("/bin/uname -m","r");
fread(sysptr,1,4,stream);
pclose(stream);
printf("%s\n",sysptr);
}
You might want to clean that up a bit and generalize it.
 
Old 07-09-2008, 01:34 AM   #3
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
ok but then also while i am including it like:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{

FILE *stream;
char sys[5],*sysptr;
sysptr = &sys[0];
sys[4] = 0x0;
stream=popen("/bin/uname -m","r");
fread(sysptr,1,4,stream);
pclose(stream);
printf("%s\n",sysptr);
if (sysptr == i686) printf("32bit\n");
else printf("64bit\n")

}

its showing error:
un2.c:16: error: ‘i686’ undeclared (first use in this function)
i am not getting the reason of this error. plz help
 
Old 07-09-2008, 01:44 AM   #4
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
and if i do

if (sysptr == "i686")
{
printf("32bit\n");
}
else
{
printf("64bit\n");
}
}
then also its not giving desired result!!! will you plz check?
 
Old 07-09-2008, 01:52 AM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
You need to declare the variable i686.

Beyond that, the boolean equality won't work; you need to look up strncmp.

Is this homework?
 
Old 07-09-2008, 02:00 AM   #6
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
no dear....i am a research student!! problem is i do fortran generally...not much familiar with C!! but since bitwise operation is much more efficient in C , i thought i will better do it as C....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i make centos 4.5's system call using vDSO(call *%gs:0x10) instead of int80 tclwp Red Hat 3 08-06-2007 12:07 AM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 05:19 AM
Calling a system call from a system call? Loc_8 Programming 0 09-16-2006 04:21 PM
perl: send output of system() call to variable drkstr Programming 4 07-23-2006 10:47 PM
How to call another cgi program to return a variable <db> Programming 3 04-27-2006 03:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration