LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   system call as variable (https://www.linuxquestions.org/questions/programming-9/system-call-as-variable-654497/)

RudraB 07-09-2008 01:02 AM

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?

jiml8 07-09-2008 01:23 AM

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.

RudraB 07-09-2008 01:34 AM

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

RudraB 07-09-2008 01:44 AM

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?

jiml8 07-09-2008 01:52 AM

You need to declare the variable i686.

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

Is this homework?

RudraB 07-09-2008 02:00 AM

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....


All times are GMT -5. The time now is 09:04 PM.