svn commit: r264781 - head/usr.sbin/pw

Baptiste Daroussin bapt at FreeBSD.org
Tue Apr 22 21:07:57 UTC 2014


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 <stdio.h>
 #include <stdlib.h>
 #include <string.h>

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;


More information about the svn-src-all mailing list