From owner-svn-src-all@FreeBSD.ORG Tue Apr 22 21:07:57 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 2B82E96E; Tue, 22 Apr 2014 21:07:57 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id F268E1C40; Tue, 22 Apr 2014 21:07:56 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s3ML7uYb033195; Tue, 22 Apr 2014 21:07:56 GMT (envelope-from bapt@svn.freebsd.org) Received: (from bapt@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s3ML7ujp033193; Tue, 22 Apr 2014 21:07:56 GMT (envelope-from bapt@svn.freebsd.org) Message-Id: <201404222107.s3ML7ujp033193@svn.freebsd.org> From: Baptiste Daroussin Date: Tue, 22 Apr 2014 21:07:56 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r264781 - head/usr.sbin/pw X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Apr 2014 21:07:57 -0000 Author: bapt Date: Tue Apr 22 21:07:56 2014 New Revision: 264781 URL: http://svnweb.freebsd.org/changeset/base/264781 Log: Simplify reading pw.conf(5) by using getline(3) Modified: head/usr.sbin/pw/pw.h head/usr.sbin/pw/pw_conf.c Modified: head/usr.sbin/pw/pw.h ============================================================================== --- head/usr.sbin/pw/pw.h Tue Apr 22 21:05:11 2014 (r264780) +++ head/usr.sbin/pw/pw.h Tue Apr 22 21:07:56 2014 (r264781) @@ -26,6 +26,7 @@ * $FreeBSD$ */ +#define _WITH_GETLINE #include #include #include Modified: head/usr.sbin/pw/pw_conf.c ============================================================================== --- head/usr.sbin/pw/pw_conf.c Tue Apr 22 21:05:11 2014 (r264780) +++ head/usr.sbin/pw/pw_conf.c Tue Apr 22 21:07:56 2014 (r264781) @@ -226,35 +226,21 @@ newstr(char const * p) struct userconf * read_userconfig(char const * file) { - FILE *fp; + FILE *fp; + char *buf, *p; + size_t linecap; + ssize_t linelen; + + buf = NULL; + linecap = 0; extendarray(&config.groups, &config.numgroups, 200); memset(config.groups, 0, config.numgroups * sizeof(char *)); if (file == NULL) file = _PATH_PW_CONF; - if ((fp = fopen(file, "r")) != NULL) { - int buflen = LNBUFSZ; - char *buf = malloc(buflen); - - nextline: - while (fgets(buf, buflen, fp) != NULL) { - char *p; - - while ((p = strchr(buf, '\n')) == NULL) { - int l; - if (extendline(&buf, &buflen, buflen + LNBUFSZ) == -1) { - int ch; - while ((ch = fgetc(fp)) != '\n' && ch != EOF); - goto nextline; /* Ignore it */ - } - l = strlen(buf); - if (fgets(buf + l, buflen - l, fp) == NULL) - break; /* Unterminated last line */ - } - - if (p != NULL) - *p = '\0'; + if ((fp = fopen(file, "r")) != NULL) { + while ((linelen = getline(&buf, &linecap, fp)) > 0) { if (*buf && (p = strtok(buf, " \t\r\n=")) != NULL && *p != '#') { static char const toks[] = " \t\r\n,="; char *q = strtok(NULL, toks); @@ -368,7 +354,8 @@ read_userconfig(char const * file) } } } - free(buf); + if (linecap > 0) + free(buf); fclose(fp); } return &config;