source: help.c @ ad5a907

Last change on this file since ad5a907 was 42a418e, checked in by dequis <dx@…>, at 2015-11-26T01:59:00Z

help: free strings added by help_add_mem()

Not really a leak, but I want valgrind to be happy because valgrind is
nice to me. I love you valgrind <3

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2010 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Help file control                                                    */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "help.h"
29#undef read
30#undef write
31
32#define BUFSIZE 2048
33
34help_t *help_init(help_t **help, const char *helpfile)
35{
36        int i, buflen = 0;
37        help_t *h;
38        char *s, *t;
39        time_t mtime;
40        struct stat stat[1];
41
42        *help = h = g_new0(help_t, 1);
43
44        h->fd = open(helpfile, O_RDONLY);
45
46        if (h->fd == -1) {
47                g_free(h);
48                return(*help = NULL);
49        }
50
51        if (fstat(h->fd, stat) != 0) {
52                g_free(h);
53                return(*help = NULL);
54        }
55        mtime = stat->st_mtime;
56
57        s = g_new(char, BUFSIZE + 1);
58        s[BUFSIZE] = 0;
59
60        while (((i = read(h->fd, s + buflen, BUFSIZE - buflen)) > 0) ||
61               (i == 0 && strstr(s, "\n%\n"))) {
62                buflen += i;
63                memset(s + buflen, 0, BUFSIZE - buflen);
64                if (!(t = strstr(s, "\n%\n")) || s[0] != '?') {
65                        /* FIXME: Clean up */
66                        help_free(help);
67                        g_free(s);
68                        return NULL;
69                }
70                i = strchr(s, '\n') - s;
71
72                if (h->title) {
73                        h = h->next = g_new0(help_t, 1);
74                }
75                h->title = g_new(char, i);
76
77                strncpy(h->title, s + 1, i - 1);
78                h->title[i - 1] = 0;
79                h->fd = (*help)->fd;
80                h->offset.file_offset = lseek(h->fd, 0, SEEK_CUR) - buflen + i + 1;
81                h->length = t - s - i - 1;
82                h->mtime = mtime;
83
84                buflen -= (t + 3 - s);
85                t = g_strdup(t + 3);
86                g_free(s);
87                s = g_renew(char, t, BUFSIZE + 1);
88                s[BUFSIZE] = 0;
89        }
90
91        g_free(s);
92
93        return(*help);
94}
95
96void help_free(help_t **help)
97{
98        help_t *h, *oh;
99        int last_fd = -1; /* Weak de-dupe */
100
101        if (help == NULL || *help == NULL) {
102                return;
103        }
104
105        h = *help;
106        while (h) {
107                if (h->fd == -1) {
108                        g_free(h->offset.mem_offset);
109                } else if (h->fd != last_fd) {
110                        close(h->fd);
111                        last_fd = h->fd;
112                }
113                g_free(h->title);
114                h = (oh = h)->next;
115                g_free(oh);
116        }
117
118        *help = NULL;
119}
120
121char *help_get(help_t **help, char *title)
122{
123        time_t mtime;
124        struct stat stat[1];
125        help_t *h;
126
127        for (h = *help; h; h = h->next) {
128                if (h->title != NULL && g_strcasecmp(h->title, title) == 0) {
129                        break;
130                }
131        }
132        if (h && h->length > 0) {
133                char *s = g_new(char, h->length + 1);
134
135                s[h->length] = 0;
136                if (h->fd >= 0) {
137                        if (fstat(h->fd, stat) != 0) {
138                                g_free(s);
139                                return NULL;
140                        }
141                        mtime = stat->st_mtime;
142
143                        if (mtime > h->mtime) {
144                                g_free(s);
145                                return NULL;
146                        }
147
148                        if (lseek(h->fd, h->offset.file_offset, SEEK_SET) == -1 ||
149                            read(h->fd, s, h->length) != h->length) {
150                                g_free(s);
151                                return NULL;
152                        }
153                } else {
154                        strncpy(s, h->offset.mem_offset, h->length);
155                }
156                return s;
157        }
158
159        return NULL;
160}
161
162int help_add_mem(help_t **help, const char *title, const char *content)
163{
164        help_t *h, *l = NULL;
165
166        for (h = *help; h; h = h->next) {
167                if (g_strcasecmp(h->title, title) == 0) {
168                        return 0;
169                }
170
171                l = h;
172        }
173
174        if (l) {
175                h = l->next = g_new0(struct help, 1);
176        } else {
177                *help = h = g_new0(struct help, 1);
178        }
179        h->fd = -1;
180        h->title = g_strdup(title);
181        h->length = strlen(content);
182        h->offset.mem_offset = g_strdup(content);
183
184        return 1;
185}
186
187char *help_get_whatsnew(help_t **help, int old)
188{
189        GString *ret = NULL;
190        help_t *h;
191        int v;
192
193        for (h = *help; h; h = h->next) {
194                if (h->title != NULL && strncmp(h->title, "whatsnew", 8) == 0 &&
195                    sscanf(h->title + 8, "%x", &v) == 1 && v > old) {
196                        char *s = help_get(&h, h->title);
197                        if (ret == NULL) {
198                                ret = g_string_new(s);
199                        } else {
200                                g_string_append_printf(ret, "\n\n%s", s);
201                        }
202                        g_free(s);
203                }
204        }
205
206        return ret ? g_string_free(ret, FALSE) : NULL;
207}
Note: See TracBrowser for help on using the repository browser.