From 96cb3a8695e35890d2f10aa508c33f2ebac40d35 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sun, 10 Apr 2016 14:30:13 +0200 Subject: [PATCH] Remove fgets and replace by getline. --- source/dialogs/run.c | 9 +++++++-- source/dialogs/script.c | 8 ++++++-- source/dialogs/ssh.c | 24 ++++++++++++++++++------ source/helper.c | 2 +- source/history.c | 14 +++++++------- test/history-test.c | 4 ++-- 6 files changed, 41 insertions(+), 20 deletions(-) diff --git a/source/dialogs/run.c b/source/dialogs/run.c index ad5d4953..00dae3fe 100644 --- a/source/dialogs/run.c +++ b/source/dialogs/run.c @@ -182,8 +182,10 @@ static char ** get_apps_external ( char **retv, unsigned int *length, unsigned i if ( fd >= 0 ) { FILE *inp = fdopen ( fd, "r" ); if ( inp ) { - char buffer[1024]; - while ( fgets ( buffer, 1024, inp ) != NULL ) { + char *buffer = NULL; + size_t buffer_length = 0; + + while ( getline ( &buffer, &buffer_length, inp ) > 0 ) { int found = 0; // Filter out line-end. if ( buffer[strlen ( buffer ) - 1] == '\n' ) { @@ -208,6 +210,9 @@ static char ** get_apps_external ( char **retv, unsigned int *length, unsigned i ( *length )++; } + if ( buffer != NULL ) { + free ( buffer ); + } if ( fclose ( inp ) != 0 ) { fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n", strerror ( errno ) ); diff --git a/source/dialogs/script.c b/source/dialogs/script.c index 667f7988..92f9129b 100644 --- a/source/dialogs/script.c +++ b/source/dialogs/script.c @@ -48,8 +48,9 @@ static char **get_script_output ( const char *command, unsigned int *length ) if ( fd >= 0 ) { FILE *inp = fdopen ( fd, "r" ); if ( inp ) { - char buffer[1024]; - while ( fgets ( buffer, 1024, inp ) != NULL ) { + char *buffer = NULL; + size_t buffer_length = 0; + while ( getline ( &buffer, &buffer_length, inp ) > 0 ) { retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) ); retv[( *length )] = g_strdup ( buffer ); retv[( *length ) + 1] = NULL; @@ -61,6 +62,9 @@ static char **get_script_output ( const char *command, unsigned int *length ) ( *length )++; } + if ( buffer ) { + free ( buffer ); + } if ( fclose ( inp ) != 0 ) { fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n", strerror ( errno ) ); diff --git a/source/dialogs/ssh.c b/source/dialogs/ssh.c index 027d8a52..95bcd080 100644 --- a/source/dialogs/ssh.c +++ b/source/dialogs/ssh.c @@ -141,9 +141,10 @@ static char **read_known_hosts_file ( char ** retv, unsigned int *length ) char *path = g_build_filename ( g_getenv ( "HOME" ), ".ssh", "known_hosts", NULL ); FILE *fd = fopen ( path, "r" ); if ( fd != NULL ) { - char buffer[1024]; + char *buffer = NULL; + size_t buffer_length = 0; // Reading one line per time. - while ( fgets ( buffer, sizeof ( buffer ), fd ) ) { + while ( getline ( &buffer, &buffer_length, fd ) > 0 ) { char *sep = strstr ( buffer, "," ); if ( sep != NULL ) { @@ -167,6 +168,9 @@ static char **read_known_hosts_file ( char ** retv, unsigned int *length ) } } } + if ( buffer != NULL ) { + free ( buffer ); + } if ( fclose ( fd ) != 0 ) { fprintf ( stderr, "Failed to close hosts file: '%s'\n", strerror ( errno ) ); } @@ -189,9 +193,10 @@ static char **read_hosts_file ( char ** retv, unsigned int *length ) // Read the hosts file. FILE *fd = fopen ( "/etc/hosts", "r" ); if ( fd != NULL ) { - char buffer[1024]; + char *buffer = NULL; + size_t buffer_length = 0; // Reading one line per time. - while ( fgets ( buffer, sizeof ( buffer ), fd ) ) { + while ( getline ( &buffer, &buffer_length, fd ) > 0 ) { // Evaluate one line. unsigned int index = 0, ti = 0; char *token = buffer; @@ -238,6 +243,9 @@ static char **read_hosts_file ( char ** retv, unsigned int *length ) index++; } while ( buffer[index] != '\0' && buffer[index] != '#' ); } + if ( buffer != NULL ) { + free ( buffer ); + } if ( fclose ( fd ) != 0 ) { fprintf ( stderr, "Failed to close hosts file: '%s'\n", strerror ( errno ) ); } @@ -281,8 +289,9 @@ static char ** get_ssh ( unsigned int *length ) fd = fopen ( path, "r" ); if ( fd != NULL ) { - char buffer[1024]; - while ( fgets ( buffer, sizeof ( buffer ), fd ) ) { + char *buffer = NULL; + size_t buffer_length = 0; + while ( getline ( &buffer, &buffer_length, fd ) > 0 ) { // Each line is either empty, a comment line starting with a '#' // character or of the form "keyword [=] arguments", where there may // be multiple (possibly quoted) arguments separated by whitespace. @@ -335,6 +344,9 @@ static char ** get_ssh ( unsigned int *length ) ( *length )++; } } + if ( buffer != NULL ) { + free ( buffer ); + } if ( fclose ( fd ) != 0 ) { fprintf ( stderr, "Failed to close ssh configuration file: '%s'\n", strerror ( errno ) ); diff --git a/source/helper.c b/source/helper.c index 955939e6..adc37d61 100644 --- a/source/helper.c +++ b/source/helper.c @@ -687,7 +687,7 @@ char * rofi_force_utf8 ( gchar *start ) /* Replacement character */ g_string_append ( string, "\uFFFD" ); length -= ( end - data ) + 1; - data = end + 1; + data = end + 1; } while ( !g_utf8_validate ( data, length, &end ) ); if ( length ) { diff --git a/source/history.c b/source/history.c index 612f0e66..094b9bcc 100644 --- a/source/history.c +++ b/source/history.c @@ -84,10 +84,10 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length ) if ( fd == NULL ) { return NULL; } - char *buffer = NULL; - size_t buffer_length = 0; - ssize_t l = 0; - while ( (l = getline ( &buffer, &buffer_length, fd )) > 0 ) { + char *buffer = NULL; + size_t buffer_length = 0; + ssize_t l = 0; + while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) { char * start = NULL; // Skip empty lines. if ( strlen ( buffer ) == 0 ) { @@ -101,9 +101,9 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length ) buffer[strlen ( buffer ) - 1] = '\0'; // Parse the number of times. retv[( *length )]->index = strtol ( buffer, &start, 10 ); - retv[( *length )]->name = g_strndup(start+1, l-1-(start+1-buffer)); + retv[( *length )]->name = g_strndup ( start + 1, l - 1 - ( start + 1 - buffer ) ); // Force trailing '\0' - retv[( *length ) + 1] = NULL; + retv[( *length ) + 1] = NULL; ( *length )++; } @@ -152,7 +152,7 @@ void history_set ( const char *filename, const char *entry ) list[length] = g_malloc ( sizeof ( _element ) ); // Copy name if ( list[length] != NULL ) { - list[length]->name = g_strdup(entry); + list[length]->name = g_strdup ( entry ); // set # hits list[length]->index = 1; diff --git a/test/history-test.c b/test/history-test.c index 75c5d7ac..2a489ac3 100644 --- a/test/history-test.c +++ b/test/history-test.c @@ -79,8 +79,8 @@ static void history_test ( void ) TASSERT ( retv != NULL ); TASSERT ( length == 25 ); for ( unsigned int in = 0; in < 24; in++ ) { - char *p = g_strdup_printf ( "aap%i", in+2); - TASSERT ( g_strcmp0(retv[in], p) == 0); + char *p = g_strdup_printf ( "aap%i", in + 2 ); + TASSERT ( g_strcmp0 ( retv[in], p ) == 0 ); g_free ( p ); }