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 ) {
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 ) );

View file

@ -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 ) );

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 );
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 ) );