Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 17 Jun 2018 06:03:48 +0000 (UTC)
From:      Devin Teske <dteske@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r335280 - head/usr.sbin/bsdconfig/share
Message-ID:  <201806170603.w5H63mHA076158@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dteske
Date: Sun Jun 17 06:03:48 2018
New Revision: 335280
URL: https://svnweb.freebsd.org/changeset/base/335280

Log:
  sysrc.subr: Fix handling of files with missing newline at EOF
  
  PR:		bin/203435
  Reported by:	Andreas Sommer <andreas.sommer87@googlemail.com>
  MFC after:	1 week
  X-MFC-to:	stable/11
  Sponsored by:	Smule, Inc.

Modified:
  head/usr.sbin/bsdconfig/share/sysrc.subr

Modified: head/usr.sbin/bsdconfig/share/sysrc.subr
==============================================================================
--- head/usr.sbin/bsdconfig/share/sysrc.subr	Sun Jun 17 05:55:31 2018	(r335279)
+++ head/usr.sbin/bsdconfig/share/sysrc.subr	Sun Jun 17 06:03:48 2018	(r335280)
@@ -559,6 +559,13 @@ f_sysrc_set()
 	# If not found, append new value to last file and return.
 	#
 	if [ "$not_found" ]; then
+		# Add a newline if missing before appending to the file
+		awk 'BEGIN { wc = 0 } NR == 1 {
+			(cmd = "wc -l " FILENAME) | getline
+			close(cmd)
+			wc = $1
+		} END { exit wc != NR }' "$file" ||
+			echo >> "$file" || return $?
 		echo "$varname=\"$new_value\"" >> "$file"
 		return $?
 	fi
@@ -606,8 +613,11 @@ f_sysrc_set()
 	#
 	# Operate on the matching file, replacing only the last occurrence.
 	#
+	# Use awk to ensure LF at end of each line, else files without ending
+	# LF will trigger a bug in `tail -r' where last two lines are joined.
+	#
 	local new_contents retval
-	new_contents=$( tail -r $file 2> /dev/null )
+	new_contents=$( awk 1 "$file" 2> /dev/null | tail -r )
 	new_contents=$( echo "$new_contents" | awk -v varname="$varname" \
 		-v new_value="$new_value" "$f_sysrc_set_awk" )
 	retval=$?



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201806170603.w5H63mHA076158>