diff options
author | Sunil Nimmagadda <sunil@cvs.openbsd.org> | 2015-10-11 12:50:01 +0000 |
---|---|---|
committer | Sunil Nimmagadda <sunil@cvs.openbsd.org> | 2015-10-11 12:50:01 +0000 |
commit | dee4e632478cc4466d2e447aa2912f7e5224a14e (patch) | |
tree | fc53383b5a8bf1174d22d0e6926ac17d3cdbf4f3 | |
parent | 6f1157191469774ffe02f8f6cab1336543ad5a20 (diff) |
Convert some fgetln to getline.
Ok gilles@, giovanni@, millert@
-rw-r--r-- | usr.sbin/smtpd/table_ldap.c | 22 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_passwd.c | 28 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_sqlite.c | 23 | ||||
-rw-r--r-- | usr.sbin/smtpd/table_static.c | 18 |
4 files changed, 28 insertions, 63 deletions
diff --git a/usr.sbin/smtpd/table_ldap.c b/usr.sbin/smtpd/table_ldap.c index 5cc48f76b78..cc650f33b23 100644 --- a/usr.sbin/smtpd/table_ldap.c +++ b/usr.sbin/smtpd/table_ldap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_ldap.c,v 1.14 2015/10/06 14:02:25 stsp Exp $ */ +/* $OpenBSD: table_ldap.c,v 1.15 2015/10/11 12:50:00 sunil Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -297,28 +297,18 @@ ldap_parse_attributes(struct query *query, const char *key, const char *line, static int ldap_config(void) { - size_t flen; + size_t sz = 0; + ssize_t flen; FILE *fp; - char *key, *value, *buf, *lbuf; + char *key, *value, *buf = NULL; fp = fopen(config, "r"); if (fp == NULL) return (0); - lbuf = NULL; - while ((buf = fgetln(fp, &flen))) { + while ((flen = getline(&buf, &sz, fp)) != -1) { if (buf[flen - 1] == '\n') buf[flen - 1] = '\0'; - else { - lbuf = malloc(flen + 1); - if (lbuf == NULL) { - log_warn("warn: table-ldap: malloc"); - return (0); - } - memcpy(lbuf, buf, flen); - lbuf[flen] = '\0'; - buf = lbuf; - } key = buf; while (isspace((unsigned char)*key)) @@ -390,7 +380,7 @@ ldap_config(void) log_warnx("warn: table-ldap: bogus entry \"%s\"", key); } - free(lbuf); + free(buf); fclose(fp); return (1); } diff --git a/usr.sbin/smtpd/table_passwd.c b/usr.sbin/smtpd/table_passwd.c index bc1524664f3..7cef61a83e4 100644 --- a/usr.sbin/smtpd/table_passwd.c +++ b/usr.sbin/smtpd/table_passwd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_passwd.c,v 1.10 2015/01/20 17:37:54 deraadt Exp $ */ +/* $OpenBSD: table_passwd.c,v 1.11 2015/10/11 12:50:00 sunil Exp $ */ /* * Copyright (c) 2013 Gilles Chehade <gilles@poolp.org> @@ -88,9 +88,10 @@ static int table_passwd_update(void) { FILE *fp; - char *buf, *lbuf = NULL; + char *buf = NULL, *p; char tmp[LINE_MAX]; - size_t len; + size_t sz = 0; + ssize_t len; char *line; struct passwd pw; struct dict *npasswd; @@ -106,17 +107,9 @@ table_passwd_update(void) dict_init(npasswd); - while ((buf = fgetln(fp, &len))) { + while ((len = getline(&buf, &sz, fp)) != -1) { if (buf[len - 1] == '\n') buf[len - 1] = '\0'; - else { - /* EOF without EOL, copy and add the NUL */ - if ((lbuf = malloc(len + 1)) == NULL) - err(1, NULL); - memcpy(lbuf, buf, len); - lbuf[len] = '\0'; - buf = lbuf; - } if (strlcpy(tmp, buf, sizeof tmp) >= sizeof tmp) { log_warnx("warn: table-passwd: line too long"); @@ -130,13 +123,12 @@ table_passwd_update(void) err(1, NULL); dict_set(npasswd, pw.pw_name, line); } - free(lbuf); fclose(fp); /* swap passwd table and release old one*/ if (passwd) - while (dict_poproot(passwd, (void**)&buf)) - free(buf); + while (dict_poproot(passwd, (void**)&p)) + free(p); passwd = npasswd; return (1); @@ -144,12 +136,12 @@ table_passwd_update(void) err: if (fp) fclose(fp); - free(lbuf); + free(buf); /* release passwd table */ if (npasswd) { - while (dict_poproot(npasswd, (void**)&buf)) - free(buf); + while (dict_poproot(npasswd, (void**)&p)) + free(p); free(npasswd); } return (0); diff --git a/usr.sbin/smtpd/table_sqlite.c b/usr.sbin/smtpd/table_sqlite.c index dd9065b7383..f65be0e59bc 100644 --- a/usr.sbin/smtpd/table_sqlite.c +++ b/usr.sbin/smtpd/table_sqlite.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_sqlite.c,v 1.16 2015/01/20 17:37:54 deraadt Exp $ */ +/* $OpenBSD: table_sqlite.c,v 1.17 2015/10/11 12:50:00 sunil Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -172,11 +172,11 @@ table_sqlite_update(void) sqlite3_stmt *_stmt_fetch_source; char *_query_fetch_source; char *queries[SQL_MAX]; - size_t flen; - size_t _source_refresh; + ssize_t flen; + size_t sz = 0, _source_refresh; int _source_expire; FILE *fp; - char *key, *value, *buf, *lbuf, *dbpath; + char *key, *value, *buf = NULL, *dbpath; const char *e; int i, ret; long long ll; @@ -199,20 +199,9 @@ table_sqlite_update(void) if (fp == NULL) return (0); - lbuf = NULL; - while ((buf = fgetln(fp, &flen))) { + while ((flen = getline(&buf, &sz, fp)) != -1) { if (buf[flen - 1] == '\n') buf[flen - 1] = '\0'; - else { - lbuf = malloc(flen + 1); - if (lbuf == NULL) { - log_warn("warn: table-sqlite: malloc"); - return (0); - } - memcpy(lbuf, buf, flen); - lbuf[flen] = '\0'; - buf = lbuf; - } key = buf; while (isspace((unsigned char)*key)) @@ -348,7 +337,7 @@ table_sqlite_update(void) free(dbpath); free(_query_fetch_source); - free(lbuf); + free(buf); fclose(fp); return (ret); } diff --git a/usr.sbin/smtpd/table_static.c b/usr.sbin/smtpd/table_static.c index 9224a576834..5ce96894e56 100644 --- a/usr.sbin/smtpd/table_static.c +++ b/usr.sbin/smtpd/table_static.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table_static.c,v 1.10 2015/01/20 17:37:54 deraadt Exp $ */ +/* $OpenBSD: table_static.c,v 1.11 2015/10/11 12:50:00 sunil Exp $ */ /* * Copyright (c) 2013 Eric Faurot <eric@openbsd.org> @@ -82,8 +82,9 @@ static int table_static_parse(struct table *t, const char *config, enum table_type type) { FILE *fp; - char *buf, *lbuf; - size_t flen; + char *buf = NULL; + size_t sz = 0; + ssize_t flen; char *keyp; char *valp; size_t ret = 0; @@ -92,16 +93,9 @@ table_static_parse(struct table *t, const char *config, enum table_type type) if (fp == NULL) return 0; - lbuf = NULL; - while ((buf = fgetln(fp, &flen))) { + while ((flen = getline(&buf, &sz, fp)) != -1) { if (buf[flen - 1] == '\n') buf[flen - 1] = '\0'; - else { - lbuf = xmalloc(flen + 1, "table_config_parse"); - memcpy(lbuf, buf, flen); - lbuf[flen] = '\0'; - buf = lbuf; - } keyp = buf; while (isspace((unsigned char)*keyp)) @@ -142,7 +136,7 @@ table_static_parse(struct table *t, const char *config, enum table_type type) ret = 1; end: - free(lbuf); + free(buf); fclose(fp); return ret; } |