aboutsummaryrefslogtreecommitdiffstats
path: root/help.c
diff options
context:
space:
mode:
authorWilmer van der Gaast <wilmer@gaast.net>2008-02-02 21:48:09 +0000
committerWilmer van der Gaast <wilmer@gaast.net>2008-02-02 21:48:09 +0000
commit0fbda19314c806e3e677847f3c977eb5a1bc2b61 (patch)
treec858cf146371845ae09eb4e65fb595948e61edc1 /help.c
parentf774e01cdb34e339455671f51413ecdc6de3208d (diff)
Added help_free() and cleaned up some very stale help-related stuff I
wasn't even aware of. This closes bug #352.
Diffstat (limited to 'help.c')
-rw-r--r--help.c66
1 files changed, 45 insertions, 21 deletions
diff --git a/help.c b/help.c
index 756eb12a..587b9940 100644
--- a/help.c
+++ b/help.c
@@ -70,21 +70,20 @@ help_t *help_init( help_t **help, const char *helpfile )
if( !( t = strstr( s, "\n%\n" ) ) || s[0] != '?' )
{
/* FIXME: Clean up */
-// help_close( *help );
- *help = NULL;
+ help_free( help );
g_free( s );
- return( NULL );
+ return NULL;
}
i = strchr( s, '\n' ) - s;
- if( h->string )
+ if( h->title )
{
h = h->next = g_new0( help_t, 1 );
}
- h->string = g_new ( char, i );
+ h->title = g_new ( char, i );
- strncpy( h->string, s + 1, i - 1 );
- h->string[i-1] = 0;
+ strncpy( h->title, s + 1, i - 1 );
+ h->title[i-1] = 0;
h->fd = (*help)->fd;
h->offset.file_offset = lseek( h->fd, 0, SEEK_CUR ) - buflen + i + 1;
h->length = t - s - i - 1;
@@ -102,7 +101,31 @@ help_t *help_init( help_t **help, const char *helpfile )
return( *help );
}
-char *help_get( help_t **help, char *string )
+void help_free( help_t **help )
+{
+ help_t *h, *oh;
+ int last_fd = -1; /* Weak de-dupe */
+
+ if( help == NULL || *help == NULL )
+ return;
+
+ h = *help;
+ while( h )
+ {
+ if( h->fd != last_fd )
+ {
+ close( h->fd );
+ last_fd = h->fd;
+ }
+ g_free( h->title );
+ h = (oh=h)->next;
+ g_free( oh );
+ }
+
+ *help = NULL;
+}
+
+char *help_get( help_t **help, char *title )
{
time_t mtime;
struct stat stat[1];
@@ -110,28 +133,29 @@ char *help_get( help_t **help, char *string )
for( h = *help; h; h = h->next )
{
- if( h->string != NULL &&
- g_strcasecmp( h->string, string ) == 0 )
+ if( h->title != NULL && g_strcasecmp( h->title, title ) == 0 )
break;
}
if( h && h->length > 0 )
{
char *s = g_new( char, h->length + 1 );
- if( fstat( h->fd, stat ) != 0 )
- {
- g_free( h );
- *help = NULL;
- return NULL;
- }
- mtime = stat->st_mtime;
-
- if( mtime > h->mtime )
- return NULL;
-
s[h->length] = 0;
if( h->fd >= 0 )
{
+ if( fstat( h->fd, stat ) != 0 )
+ {
+ g_free( s );
+ return NULL;
+ }
+ mtime = stat->st_mtime;
+
+ if( mtime > h->mtime )
+ {
+ g_free( s );
+ return NULL;
+ }
+
lseek( h->fd, h->offset.file_offset, SEEK_SET );
read( h->fd, s, h->length );
}