about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/chat_log.c15
-rw-r--r--src/common.c49
-rw-r--r--src/common.h2
3 files changed, 54 insertions, 12 deletions
diff --git a/src/chat_log.c b/src/chat_log.c
index 43ba820a..e1aa6865 100644
--- a/src/chat_log.c
+++ b/src/chat_log.c
@@ -129,8 +129,7 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient,
         char *filename = _get_log_filename(recipient, login, log_date, FALSE);
 
         FILE *logp = fopen(filename, "r");
-        char *line = NULL;
-        size_t read = 0;
+        char *line;
         if (logp != NULL) {
             GString *gs_header = g_string_new("");
             g_string_append_printf(gs_header, "%d/%d/%d:",
@@ -141,16 +140,8 @@ chat_log_get_previous(const gchar * const login, const gchar * const recipient,
             history = g_slist_append(history, header);
             g_string_free(gs_header, TRUE);
 
-            size_t length = getline(&line, &read, logp);
-            while (length != -1) {
-                char *copy = malloc(length);
-                copy = strncpy(copy, line, length);
-                copy[length -1] = '\0';
-                history = g_slist_append(history, copy);
-                free(line);
-                line = NULL;
-                read = 0;
-                length = getline(&line, &read, logp);
+            while ((line = prof_getline(logp)) != NULL) {
+                history = g_slist_append(history, line);
             }
 
             fclose(logp);
diff --git a/src/common.c b/src/common.c
index 840a0803..cdfa944c 100644
--- a/src/common.c
+++ b/src/common.c
@@ -29,6 +29,10 @@
 
 #include "common.h"
 
+// assume malloc stores at most 8 bytes for size of allocated memory
+// and page size is at least 4KB
+#define READ_BUF_SIZE 4088
+
 // backwards compatibility for GLib version < 2.28
 void
 p_slist_free_full(GSList *items, GDestroyNotify free_func)
@@ -118,3 +122,48 @@ encode_xml(const char * const xml)
 
     return coded_msg3;
 }
+
+char *
+prof_getline(FILE *stream)
+{
+    char *buf;
+    size_t buf_size;
+    char *result;
+    char *s = NULL;
+    size_t s_size = 1;
+    int need_exit = 0;
+
+    buf = (char *)malloc(READ_BUF_SIZE);
+
+    while (TRUE) {
+        result = fgets(buf, READ_BUF_SIZE, stream);
+        if (result == NULL)
+            break;
+        buf_size = strlen(buf);
+        if (buf[buf_size - 1] == '\n') {
+            buf_size--;
+            buf[buf_size] = '\0';
+            need_exit = 1;
+        }
+
+        result = (char *)realloc(s, s_size + buf_size);
+        if (result == NULL) {
+            if (s != NULL) {
+                free(s);
+                s = NULL;
+            }
+            break;
+        }
+        s = result;
+
+        memcpy(s + s_size - 1, buf, buf_size);
+        s_size += buf_size;
+        s[s_size - 1] = '\0';
+
+        if (need_exit != 0 || feof(stream) != 0)
+            break;
+    }
+
+    free(buf);
+    return s;
+}
diff --git a/src/common.h b/src/common.h
index 7ee774c4..47de27fb 100644
--- a/src/common.h
+++ b/src/common.h
@@ -23,6 +23,7 @@
 #ifndef COMMON_H
 #define COMMON_H
 
+#include <stdio.h>
 #include <glib.h>
 
 #if !GLIB_CHECK_VERSION(2,28,0)
@@ -41,5 +42,6 @@ char * str_replace(const char *string, const char *substr,
     const char *replacement);
 int str_contains(char str[], int size, char ch);
 char* encode_xml(const char * const xml);
+char * prof_getline(FILE *stream);
 
 #endif
23:44:10 -0700 committer Kartik Agaram <vc@akkartik.com> 2020-07-10 23:53:28 -0700 6631' href='/akkartik/mu/commit/html/129emit-hex.subx.html?h=hlt&id=ec73ed1230d75deb0f913a32617c9f1e0a5ca640'>ec73ed12 ^
4a4a392d ^






ec73ed12 ^
4a4a392d ^














b1635a5c ^
4a4a392d ^







ec73ed12 ^
4a4a392d ^
ec73ed12 ^
4a4a392d ^

8aeb85f0 ^
4a4a392d ^
8aeb85f0 ^
999c529c ^
ec73ed12 ^
999c529c ^





ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^



ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^






ec73ed12 ^
999c529c ^

ec73ed12 ^
999c529c ^








ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^

8aeb85f0 ^
999c529c ^
8aeb85f0 ^
999c529c ^
ec73ed12 ^
999c529c ^





ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^



ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^



ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^








ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^

8aeb85f0 ^
999c529c ^
8aeb85f0 ^
999c529c ^
ec73ed12 ^
999c529c ^





ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^



ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^



ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^








ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^

8aeb85f0 ^
999c529c ^
8aeb85f0 ^
999c529c ^
ec73ed12 ^
999c529c ^





ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^



ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^



ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^
ec73ed12 ^
999c529c ^





4a4a392d ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304