1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-04-07 17:33:14 -04:00

Move to glib's based memory allocation functions

* replace calloc by malloc0_n
        * malloc by g_malloc
        * strdup by g_strdup
        * asprintf by g_strdup_printf
        * realloc by g_realloc
        * free loop by g_strfreev (if possible)
This commit is contained in:
QC 2014-08-09 11:40:42 +02:00
parent 9ac8f408f4
commit 98dfbddadb
8 changed files with 178 additions and 249 deletions

View file

@ -46,12 +46,8 @@ static char **get_dmenu ( unsigned int *length )
*length = 0;
while ( fgets ( buffer, 1024, stdin ) != NULL ) {
char **tr = realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
if ( tr == NULL ) {
return retv;
}
retv = tr;
retv[( *length )] = strdup ( buffer );
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
retv[( *length )] = g_strdup ( buffer );
retv[( *length ) + 1] = NULL;
// Filter out line-end.
@ -103,13 +99,9 @@ int dmenu_switcher_dialog ( char **input )
retv = TRUE;
}
} while ( restart );
for ( unsigned int i = 0; i < length; i++ ) {
free ( list[i] );
}
if ( list != NULL ) {
free ( list );
}
g_strfreev ( list );
return retv;
}

View file

@ -89,13 +89,9 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
continue;
}
// Resize and check.
_element **tr = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
if ( tr == NULL ) {
return retv;
}
retv = tr;
retv = g_realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
retv[( *length )] = malloc ( sizeof ( _element ) );
retv[( *length )] = g_malloc ( sizeof ( _element ) );
// remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0';
// Parse the number of times.
@ -144,20 +140,17 @@ void history_set ( const char *filename, const char *entry )
else{
// If not exists, add it.
// Increase list by one
_element **tr = realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
if ( tr != NULL ) {
list = tr;
list[length] = malloc ( sizeof ( _element ) );
// Copy name
if ( list[length] != NULL ) {
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits
list[length]->index = 1;
list = g_realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
list[length] = g_malloc ( sizeof ( _element ) );
// Copy name
if ( list[length] != NULL ) {
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits
list[length]->index = 1;
length++;
list[length] = NULL;
}
length++;
list[length] = NULL;
}
}
@ -173,9 +166,9 @@ void history_set ( const char *filename, const char *entry )
}
// Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) {
free ( list[iter] );
g_free ( list[iter] );
}
free ( list );
g_free ( list );
// Close file.
fclose ( fd );
}
@ -209,7 +202,7 @@ void history_remove ( const char *filename, const char *entry )
// If found, remove it and write out new file.
if ( found ) {
// Remove the entry.
free ( list[curr] );
g_free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets).
list[curr] = list[length - 1];
// Empty last.
@ -230,10 +223,10 @@ void history_remove ( const char *filename, const char *entry )
// Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) {
free ( list[iter] );
g_free ( list[iter] );
}
if ( list != NULL ) {
free ( list );
g_free ( list );
}
// Close file.
fclose ( fd );
@ -262,13 +255,13 @@ char ** history_get_list ( const char *filename, unsigned int *length )
// Copy list in right format.
// Lists are always short, so performance should not be an issue.
if ( ( *length ) > 0 ) {
retv = malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
retv = g_malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
for ( int iter = 0; iter < ( *length ); iter++ ) {
retv[iter] = strdup ( list[iter]->name );
free ( list[iter] );
retv[iter] = g_strdup ( list[iter]->name );
g_free ( list[iter] );
}
retv[( *length )] = NULL;
free ( list );
g_free ( list );
}
fclose ( fd );

View file

@ -167,25 +167,25 @@ static char **tokenize ( const char *input )
int num_tokens = 0;
// Copy the string, 'strtok_r' modifies it.
char *str = strdup ( input );
char *str = g_strdup ( input );
// Iterate over tokens.
// strtok should still be valid for utf8.
for ( token = strtok_r ( str, " ", &saveptr );
token != NULL;
token = strtok_r ( NULL, " ", &saveptr ) ) {
char **tr = realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) );
if ( tr != NULL ) {
char *tmp = g_utf8_casefold ( token, -1 );
retv = tr;
retv[num_tokens + 1] = NULL;
retv[num_tokens] = g_utf8_collate_key ( tmp, -1 );
num_tokens++;
g_free ( tmp );
}
// Get case insensitive version of the string.
char *tmp = g_utf8_casefold ( token, -1 );
retv = g_realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) );
retv[num_tokens + 1] = NULL;
// Create compare key from the case insensitive version.
retv[num_tokens] = g_utf8_collate_key ( tmp, -1 );
num_tokens++;
g_free ( tmp );
}
// Free str.
free ( str );
g_free ( str );
return retv;
}
@ -199,7 +199,7 @@ static inline void tokenize_free ( char **ip )
for ( int i = 0; ip[i] != NULL; i++ ) {
g_free ( ip[i] );
}
free ( ip );
g_free ( ip );
}
#ifdef HAVE_I3_IPC_H
@ -360,17 +360,17 @@ winlist *cache_xattr = NULL;
winlist* winlist_new ()
{
winlist *l = malloc ( sizeof ( winlist ) );
winlist *l = g_malloc ( sizeof ( winlist ) );
l->len = 0;
l->array = malloc ( sizeof ( Window ) * ( WINLIST + 1 ) );
l->data = malloc ( sizeof ( void* ) * ( WINLIST + 1 ) );
l->array = g_malloc_n ( WINLIST + 1, sizeof ( Window ) );
l->data = g_malloc_n ( WINLIST + 1, sizeof ( void* ) );
return l;
}
int winlist_append ( winlist *l, Window w, void *d )
{
if ( l->len > 0 && !( l->len % WINLIST ) ) {
l->array = realloc ( l->array, sizeof ( Window ) * ( l->len + WINLIST + 1 ) );
l->data = realloc ( l->data, sizeof ( void* ) * ( l->len + WINLIST + 1 ) );
l->array = g_realloc ( l->array, sizeof ( Window ) * ( l->len + WINLIST + 1 ) );
l->data = g_realloc ( l->data, sizeof ( void* ) * ( l->len + WINLIST + 1 ) );
}
// Make clang-check happy.
// TODO: make clang-check clear this should never be 0.
@ -385,15 +385,15 @@ int winlist_append ( winlist *l, Window w, void *d )
void winlist_empty ( winlist *l )
{
while ( l->len > 0 ) {
free ( l->data[--( l->len )] );
g_free ( l->data[--( l->len )] );
}
}
void winlist_free ( winlist *l )
{
winlist_empty ( l );
free ( l->array );
free ( l->data );
free ( l );
g_free ( l->array );
g_free ( l->data );
g_free ( l );
}
int winlist_find ( winlist *l, Window w )
{
@ -432,7 +432,7 @@ typedef struct
// malloc a pixel value for an X named color
// g_malloc a pixel value for an X named color
static unsigned int color_get ( Display *display, const char *const name )
{
int screen_id = DefaultScreen ( display );
@ -485,14 +485,14 @@ XWindowAttributes* window_get_attributes ( Window w )
int idx = winlist_find ( cache_xattr, w );
if ( idx < 0 ) {
XWindowAttributes *cattr = malloc ( sizeof ( XWindowAttributes ) );
XWindowAttributes *cattr = g_malloc ( sizeof ( XWindowAttributes ) );
if ( XGetWindowAttributes ( display, w, cattr ) ) {
winlist_append ( cache_xattr, w, cattr );
return cattr;
}
free ( cattr );
g_free ( cattr );
return NULL;
}
@ -552,14 +552,14 @@ char* window_get_text_prop ( Window w, Atom atom )
if ( XGetTextProperty ( display, w, &prop, atom ) && prop.value && prop.nitems ) {
if ( prop.encoding == XA_STRING ) {
res = malloc ( strlen ( ( char * ) prop.value ) + 1 );
res = g_malloc ( strlen ( ( char * ) prop.value ) + 1 );
// make clang-check happy.
if ( res ) {
strcpy ( res, ( char * ) prop.value );
}
}
else if ( Xutf8TextPropertyToTextList ( display, &prop, &list, &count ) >= Success && count > 0 && *list ) {
res = malloc ( strlen ( *list ) + 1 );
res = g_malloc ( strlen ( *list ) + 1 );
// make clang-check happy.
if ( res ) {
strcpy ( res, *list );
@ -688,7 +688,7 @@ client* window_client ( Window win )
return NULL;
}
client *c = calloc ( 1, sizeof ( client ) );
client *c = g_malloc0 ( sizeof ( client ) );
c->window = win;
// copy xattr so we don't have to care when stuff is freed
@ -701,7 +701,7 @@ client* window_client ( Window win )
if ( ( name = window_get_text_prop ( c->window, netatoms[_NET_WM_NAME] ) ) && name ) {
snprintf ( c->title, CLIENTTITLE, "%s", name );
free ( name );
g_free ( name );
}
else if ( XFetchName ( display, c->window, &name ) ) {
snprintf ( c->title, CLIENTTITLE, "%s", name );
@ -1084,7 +1084,7 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
textbox_show ( prompt_tb );
// filtered list display
textbox **boxes = calloc ( 1, sizeof ( textbox* ) * max_elements );
textbox **boxes = g_malloc0_n ( max_elements, sizeof ( textbox* ) );
for ( i = 0; i < max_elements; i++ ) {
int line = ( i ) % max_rows + ( ( config.hmode == FALSE ) ? 1 : 0 );
@ -1128,11 +1128,11 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
}
// filtered list
char **filtered = calloc ( num_lines, sizeof ( char* ) );
int *line_map = calloc ( num_lines, sizeof ( int ) );
char **filtered = g_malloc0_n ( num_lines, sizeof ( char* ) );
int *line_map = g_malloc0_n ( num_lines, sizeof ( int ) );
int *distance = NULL;
if ( sorting ) {
distance = calloc ( num_lines, sizeof ( int ) );
distance = g_malloc0_n ( num_lines, sizeof ( int ) );
}
unsigned int filtered_lines = 0;
// We want to filter on the first run.
@ -1476,9 +1476,9 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
release_keyboard ();
}
free ( *input );
g_free ( *input );
*input = strdup ( text->text );
*input = g_strdup ( text->text );
textbox_free ( text );
textbox_free ( prompt_tb );
@ -1489,11 +1489,11 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom
textbox_free ( boxes[i] );
}
free ( boxes );
g_free ( boxes );
free ( filtered );
free ( line_map );
free ( distance );
g_free ( filtered );
g_free ( line_map );
g_free ( distance );
return retv;
}
@ -1569,7 +1569,7 @@ SwitcherMode run_switcher_window ( char **input, void *data )
#ifdef HAVE_I3_IPC_H
}
#endif
char **list = calloc ( ( ids->len + 1 ), sizeof ( char* ) );
char **list = g_malloc0_n ( ( ids->len + 1 ), sizeof ( char* ) );
unsigned int lines = 0;
// build the actual list
@ -1583,7 +1583,7 @@ SwitcherMode run_switcher_window ( char **input, void *data )
unsigned long wmdesktop;
char desktop[5];
desktop[0] = 0;
char *line = malloc ( strlen ( c->title ) + strlen ( c->class ) + classfield + 50 );
char *line = g_malloc ( strlen ( c->title ) + strlen ( c->class ) + classfield + 50 );
#ifdef HAVE_I3_IPC_H
if ( !config_i3_mode ) {
#endif
@ -1642,10 +1642,10 @@ SwitcherMode run_switcher_window ( char **input, void *data )
for ( i = 0; i < lines; i++ ) {
free ( list[i] );
g_free ( list[i] );
}
free ( list );
g_free ( list );
winlist_free ( ids );
}
@ -1664,7 +1664,7 @@ static int run_dmenu ()
// Dmenu modi has a return state.
ret_state = dmenu_switcher_dialog ( &input );
free ( input );
g_free ( input );
// Cleanup font setup.
textbox_cleanup ();
@ -1715,7 +1715,7 @@ static void run_switcher ( int do_fork, SwitcherMode mode )
}
} while ( mode != MODE_EXIT );
}
free ( input );
g_free ( input );
// Cleanup font setup.
textbox_cleanup ();
@ -1958,7 +1958,7 @@ static void cleanup ()
#ifdef HAVE_I3_IPC_H
if ( i3_socket_path != NULL ) {
free ( i3_socket_path );
g_free ( i3_socket_path );
}
#endif
@ -1976,7 +1976,7 @@ static void cleanup ()
script_switcher_free_options ( switchers[i].cb_data );
}
}
free ( switchers );
g_free ( switchers );
}
/**
@ -2013,27 +2013,27 @@ static void config_sanity_check ( void )
static void setup_switchers ( void )
{
char *savept;
char *switcher_str = strdup ( config.switchers );
char *switcher_str = g_strdup ( config.switchers );
char *token;
for ( token = strtok_r ( switcher_str, ",", &savept );
token != NULL;
token = strtok_r ( NULL, ",", &savept ) ) {
if ( strcasecmp ( token, "window" ) == 0 ) {
switchers = (Switcher *) realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
switchers = (Switcher *) g_realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
copy_string ( switchers[num_switchers].name, "window", 32 );
switchers[num_switchers].cb = run_switcher_window;
switchers[num_switchers].cb_data = NULL;
num_switchers++;
}
else if ( strcasecmp ( token, "ssh" ) == 0 ) {
switchers = (Switcher *) realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
switchers = (Switcher *) g_realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
copy_string ( switchers[num_switchers].name, "ssh", 32 );
switchers[num_switchers].cb = ssh_switcher_dialog;
switchers[num_switchers].cb_data = NULL;
num_switchers++;
}
else if ( strcasecmp ( token, "run" ) == 0 ) {
switchers = (Switcher *) realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
switchers = (Switcher *) g_realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
copy_string ( switchers[num_switchers].name, "run", 32 );
switchers[num_switchers].cb = run_switcher_dialog;
switchers[num_switchers].cb_data = NULL;
@ -2042,7 +2042,7 @@ static void setup_switchers ( void )
else {
ScriptOptions *sw = script_switcher_parse_setup ( token );
if ( sw != NULL ) {
switchers = (Switcher *) realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
switchers = (Switcher *) g_realloc ( switchers, sizeof ( Switcher ) * ( num_switchers + 1 ) );
copy_string ( switchers[num_switchers].name, sw->name, 32 );
switchers[num_switchers].cb = script_switcher_dialog;
switchers[num_switchers].cb_data = sw;
@ -2055,7 +2055,7 @@ static void setup_switchers ( void )
}
}
free ( switcher_str );
g_free ( switcher_str );
}

View file

@ -78,13 +78,11 @@ static pid_t exec_cmd ( const char *cmd, int run_in_term )
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) {
return -1;
}
char *path = g_strdup_printf ( "%s/%s", cache_dir, RUN_CACHE_FILE );
history_set ( path, cmd );
free ( path );
g_free ( path );
return pid;
}
@ -95,13 +93,11 @@ static void delete_entry ( const char *cmd )
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) {
return;
}
char *path = g_strdup_printf ( "%s/%s", cache_dir, RUN_CACHE_FILE );
history_remove ( path, cmd );
free ( path );
g_free ( path );
}
static int sort_func ( const void *a, const void *b )
{
@ -124,15 +120,14 @@ static char ** get_apps ( unsigned int *length )
}
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0 ) {
retv = history_get_list ( path, length );
free ( path );
// Keep track of how many where loaded as favorite.
num_favorites = ( *length );
}
path = g_strdup_printf ( "%s/%s", cache_dir, RUN_CACHE_FILE );
retv = history_get_list ( path, length );
g_free ( path );
// Keep track of how many where loaded as favorite.
num_favorites = ( *length );
path = strdup ( getenv ( "PATH" ) );
path = g_strdup ( getenv ( "PATH" ) );
for ( const char *dirname = strtok ( path, ":" );
dirname != NULL;
@ -167,13 +162,10 @@ static char ** get_apps ( unsigned int *length )
continue;
}
char ** tr = realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
if ( tr != NULL ) {
retv = tr;
retv[( *length )] = strdup ( dent->d_name );
retv[( *length ) + 1] = NULL;
( *length )++;
}
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
retv[( *length )] = g_strdup ( dent->d_name );
retv[( *length ) + 1] = NULL;
( *length )++;
}
closedir ( dir );
@ -184,7 +176,7 @@ static char ** get_apps ( unsigned int *length )
if ( ( *length ) > num_favorites ) {
qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func );
}
free ( path );
g_free ( path );
#ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop );
@ -208,8 +200,8 @@ SwitcherMode run_switcher_dialog ( char **input, void *data )
char **cmd_list = get_apps ( &cmd_list_length );
if ( cmd_list == NULL ) {
cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No applications found" );
cmd_list = g_malloc_n ( 2, sizeof ( char * ) );
cmd_list[0] = g_strdup ( "No applications found" );
cmd_list[1] = NULL;
}
@ -233,13 +225,7 @@ SwitcherMode run_switcher_dialog ( char **input, void *data )
retv = RELOAD_DIALOG;
}
for ( int i = 0; cmd_list != NULL && cmd_list[i] != NULL; i++ ) {
free ( cmd_list[i] );
}
if ( cmd_list != NULL ) {
free ( cmd_list );
}
g_strfreev ( cmd_list );
return retv;
}

View file

@ -85,12 +85,8 @@ static char **get_script_output ( char *command, unsigned int *length )
*length = 0;
execute_generator ( command );
while ( fgets ( buffer, 1024, stdin ) != NULL ) {
char **tr = realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
if ( tr == NULL ) {
return retv;
}
retv = tr;
retv[( *length )] = strdup ( buffer );
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
retv[( *length )] = g_strdup ( buffer );
retv[( *length ) + 1] = NULL;
// Filter out line-end.
@ -106,12 +102,10 @@ static char **get_script_output ( char *command, unsigned int *length )
char **execute_executor ( ScriptOptions *options, const char *result, unsigned int *length )
{
char **retv = NULL;
char *command;
if ( asprintf ( &command, "%s '%s'", options->script_path, result ) > 0 ) {
retv = get_script_output ( command, length );
free ( command );
}
char **retv = NULL;
char *command = g_strdup_printf ( "%s '%s'", options->script_path, result );
retv = get_script_output ( command, length );
g_free ( command );
return retv;
}
@ -123,12 +117,7 @@ SwitcherMode script_switcher_dialog ( char **input, void *data )
SwitcherMode retv = MODE_EXIT;
unsigned int length = 0;
char **list = get_script_output ( options->script_path, &length );
char *prompt = NULL;
if ( asprintf ( &( prompt ), "%s:", options->name ) <= 0 ) {
fprintf ( stderr, "Failed to allocate string.\n" );
abort ();
}
char *prompt = g_strdup_printf ( "%s:", options->name );
do {
unsigned int new_length = 0;
@ -150,24 +139,19 @@ SwitcherMode script_switcher_dialog ( char **input, void *data )
}
// Free old list.
for ( unsigned int i = 0; i < length; i++ ) {
free ( list[i] );
}
g_strfreev ( list );
list = NULL;
if ( list != NULL ) {
free ( list );
list = NULL;
}
// If a new list was generated, use that an loop around.
if ( new_list != NULL ) {
list = new_list;
length = new_length;
free ( *input );
g_free ( *input );
*input = NULL;
}
} while ( list != NULL );
free ( prompt );
g_free ( prompt );
return retv;
}
@ -176,29 +160,29 @@ void script_switcher_free_options ( ScriptOptions *sw )
if ( sw == NULL ) {
return;
}
free ( sw->name );
free ( sw->script_path );
free ( sw );
g_free ( sw->name );
g_free ( sw->script_path );
g_free ( sw );
}
ScriptOptions *script_switcher_parse_setup ( const char *str )
{
ScriptOptions *sw = calloc ( 1, sizeof ( *sw ) );
ScriptOptions *sw = g_malloc0 ( sizeof ( *sw ) );
char *endp = NULL;
char *parse = strdup ( str );
char *parse = g_strdup ( str );
unsigned int index = 0;
// TODO: This is naive and can be improved.
for ( char *token = strtok_r ( parse, ":", &endp ); token != NULL; token = strtok_r ( NULL, ":", &endp ) ) {
if ( index == 0 ) {
sw->name = strdup ( token );
sw->name = g_strdup ( token );
}
else if ( index == 1 ) {
sw->script_path = strdup ( token );
sw->script_path = g_strdup ( token );
}
index++;
}
free ( parse );
g_free ( parse );
if ( index == 2 ) {
return sw;
}

View file

@ -53,29 +53,21 @@ static inline int execshssh ( const char *host )
/**
* I am not happy about this code, it causes 7 mallocs and frees
*/
char **args = malloc ( sizeof ( char* ) * 7 );
char **args = g_malloc_n ( 7, sizeof ( char* ) );
int i = 0;
args[i++] = config.terminal_emulator;
if ( config.ssh_set_title ) {
char *buffer = NULL;
if ( asprintf ( &buffer, "ssh %s", host ) > 0 ) {
args[i++] = strdup ( "-T" );
args[i++] = buffer;
}
args[i++] = g_strdup ( "-T" );
args[i++] = g_strdup_printf ( "ssh %s", host );
}
args[i++] = strdup ( "-e" );
args[i++] = strdup ( "ssh" );
args[i++] = strdup ( host );
args[i++] = g_strdup ( "-e" );
args[i++] = g_strdup ( "ssh" );
args[i++] = g_strdup ( host );
args[i++] = NULL;
int retv = execvp ( config.terminal_emulator, (char * const *) args );
// Free the args list.
for ( i = 0; i < 7; i++ ) {
if ( args[i] != NULL ) {
free ( args[i] );
}
}
free ( args );
g_strfreev ( args );
return retv;
}
// execute sub-process
@ -98,11 +90,10 @@ static pid_t exec_ssh ( const char *cmd )
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
history_set ( path, cmd );
free ( path );
}
char *path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
history_set ( path, cmd );
g_free ( path );
return pid;
}
static void delete_ssh ( const char *cmd )
@ -111,10 +102,9 @@ static void delete_ssh ( const char *cmd )
return;
}
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
history_remove ( path, cmd );
free ( path );
}
path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
history_remove ( path, cmd );
g_free ( path );
}
static int sort_func ( const void *a, const void *b )
{
@ -136,18 +126,15 @@ static char ** get_ssh ( unsigned int *length )
return NULL;
}
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
retv = history_get_list ( path, length );
free ( path );
num_favorites = ( *length );
}
path = g_strdup_printf ( "%s/%s", cache_dir, SSH_CACHE_FILE );
retv = history_get_list ( path, length );
g_free ( path );
num_favorites = ( *length );
FILE *fd = NULL;
const char *hd = getenv ( "HOME" );
if ( asprintf ( &path, "%s/%s", hd, ".ssh/config" ) >= 0 ) {
fd = fopen ( path, "r" );
}
path = g_strdup_printf ( "%s/%s", hd, ".ssh/config" );
fd = fopen ( path, "r" );
if ( fd != NULL ) {
char buffer[1024];
@ -184,13 +171,10 @@ static char ** get_ssh ( unsigned int *length )
continue;
}
char **tr = realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
if ( tr != NULL ) {
retv = tr;
retv[( *length )] = strndup ( &buffer[start], stop - start );
retv[( *length ) + 1] = NULL;
( *length )++;
}
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
retv[( *length )] = strndup ( &buffer[start], stop - start );
retv[( *length ) + 1] = NULL;
( *length )++;
}
}
@ -201,7 +185,7 @@ static char ** get_ssh ( unsigned int *length )
if ( ( *length ) > num_favorites ) {
qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func );
}
free ( path );
g_free ( path );
#ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop );
@ -223,8 +207,8 @@ SwitcherMode ssh_switcher_dialog ( char **input, void *data )
char **cmd_list = get_ssh ( &cmd_list_length );
if ( cmd_list == NULL ) {
cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No ssh hosts found" );
cmd_list = g_malloc_n ( 2, sizeof ( char * ) );
cmd_list[0] = g_strdup ( "No ssh hosts found" );
cmd_list[1] = NULL;
}
@ -251,13 +235,7 @@ SwitcherMode ssh_switcher_dialog ( char **input, void *data )
retv = RELOAD_DIALOG;
}
for ( int i = 0; cmd_list[i] != NULL; i++ ) {
free ( cmd_list[i] );
}
if ( cmd_list != NULL ) {
free ( cmd_list );
}
g_strfreev ( cmd_list );
return retv;
}

View file

@ -67,7 +67,7 @@ textbox* textbox_create ( Window parent,
TextBoxFontType tbft,
char *text )
{
textbox *tb = calloc ( 1, sizeof ( textbox ) );
textbox *tb = g_malloc0 ( sizeof ( textbox ) );
tb->flags = flags;
tb->parent = parent;
@ -130,14 +130,13 @@ void textbox_extents ( textbox *tb )
// set the default text to display
void textbox_text ( textbox *tb, char *text )
{
if ( tb->text ) {
free ( tb->text );
}
g_free ( tb->text );
if ( g_utf8_validate ( text, -1, NULL ) ) {
tb->text = strdup ( text );
tb->text = g_strdup ( text );
}
else {
tb->text = strdup ( "Invalid UTF-8 string." );
tb->text = g_strdup ( "Invalid UTF-8 string." );
}
pango_layout_set_text ( tb->layout, tb->text, strlen ( tb->text ) );
@ -208,15 +207,14 @@ void textbox_free ( textbox *tb )
XCloseIM ( tb->xim );
}
if ( tb->text ) {
free ( tb->text );
}
g_free ( tb->text );
if ( tb->layout != NULL ) {
g_object_unref ( tb->layout );
}
XDestroyWindow ( display, tb->window );
free ( tb );
g_free ( tb );
}
void textbox_draw ( textbox *tb )
@ -309,7 +307,7 @@ void textbox_insert ( textbox *tb, int pos, char *str )
int len = ( int ) strlen ( tb->text ), slen = ( int ) strlen ( str );
pos = MAX ( 0, MIN ( len, pos ) );
// expand buffer
tb->text = realloc ( tb->text, len + slen + 1 );
tb->text = g_realloc ( tb->text, len + slen + 1 );
// move everything after cursor upward
char *at = tb->text + pos;
memmove ( at + slen, at, len - pos + 1 );

View file

@ -126,38 +126,36 @@ void parse_xresource_options ( Display *display )
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
char *name, *class;
if ( asprintf ( &name, "%s.%s", namePrefix, xrmOptions[i].name ) == -1 ) {
continue;
}
if ( asprintf ( &class, "%s.%s", classPrefix, xrmOptions[i].name ) > 0 ) {
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) ) {
if ( xrmOptions[i].type == xrm_String ) {
if ( xrmOptions[i].mem != NULL ) {
free ( xrmOptions[i].mem );
xrmOptions[i].mem = NULL;
}
*xrmOptions[i].str = ( char * ) malloc ( xrmValue.size * sizeof ( char ) );
strncpy ( *xrmOptions[i].str, xrmValue.addr, xrmValue.size );
// Memory
xrmOptions[i].mem = ( *xrmOptions[i].str );
name = g_strdup_printf ( "%s.%s", namePrefix, xrmOptions[i].name );
class = g_strdup_printf ( "%s.%s", classPrefix, xrmOptions[i].name );
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) ) {
if ( xrmOptions[i].type == xrm_String ) {
if ( xrmOptions[i].mem != NULL ) {
g_free ( xrmOptions[i].mem );
xrmOptions[i].mem = NULL;
}
else if ( xrmOptions[i].type == xrm_Number ) {
*xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 );
*xrmOptions[i].str = g_strndup ( xrmValue.addr, xrmValue.size );
// Memory
xrmOptions[i].mem = ( *xrmOptions[i].str );
}
else if ( xrmOptions[i].type == xrm_Number ) {
*xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 );
}
else if ( xrmOptions[i].type == xrm_Boolean ) {
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) {
*xrmOptions[i].num = TRUE;
}
else if ( xrmOptions[i].type == xrm_Boolean ) {
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) {
*xrmOptions[i].num = TRUE;
}
else{
*xrmOptions[i].num = FALSE;
}
else{
*xrmOptions[i].num = FALSE;
}
}
free ( class );
}
free ( name );
g_free ( class );
g_free ( name );
}
XrmDestroyDatabase ( xDB );
}
@ -166,7 +164,7 @@ void parse_xresource_free ( void )
{
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
if ( xrmOptions[i].mem != NULL ) {
free ( xrmOptions[i].mem );
g_free ( xrmOptions[i].mem );
xrmOptions[i].mem = NULL;
}
}