I'm writing a little code, nothing fancy, which gets the open file from /proc/<pid>/maps:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *file;
char filename[1024];
char input[4096];
snprintf(filename, sizeof(filename), "/proc/1/maps");
file = fopen(filename, "re");
if (file) {
while (fgets(input, 4096, file) != NULL) {
char *pos;
pos = strstr(input, "/");
if (pos != NULL) {
size_t len = strlen(pos);
if (pos[len - 1] == '\n')
pos[len - 1] = '\0';
}
}
}
return 0;
}
I would like to put all 'pos' into an char array or point char array, as you wish. I've tried some 'ideas', but all end-up with segfault or errors.
Never used a proper char array, so really not sure how to. Can anyone show me how or point me in the right direction?
when I've tried my luck, I've used something like:
Code:
char **s_array;
int count = 0;
s_array = calloc(1, sizeof(char *));
...
strcpy(s_array[count], pos);
count++;
s_array = realloc(s_array, (count + 1) * sizeof(char *);
...
but this didn't really work.