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.somme...@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=$?
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to