r26: set -e bug corrected in cp_check_and_sed
[carnet-tools-cn.git] / functions.sh
index 13429de..f2fc9a1 100644 (file)
@@ -1,7 +1,11 @@
 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
 cp_get_ifaddr() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
-  local ifaddr interface
+  local ifaddr interface echo_return
+  echo_return="$CP_ECHO_RETURN" # since we're making our own calls
+  # in our own way we need to set CP_ECHO_RETURN the way we want it
+  # but preserving initial state so it could be used afterwards
+  CP_ECHO_RETURN=""
 
   interface="$1"
   if [ -z "$interface" ]; then
@@ -12,6 +16,7 @@ cp_get_ifaddr() {
 
   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
     echo "cp_get_ifaddr: $interface: No such interface" 1>&2
+    CP_ECHO_RETURN="$echo_return"
     return 1
   fi
 
@@ -19,17 +24,23 @@ cp_get_ifaddr() {
 
   if [ -z $ifaddr ]; then
     echo "cp_get_ifaddr: $interface: No such ipaddress" 1>&2
+    CP_ECHO_RETURN="$echo_return"
     return 1
   fi
 
   RET="$ifaddr"
-  [ -z "$CP_ECHO_RETURN" ] || echo $RET
+  [ -z "$echo_return" ] || echo $RET
+  CP_ECHO_RETURN="$echo_return"
 }
 
 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
 cp_get_ifmask() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
-  local ifmask interface
+  local ifmask interface echo_return
+  echo_return="$CP_ECHO_RETURN" # since we're making our own calls
+  # in our own way we need to set CP_ECHO_RETURN the way we want it
+  # but preserving initial state so it could be used afterwards
+  CP_ECHO_RETURN=""
 
   interface="$1"
   if [ -z "$interface" ]; then
@@ -40,6 +51,7 @@ cp_get_ifmask() {
 
   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
     echo "cp_get_ifmask: $interface: No such interface" 1>&2
+    CP_ECHO_RETURN="$echo_return"
     return 1
   fi
 
@@ -47,15 +59,23 @@ cp_get_ifmask() {
 
   if [ -z "$ifmask" ]; then
     echo "cp_get_ifmask: $interface: No such netmask" 1>&2
+    CP_ECHO_RETURN="$echo_return"
     return 1
   fi
 
   RET="$ifmask"
-  [ -z "$CP_ECHO_RETURN" ] || echo $RET
+  [ -z "$echo_return" ] || echo $RET
+  CP_ECHO_RETURN="$echo_return"
 }
 
 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
 cp_get_ifdefault() {
+  local echo_return
+  echo_return="$CP_ECHO_RETURN" # since we're making our own calls
+  # in our own way we need to set CP_ECHO_RETURN the way we want it
+  # but preserving initial state so it could be used afterwards
+  CP_ECHO_RETURN=""
+
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
   RET=""
   case $1 in
@@ -64,7 +84,8 @@ cp_get_ifdefault() {
     *)    echo "cp_get_ifdefault: Argument required (dev or addr)" >&2 ;;
   esac
   [ -z "$RET" -a "$1" -a "$CP_VERBOSE" ] && echo "cp_get_ifdefault: No default route" >&2
-  [ -z "$CP_ECHO_RETURN" ] || echo $RET
+  [ -z "$echo_return" ] || echo $RET
+  CP_ECHO_RETURN="$echo_return"
   [ -z "$RET" ] && return 1
 }
 
@@ -86,19 +107,23 @@ cp_get_netaddr() {
 
   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
     echo "cp_get_netaddr: $interface: No such interface" 1>&2
-    goto return1
+    CP_ECHO_RETURN="$echo_return"
+    return 1
   fi
 
   cp_get_ifaddr "$interface"
   ipaddress="$RET"
   if [ -z $ipaddress ]; then
     echo "cp_get_netaddr: $interface: No such ipaddress" 1>&2
-    goto return1
+    CP_ECHO_RETURN="$echo_return"
+    return 1
   fi
   cp_get_ifmask "$interface"
   netmask="$RET"
   if [ -z $netmask ]; then
     echo "cp_get_netaddr: $interface: No such netmask" 1>&2
+    CP_ECHO_RETURN="$echo_return"
+    return 1
   fi
 
   if [ "$netmask" = "255.255.255.255" ]; then
@@ -110,11 +135,6 @@ cp_get_netaddr() {
   RET="$cidr"
   [ -z "$echo_return" ] || echo $RET
   CP_ECHO_RETURN="$echo_return"
-  return 0
-  
-  return1:
-    CP_ECHO_RETURN="$echo_return"
-    return 1
 }
 
 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
@@ -123,7 +143,6 @@ cp_backup_conffile() {
   local file_bak
 
   if [ -z "$1" ]; then
-    echo "Usage: cp_backup_conffile <file>" 1>&2
     return 1
   fi
   if [ ! -f "$1" ]; then
@@ -160,13 +179,13 @@ cp_check_and_sed() {
   for i in "$@"
   do
     [ -e "$i" ]        || continue
-    egrep -q "$s" "$i" || continue
+    if egrep -q "$s" "$i"; then
+      continue
+    fi
     [ -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_mv "$i.dpkg-tmp" "$i"
     else
       rm "$i.dpkg-tmp"
     fi
@@ -220,3 +239,19 @@ ${CP_NOTICE}----------"
     sleep 1
   fi
 }
+
+# by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
+cp_mv () {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+  local new old
+  if [ -z "$2" ]; then
+    return 1
+  fi
+  new="$1"
+  old="$2"
+  if [ -e "$old" ]; then
+    chown --reference "$old" "$new"
+    chmod --reference "$old" "$new"
+  fi
+  mv "$new" "$old"
+}