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 ) {
184 unsigned int curr = 0;
185 unsigned int length = 0;
188 FILE *fd = g_fopen ( filename,
"r" );
193 if ( fclose ( fd ) != 0 ) {
194 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
198 for (
unsigned int iter = 0; !found && iter < length; iter++ ) {
199 if ( strcmp ( list[iter]->name, entry ) == 0 ) {
212 list = g_realloc ( list, ( length + 2 ) *
sizeof (
_element* ) );
213 list[length] = g_malloc (
sizeof (
_element ) );
215 if ( list[length] != NULL ) {
216 list[length]->
name = g_strdup ( entry );
218 list[length]->
index = 1;
225 fd = fopen ( filename,
"w" );
227 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
233 if ( fclose ( fd ) != 0 ) {
234 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
238 for (
unsigned int iter = 0; iter < length; iter++ ) {
239 g_free ( list[iter]->name );
240 g_free ( list[iter] );
252 unsigned int curr = 0;
253 unsigned int length = 0;
255 FILE *fd = g_fopen ( filename,
"r" );
257 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
264 if ( fclose ( fd ) != 0 ) {
265 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
268 for (
unsigned int iter = 0; !found && iter < length; iter++ ) {
269 if ( strcmp ( list[iter]->name, entry ) == 0 ) {
278 g_free ( list[curr]->name );
279 g_free ( list[curr] );
281 list[curr] = list[length - 1];
283 list[length - 1] = NULL;
286 fd = g_fopen ( filename,
"w" );
292 if ( fclose ( fd ) != 0 ) {
293 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
297 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
302 for (
unsigned int iter = 0; iter < length; iter++ ) {
303 g_free ( list[iter]->name );
304 g_free ( list[iter] );
306 if ( list != NULL ) {
320 FILE *fd = g_fopen ( filename,
"r" );
324 if ( errno != ENOENT ) {
325 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
333 if ( fclose ( fd ) != 0 ) {
334 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
void history_set(const char *filename, const char *entry)
char ** history_get_list(const char *filename, unsigned int *length)
static char ** __history_get_element_list_fields(FILE *fd, unsigned int *length)
struct __element _element
unsigned int max_history_size
void history_remove(const char *filename, const char *entry)
static _element ** __history_get_element_list(FILE *fd, unsigned int *length)
static int __element_sort_func(const void *ea, const void *eb, void *data __attribute__((unused)))
static void __history_write_element_list(FILE *fd, _element **list, unsigned int length)
unsigned int disable_history