I am NOT a C developer but I need to make a quickie C interface to an application that can run my PHP scripts, do logic on the results, etc. I am having a hard time getting the returned array to return the correct value. I wish C was as easy as PHP :/ Currently it returns the same value in every element and I have no idea why. Here is what I have so far:
The output of this on a linux machine is:PHP Code:
#include <stdio.h>
#include <string.h>
#define MAX_SHELL_EXEC_RETURN_VALUE 1000
#include <ctype.h>
//I dont like the shell_exec_.results syntax, I'd rather use shell_exec_data->results instead. How do I do that?
typedef struct shell_exec_data
{
//Keep it small for testing purposes, make it bigger later, ideally it should only be as big as the actual results. I dont know if thats possible with C though.
char *results[5];
}
SHELL_EXEC_DATA;
SHELL_EXEC_DATA shell_exec();
//Testing area, all your main are belong to us
int main()
{
SHELL_EXEC_DATA test;
test = shell_exec("ls -l"); //Quickie command to get \n terminated lines for test data
printf("main() print: %s, size: %d", test.results[2], sizeof(test.results));//Print element 2 of the results array.
}
//The guts of the thing, shell_exec() function
SHELL_EXEC_DATA shell_exec(char* command)
{
FILE *fp;
int i = 0;
int key = 0;
char data[255];
SHELL_EXEC_DATA shell_exec;
fp = popen(command, "r");
if (fp == NULL)
{
//error handling here
return;
}
while (fgets(data, MAX_SHELL_EXEC_RETURN_VALUE, fp) != NULL)
{
printf("%s", data);//Print out the value of path to make sure its right
shell_exec.results[i] = path;//Stuff the value into array
i++;
}
pclose(fp);
//Before returning, make sure array is correct
for (key=0;key<5;key++)
{
printf("%d %s\n", key, shell_exec.results[key]);//print out each element in array
}
//return struct/object whatever which contains array since C cant return an array.
return shell_exec;
}
Notice how the date is 4,,,,, in the bad results. I dont know why it does that, but in the terminal its a wierd looking character. Like memory corruption or something, so theres something done wrong somewhere. Also notice that the returned array has the LAST result in every single element. What the heck?[root@localhost ryan]# gcc test2.c
[root@localhost ryan]# ./a.out
total 1122
drwxr-xr-x 3 ryan ryan 216 May 8 16:39 Desktop
drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Documents
drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Movies
drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Music
drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Pictures
-rwxr-xr-x 1 root root 7417 Jun 17 15:36 a.out
drwxr-xr-x 15 root root 888 May 30 10:09 ldir
drwxr-xr-x 10 root root 712 May 30 10:50 pvpgn-199.r521
-rw-r--r-- 1 root root 1125922 May 30 10:11 pvpgn-199.r521.tar.gz
-rw-r--r-- 1 root root 2121 Jun 17 12:20 test.c
-rw-r--r-- 1 root root 50 Jun 17 00:53 test.php
-rw-r--r-- 1 root root 1589 Jun 17 15:36 test2.c
lrwxrwxrwx 1 ryan ryan 4 May 8 16:39 tmp -> /tmp
0 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp
1 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp
2 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp
3 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp
4 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp
main() print: lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp
, size: 20[root@localhost ryan]#