summaryrefslogtreecommitdiffstats
path: root/source/history.c
diff options
context:
space:
mode:
authorDave Davenport <qball@gmpclient.org>2016-01-03 20:13:08 +0100
committerDave Davenport <qball@gmpclient.org>2016-01-03 20:13:08 +0100
commitd83f0531d611f5f5d24b74da43730998c3e0bf53 (patch)
tree5d491770ed9e1db9a6e1b32089a82afd34581cbd /source/history.c
parent62c1b577785013e8bf02d56db3e3c1763aaec118 (diff)
Change writing of file, a+ does not allow fseek start on bsd.
Diffstat (limited to 'source/history.c')
-rw-r--r--source/history.c55
1 files changed, 28 insertions, 27 deletions
diff --git a/source/history.c b/source/history.c
index 52eb02ea..2c5537f2 100644
--- a/source/history.c
+++ b/source/history.c
@@ -119,14 +119,15 @@ void history_set ( const char *filename, const char *entry )
unsigned int length = 0;
_element **list = NULL;
// Open file for reading and writing.
- FILE *fd = g_fopen ( filename, "a+" );
- if ( fd == NULL ) {
- fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
- return;
+ FILE *fd = g_fopen ( filename, "r" );
+ if ( fd != NULL ) {
+ // Get list.
+ list = __history_get_element_list ( fd, &length );
+ // Close file, if fails let user know on stderr.
+ if ( fclose ( fd ) != 0 ) {
+ fprintf ( stderr, "Failed to close history file: %s\n", strerror ( errno ) );
+ }
}
- // Get list.
- list = __history_get_element_list ( fd, &length );
-
// Look if the entry exists.
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
@@ -156,25 +157,23 @@ void history_set ( const char *filename, const char *entry )
}
}
- // Rewind.
- fseek ( fd, 0L, SEEK_SET );
- // Clear file.
- if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
+ fd = fopen ( filename, "w" );
+ if ( fd == NULL ) {
+ fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
+ }
+ else {
// Write list.
__history_write_element_list ( fd, list, length );
- }
- else{
- fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) );
+ // Close file, if fails let user know on stderr.
+ if ( fclose ( fd ) != 0 ) {
+ fprintf ( stderr, "Failed to close history file: %s\n", strerror ( errno ) );
+ }
}
// Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) {
g_free ( list[iter] );
}
g_free ( list );
- // Close file, if fails let user know on stderr.
- if ( fclose ( fd ) != 0 ) {
- fprintf ( stderr, "Failed to close history file: %s\n", strerror ( errno ) );
- }
}
void history_remove ( const char *filename, const char *entry )
@@ -187,7 +186,7 @@ void history_remove ( const char *filename, const char *entry )
unsigned int curr = 0;
unsigned int length = 0;
// Open file for reading and writing.
- FILE *fd = g_fopen ( filename, "a+" );
+ FILE *fd = g_fopen ( filename, "r" );
if ( fd == NULL ) {
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return;
@@ -195,6 +194,10 @@ void history_remove ( const char *filename, const char *entry )
// Get list.
list = __history_get_element_list ( fd, &length );
+ // Close file, if fails let user know on stderr.
+ if ( fclose ( fd ) != 0 ) {
+ fprintf ( stderr, "Failed to close history file: %s\n", strerror ( errno ) );
+ }
// Find entry.
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
@@ -213,12 +216,15 @@ void history_remove ( const char *filename, const char *entry )
list[length - 1] = NULL;
length--;
- // Rewind.
- fseek ( fd, 0L, SEEK_SET );
+ fd = g_fopen ( filename, "w" );
// Clear list.
- if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
+ if ( fd != NULL ) {
// Write list.
__history_write_element_list ( fd, list, length );
+ // Close file, if fails let user know on stderr.
+ if ( fclose ( fd ) != 0 ) {
+ fprintf ( stderr, "Failed to close history file: %s\n", strerror ( errno ) );
+ }
}
else{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
@@ -232,11 +238,6 @@ void history_remove ( const char *filename, const char *entry )
if ( list != NULL ) {
g_free ( list );
}
-
- // Close file, if fails let user know on stderr.
- if ( fclose ( fd ) != 0 ) {
- fprintf ( stderr, "Failed to close history file: %s\n", strerror ( errno ) );
- }
}
char ** history_get_list ( const char *filename, unsigned int *length )