59 unsigned int length) {
60 if (list == NULL || length == 0) {
68 int min_value = list[length - 1]->
index;
72 (length >
config.max_history_size) ?
config.max_history_size : length;
75 for (
unsigned int iter = 0; iter < length; iter++) {
76 fprintf(fd,
"%ld %s\n", list[iter]->index - min_value, list[iter]->name);
81 unsigned int *length) {
82 unsigned int real_length = 0;
94 size_t buffer_length = 0;
96 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
98 const char *start = strchr(buffer,
' ');
105 buffer[l - 1] =
'\0';
106 if (real_length < (*length + 2)) {
109 retv = g_realloc(retv, (real_length) *
sizeof(
char *));
112 retv[(*length)] = g_strndup(start, l - 1 - (start - buffer));
114 retv[(*length) + 1] = NULL;
120 if (buffer != NULL) {
127 unsigned int real_length = 0;
130 if (length == NULL) {
139 size_t buffer_length = 0;
141 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
148 long int index = strtol(buffer, &start, 10);
149 if (start == buffer || *start ==
'\0') {
153 if ((l - (start - buffer)) < 2) {
156 if (real_length < (*length + 2)) {
159 retv = g_realloc(retv, (real_length) *
sizeof(
_element *));
162 retv[(*length)] = g_malloc(
sizeof(
_element));
165 buffer[l - 1] =
'\0';
167 retv[(*length)]->
index = index;
168 retv[(*length)]->
name = g_strndup(start, l - 1 - (start - buffer));
170 retv[(*length) + 1] = NULL;
174 if (buffer != NULL) {
182 if (
config.disable_history) {
188 for (
char *checked_prefix = strtok(
config.ignored_prefixes,
";");
189 checked_prefix != NULL; checked_prefix = strtok(NULL,
";")) {
192 while (g_unichar_isspace(g_utf8_get_char(checked_prefix))) {
193 checked_prefix = g_utf8_next_char(
198 if (g_str_has_prefix(entry, checked_prefix)) {
204 unsigned int curr = 0;
205 unsigned int length = 0;
208 FILE *fd = g_fopen(filename,
"r");
213 if (fclose(fd) != 0) {
214 g_warning(
"Failed to close history file: %s", g_strerror(errno));
218 for (
unsigned int iter = 0; !found && iter < length; iter++) {
219 if (strcmp(list[iter]->name, entry) == 0) {
231 list = g_realloc(list, (length + 2) *
sizeof(
_element *));
232 list[length] = g_malloc(
sizeof(
_element));
234 if (list[length] != NULL) {
235 list[length]->
name = g_strdup(entry);
237 list[length]->
index = 1;
244 fd = fopen(filename,
"w");
246 g_warning(
"Failed to open file: %s", g_strerror(errno));
251 if (fclose(fd) != 0) {
252 g_warning(
"Failed to close history file: %s", g_strerror(errno));
256 for (
unsigned int iter = 0; iter < length; iter++) {
257 g_free(list[iter]->name);
264 if (
config.disable_history) {
269 unsigned int curr = 0;
270 unsigned int length = 0;
272 FILE *fd = g_fopen(filename,
"r");
274 g_warning(
"Failed to open file: %s", g_strerror(errno));
281 if (fclose(fd) != 0) {
282 g_warning(
"Failed to close history file: %s", g_strerror(errno));
285 for (
unsigned int iter = 0; !found && iter < length; iter++) {
286 if (strcmp(list[iter]->name, entry) == 0) {
295 g_free(list[curr]->name);
298 list[curr] = list[length - 1];
300 list[length - 1] = NULL;
303 fd = g_fopen(filename,
"w");
309 if (fclose(fd) != 0) {
310 g_warning(
"Failed to close history file: %s", g_strerror(errno));
313 g_warning(
"Failed to open file: %s", g_strerror(errno));
318 for (
unsigned int iter = 0; iter < length; iter++) {
319 g_free(list[iter]->name);
void history_set(const char *filename, const char *entry)
void history_remove(const char *filename, const char *entry)
char ** history_get_list(const char *filename, unsigned int *length)
struct __element _element
static char ** __history_get_element_list_fields(FILE *fd, unsigned int *length)
static void __history_write_element_list(FILE *fd, _element **list, unsigned int length)
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)))