Author: dteske
Date: Thu Jun 21 14:41:58 2018
New Revision: 335484
URL: https://svnweb.freebsd.org/changeset/base/335484

Log:
  MFC r335280-r335281, r335302: sysrc.subr updates
  
  r335280: Fix handling of files with missing newline at EOF
  r335302: Fix a comment for accuracy
  
  PR:           bin/203435
  Reported by:  Andreas Sommer <andreas.somme...@googlemail.com>
  
  r335281: Fix display when value is "-n"
  
  PR:           bin/226406
  Reported by:  Marius Halden <marius.hal...@modirum.com>
  
  Sponsored by: Smule, Inc.

Modified:
  stable/11/usr.sbin/bsdconfig/share/sysrc.subr
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.sbin/bsdconfig/share/sysrc.subr
==============================================================================
--- stable/11/usr.sbin/bsdconfig/share/sysrc.subr       Thu Jun 21 14:30:14 
2018        (r335483)
+++ stable/11/usr.sbin/bsdconfig/share/sysrc.subr       Thu Jun 21 14:41:58 
2018        (r335484)
@@ -200,7 +200,7 @@ f_sysrc_get()
                # such as "${varname?}" and "${varname:?}" (see "Parameter
                # Expansion" in sh(1) for more information).
                #
-               eval echo '"${'"$1"'}"' 2> /dev/null
+               eval printf "'%s\\n'" '"${'"$1"'}"' 2> /dev/null
        )
 }
 
@@ -338,7 +338,7 @@ f_sysrc_get_default()
                # such as "${varname?}" and "${varname:?}" (see "Parameter
                # Expansion" in sh(1) for more information).
                #
-               eval echo '"${'"$1"'}"' 2> /dev/null
+               eval printf "'%s\\n'" '"${'"$1"'}"' 2> /dev/null
        )
 }
 
@@ -556,9 +556,16 @@ f_sysrc_set()
        fi
 
        #
-       # If not found, append new value to last file and return.
+       # If not found, append new value to first 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-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to