diff options
author | okan | 2017-07-10 13:36:58 +0000 |
---|---|---|
committer | okan | 2017-07-10 13:36:58 +0000 |
commit | 1002ccec68fff4b751e53ae0720facd692e0caae (patch) | |
tree | da74a9de3cb2f508d33f873b8fa4753b90244052 /kbfunc.c | |
parent | 86f80792e891080fdbef573212fd38c2ce7435cb (diff) | |
download | cwm-1002ccec68fff4b751e53ae0720facd692e0caae.tar.gz |
Replace fgetln(3) with POSIX getline(3); inspired by brynet and Ingo.
feedback and ok brynet@
Diffstat (limited to 'kbfunc.c')
-rw-r--r-- | kbfunc.c | 23 |
1 files changed, 11 insertions, 12 deletions
@@ -15,7 +15,7 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $OpenBSD: kbfunc.c,v 1.145 2017/05/09 13:40:18 okan Exp $ + * $OpenBSD: kbfunc.c,v 1.146 2017/07/10 13:36:58 okan Exp $ */ #include <sys/types.h> @@ -468,6 +468,7 @@ kbfunc_menu_ssh(void *ctx, struct cargs *cargs) char path[PATH_MAX]; int l; size_t len; + ssize_t slen; TAILQ_FOREACH(cmd, &Conf.cmdq, entry) { if (strcmp(cmd->name, "term") == 0) @@ -481,20 +482,16 @@ kbfunc_menu_ssh(void *ctx, struct cargs *cargs) } lbuf = NULL; - while ((buf = fgetln(fp, &len))) { - if (buf[len - 1] == '\n') - buf[len - 1] = '\0'; - else { - /* EOF without EOL, copy and add the NUL */ - lbuf = xmalloc(len + 1); - (void)memcpy(lbuf, buf, len); - lbuf[len] = '\0'; - buf = lbuf; - } + len = 0; + while ((slen = getline(&lbuf, &len, fp)) != -1) { + buf = lbuf; + if (buf[slen - 1] == '\n') + buf[slen - 1] = '\0'; + /* skip hashed hosts */ if (strncmp(buf, HASH_MARKER, strlen(HASH_MARKER)) == 0) continue; - for (p = buf; *p != ',' && *p != ' ' && p != buf + len; p++) { + for (p = buf; *p != ',' && *p != ' ' && p != buf + slen; p++) { /* do nothing */ } /* ignore badness */ @@ -504,6 +501,8 @@ kbfunc_menu_ssh(void *ctx, struct cargs *cargs) menuq_add(&menuq, NULL, "%s", hostbuf); } free(lbuf); + if (ferror(fp)) + err(1, "%s", path); (void)fclose(fp); menu: if ((mi = menu_filter(sc, &menuq, "ssh", NULL, (CWM_MENU_DUMMY), |