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;
121 unsigned int real_length = 0;
124 if ( length == NULL ) {
133 size_t buffer_length = 0;
135 while ( ( l = getline ( &buffer, &buffer_length, fd ) ) > 0 ) {
142 long int index = strtol ( buffer, &start, 10 );
143 if ( start == buffer || *start ==
'\0' ) {
147 if ( ( l - ( start - buffer ) ) < 2 ) {
150 if ( real_length < ( *length + 2 ) ) {
153 retv = g_realloc ( retv, ( real_length ) *
sizeof (
_element* ) );
156 retv[( *length )] = g_malloc (
sizeof (
_element ) );
159 buffer[l - 1] =
'\0';
161 retv[( *length )]->
index = index;
162 retv[( *length )]->
name = g_strndup ( start, l - 1 - ( start - buffer ) );
164 retv[( *length ) + 1] = NULL;
168 if ( buffer != NULL ) {
181 unsigned int curr = 0;
182 unsigned int length = 0;
185 FILE *fd = g_fopen ( filename,
"r" );
190 if ( fclose ( fd ) != 0 ) {
191 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
195 for (
unsigned int iter = 0; !found && iter < length; iter++ ) {
196 if ( strcmp ( list[iter]->name, entry ) == 0 ) {
209 list = g_realloc ( list, ( length + 2 ) *
sizeof (
_element* ) );
210 list[length] = g_malloc (
sizeof (
_element ) );
212 if ( list[length] != NULL ) {
213 list[length]->
name = g_strdup ( entry );
215 list[length]->
index = 1;
222 fd = fopen ( filename,
"w" );
224 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
230 if ( fclose ( fd ) != 0 ) {
231 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
235 for (
unsigned int iter = 0; iter < length; iter++ ) {
236 g_free ( list[iter]->name );
237 g_free ( list[iter] );
249 unsigned int curr = 0;
250 unsigned int length = 0;
252 FILE *fd = g_fopen ( filename,
"r" );
254 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
261 if ( fclose ( fd ) != 0 ) {
262 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
265 for (
unsigned int iter = 0; !found && iter < length; iter++ ) {
266 if ( strcmp ( list[iter]->name, entry ) == 0 ) {
275 g_free ( list[curr]->name );
276 g_free ( list[curr] );
278 list[curr] = list[length - 1];
280 list[length - 1] = NULL;
283 fd = g_fopen ( filename,
"w" );
289 if ( fclose ( fd ) != 0 ) {
290 g_warning (
"Failed to close history file: %s", g_strerror ( errno ) );
294 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
299 for (
unsigned int iter = 0; iter < length; iter++ ) {
300 g_free ( list[iter]->name );
301 g_free ( list[iter] );
303 if ( list != NULL ) {
317 FILE *fd = g_fopen ( filename,
"r" );
321 if ( errno != ENOENT ) {
322 g_warning (
"Failed to open file: %s", g_strerror ( errno ) );
330 if ( fclose ( fd ) != 0 ) {
331 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