This is my code:
Code:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int parse_string (const char *s1, char *s2) {
int pos = 0;
while ( (*s1 != '\0') || (*s1 != '\t') ) {
*s2 = *s1;
s2++;
s1++;
pos++;
}
*s2 = '\0';
return pos;
}
int main() {
FILE *ptr;
int index;
printf ("We're going to read the first five entries in pkg-config:\n");
ptr = popen ("pkg-config --list-all", "r");
char line[LINE_MAX];
index = 0;
while ( (index <= 5) && (fgets(line, LINE_MAX, ptr) != NULL) ) {
char *tmp = malloc (LINE_MAX);
parse_string (line, tmp);
printf ("%s", tmp);
free (tmp);
index++;
}
fclose (ptr);
return 0;
}
I'm trying to read the first entries and remove the tab character, but it gives me Segmentation fault, any ideas?