From c7dcb4821a91c4c41f8ff9a80cc2d7be177a2f87 Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Thu, 12 Nov 2015 14:15:33 +0100 Subject: [PATCH] Use getdelim instead of custom fgets function. --- source/dialogs/dmenu.c | 21 ++++++++------------- source/helper.c | 26 -------------------------- source/rofi.c | 2 +- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index 68b84efa..07163b4f 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -59,27 +59,22 @@ typedef struct _DmenuModePrivateData static char **get_dmenu ( unsigned int *length ) { - const unsigned int buf_size = 1024; - char buffer[buf_size]; - char **retv = NULL; - char *buffer_end = NULL; - unsigned int rvlength = 1; + char **retv = NULL; + unsigned int rvlength = 1; *length = 0; - - while ( ( buffer_end = fgets_s ( buffer, buf_size, stdin, (char) config.separator ) ) != NULL ) { + gchar *data = NULL; + size_t data_l = 0; + while ( ( getdelim ( &data, &data_l, config.separator, stdin ) > 0 ) ) { if ( rvlength < ( *length + 2 ) ) { rvlength *= 2; retv = g_realloc ( retv, ( rvlength ) * sizeof ( char* ) ); } - size_t blength = buffer_end - &( buffer[0] ); - - char *copy = g_malloc0 ( blength + 1 ); - memcpy ( copy, buffer, blength ); - - retv[( *length )] = copy; + retv[( *length )] = data; //copy; retv[( *length ) + 1] = NULL; + data = NULL; + data_l = 0; ( *length )++; // Stop when we hit 2³¹ entries. diff --git a/source/helper.c b/source/helper.c index 7157799f..0d0070cc 100644 --- a/source/helper.c +++ b/source/helper.c @@ -55,32 +55,6 @@ void cmd_set_arguments ( int argc, char **argv ) stored_argv = argv; } -/** - * `fgets` implementation with custom separator. - */ -char* fgets_s ( char* s, unsigned int n, FILE *iop, char sep ) -{ - // Map these to registers. - register int c = EOF; - register char* cs; - cs = s; - // read until EOF or buffer is full. - while ( --n > 0 && ( c = getc ( iop ) ) != EOF ) { - // put the input char into the current pointer position, then increment it - // if a newline entered, break - if ( ( *cs++ = c ) == sep ) { - // Whipe separator - cs[-1] = '\0'; - break; - } - } - // Always, 0 terminate the buffer. - *cs = '\0'; - // if last read was end of file and current index is start, we are done: - // Return NULL. - return ( c == EOF && cs == s ) ? NULL : cs; -} - /** * @param info To Match information on. * @param res The string being generated. diff --git a/source/rofi.c b/source/rofi.c index 90a2eda1..f5f2e2aa 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -749,7 +749,7 @@ static void menu_refilter ( MenuState *state ) * If number of threads > 1 and there are enough (> 20000) items, spawn threads. * For large lists with 8 threads I see a factor three speedup of the whole function. */ - unsigned int nt = MAX( 1, MIN ( state->num_lines / 1000, config.threads )); + unsigned int nt = MAX ( 1, MIN ( state->num_lines / 1000, config.threads ) ); thread_state states[nt]; GThread *threads[nt]; unsigned int steps = ( state->num_lines + nt ) / nt;