this is my code:
void Available_firmware_version(){
char *version = (char *) malloc(2);
strcpy(version, "");
FILE *fp;
int status = 0;
char path[PATH_MAX];
fp = popen("swift list Manto", "r");
if (fp == NULL){
printf("Error: Popen is NULL \n");
return;
}
while (fgets(path, PATH_MAX, fp) != NULL){
version = (char *) realloc(version, strlen(version) + strlen(path));
strcat(version, path);
printf("%s\n", version);
}
status = pclose(fp);
if (status == -1) {
printf("Error: Popen not closed (pclose)\n");
return;
}
free(version);
}
When I execute code my program crash. Output is segmentation fault, before pclose(fp) command. When I delete realloc program run fine. Maybe I doing something wrong with realloc?