How to execute nm from C and capture output

281 views Asked by At

I'm trying to write a program that will check the size of the global variables of another program. Let's call my program "check" and the checked program "a.out," meaning I would just run my program like this:

./check a.out

I think that I can find this value using the command:

nm -t d -S a.out | grep ' B ' | awk '{print $2}'

I know that you can use fork/execl to execute a program from within a program, but I'm having a little trouble writing the piping of the IO.

Using some simple strcpy/strcats, I have a c-string that contains the above string, which I can call using execl, but it doesn't seem to print anything. Right now it looks something like this:

// Code setting char command[1024] equal to the above string
int pid;
if(pid = fork()){ // Parent
    // Need some pipes here?
}
else{ // Child
    execl(command, NULL);
}

Can anyone point me in the right direction to setting these pipes up?

1

There are 1 answers

0
Armali On

pubs.opengroup.org/onlinepubs/009696799/functions/popen.html might be what you need. – Mat

It looks like I can get popen to do what I want. I still need to parse the output and convert it to an integer, but it looks like this will do the job! – nchen24