Sorry if this has been addressed in previous posts - I quickly searched, but couldn't fin and answer ...
I'm statically linking a C library to existing fortran77 code. I need to pass, from a higher-level C function, a f77 string to copy over to the C side. I've written this function that accepts as arguments:
1. a native f77 string
2. the address of a pointer (previously NULLed) to a C string (to fill in)
3. the 'lnblnk' of the original f77 string (i.e. the position of the last non-blank character = strlen)
4. error flagging and messaging variables
--
int AddNull( char* f77string, char** cstring, int f77length, int* eflg, char* emsg )
/*
objective : make C copy of native f77 string, replace first blank character
after last non-blank character (starting from the end) with NULL
requirements : f77string, native f77 character string
cstring, C copy
f77length, defined string length in f77
eflg, error handle
emsg, error message
FAIL, YUP, NOPE
input : f77string, cstring, f77length, eflg, emsg
constants : FAIL, YUP, NOPE (defined in std_constants.h)
globals : none
output : if successful, position of NULL character within string [0 to ~]; else, return 0
side effects : if assumptions OK, cstring is dynamically allocated and NULL terminated, else eflg = YUP
if string does not contain at least one blank character, one will be added at cstring[f77length+1]
assumptions : f77length > 0; cstring == NULL
*/
{
int i, answer, found, size;
char* temp = NULL;
char* string = NULL;
found = NOPE;
answer = 0;
*eflg = NOPE;
if( f77length > 0 && *cstring == NULL )
{
// assumptions OK
size = f77length + 1;
temp = malloc( sizeof( char ) * size );
string = malloc( sizeof( char ) * size );
if( temp != NULL && string != NULL )
{
// memory allocation OK
i = size - 2;
while( i >= 0 )
{
if( i == 0 && found == NOPE )
{
temp = '\0';
answer = i;
}
else
{
temp = f77string;
if( found == NOPE )
{
found = YUP;
temp[i+1] = '\0';
answer = i + 1;
}
}
i--;
}
/* string may have leading blank chars - to eliminate */
while( temp[0] == ' ' )
{
strcpy( string, temp + 1 );
strcpy( temp, string );
}
}
else
{
*eflg = YUP;
strcpy( emsg, "AddNull failed: insufficient memory\n" );
}
}
else
{
*eflg = YUP;
strcpy( emsg, "AddNull failed: invalid input\n" );
}
*cstring = temp;
temp = NULL;
string = NULL;
return answer;
}
--
If I actually printf the content of *cstring before exiting the function, it works fine, but for certain strings, it segfaults when exiting the function. I'm not sure how to proceed from here - any suggestions? Many thanks.
I'm statically linking a C library to existing fortran77 code. I need to pass, from a higher-level C function, a f77 string to copy over to the C side. I've written this function that accepts as arguments:
1. a native f77 string
2. the address of a pointer (previously NULLed) to a C string (to fill in)
3. the 'lnblnk' of the original f77 string (i.e. the position of the last non-blank character = strlen)
4. error flagging and messaging variables
--
int AddNull( char* f77string, char** cstring, int f77length, int* eflg, char* emsg )
/*
objective : make C copy of native f77 string, replace first blank character
after last non-blank character (starting from the end) with NULL
requirements : f77string, native f77 character string
cstring, C copy
f77length, defined string length in f77
eflg, error handle
emsg, error message
FAIL, YUP, NOPE
input : f77string, cstring, f77length, eflg, emsg
constants : FAIL, YUP, NOPE (defined in std_constants.h)
globals : none
output : if successful, position of NULL character within string [0 to ~]; else, return 0
side effects : if assumptions OK, cstring is dynamically allocated and NULL terminated, else eflg = YUP
if string does not contain at least one blank character, one will be added at cstring[f77length+1]
assumptions : f77length > 0; cstring == NULL
*/
{
int i, answer, found, size;
char* temp = NULL;
char* string = NULL;
found = NOPE;
answer = 0;
*eflg = NOPE;
if( f77length > 0 && *cstring == NULL )
{
// assumptions OK
size = f77length + 1;
temp = malloc( sizeof( char ) * size );
string = malloc( sizeof( char ) * size );
if( temp != NULL && string != NULL )
{
// memory allocation OK
i = size - 2;
while( i >= 0 )
{
if( i == 0 && found == NOPE )
{
temp = '\0';
answer = i;
}
else
{
temp = f77string;
if( found == NOPE )
{
found = YUP;
temp[i+1] = '\0';
answer = i + 1;
}
}
i--;
}
/* string may have leading blank chars - to eliminate */
while( temp[0] == ' ' )
{
strcpy( string, temp + 1 );
strcpy( temp, string );
}
}
else
{
*eflg = YUP;
strcpy( emsg, "AddNull failed: insufficient memory\n" );
}
}
else
{
*eflg = YUP;
strcpy( emsg, "AddNull failed: invalid input\n" );
}
*cstring = temp;
temp = NULL;
string = NULL;
return answer;
}
--
If I actually printf the content of *cstring before exiting the function, it works fine, but for certain strings, it segfaults when exiting the function. I'm not sure how to proceed from here - any suggestions? Many thanks.