1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-25 13:55:34 -05:00

Change writing of file, a+ does not allow fseek start on bsd.

This commit is contained in:
Dave Davenport 2016-01-03 20:13:08 +01:00
parent 62c1b57778
commit d83f0531d6

View file

@ -119,14 +119,15 @@ void history_set ( const char *filename, const char *entry )
unsigned int length = 0; unsigned int length = 0;
_element **list = NULL; _element **list = NULL;
// Open file for reading and writing. // Open file for reading and writing.
FILE *fd = g_fopen ( filename, "a+" ); FILE *fd = g_fopen ( filename, "r" );
if ( fd == NULL ) { if ( fd != NULL ) {
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return;
}
// Get list. // Get list.
list = __history_get_element_list ( fd, &length ); 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 ) );
}
}
// Look if the entry exists. // Look if the entry exists.
for ( unsigned int iter = 0; !found && iter < length; iter++ ) { for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) { if ( strcmp ( list[iter]->name, entry ) == 0 ) {
@ -156,25 +157,23 @@ void history_set ( const char *filename, const char *entry )
} }
} }
// Rewind. fd = fopen ( filename, "w" );
fseek ( fd, 0L, SEEK_SET ); if ( fd == NULL ) {
// Clear file. fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) { }
else {
// Write list. // Write list.
__history_write_element_list ( fd, list, length ); __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 truncate file: %s\n", strerror ( errno ) );
} }
// Free the list. // Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) { for ( unsigned int iter = 0; iter < length; iter++ ) {
g_free ( list[iter] ); g_free ( list[iter] );
} }
g_free ( list ); 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 ) 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 curr = 0;
unsigned int length = 0; unsigned int length = 0;
// Open file for reading and writing. // Open file for reading and writing.
FILE *fd = g_fopen ( filename, "a+" ); FILE *fd = g_fopen ( filename, "r" );
if ( fd == NULL ) { if ( fd == NULL ) {
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) ); fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return; return;
@ -195,6 +194,10 @@ void history_remove ( const char *filename, const char *entry )
// Get list. // Get list.
list = __history_get_element_list ( fd, &length ); 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. // Find entry.
for ( unsigned int iter = 0; !found && iter < length; iter++ ) { for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) { if ( strcmp ( list[iter]->name, entry ) == 0 ) {
@ -213,12 +216,15 @@ void history_remove ( const char *filename, const char *entry )
list[length - 1] = NULL; list[length - 1] = NULL;
length--; length--;
// Rewind. fd = g_fopen ( filename, "w" );
fseek ( fd, 0L, SEEK_SET );
// Clear list. // Clear list.
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) { if ( fd != NULL ) {
// Write list. // Write list.
__history_write_element_list ( fd, list, length ); __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{ else{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) ); 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 ) { if ( list != NULL ) {
g_free ( list ); 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 ) char ** history_get_list ( const char *filename, unsigned int *length )