From owner-dev-commits-src-main@freebsd.org Tue Apr 27 03:09:22 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 352A05FC14D; Tue, 27 Apr 2021 03:09:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FTmvQ141Pz4tGj; Tue, 27 Apr 2021 03:09:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 1776A131F3; Tue, 27 Apr 2021 03:09:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 13R39LdO024106; Tue, 27 Apr 2021 03:09:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 13R39Lur024105; Tue, 27 Apr 2021 03:09:21 GMT (envelope-from git) Date: Tue, 27 Apr 2021 03:09:21 GMT Message-Id: <202104270309.13R39Lur024105@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 0cd4b781a6fa - main - pw(8): use openmemstream instead of sbuf(9) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0cd4b781a6fa1ed4ca04a7b642f41652e25bbc9b Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Apr 2021 03:09:22 -0000 The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=0cd4b781a6fa1ed4ca04a7b642f41652e25bbc9b commit 0cd4b781a6fa1ed4ca04a7b642f41652e25bbc9b Author: Baptiste Daroussin AuthorDate: 2021-04-27 03:05:13 +0000 Commit: Baptiste Daroussin CommitDate: 2021-04-27 03:09:07 +0000 pw(8): use openmemstream instead of sbuf(9) --- usr.sbin/pw/Makefile | 2 +- usr.sbin/pw/pw_conf.c | 86 +++++++++++++++++++++++++++------------------------ 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/usr.sbin/pw/Makefile b/usr.sbin/pw/Makefile index 8db96d90f6e2..353eac132c7d 100644 --- a/usr.sbin/pw/Makefile +++ b/usr.sbin/pw/Makefile @@ -8,7 +8,7 @@ SRCS= pw.c pw_conf.c pw_user.c pw_group.c pw_log.c pw_nis.c pw_vpw.c \ WARNS?= 3 -LIBADD= crypt util sbuf +LIBADD= crypt util .include diff --git a/usr.sbin/pw/pw_conf.c b/usr.sbin/pw/pw_conf.c index c446a663f082..b9b80f85cd48 100644 --- a/usr.sbin/pw/pw_conf.c +++ b/usr.sbin/pw/pw_conf.c @@ -31,9 +31,6 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ -#include -#include - #include #include #include @@ -414,9 +411,11 @@ write_userconfig(struct userconf *cnf, const char *file) { int fd; int i, j; - struct sbuf *buf; + FILE *buffp; FILE *fp; char cfgfile[MAXPATHLEN]; + char *buf; + size_t sz; if (file == NULL) { snprintf(cfgfile, sizeof(cfgfile), "%s/" _PW_CONF, @@ -431,117 +430,124 @@ write_userconfig(struct userconf *cnf, const char *file) close(fd); return (0); } - - buf = sbuf_new_auto(); + + sz = 0; + buf = NULL; + buffp = open_memstream(&buf, &sz); + if (buffp == NULL) + err(EXIT_FAILURE, "open_memstream()"); + for (i = _UC_NONE; i < _UC_FIELDS; i++) { int quote = 1; - sbuf_clear(buf); + if (buf != NULL) + memset(buf, 0, sz); + rewind(buffp); switch (i) { case _UC_DEFAULTPWD: - sbuf_cat(buf, boolean_str(cnf->default_password)); + fputs(boolean_str(cnf->default_password), buffp); break; case _UC_REUSEUID: - sbuf_cat(buf, boolean_str(cnf->reuse_uids)); + fputs(boolean_str(cnf->reuse_uids), buffp); break; case _UC_REUSEGID: - sbuf_cat(buf, boolean_str(cnf->reuse_gids)); + fputs(boolean_str(cnf->reuse_gids), buffp); break; case _UC_NISPASSWD: - sbuf_cat(buf, cnf->nispasswd ? cnf->nispasswd : ""); + fputs(cnf->nispasswd ? cnf->nispasswd : "", buffp); quote = 0; break; case _UC_DOTDIR: - sbuf_cat(buf, cnf->dotdir ? cnf->dotdir : - boolean_str(0)); + fputs(cnf->dotdir ? cnf->dotdir : boolean_str(0), + buffp); break; case _UC_NEWMAIL: - sbuf_cat(buf, cnf->newmail ? cnf->newmail : - boolean_str(0)); + fputs(cnf->newmail ? cnf->newmail : boolean_str(0), + buffp); break; case _UC_LOGFILE: - sbuf_cat(buf, cnf->logfile ? cnf->logfile : - boolean_str(0)); + fputs(cnf->logfile ? cnf->logfile : boolean_str(0), + buffp); break; case _UC_HOMEROOT: - sbuf_cat(buf, cnf->home); + fputs(cnf->home, buffp); break; case _UC_HOMEMODE: - sbuf_printf(buf, "%04o", cnf->homemode); + fprintf(buffp, "%04o", cnf->homemode); quote = 0; break; case _UC_SHELLPATH: - sbuf_cat(buf, cnf->shelldir); + fputs(cnf->shelldir, buffp); break; case _UC_SHELLS: for (j = 0; j < _UC_MAXSHELLS && system_shells[j] != NULL; j++) - sbuf_printf(buf, "%s\"%s\"", j ? + fprintf(buffp, "%s\"%s\"", j ? "," : "", system_shells[j]); quote = 0; break; case _UC_DEFAULTSHELL: - sbuf_cat(buf, cnf->shell_default ? - cnf->shell_default : bourne_shell); + fputs(cnf->shell_default ? cnf->shell_default : + bourne_shell, buffp); break; case _UC_DEFAULTGROUP: - sbuf_cat(buf, cnf->default_group ? - cnf->default_group : ""); + fputs(cnf->default_group ? cnf->default_group : "", + buffp); break; case _UC_EXTRAGROUPS: for (j = 0; cnf->groups != NULL && j < (int)cnf->groups->sl_cur; j++) - sbuf_printf(buf, "%s\"%s\"", j ? + fprintf(buffp, "%s\"%s\"", j ? "," : "", cnf->groups->sl_str[j]); quote = 0; break; case _UC_DEFAULTCLASS: - sbuf_cat(buf, cnf->default_class ? - cnf->default_class : ""); + fputs(cnf->default_class ? cnf->default_class : "", + buffp); break; case _UC_MINUID: - sbuf_printf(buf, "%ju", (uintmax_t)cnf->min_uid); + fprintf(buffp, "%ju", (uintmax_t)cnf->min_uid); quote = 0; break; case _UC_MAXUID: - sbuf_printf(buf, "%ju", (uintmax_t)cnf->max_uid); + fprintf(buffp, "%ju", (uintmax_t)cnf->max_uid); quote = 0; break; case _UC_MINGID: - sbuf_printf(buf, "%ju", (uintmax_t)cnf->min_gid); + fprintf(buffp, "%ju", (uintmax_t)cnf->min_gid); quote = 0; break; case _UC_MAXGID: - sbuf_printf(buf, "%ju", (uintmax_t)cnf->max_gid); + fprintf(buffp, "%ju", (uintmax_t)cnf->max_gid); quote = 0; break; case _UC_EXPIRE: - sbuf_printf(buf, "%jd", (intmax_t)cnf->expire_days); + fprintf(buffp, "%jd", (intmax_t)cnf->expire_days); quote = 0; break; case _UC_PASSWORD: - sbuf_printf(buf, "%jd", (intmax_t)cnf->password_days); + fprintf(buffp, "%jd", (intmax_t)cnf->password_days); quote = 0; break; case _UC_NONE: break; } - sbuf_finish(buf); + fflush(buffp); if (comments[i]) fputs(comments[i], fp); if (*kwds[i]) { if (quote) - fprintf(fp, "%s = \"%s\"\n", kwds[i], - sbuf_data(buf)); + fprintf(fp, "%s = \"%s\"\n", kwds[i], buf); else - fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf)); + fprintf(fp, "%s = %s\n", kwds[i], buf); #if debugging - printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf)); + printf("WROTE: %s = %s\n", kwds[i], buf); #endif } } - sbuf_delete(buf); + fclose(buffp); + free(buf); return (fclose(fp) != EOF); }