Code:
#define ARRAY_SIZE 6080
#define MAXTOKENS 2056
#define MAXLINE 2024 /* buff */
#define MINLEN 3 /* skip lines shorter as */
#define NUL '\0'
#define TRUE 1
#define FALSE 0
char *strtok( char *str1, const char *str2 );
typedef struct Hash{
char key[ARRAY_SIZE];
char value[ARRAY_SIZE];
}hash,f_hash;
typedef struct filedet{ char filename[83]; hash *mainfile; }filedet;
typedef struct fl_det{ char f_name[83]; f_hash *file; }fl_det;
typedef struct second_stage{ char server[83]; filedet *stage; } second_stage;
typedef struct o_stg_two{ char svrname[83]; fl_det *s_no; } o_stg_two;
typedef struct third{ char typ[83]; second_stage *wed; } third;
typedef struct o_stg_thr{ char t_nam[83]; o_stg_two *tue; } o_stg_thr;
char delims[] = "=";
char **split(char *string, char *delim);
char *rem_spc(char *buff);
struct third *ptr[6];
struct o_stg_thr *thu[6];
third stagethrd_info;
o_stg_thr otherthrd_info;
int main()
{
FILE *alp,*popen();
char *delim = "=";
char **tokens = NULL;
int i = 0, mcount = 0, lcount = 0;
int loop;
int sort;
int a=0;
int b=0;
int j;
int size = 3;
int count = 0, reg = 0, cnt = 0, regie = 0;
char line[MAXLINE],line[MAXLINE],line3[MAXLINE];
char output[100];
char exe_str[]=" echo FIRST ; more fileone.txt; echo filetwo; more filetwo.txt; echo SECOND; more filethree.txt; more filefour.txt";
int read = 0, read01 = 0 , tab = 0 , tab01 = 0;
thu[1] = malloc(sizeof(o_stg_thr)*(3500));
thu[1]->tue = malloc(sizeof(o_stg_two)*(4500)+1);
thu[1]->tue->s_no = malloc(sizeof(fl_det)*(4600)+1);
thu[1]->tue->s_no->file= malloc(sizeof(f_hash)*(8020)+1);
ptr[1] = malloc(sizeof(third)*(77));
ptr[1]->wed = malloc(sizeof(second_stage)*(6070)+1);
ptr[1]->wed->stage = malloc(sizeof(filedet)*(7070)+1);
ptr[1]->wed->stage->mainfile = malloc(sizeof(hash)*(8800));
thu[0] = malloc(sizeof(o_stg_thr)*(1000));
thu[0]->tue = malloc(sizeof(o_stg_two)*(6));
thu[0]->tue->s_no = malloc(sizeof(fl_det));
thu[0]->tue->s_no->file= malloc(sizeof(f_hash));
ptr[0] = malloc(sizeof(third));
ptr[0]->wed = malloc(sizeof(second_stage)*(607)+1);
ptr[0]->wed->stage = malloc(sizeof(filedet)*(70)+1);
ptr[0]->wed->stage->mainfile = malloc(sizeof(hash)*(9700));
alp=popen(exe_str,"r");
while (fgets(line, 100, alp) != NULL)
{
if ( strncmp ( line, "FIRST", 5) == 0 ) {
strcpy(ptr[0]->typ,"configone");
ptr[0]->wed = malloc(64);
strcpy(ptr[0]->wed->server,"detail_one");
ptr[0]->wed->stage = malloc(24);
strcpy(ptr[0]->wed->stage->filename,"boxone");
}
if( strncmp( line, " start reading", 34 ) == 0 ) {
read = 1;
printf("match %s",line);
}
if(strncmp( line, " f_name2start/f_name2stop", 17 ) == 0){
read = 0;
printf("match end %s",line);
}
if ((strlen(line) > 1)&&(read!=0)&&(strncmp(line,"START",5)!=0)&&((ptr[0]->wed->stage->mainfile=malloc(strlen(line)+count+2))!=NULL))
{
ptr[0]->wed->stage->mainfile=(hash*)malloc(9999*sizeof(hash)*strlen(line)*(count+2));
tokens = split(line,delim);
for(i = 0; tokens[i] != NULL; i++)
{
if (i==0) {
rem_spc(tokens[i]);
ptr[0]->wed->stage->mainfile=realloc(ptr[0]->wed->stage->mainfile,(5777*strlen(tokens[i])+(count+7)));
strcpy(ptr[0]->wed->stage->mainfile[count].key,tokens[i]);
}
if (i==1){
rem_spc(tokens[i]);
ptr[0]->wed->stage->mainfile=realloc(ptr[0]->wed->stage->mainfile,(8888*strlen(tokens[i])+(count+10)));
strcpy(ptr[0]->wed->stage->mainfile[count].value,tokens[i]);
}
}
free(ptr[0]->wed->stage->mainfile);
for(i = 0; tokens[i] != NULL; i++)
free(tokens[i]);
count++;
}
if ( strncmp ( line, "filetwo", 5) == 0 ) {
strcpy(ptr[1]->typ,"configtwo");
ptr[1]->wed = malloc(strlen("detail_one")*(15)+1);
strcpy(ptr[1]->wed->server,"detail_one");
ptr[1]->wed->stage = malloc(strlen("boxone")+1);
strcpy(ptr[1]->wed->stage->filename,"boxone");
}
if ( strncmp( line, "START regis", 11 ) == 0 ) {
tab = 1;
regie++;
}
if ( strncmp( line, "ENDREGIS", 8 ) == 0){
tab = 0;
}
if ((strlen(line) > 1)&&(tab!=0)){
ptr[1]->wed->stage->mainfile=(hash *)malloc(2999*sizeof(hash)*(strlen(line))*(regie+2));
tokens = split(line, delim);
for(i = 0; tokens[i] != NULL; i++) {
if (i==0){
rem_spc(tokens[i]);
ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((7923)*strlen(tokens[i])+regie+6));
strcpy(ptr[1]->wed->stage->mainfile[regie].key,tokens[i]);
}
if (i==1){
rem_spc(tokens[i]);
ptr[1]->wed->stage->mainfile=(hash *)realloc(ptr[1]->wed->stage->mainfile,((8923)*strlen(tokens[i])+regie+8));
strcpy(ptr[1]->wed->stage->mainfile[regie].value,tokens[i]);
}
}
free(ptr[1]->wed->stage->mainfile);
regie++;
}
// start f_name2cfg2
if ( strncmp ( line, "filethree",9) == 0){
strcpy(thu[0]->t_nam,"filethree");
thu[0]->tue=malloc(24);
strcpy(thu[0]->tue->svrname,"detail_two");
thu[0]->tue->s_no = malloc(34);
strcpy(thu[0]->tue->s_no->f_name,"boxone");
}
if ( strncmp( line, "start reading", 34 ) == 0 ) {
read01 = 1;
}
if ( strncmp( line, " f_name2start/f_name2stop", 17 ) == 0){
read01 = 0;
}
if ((strlen(line) > 1)&&(read01==1)&&(strncmp(line,"START regis",11)!=0)){
thu[0]->tue->s_no->file=(f_hash *)malloc(33999*sizeof(f_hash)*(strlen(line))*(cnt+2));
tokens = split(line, delim);
for(i = 0;tokens[i]!=NULL;i++) {
if (i==0){
rem_spc(tokens[i]);
thu[0]->tue->s_no->file=(f_hash *)realloc(thu[0]->tue->s_no->file,(6999*(strlen(tokens[i]))+(cnt+7)));
strcpy(thu[0]->tue->s_no->file[cnt].key,tokens[i]);
}
if (i==1){
rem_spc(tokens[i]);
thu[0]->tue->s_no->file=(f_hash *)realloc(thu[0]->tue->s_no->file,(4555*(strlen(tokens[i]))+(cnt+10)));
strcpy(thu[0]->tue->s_no->file[cnt].value,tokens[i]);
}
}
free(thu[0]->tue->s_no->file);
for(i = 0; tokens[i] != NULL; i++)
free(tokens[i]);
cnt++;
} // add f_namemcfg
//printf("REGistry\n");
if ( strncmp(line,"START",5) == 0 ) {
strcpy(thu[1]->t_nam,"configfour");
thu[1]->tue = malloc(74);
strcpy(thu[1]->tue->svrname,"detail_two");
thu[1]->tue->s_no = malloc(80);
strcpy(thu[1]->tue->s_no->f_name,"boxone");
}
if ( strncmp( line, "START regis", 11 ) == 0 ) {
tab01 = 1;
cnt = 0;
}
if ( strncmp( line, "ENDREGIS", 8 ) == 0){
tab01 = 0;
// printf("match end %s",line);
}
thu[1]->tue->s_no->file= (f_hash *)malloc(3000*(strlen(line))+reg+2);
if ((strlen(line) > 1)&&(tab01!=0)){
rem_spc(line);
tokens = split(line, delim);
for(i = 0; tokens[i] != NULL; i++) {
if (i==0){
thu[1]->tue->s_no->file= (f_hash *)realloc(thu[1]->tue->s_no->file,(strlen(tokens[i])*(8923)+reg+6));
strcpy(thu[1]->tue->s_no->file[reg].key,tokens[i]);
}
if (i==1){
thu[1]->tue->s_no->file= (f_hash *)realloc(thu[1]->tue->s_no->file,(strlen(tokens[i])*(9400)+reg+8));
strcpy(thu[1]->tue->s_no->file[reg].value,tokens[i]);
}
}
free(thu[1]->tue->s_no->file);
reg++;
} // add for(j=0 ;j<=cnt
// just added
}
pclose(alp);
for(n=0 ; n < d; n++){
printf(" %d %d ptr[0]->wed->stage->mainfile %s %s \n",m,n,ptr[0]->wed->stage->mainfile[n].key,ptr[0]->wed->stage->mainfile[n].value);
}
for(n=0 ; n < 12; n++){
printf(" %d %d ptr[1]->wed->stage->mainfile %s %s \n",m,n,ptr[1]->wed->stage->mainfile[n].key,ptr[1]->wed->stage->mainfile[n].value);
}
for(n=0 ; n < d; n++){
printf(" %d %d thu[0]->tue->s_no->file%s %s \n",m,n,thu[0]->tue->s_no->file[n].key,thu[0]->tue->s_no->file[n].value);
}
for(n=0 ; n < 12; n++){
printf(" %d %d thu[1]->tue->s_no->file%s %s \n",m,n,thu[1]->tue->s_no->file[n].key,thu[1]->tue->s_no->file[n].value);
}
return 0;
exit(0);
}
char **split(char *string, char *delim) {
char **tokens = NULL;
char *working = NULL;
char *token = NULL;
int idx = 0;
tokens = malloc(sizeof(char *) * MAXTOKENS);
if(tokens == NULL)
return NULL;
working = malloc(sizeof(char) * strlen(string) + 1);
if(working == NULL)
return NULL;
// printf("string %s",string);
/* to make sure, copy string to a safe place */
strcpy(working, string);
for(idx = 0; idx < MAXTOKENS; idx++)
tokens[idx] = NULL;
token = strtok(working, delim);
idx = 0;
/* always keep the last entry NULL termindated */
while((idx < (MAXTOKENS - 1)) && (token != NULL)) {
tokens[idx] = malloc(sizeof(char) * strlen(token) + 1);
if(tokens[idx] != NULL) {
strcpy(tokens[idx], token);
idx++;
token = strtok(NULL, delim);
}
}
free(working);
return tokens;
}
char *rem_spc(char *buff)
{
char *ptr, *new;
register int wspace = 0;
for(ptr = buff; *ptr && ' ' >= *ptr; ++ptr);
if(ptr != buff) strcpy(buff,ptr);
for(ptr = new = buff; *ptr != '\0'; ++ptr){
if(' ' >= *ptr){ ++wspace; continue; }
if(wspace) *new++ = ' ';
*new++ = *ptr;
wspace = 0;
}
*new = 0;
return(buff);
}
please tell me where my code is wrong...