33 #include <sys/types.h>
36 #include <glib/gstdio.h>
52 static int __element_sort_func (
const void *ea,
const void *eb,
void *data __attribute__( ( unused ) ) )
61 if ( list == NULL || length == 0 ) {
68 int min_value = list[length - 1]->
index;
74 for (
unsigned int iter = 0; iter < length; iter++ ) {
75 fprintf ( fd,
"%ld %s\n", list[iter]->index - min_value, list[iter]->name );
81 unsigned int real_length = 0;
83 if ( length == NULL ) {
92 size_t buffer_length = 0;
94 while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
96 const char *start = strchr ( buffer,
' ' );
98 if ( start == NULL ) {
103 buffer[l - 1] =
'\0';
104 if ( real_length < ( *length + 2 ) ) {
107 retv = g_realloc ( retv, ( real_length ) *
sizeof (
char* ) );
110 retv[( *length )] = g_strndup ( start, l - 1 - ( start - buffer ) );
112 retv[( *length ) + 1] = NULL;
116 if ( buffer_length > 0 ) {
124 unsigned int real_length = 0;
127 if ( length == NULL ) {
136 size_t buffer_length = 0;
138 while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
145 long int index = strtol ( buffer, &start, 10 );
146 if ( start == buffer || *start ==
'\0' ) {
150 if ( ( l - ( start - buffer ) ) < 2 ) {
153 if ( real_length < ( *length + 2 ) ) {
156 retv = g_realloc ( retv, ( real_length ) *
sizeof (
_element* ) );
159 retv[( *length )] = g_malloc (
sizeof (
_element ) );
162 buffer[l - 1] =
'\0';
164 retv[( *length )]->
index = index;
165 retv[( *length )]->
name = g_strndup ( start, l - 1 - ( start - buffer ) );
167 retv[( *length ) + 1] = NULL;
171 if ( buffer != NULL ) {
185 for (
char *checked_prefix = strtok (
config.
ignored_prefixes,
";" ); checked_prefix != NULL; checked_prefix = strtok ( NULL,
";" ) ) {
188 while ( g_unichar_isspace ( g_utf8_get_char ( checked_prefix ) ) ) {
189 checked_prefix = g_utf8_next_char ( checked_prefix );
192 if ( g_str_has_prefix ( entry, checked_prefix ) ) {
198 unsigned int curr = 0;
199 unsigned int length = 0;
202 FILE *fd = g_fopen ( filename,
"r" );
207 if ( fclose ( fd ) != 0 ) {
208 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
212 for (
unsigned int iter = 0; !found && iter < length; iter++ ) {
213 if ( strcmp ( list[iter]->name, entry ) == 0 ) {
226 list = g_realloc ( list, ( length + 2 ) *
sizeof (
_element* ) );
227 list[length] = g_malloc (
sizeof (
_element ) );
229 if ( list[length] != NULL ) {
230 list[length]->
name = g_strdup ( entry );
232 list[length]->
index = 1;
239 fd = fopen ( filename,
"w" );
241 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
247 if ( fclose ( fd ) != 0 ) {
248 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
252 for (
unsigned int iter = 0; iter < length; iter++ ) {
253 g_free ( list[iter]->name );
254 g_free ( list[iter] );
266 unsigned int curr = 0;
267 unsigned int length = 0;
269 FILE *fd = g_fopen ( filename,
"r" );
271 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
278 if ( fclose ( fd ) != 0 ) {
279 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
282 for (
unsigned int iter = 0; !found && iter < length; iter++ ) {
283 if ( strcmp ( list[iter]->name, entry ) == 0 ) {
292 g_free ( list[curr]->name );
293 g_free ( list[curr] );
295 list[curr] = list[length - 1];
297 list[length - 1] = NULL;
300 fd = g_fopen ( filename,
"w" );
306 if ( fclose ( fd ) != 0 ) {
307 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
311 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
316 for (
unsigned int iter = 0; iter < length; iter++ ) {
317 g_free ( list[iter]->name );
318 g_free ( list[iter] );
320 if ( list != NULL ) {
334 FILE *fd = g_fopen ( filename,
"r" );
338 if ( errno != ENOENT ) {
339 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
347 if ( fclose ( fd ) != 0 ) {
348 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );