Make daemon_get_file_str return first line in specified file

The current implementation will return only the first
group of characters up to the first space from the first
line of the specified file.

This function was intended to return the entire first line.

Change-Id: Ic34361c32aeff564f4645070279cdb53d5b87626
Closes-Bug: 1896669
Signed-off-by: Eric MacDonald <eric.macdonald@windriver.com>
This commit is contained in:
Eric MacDonald 2020-09-22 18:19:24 -04:00
parent c7e18ca9e9
commit 126cdfa369
1 changed files with 9 additions and 14 deletions

View File

@ -183,33 +183,28 @@ int daemon_get_file_int ( const char * filename )
/* reads the first line of a file and returns it as a string */
string daemon_get_file_str ( const char * filename )
{
string value = "null" ;
string value = "" ;
FILE * __stream = fopen ( filename, "r" );
if ( __stream != NULL )
{
int rc ;
char buffer [MAX_CHARS_ON_LINE];
char data [MAX_CHARS_ON_LINE];
memset(buffer, 0 , MAX_CHARS_ON_LINE);
memset(data, 0 , MAX_CHARS_ON_LINE);
if ( fgets (buffer,MAX_CHARS_ON_LINE, __stream) != NULL )
{
rc = sscanf ( &buffer[0], "%s", &data[0] );
if ( rc >= 1 )
int len = strlen(buffer) ;
if ( len )
{
value = data ;
dlog ("%s contains '%s'\n", filename, value.c_str());
/* strip of a newline if that's the last character */
if ( buffer[len-1] == '\n' )
buffer[len-1] = '\0' ;
value = buffer ;
dlog("'%s' read from %s", value.c_str(), filename);
}
else
{
wlog ("failed to sscanf string from file:%s\n", filename );
wlog("no string data in %s", filename);
}
}
else
{
wlog ("failed to read string from file:%s\n", filename );
}
fclose(__stream);
}
else