commit:     92de436359da8f14b7dbef5f62af959095b79d06
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Fri Nov 27 18:14:14 2015 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Fri Nov 27 18:14:14 2015 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=92de4363

use the return value of getline

This allows us to avoid calling strlen to get a value getline already
calculated.  We can also pass this value on to rmspace to let it trim
space for us.

 libq/colors.c  | 16 ++++++----------
 libq/profile.c |  7 ++++---
 libq/rmspace.c |  8 ++++++--
 main.c         |  1 +
 qcache.c       |  9 ++++-----
 qfile.c        |  8 ++++----
 qlop.c         | 19 ++++++++-----------
 qmerge.c       | 15 +++++++--------
 qsearch.c      | 11 +++++------
 quse.c         | 25 ++++++++++---------------
 10 files changed, 55 insertions(+), 64 deletions(-)

diff --git a/libq/colors.c b/libq/colors.c
index bd2c1e0..6ddcdda 100644
--- a/libq/colors.c
+++ b/libq/colors.c
@@ -50,7 +50,7 @@ void color_remap(void)
 {
        FILE *fp;
        unsigned int i;
-       size_t buflen;
+       size_t buflen, linelen;
        char *buf;
        char *p;
        unsigned int lineno = 0;
@@ -59,22 +59,18 @@ void color_remap(void)
                return;
 
        buf = NULL;
-       while (getline(&buf, &buflen, fp) != -1) {
+       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
                lineno++;
                /* eat comments */
                if ((p = strchr(buf, '#')) != NULL)
                        *p = '\0';
 
-               if (strchr(buf, '=') == NULL)
-                       continue;
-
-               /* eat the end of the buffer first */
-               if ((p = strchr(buf, '\r')) != NULL)
-                       *p = 0;
-               if ((p = strchr(buf, '\n')) != NULL)
-                       *p = 0;
+               rmspace_len(buf, linelen);
 
                p = strchr(buf, '=');
+               if (p == NULL)
+                       continue;
+
                *p++ = 0; /* split the pair */
                for (i = 0; i < ARRAY_SIZE(color_pairs); ++i)
                        if (strcmp(buf, color_pairs[i].name) == 0) {

diff --git a/libq/profile.c b/libq/profile.c
index 75f3cb0..66e5caf 100644
--- a/libq/profile.c
+++ b/libq/profile.c
@@ -6,7 +6,7 @@ q_profile_walk_at(int dir_fd, const char *dir, const char *file,
 {
        FILE *fp;
        int subdir_fd, fd;
-       size_t buflen;
+       size_t buflen, linelen;
        char *buf;
 
        /* Pop open this profile dir */
@@ -46,13 +46,14 @@ q_profile_walk_at(int dir_fd, const char *dir, const char 
*file,
        }
 
        buf = NULL;
-       while (getline(&buf, &buflen, fp) != -1) {
+       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
                char *s;
 
+               rmspace_len(buf, linelen);
+
                s = strchr(buf, '#');
                if (s)
                        *s = '\0';
-               rmspace(buf);
 
                data = q_profile_walk_at(subdir_fd, buf, file, callback, data);
        }

diff --git a/libq/rmspace.c b/libq/rmspace.c
index d374d7f..b8ac5a3 100644
--- a/libq/rmspace.c
+++ b/libq/rmspace.c
@@ -1,9 +1,8 @@
 
 /* removed leading/trailing extraneous white space */
-static char *rmspace(char *s)
+static char *rmspace_len(char *s, size_t len)
 {
        char *p;
-       size_t len = strlen(s);
        /* find the start of trailing space and set it to \0 */
        for (p = s + len - 1; (p >= s && isspace(*p)); --p)
                continue;
@@ -17,3 +16,8 @@ static char *rmspace(char *s)
                memmove(s, p, len - (p - s) + 1);
        return s;
 }
+
+static char *rmspace(char *s)
+{
+       return rmspace_len(s, strlen(s));
+}

diff --git a/main.c b/main.c
index 10af2db..42a3f26 100644
--- a/main.c
+++ b/main.c
@@ -15,6 +15,7 @@ static bool eat_file_fd(int, char **, size_t *);
 static bool eat_file_at(int, const char *, char **, size_t *);
 int rematch(const char *, const char *, int);
 static char *rmspace(char *);
+static char *rmspace_len(char *, size_t);
 
 void initialize_portage_env(void);
 void initialize_ebuild_flat(void);

diff --git a/qcache.c b/qcache.c
index 0968d33..5f65fe0 100644
--- a/qcache.c
+++ b/qcache.c
@@ -319,10 +319,10 @@ _q_static
 portage_cache *qcache_read_cache_file(const char *filename)
 {
        struct stat s;
-       char *ptr, *buf;
+       char *buf;
        FILE *f;
        portage_cache *ret = NULL;
-       size_t len, buflen;
+       size_t len, buflen, linelen;
 
        if ((f = fopen(filename, "r")) == NULL)
                goto err;
@@ -336,9 +336,8 @@ portage_cache *qcache_read_cache_file(const char *filename)
        len = sizeof(*ret) + s.st_size + 1;
        ret = xzalloc(len);
 
-       while (getline(&buf, &buflen, f) != -1) {
-               if ((ptr = strrchr(buf, '\n')) != NULL)
-                       *ptr = 0;
+       while ((linelen = getline(&buf, &buflen, f)) != -1) {
+               rmspace_len(buf, linelen);
 
                if (strncmp(buf, "DEPEND=", 7) == 0)
                        ret->DEPEND = xstrdup(buf + 7);

diff --git a/qfile.c b/qfile.c
index a32e512..6d36e96 100644
--- a/qfile.c
+++ b/qfile.c
@@ -513,10 +513,10 @@ int qfile_main(int argc, char **argv)
                        for (i = 0; i < qargc; ++i)
                                free(qargv[i]);
                        qargc = 0;
-                       while (getline(&state.buf, &state.buflen, args_file) != 
-1) {
-                               if ((p = strchr(state.buf, '\n')) != NULL)
-                                       *p = '\0';
-                               if (state.buf == p)
+                       size_t linelen;
+                       while ((linelen = getline(&state.buf, &state.buflen, 
args_file)) != -1) {
+                               rmspace_len(state.buf, linelen);
+                               if (state.buf[0] == '\0')
                                        continue;
                                qargv[qargc] = xstrdup(state.buf);
                                if (++qargc >= max_args)

diff --git a/qlop.c b/qlop.c
index 390865a..f2f97c6 100644
--- a/qlop.c
+++ b/qlop.c
@@ -240,7 +240,7 @@ _q_static void
 show_emerge_history(char listflag, int argc, char **argv, const char *logfile)
 {
        FILE *fp;
-       size_t buflen;
+       size_t buflen, linelen;
        char *buf, merged;
        char *p, *q;
        int i;
@@ -252,8 +252,8 @@ show_emerge_history(char listflag, int argc, char **argv, 
const char *logfile)
        }
 
        buf = NULL;
-       while (getline(&buf, &buflen, fp) != -1) {
-               if (strlen(buf) < 30)
+       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
+               if (linelen < 30)
                        continue;
 
                for (i = 0; i < argc; ++i)
@@ -262,8 +262,7 @@ show_emerge_history(char listflag, int argc, char **argv, 
const char *logfile)
                if (argc && i == argc)
                        continue;
 
-               if ((p = strchr(buf, '\n')) != NULL)
-                       *p = 0;
+               rmspace_len(buf, linelen);
                if ((p = strchr(buf, ':')) == NULL)
                        continue;
                *p = 0;
@@ -327,7 +326,7 @@ _q_static void
 show_sync_history(const char *logfile)
 {
        FILE *fp;
-       size_t buflen, len;
+       size_t buflen, linelen;
        char *buf, *p;
        time_t t;
 
@@ -338,10 +337,9 @@ show_sync_history(const char *logfile)
 
        buf = NULL;
        /* Just find the finish lines. */
-       while (getline(&buf, &buflen, fp) != -1) {
-               len = strlen(buf);
+       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
                /* This cuts out like ~10% of the log. */
-               if (len < 35)
+               if (linelen < 35)
                        continue;
 
                /* Make sure there's a timestamp in here. */
@@ -353,8 +351,7 @@ show_sync_history(const char *logfile)
                        continue;
                p += 19;
 
-               if (buf[len - 1] == '\n')
-                       buf[len - 1] = '\0';
+               rmspace_len(buf, linelen);
 
                t = (time_t)atol(buf);
 

diff --git a/qmerge.c b/qmerge.c
index ddc0a10..297f939 100644
--- a/qmerge.c
+++ b/qmerge.c
@@ -1633,7 +1633,7 @@ _q_static int
 parse_packages(queue *todo)
 {
        FILE *fp;
-       size_t buflen;
+       size_t buflen, linelen;
        char *buf, *p;
        struct pkg_t Pkg;
        depend_atom *pkg_atom;
@@ -1645,12 +1645,11 @@ parse_packages(queue *todo)
        repo[0] = '\0';
 
        /* First consume the header with the common data. */
-       while (getline(&buf, &buflen, fp) != -1) {
-               if (*buf == '\n')
+       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
+               rmspace_len(buf, linelen);
+               if (buf[0] == '\0')
                        break;
 
-               if ((p = strchr(buf, '\n')) != NULL)
-                       *p = 0;
                if ((p = strchr(buf, ':')) == NULL)
                        continue;
                if (p[1] != ' ')
@@ -1766,7 +1765,7 @@ _q_static queue *
 qmerge_add_set_file(const char *dir, const char *file, queue *set)
 {
        FILE *fp;
-       size_t buflen;
+       size_t buflen, linelen;
        char *buf, *fname;
 
        /* Find the file to read */
@@ -1781,8 +1780,8 @@ qmerge_add_set_file(const char *dir, const char *file, 
queue *set)
 
        /* Load each entry */
        buf = NULL;
-       while (getline(&buf, &buflen, fp) != -1) {
-               rmspace(buf);
+       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
+               rmspace_len(buf, linelen);
                set = add_set(buf, set);
        }
        free(buf);

diff --git a/qsearch.c b/qsearch.c
index 427580d..02d43ca 100644
--- a/qsearch.c
+++ b/qsearch.c
@@ -41,7 +41,7 @@ int qsearch_main(int argc, char **argv)
        char show_homepage = 0, show_name_only = 0;
        char search_desc = 0, search_all = 0, search_name = 1, search_cache = 
CACHE_EBUILD;
        const char *search_vars[] = { "DESCRIPTION=", "HOMEPAGE=" };
-       size_t search_len, ebuild_len;
+       size_t search_len, ebuild_len, linelen;
        int i, idx=0;
 
        DBG("argc=%d argv[0]=%s argv[1]=%s",
@@ -80,9 +80,8 @@ int qsearch_main(int argc, char **argv)
        q = NULL; /* Silence a gcc warning. */
        search_len = strlen(search_vars[idx]);
 
-       while (getline(&ebuild, &ebuild_len, fp) != -1) {
-               if ((p = strchr(ebuild, '\n')) != NULL)
-                       *p = 0;
+       while ((linelen = getline(&ebuild, &ebuild_len, fp)) != -1) {
+               rmspace_len(ebuild, linelen);
                if (!ebuild[0])
                        continue;
 
@@ -141,8 +140,8 @@ int qsearch_main(int argc, char **argv)
 
                                char *buf = NULL;
                                size_t buflen;
-                               while (getline(&buf, &buflen, ebuildfp) != -1) {
-                                       if (strlen(buf) <= search_len)
+                               while ((linelen = getline(&buf, &buflen, 
ebuildfp)) != -1) {
+                                       if (linelen <= search_len)
                                                continue;
                                        if (strncmp(buf, search_vars[idx], 
search_len) != 0)
                                                continue;

diff --git a/quse.c b/quse.c
index ab257bf..8ba0be1 100644
--- a/quse.c
+++ b/quse.c
@@ -82,7 +82,7 @@ static void
 quse_describe_flag(const char *overlay, unsigned int ind, unsigned int argc, 
char **argv)
 {
 #define NUM_SEARCH_FILES ARRAY_SIZE(search_files)
-       size_t buflen;
+       size_t buflen, linelen;
        char *buf, *p;
        unsigned int i, f;
        size_t s;
@@ -110,13 +110,11 @@ quse_describe_flag(const char *overlay, unsigned int ind, 
unsigned int argc, cha
                        if (fp[f] == NULL)
                                continue;
 
-                       while (getline(&buf, &buflen, fp[f]) != -1) {
-                               if (buf[0] == '#' || buf[0] == '\n')
+                       while ((linelen = getline(&buf, &buflen, fp[f])) != -1) 
{
+                               rmspace_len(buf, linelen);
+                               if (buf[0] == '#' || buf[0] == '\0')
                                        continue;
 
-                               if ((p = strrchr(buf, '\n')) != NULL)
-                                       *p = '\0';
-
                                switch (f) {
                                        case 0: /* Global use.desc */
                                                if (!strncmp(buf, argv[i], s))
@@ -193,13 +191,11 @@ quse_describe_flag(const char *overlay, unsigned int ind, 
unsigned int argc, cha
                /* Chop the trailing .desc for better display */
                *p = '\0';
 
-               while (getline(&buf, &buflen, fp[0]) != -1) {
-                       if (buf[0] == '#' || buf[0] == '\n')
+               while ((linelen = getline(&buf, &buflen, fp[0])) != -1) {
+                       rmspace_len(buf, linelen);
+                       if (buf[0] == '#' || buf[0] == '\0')
                                continue;
 
-                       if ((p = strrchr(buf, '\n')) != NULL)
-                               *p = '\0';
-
                        if ((p = strchr(buf, '-')) == NULL) {
  invalid_line:
                                warn("Invalid line in '%s': %s", de->d_name, 
buf);
@@ -235,7 +231,7 @@ int quse_main(int argc, char **argv)
        char buf1[_Q_PATH_MAX];
        char buf2[_Q_PATH_MAX];
 
-       size_t ebuildlen;
+       size_t ebuildlen, linelen;
        char *ebuild;
 
        const char *search_var = NULL;
@@ -284,12 +280,11 @@ int quse_main(int argc, char **argv)
        int portdir_fd = open(portdir, O_RDONLY|O_CLOEXEC|O_PATH);
 
        ebuild = NULL;
-       while (getline(&ebuild, &ebuildlen, fp) != -1) {
+       while ((linelen = getline(&ebuild, &ebuildlen, fp)) != -1) {
                FILE *newfp;
                int fd;
 
-               if ((p = strchr(ebuild, '\n')) != NULL)
-                       *p = 0;
+               rmspace_len(ebuild, linelen);
 
                fd = openat(portdir_fd, ebuild, O_RDONLY|O_CLOEXEC);
                if (fd < 0)

Reply via email to