1 |
commit: 92de436359da8f14b7dbef5f62af959095b79d06 |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Nov 27 18:14:14 2015 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Nov 27 18:14:14 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=92de4363 |
7 |
|
8 |
use the return value of getline |
9 |
|
10 |
This allows us to avoid calling strlen to get a value getline already |
11 |
calculated. We can also pass this value on to rmspace to let it trim |
12 |
space for us. |
13 |
|
14 |
libq/colors.c | 16 ++++++---------- |
15 |
libq/profile.c | 7 ++++--- |
16 |
libq/rmspace.c | 8 ++++++-- |
17 |
main.c | 1 + |
18 |
qcache.c | 9 ++++----- |
19 |
qfile.c | 8 ++++---- |
20 |
qlop.c | 19 ++++++++----------- |
21 |
qmerge.c | 15 +++++++-------- |
22 |
qsearch.c | 11 +++++------ |
23 |
quse.c | 25 ++++++++++--------------- |
24 |
10 files changed, 55 insertions(+), 64 deletions(-) |
25 |
|
26 |
diff --git a/libq/colors.c b/libq/colors.c |
27 |
index bd2c1e0..6ddcdda 100644 |
28 |
--- a/libq/colors.c |
29 |
+++ b/libq/colors.c |
30 |
@@ -50,7 +50,7 @@ void color_remap(void) |
31 |
{ |
32 |
FILE *fp; |
33 |
unsigned int i; |
34 |
- size_t buflen; |
35 |
+ size_t buflen, linelen; |
36 |
char *buf; |
37 |
char *p; |
38 |
unsigned int lineno = 0; |
39 |
@@ -59,22 +59,18 @@ void color_remap(void) |
40 |
return; |
41 |
|
42 |
buf = NULL; |
43 |
- while (getline(&buf, &buflen, fp) != -1) { |
44 |
+ while ((linelen = getline(&buf, &buflen, fp)) != -1) { |
45 |
lineno++; |
46 |
/* eat comments */ |
47 |
if ((p = strchr(buf, '#')) != NULL) |
48 |
*p = '\0'; |
49 |
|
50 |
- if (strchr(buf, '=') == NULL) |
51 |
- continue; |
52 |
- |
53 |
- /* eat the end of the buffer first */ |
54 |
- if ((p = strchr(buf, '\r')) != NULL) |
55 |
- *p = 0; |
56 |
- if ((p = strchr(buf, '\n')) != NULL) |
57 |
- *p = 0; |
58 |
+ rmspace_len(buf, linelen); |
59 |
|
60 |
p = strchr(buf, '='); |
61 |
+ if (p == NULL) |
62 |
+ continue; |
63 |
+ |
64 |
*p++ = 0; /* split the pair */ |
65 |
for (i = 0; i < ARRAY_SIZE(color_pairs); ++i) |
66 |
if (strcmp(buf, color_pairs[i].name) == 0) { |
67 |
|
68 |
diff --git a/libq/profile.c b/libq/profile.c |
69 |
index 75f3cb0..66e5caf 100644 |
70 |
--- a/libq/profile.c |
71 |
+++ b/libq/profile.c |
72 |
@@ -6,7 +6,7 @@ q_profile_walk_at(int dir_fd, const char *dir, const char *file, |
73 |
{ |
74 |
FILE *fp; |
75 |
int subdir_fd, fd; |
76 |
- size_t buflen; |
77 |
+ size_t buflen, linelen; |
78 |
char *buf; |
79 |
|
80 |
/* Pop open this profile dir */ |
81 |
@@ -46,13 +46,14 @@ q_profile_walk_at(int dir_fd, const char *dir, const char *file, |
82 |
} |
83 |
|
84 |
buf = NULL; |
85 |
- while (getline(&buf, &buflen, fp) != -1) { |
86 |
+ while ((linelen = getline(&buf, &buflen, fp)) != -1) { |
87 |
char *s; |
88 |
|
89 |
+ rmspace_len(buf, linelen); |
90 |
+ |
91 |
s = strchr(buf, '#'); |
92 |
if (s) |
93 |
*s = '\0'; |
94 |
- rmspace(buf); |
95 |
|
96 |
data = q_profile_walk_at(subdir_fd, buf, file, callback, data); |
97 |
} |
98 |
|
99 |
diff --git a/libq/rmspace.c b/libq/rmspace.c |
100 |
index d374d7f..b8ac5a3 100644 |
101 |
--- a/libq/rmspace.c |
102 |
+++ b/libq/rmspace.c |
103 |
@@ -1,9 +1,8 @@ |
104 |
|
105 |
/* removed leading/trailing extraneous white space */ |
106 |
-static char *rmspace(char *s) |
107 |
+static char *rmspace_len(char *s, size_t len) |
108 |
{ |
109 |
char *p; |
110 |
- size_t len = strlen(s); |
111 |
/* find the start of trailing space and set it to \0 */ |
112 |
for (p = s + len - 1; (p >= s && isspace(*p)); --p) |
113 |
continue; |
114 |
@@ -17,3 +16,8 @@ static char *rmspace(char *s) |
115 |
memmove(s, p, len - (p - s) + 1); |
116 |
return s; |
117 |
} |
118 |
+ |
119 |
+static char *rmspace(char *s) |
120 |
+{ |
121 |
+ return rmspace_len(s, strlen(s)); |
122 |
+} |
123 |
|
124 |
diff --git a/main.c b/main.c |
125 |
index 10af2db..42a3f26 100644 |
126 |
--- a/main.c |
127 |
+++ b/main.c |
128 |
@@ -15,6 +15,7 @@ static bool eat_file_fd(int, char **, size_t *); |
129 |
static bool eat_file_at(int, const char *, char **, size_t *); |
130 |
int rematch(const char *, const char *, int); |
131 |
static char *rmspace(char *); |
132 |
+static char *rmspace_len(char *, size_t); |
133 |
|
134 |
void initialize_portage_env(void); |
135 |
void initialize_ebuild_flat(void); |
136 |
|
137 |
diff --git a/qcache.c b/qcache.c |
138 |
index 0968d33..5f65fe0 100644 |
139 |
--- a/qcache.c |
140 |
+++ b/qcache.c |
141 |
@@ -319,10 +319,10 @@ _q_static |
142 |
portage_cache *qcache_read_cache_file(const char *filename) |
143 |
{ |
144 |
struct stat s; |
145 |
- char *ptr, *buf; |
146 |
+ char *buf; |
147 |
FILE *f; |
148 |
portage_cache *ret = NULL; |
149 |
- size_t len, buflen; |
150 |
+ size_t len, buflen, linelen; |
151 |
|
152 |
if ((f = fopen(filename, "r")) == NULL) |
153 |
goto err; |
154 |
@@ -336,9 +336,8 @@ portage_cache *qcache_read_cache_file(const char *filename) |
155 |
len = sizeof(*ret) + s.st_size + 1; |
156 |
ret = xzalloc(len); |
157 |
|
158 |
- while (getline(&buf, &buflen, f) != -1) { |
159 |
- if ((ptr = strrchr(buf, '\n')) != NULL) |
160 |
- *ptr = 0; |
161 |
+ while ((linelen = getline(&buf, &buflen, f)) != -1) { |
162 |
+ rmspace_len(buf, linelen); |
163 |
|
164 |
if (strncmp(buf, "DEPEND=", 7) == 0) |
165 |
ret->DEPEND = xstrdup(buf + 7); |
166 |
|
167 |
diff --git a/qfile.c b/qfile.c |
168 |
index a32e512..6d36e96 100644 |
169 |
--- a/qfile.c |
170 |
+++ b/qfile.c |
171 |
@@ -513,10 +513,10 @@ int qfile_main(int argc, char **argv) |
172 |
for (i = 0; i < qargc; ++i) |
173 |
free(qargv[i]); |
174 |
qargc = 0; |
175 |
- while (getline(&state.buf, &state.buflen, args_file) != -1) { |
176 |
- if ((p = strchr(state.buf, '\n')) != NULL) |
177 |
- *p = '\0'; |
178 |
- if (state.buf == p) |
179 |
+ size_t linelen; |
180 |
+ while ((linelen = getline(&state.buf, &state.buflen, args_file)) != -1) { |
181 |
+ rmspace_len(state.buf, linelen); |
182 |
+ if (state.buf[0] == '\0') |
183 |
continue; |
184 |
qargv[qargc] = xstrdup(state.buf); |
185 |
if (++qargc >= max_args) |
186 |
|
187 |
diff --git a/qlop.c b/qlop.c |
188 |
index 390865a..f2f97c6 100644 |
189 |
--- a/qlop.c |
190 |
+++ b/qlop.c |
191 |
@@ -240,7 +240,7 @@ _q_static void |
192 |
show_emerge_history(char listflag, int argc, char **argv, const char *logfile) |
193 |
{ |
194 |
FILE *fp; |
195 |
- size_t buflen; |
196 |
+ size_t buflen, linelen; |
197 |
char *buf, merged; |
198 |
char *p, *q; |
199 |
int i; |
200 |
@@ -252,8 +252,8 @@ show_emerge_history(char listflag, int argc, char **argv, const char *logfile) |
201 |
} |
202 |
|
203 |
buf = NULL; |
204 |
- while (getline(&buf, &buflen, fp) != -1) { |
205 |
- if (strlen(buf) < 30) |
206 |
+ while ((linelen = getline(&buf, &buflen, fp)) != -1) { |
207 |
+ if (linelen < 30) |
208 |
continue; |
209 |
|
210 |
for (i = 0; i < argc; ++i) |
211 |
@@ -262,8 +262,7 @@ show_emerge_history(char listflag, int argc, char **argv, const char *logfile) |
212 |
if (argc && i == argc) |
213 |
continue; |
214 |
|
215 |
- if ((p = strchr(buf, '\n')) != NULL) |
216 |
- *p = 0; |
217 |
+ rmspace_len(buf, linelen); |
218 |
if ((p = strchr(buf, ':')) == NULL) |
219 |
continue; |
220 |
*p = 0; |
221 |
@@ -327,7 +326,7 @@ _q_static void |
222 |
show_sync_history(const char *logfile) |
223 |
{ |
224 |
FILE *fp; |
225 |
- size_t buflen, len; |
226 |
+ size_t buflen, linelen; |
227 |
char *buf, *p; |
228 |
time_t t; |
229 |
|
230 |
@@ -338,10 +337,9 @@ show_sync_history(const char *logfile) |
231 |
|
232 |
buf = NULL; |
233 |
/* Just find the finish lines. */ |
234 |
- while (getline(&buf, &buflen, fp) != -1) { |
235 |
- len = strlen(buf); |
236 |
+ while ((linelen = getline(&buf, &buflen, fp)) != -1) { |
237 |
/* This cuts out like ~10% of the log. */ |
238 |
- if (len < 35) |
239 |
+ if (linelen < 35) |
240 |
continue; |
241 |
|
242 |
/* Make sure there's a timestamp in here. */ |
243 |
@@ -353,8 +351,7 @@ show_sync_history(const char *logfile) |
244 |
continue; |
245 |
p += 19; |
246 |
|
247 |
- if (buf[len - 1] == '\n') |
248 |
- buf[len - 1] = '\0'; |
249 |
+ rmspace_len(buf, linelen); |
250 |
|
251 |
t = (time_t)atol(buf); |
252 |
|
253 |
|
254 |
diff --git a/qmerge.c b/qmerge.c |
255 |
index ddc0a10..297f939 100644 |
256 |
--- a/qmerge.c |
257 |
+++ b/qmerge.c |
258 |
@@ -1633,7 +1633,7 @@ _q_static int |
259 |
parse_packages(queue *todo) |
260 |
{ |
261 |
FILE *fp; |
262 |
- size_t buflen; |
263 |
+ size_t buflen, linelen; |
264 |
char *buf, *p; |
265 |
struct pkg_t Pkg; |
266 |
depend_atom *pkg_atom; |
267 |
@@ -1645,12 +1645,11 @@ parse_packages(queue *todo) |
268 |
repo[0] = '\0'; |
269 |
|
270 |
/* First consume the header with the common data. */ |
271 |
- while (getline(&buf, &buflen, fp) != -1) { |
272 |
- if (*buf == '\n') |
273 |
+ while ((linelen = getline(&buf, &buflen, fp)) != -1) { |
274 |
+ rmspace_len(buf, linelen); |
275 |
+ if (buf[0] == '\0') |
276 |
break; |
277 |
|
278 |
- if ((p = strchr(buf, '\n')) != NULL) |
279 |
- *p = 0; |
280 |
if ((p = strchr(buf, ':')) == NULL) |
281 |
continue; |
282 |
if (p[1] != ' ') |
283 |
@@ -1766,7 +1765,7 @@ _q_static queue * |
284 |
qmerge_add_set_file(const char *dir, const char *file, queue *set) |
285 |
{ |
286 |
FILE *fp; |
287 |
- size_t buflen; |
288 |
+ size_t buflen, linelen; |
289 |
char *buf, *fname; |
290 |
|
291 |
/* Find the file to read */ |
292 |
@@ -1781,8 +1780,8 @@ qmerge_add_set_file(const char *dir, const char *file, queue *set) |
293 |
|
294 |
/* Load each entry */ |
295 |
buf = NULL; |
296 |
- while (getline(&buf, &buflen, fp) != -1) { |
297 |
- rmspace(buf); |
298 |
+ while ((linelen = getline(&buf, &buflen, fp)) != -1) { |
299 |
+ rmspace_len(buf, linelen); |
300 |
set = add_set(buf, set); |
301 |
} |
302 |
free(buf); |
303 |
|
304 |
diff --git a/qsearch.c b/qsearch.c |
305 |
index 427580d..02d43ca 100644 |
306 |
--- a/qsearch.c |
307 |
+++ b/qsearch.c |
308 |
@@ -41,7 +41,7 @@ int qsearch_main(int argc, char **argv) |
309 |
char show_homepage = 0, show_name_only = 0; |
310 |
char search_desc = 0, search_all = 0, search_name = 1, search_cache = CACHE_EBUILD; |
311 |
const char *search_vars[] = { "DESCRIPTION=", "HOMEPAGE=" }; |
312 |
- size_t search_len, ebuild_len; |
313 |
+ size_t search_len, ebuild_len, linelen; |
314 |
int i, idx=0; |
315 |
|
316 |
DBG("argc=%d argv[0]=%s argv[1]=%s", |
317 |
@@ -80,9 +80,8 @@ int qsearch_main(int argc, char **argv) |
318 |
q = NULL; /* Silence a gcc warning. */ |
319 |
search_len = strlen(search_vars[idx]); |
320 |
|
321 |
- while (getline(&ebuild, &ebuild_len, fp) != -1) { |
322 |
- if ((p = strchr(ebuild, '\n')) != NULL) |
323 |
- *p = 0; |
324 |
+ while ((linelen = getline(&ebuild, &ebuild_len, fp)) != -1) { |
325 |
+ rmspace_len(ebuild, linelen); |
326 |
if (!ebuild[0]) |
327 |
continue; |
328 |
|
329 |
@@ -141,8 +140,8 @@ int qsearch_main(int argc, char **argv) |
330 |
|
331 |
char *buf = NULL; |
332 |
size_t buflen; |
333 |
- while (getline(&buf, &buflen, ebuildfp) != -1) { |
334 |
- if (strlen(buf) <= search_len) |
335 |
+ while ((linelen = getline(&buf, &buflen, ebuildfp)) != -1) { |
336 |
+ if (linelen <= search_len) |
337 |
continue; |
338 |
if (strncmp(buf, search_vars[idx], search_len) != 0) |
339 |
continue; |
340 |
|
341 |
diff --git a/quse.c b/quse.c |
342 |
index ab257bf..8ba0be1 100644 |
343 |
--- a/quse.c |
344 |
+++ b/quse.c |
345 |
@@ -82,7 +82,7 @@ static void |
346 |
quse_describe_flag(const char *overlay, unsigned int ind, unsigned int argc, char **argv) |
347 |
{ |
348 |
#define NUM_SEARCH_FILES ARRAY_SIZE(search_files) |
349 |
- size_t buflen; |
350 |
+ size_t buflen, linelen; |
351 |
char *buf, *p; |
352 |
unsigned int i, f; |
353 |
size_t s; |
354 |
@@ -110,13 +110,11 @@ quse_describe_flag(const char *overlay, unsigned int ind, unsigned int argc, cha |
355 |
if (fp[f] == NULL) |
356 |
continue; |
357 |
|
358 |
- while (getline(&buf, &buflen, fp[f]) != -1) { |
359 |
- if (buf[0] == '#' || buf[0] == '\n') |
360 |
+ while ((linelen = getline(&buf, &buflen, fp[f])) != -1) { |
361 |
+ rmspace_len(buf, linelen); |
362 |
+ if (buf[0] == '#' || buf[0] == '\0') |
363 |
continue; |
364 |
|
365 |
- if ((p = strrchr(buf, '\n')) != NULL) |
366 |
- *p = '\0'; |
367 |
- |
368 |
switch (f) { |
369 |
case 0: /* Global use.desc */ |
370 |
if (!strncmp(buf, argv[i], s)) |
371 |
@@ -193,13 +191,11 @@ quse_describe_flag(const char *overlay, unsigned int ind, unsigned int argc, cha |
372 |
/* Chop the trailing .desc for better display */ |
373 |
*p = '\0'; |
374 |
|
375 |
- while (getline(&buf, &buflen, fp[0]) != -1) { |
376 |
- if (buf[0] == '#' || buf[0] == '\n') |
377 |
+ while ((linelen = getline(&buf, &buflen, fp[0])) != -1) { |
378 |
+ rmspace_len(buf, linelen); |
379 |
+ if (buf[0] == '#' || buf[0] == '\0') |
380 |
continue; |
381 |
|
382 |
- if ((p = strrchr(buf, '\n')) != NULL) |
383 |
- *p = '\0'; |
384 |
- |
385 |
if ((p = strchr(buf, '-')) == NULL) { |
386 |
invalid_line: |
387 |
warn("Invalid line in '%s': %s", de->d_name, buf); |
388 |
@@ -235,7 +231,7 @@ int quse_main(int argc, char **argv) |
389 |
char buf1[_Q_PATH_MAX]; |
390 |
char buf2[_Q_PATH_MAX]; |
391 |
|
392 |
- size_t ebuildlen; |
393 |
+ size_t ebuildlen, linelen; |
394 |
char *ebuild; |
395 |
|
396 |
const char *search_var = NULL; |
397 |
@@ -284,12 +280,11 @@ int quse_main(int argc, char **argv) |
398 |
int portdir_fd = open(portdir, O_RDONLY|O_CLOEXEC|O_PATH); |
399 |
|
400 |
ebuild = NULL; |
401 |
- while (getline(&ebuild, &ebuildlen, fp) != -1) { |
402 |
+ while ((linelen = getline(&ebuild, &ebuildlen, fp)) != -1) { |
403 |
FILE *newfp; |
404 |
int fd; |
405 |
|
406 |
- if ((p = strchr(ebuild, '\n')) != NULL) |
407 |
- *p = 0; |
408 |
+ rmspace_len(ebuild, linelen); |
409 |
|
410 |
fd = openat(portdir_fd, ebuild, O_RDONLY|O_CLOEXEC); |
411 |
if (fd < 0) |