diff --git a/AmberTools/src/nab/nab.c b/AmberTools/src/nab/nab.c index 5c80b67..506972a 100644 --- a/AmberTools/src/nab/nab.c +++ b/AmberTools/src/nab/nab.c @@ -200,7 +200,8 @@ char nfmask[]; { int ac; char *dotp, *amberhome; - char cmd[ 1024 ], word[ 1024 ]; + char *cmd, word[ 1024 ]; + int cmd_sz; int status; amberhome = (char *) getenv("AMBERHOME"); @@ -208,20 +209,32 @@ char nfmask[]; fprintf( stderr, "AMBERHOME is not set!\n" ); exit(1); } + cmd_sz = 1024; + cmd = malloc(cmd_sz); sprintf( cmd, "%s -I%s/include", CC, amberhome ); for( ac = 1; ac < argc; ac++ ){ + word[0] = '\0'; if( nfmask[ ac ] ){ dotp = strrchr( argv[ ac ], '.' ); strcpy( &dotp[ 1 ], "c" ); sprintf( word, " %s", argv[ ac ] ); - strcat( cmd, word ); }else if( argv[ ac ] ){ sprintf( word, " %s", argv[ ac ] ); - strcat( cmd, word ); + } + if (strlen(cmd) + strlen(word) + 1 > cmd_sz) { + cmd_sz += 1024; + cmd = realloc(cmd, cmd_sz); + } + if (strlen(word) > 0) { + strcat( cmd, word ); } } if( !copt ){ sprintf( word, " -L%s/lib -lnab -lcifparse", amberhome ); + if (strlen(cmd) + strlen(word) + strlen(FLIBS) + 7 > cmd_sz) { + cmd_sz += strlen(word) + strlen(FLIBS) + 7; + cmd = realloc(cmd, cmd_sz); + } strcat( cmd, word ); sprintf( word, " %s ", FLIBS ); strcat( cmd, word ); @@ -232,4 +245,5 @@ char nfmask[]; if (status != 0) { fprintf(stderr,"cc failed!\n"); exit (1); } + free(cmd); }