r19:
[carnet-tools-cn.git] / functions.sh
index 45d15e2..5bb7ae6 100644 (file)
@@ -4,17 +4,21 @@ cp_get_ifaddr() {
   local ifaddr interface
 
   interface="$1"
-  [ -z "$interface" ] && interface=lo
+  if [ -z "$interface" ]; then
+    cp_get_ifdefault dev
+    interface="$RET"
+    [ -z "$interface" ] && interface=lo
+  fi
 
   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
-    [ -z "$CP_ECHO_RETURN" ] || echo "cp_get_ifaddr: $interface: No such interface"
+    echo "cp_get_ifaddr: $interface: No such interface" 1>&2
     return 1
   fi
 
   ifaddr="`/sbin/ifconfig $interface | awk '/inet/{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
 
   if [ -z $ifaddr ]; then
-    [ -z "$CP_ECHO_RETURN" ] || echo "cp_get_ifaddr: $interface: No such ipaddress"
+    echo "cp_get_ifaddr: $interface: No such ipaddress" 1>&2
     return 1
   fi
 
@@ -28,17 +32,21 @@ cp_get_ifmask() {
   local ifmask interface
 
   interface="$1"
-  [ -z "$interface" ] && interface=lo
+  if [ -z "$interface" ]; then
+    cp_get_ifdefault dev
+    interface="$RET"
+    [ -z "$interface" ] && interface=lo
+  fi
 
   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
-    [ -z "$CP_ECHO_RETURN" ] || echo "cp_get_ifmask: $interface: No such interface"
+    echo "cp_get_ifmask: $interface: No such interface" 1>&2
     return 1
   fi
 
   ifmask="`/sbin/ifconfig $interface | awk '/Mask/{if($3~/Mask/)a=$3;else if ($4~/Mask/)a=$4;printf ("%s\n", substr(a,index(a,":")+1))}'`"
 
   if [ -z "$ifmask" ]; then
-    [ -z "$CP_ECHO_RETURN" ] || echo "cp_get_ifmask: $interface: No such netmask"
+    echo "cp_get_ifmask: $interface: No such netmask" 1>&2
     return 1
   fi
 
@@ -77,20 +85,20 @@ cp_get_netaddr() {
   fi
 
   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
-    [ "$CP_VERBOSE" ] || echo "cp_get_netaddr: $interface: No such interface"
+    echo "cp_get_netaddr: $interface: No such interface" 1>&2
     goto return1
   fi
 
   cp_get_ifaddr "$interface"
   ipaddress="$RET"
   if [ -z $ipaddress ]; then
-    [ "$CP_VERBOSE" ] || echo "cp_get_netaddr: $interface: No such ipaddress"
+    echo "cp_get_netaddr: $interface: No such ipaddress" 1>&2
     goto return1
   fi
   cp_get_ifmask "$interface"
   netmask="$RET"
   if [ -z $netmask ]; then
-    [ "$CP_VERBOSE" ] || echo "cp_get_netaddr: $interface: No such netmask"
+    echo "cp_get_netaddr: $interface: No such netmask" 1>&2
   fi
 
   if [ "$netmask" = "255.255.255.255" ]; then
@@ -114,21 +122,21 @@ cp_backup_conffile() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
   local file_bak
 
-  if [ -z $1 ]; then
-    [ -z "$CP_ECHO_RETURN" ] || echo "Usage: cp_backup_conffile <file>"
+  if [ -z "$1" ]; then
+    echo "Usage: cp_backup_conffile <file>" 1>&2
     return 1
   fi
-  if [ ! -f $1 ]; then
-    [ -z "$CP_ECHO_RETURN" ] || echo "cp_backup_conffile: $1: No such file"
+  if [ ! -f "$1" ]; then
+    echo "cp_backup_conffile: $1: No such file" 1>&2
     return 1
   fi
 
   file_bak="/var/backups/`basename $1`.bak"
 
   if [ ! -f $file_bak ]; then
-    cp -pf $1 $file_bak
+    cp -pf "$1" $file_bak
   else
-    if ! cmp -s $1 $file_bak; then
+    if ! cmp -s "$1" $file_bak; then
       /usr/bin/savelog -p -c 7 $file_bak > /dev/null 2> /dev/null
       cp -pf $1 $file_bak
     fi
@@ -155,10 +163,8 @@ cp_check_and_sed() {
     egrep -q "$s" "$i" || continue
     [ -h "$i" ]        && i=$(readlink -f "$i")
     sed "$sedcmd" "$i" > "$i.dpkg-tmp"
-    if ! cmp -s "$i" "$i.dpkg-tmp"; then
-      chown --reference "$i" "$i.dpkg-tmp"
-      chmod --reference "$i" "$i.dpkg-tmp"
-      mv "$i.dpkg-tmp" "$i"
+    if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
+      cp_safe_file_replace "$i.dpkg-tmp" "$i"
     else
       rm "$i.dpkg-tmp"
     fi
@@ -212,3 +218,20 @@ ${CP_NOTICE}----------"
     sleep 1
   fi
 }
+
+# by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
+cp_safe_file_replace () {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+  local new old
+  if [ -z "$2" ]; then
+    echo "Usage: cp_safe_file_replace <new-file> <target-file>" 1>&2
+    return 1
+  fi
+  new="$1"
+  old="$2"
+  if [ -e "$old" ]; then
+    chown --reference "$old" "$new"
+    chmod --reference "$old" "$new"
+  fi
+  mv "$new" "$old"
+}