I am developing a Linux program that receives some user parameters by the command line.
The user interface was made in C and data processing is in Fortran, so the main function in C passes some parameters to the Fortran subroutine. Some of these parameters are strings and it is with them that I am having problems. Here are some parts of the codes:
The C main function:
extern void anaconv_(char *FileName,int *n,char *PlanName,int *m,char *TipoDados,char *FrontName,int *l,char *Dirdat, int *h, bool *kvuge, bool *ch_serie, bool *ch_shuntb, bool *simulacao);
void ajuda_anaconv(void);
int main (int argc, char **argv)
{
int i;
int n,m,l,h;
bool flagk = 0;
bool flags = 0;
bool flagp = 0;
bool flagsimul = 0;
char *dirpwf = malloc(sizeof(char)*80);
char *dirbar = malloc(sizeof(char)*80);
char *dirfro = malloc(sizeof(char)*80);
char *dirdat = malloc(sizeof(char)*500);
char *tpdados = malloc(sizeof(char));
...
dirpwf = argv[i+1];
i++;
...
dirbar = argv[i+1];
i++;
...
dirfro = argv[i+1];
i++;
...
dirdat = argv[i+1];
i++;
...
tpdados = argv[i+1];
i++;
...
n = strlen(dirpwf)
m = strlen(dirbar)
l = strlen(dirfro)
h = strlen(dirdat)
...
anaconv_(dirpwf,&n,dirbar,&m,tpdados,dirfro,&l,dirdat,&h,&flagk,&flags,&flagp,&flagsimul);
...
free(dirpwf);
free(dirbar);
free(dirfro);
free(dirdat);
free(dirdatcomp);
free(tpdados);
...
return 1;
}
And them, the Fortran subroutine is something like:
SUBROUTINE ANACONV
+(FileName,FileNameSize,PlanName,PlanNameSize,TpData,FrontName,FrontNameSize,Dirdat,DirdatSize,kVUGE,CHSERIE,CHSHUNTB,SIMUL)
IMPLICIT NONE
CHARACTER*80 FileName,PlanName,FrontName
CHARACTER*500 Dirdat
INTEGER*4 FileNameSize,PlanNameSize,FrontNameSize,DirdatSize
CHARACTER*1 TpData
LOGICAL*1 kVUGE,CHSERIE,CHSHUNTB,SIMUL
...
END SUBROUTINE ANACONV
The compiler is gcc version 4.8.5 20150623 and I'm running the program in a CentOS Linux release 7.3.1611.
Although the code is compiling without problems, when I run the program passing some parameters, I get the following message:
Fortran runtime error: Actual string length is shorter than the declared one for dummy argument 'filename' (-136420716/80)
I tried to find the problem debugging with gdb, but I could not find the error. The FileName
string gets the correct value from the dirpwf
pointer of the main function in C, and the string size stored in the FileNameSize
variable is also correct, whereas Fortran understands that the received size (apparently -136420716) is less than expected (80 ).
How can I fix this?