1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-25 13:55:34 -05:00

Remove fgets and replace by getline.

This commit is contained in:
Dave Davenport 2016-04-10 14:30:13 +02:00
parent b63f8e2275
commit 96cb3a8695
6 changed files with 41 additions and 20 deletions

View file

@ -182,8 +182,10 @@ static char ** get_apps_external ( char **retv, unsigned int *length, unsigned i
if ( fd >= 0 ) { if ( fd >= 0 ) {
FILE *inp = fdopen ( fd, "r" ); FILE *inp = fdopen ( fd, "r" );
if ( inp ) { if ( inp ) {
char buffer[1024]; char *buffer = NULL;
while ( fgets ( buffer, 1024, inp ) != NULL ) { size_t buffer_length = 0;
while ( getline ( &buffer, &buffer_length, inp ) > 0 ) {
int found = 0; int found = 0;
// Filter out line-end. // Filter out line-end.
if ( buffer[strlen ( buffer ) - 1] == '\n' ) { if ( buffer[strlen ( buffer ) - 1] == '\n' ) {
@ -208,6 +210,9 @@ static char ** get_apps_external ( char **retv, unsigned int *length, unsigned i
( *length )++; ( *length )++;
} }
if ( buffer != NULL ) {
free ( buffer );
}
if ( fclose ( inp ) != 0 ) { if ( fclose ( inp ) != 0 ) {
fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n", fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n",
strerror ( errno ) ); strerror ( errno ) );

View file

@ -48,8 +48,9 @@ static char **get_script_output ( const char *command, unsigned int *length )
if ( fd >= 0 ) { if ( fd >= 0 ) {
FILE *inp = fdopen ( fd, "r" ); FILE *inp = fdopen ( fd, "r" );
if ( inp ) { if ( inp ) {
char buffer[1024]; char *buffer = NULL;
while ( fgets ( buffer, 1024, inp ) != NULL ) { size_t buffer_length = 0;
while ( getline ( &buffer, &buffer_length, inp ) > 0 ) {
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) ); retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
retv[( *length )] = g_strdup ( buffer ); retv[( *length )] = g_strdup ( buffer );
retv[( *length ) + 1] = NULL; retv[( *length ) + 1] = NULL;
@ -61,6 +62,9 @@ static char **get_script_output ( const char *command, unsigned int *length )
( *length )++; ( *length )++;
} }
if ( buffer ) {
free ( buffer );
}
if ( fclose ( inp ) != 0 ) { if ( fclose ( inp ) != 0 ) {
fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n", fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n",
strerror ( errno ) ); strerror ( errno ) );

View file

@ -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 ); char *path = g_build_filename ( g_getenv ( "HOME" ), ".ssh", "known_hosts", NULL );
FILE *fd = fopen ( path, "r" ); FILE *fd = fopen ( path, "r" );
if ( fd != NULL ) { if ( fd != NULL ) {
char buffer[1024]; char *buffer = NULL;
size_t buffer_length = 0;
// Reading one line per time. // Reading one line per time.
while ( fgets ( buffer, sizeof ( buffer ), fd ) ) { while ( getline ( &buffer, &buffer_length, fd ) > 0 ) {
char *sep = strstr ( buffer, "," ); char *sep = strstr ( buffer, "," );
if ( sep != NULL ) { 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 ) { if ( fclose ( fd ) != 0 ) {
fprintf ( stderr, "Failed to close hosts file: '%s'\n", strerror ( errno ) ); 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. // Read the hosts file.
FILE *fd = fopen ( "/etc/hosts", "r" ); FILE *fd = fopen ( "/etc/hosts", "r" );
if ( fd != NULL ) { if ( fd != NULL ) {
char buffer[1024]; char *buffer = NULL;
size_t buffer_length = 0;
// Reading one line per time. // Reading one line per time.
while ( fgets ( buffer, sizeof ( buffer ), fd ) ) { while ( getline ( &buffer, &buffer_length, fd ) > 0 ) {
// Evaluate one line. // Evaluate one line.
unsigned int index = 0, ti = 0; unsigned int index = 0, ti = 0;
char *token = buffer; char *token = buffer;
@ -238,6 +243,9 @@ static char **read_hosts_file ( char ** retv, unsigned int *length )
index++; index++;
} while ( buffer[index] != '\0' && buffer[index] != '#' ); } while ( buffer[index] != '\0' && buffer[index] != '#' );
} }
if ( buffer != NULL ) {
free ( buffer );
}
if ( fclose ( fd ) != 0 ) { if ( fclose ( fd ) != 0 ) {
fprintf ( stderr, "Failed to close hosts file: '%s'\n", strerror ( errno ) ); 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" ); fd = fopen ( path, "r" );
if ( fd != NULL ) { if ( fd != NULL ) {
char buffer[1024]; char *buffer = NULL;
while ( fgets ( buffer, sizeof ( buffer ), fd ) ) { size_t buffer_length = 0;
while ( getline ( &buffer, &buffer_length, fd ) > 0 ) {
// Each line is either empty, a comment line starting with a '#' // Each line is either empty, a comment line starting with a '#'
// character or of the form "keyword [=] arguments", where there may // character or of the form "keyword [=] arguments", where there may
// be multiple (possibly quoted) arguments separated by whitespace. // be multiple (possibly quoted) arguments separated by whitespace.
@ -335,6 +344,9 @@ static char ** get_ssh ( unsigned int *length )
( *length )++; ( *length )++;
} }
} }
if ( buffer != NULL ) {
free ( buffer );
}
if ( fclose ( fd ) != 0 ) { if ( fclose ( fd ) != 0 ) {
fprintf ( stderr, "Failed to close ssh configuration file: '%s'\n", strerror ( errno ) ); fprintf ( stderr, "Failed to close ssh configuration file: '%s'\n", strerror ( errno ) );

View file

@ -687,7 +687,7 @@ char * rofi_force_utf8 ( gchar *start )
/* Replacement character */ /* Replacement character */
g_string_append ( string, "\uFFFD" ); g_string_append ( string, "\uFFFD" );
length -= ( end - data ) + 1; length -= ( end - data ) + 1;
data = end + 1; data = end + 1;
} while ( !g_utf8_validate ( data, length, &end ) ); } while ( !g_utf8_validate ( data, length, &end ) );
if ( length ) { if ( length ) {

View file

@ -84,10 +84,10 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
if ( fd == NULL ) { if ( fd == NULL ) {
return NULL; return NULL;
} }
char *buffer = NULL; char *buffer = NULL;
size_t buffer_length = 0; size_t buffer_length = 0;
ssize_t l = 0; ssize_t l = 0;
while ( (l = getline ( &buffer, &buffer_length, fd )) > 0 ) { while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
char * start = NULL; char * start = NULL;
// Skip empty lines. // Skip empty lines.
if ( strlen ( buffer ) == 0 ) { if ( strlen ( buffer ) == 0 ) {
@ -101,9 +101,9 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
buffer[strlen ( buffer ) - 1] = '\0'; buffer[strlen ( buffer ) - 1] = '\0';
// Parse the number of times. // Parse the number of times.
retv[( *length )]->index = strtol ( buffer, &start, 10 ); 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' // Force trailing '\0'
retv[( *length ) + 1] = NULL; retv[( *length ) + 1] = NULL;
( *length )++; ( *length )++;
} }
@ -152,7 +152,7 @@ void history_set ( const char *filename, const char *entry )
list[length] = g_malloc ( sizeof ( _element ) ); list[length] = g_malloc ( sizeof ( _element ) );
// Copy name // Copy name
if ( list[length] != NULL ) { if ( list[length] != NULL ) {
list[length]->name = g_strdup(entry); list[length]->name = g_strdup ( entry );
// set # hits // set # hits
list[length]->index = 1; list[length]->index = 1;

View file

@ -79,8 +79,8 @@ static void history_test ( void )
TASSERT ( retv != NULL ); TASSERT ( retv != NULL );
TASSERT ( length == 25 ); TASSERT ( length == 25 );
for ( unsigned int in = 0; in < 24; in++ ) { for ( unsigned int in = 0; in < 24; in++ ) {
char *p = g_strdup_printf ( "aap%i", in+2); char *p = g_strdup_printf ( "aap%i", in + 2 );
TASSERT ( g_strcmp0(retv[in], p) == 0); TASSERT ( g_strcmp0 ( retv[in], p ) == 0 );
g_free ( p ); g_free ( p );
} }