From 126cdfa36911d52995ee6fff7fbafdc52d8ae1d8 Mon Sep 17 00:00:00 2001 From: Eric MacDonald Date: Tue, 22 Sep 2020 18:19:24 -0400 Subject: [PATCH] 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 --- mtce-common/src/daemon/daemon_files.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/mtce-common/src/daemon/daemon_files.cpp b/mtce-common/src/daemon/daemon_files.cpp index 653b2ef2..8272e7a8 100755 --- a/mtce-common/src/daemon/daemon_files.cpp +++ b/mtce-common/src/daemon/daemon_files.cpp @@ -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