Added gitignore entries needed to ignore derived objects generated from full build...
[bash.git] / examples / functions / inetaddr
blobf3e228f524892bcc6534c8bdf56fa89b7dd63a23
2 # inet2hex - Internet address conversion, dotted-decimal to hex
4 inet2hex ()
6         local IFS
8         IFS=.
9         set -- $1
11         if (( $# != 4 )); then
12                 echo "inet2hex: incorrect input format: $1" >&2
13                 echo "inet2hex: usage: inet2hex XX.XX.XX.XX" >&2
14                 return 2
15         fi
16   
17         printf "0x%02x%02x%02x%02x\n" $1 $2 $3 $4
21 # hex2inet - Internet address conversion, hex to dotted-decimal
23 hex2inet ()
25         local x1 x2 x3 x4
26         local rev
28         OPTIND=1
29         while getopts "r" o
30         do
31                 case "$o" in
32                 r)      rev=true;;
33                 *)      echo "hex2inet: usage: hex2inet [-r] [0x]XXXXXXXX" >&2 ; exit 2;;
34                 esac
35         done
36         shift $(( $OPTIND - 1 ))
38         case "$1" in
39         0x*)    h=${1#??} ;;
40         *)      h=$1 ;;
41         esac
43         if (( ${#h} != 8 )); then
44                 echo "hex2inet: $h not in inet format" >&2
45                 echo "hex2inet: usage: hex2inet [0x]XXXXXXXX" >&2
46                 return 2
47         fi
49         x1=$(( 0x${h:0:2} ))
50         x2=$(( 0x${h:2:2} ))
51         x3=$(( 0x${h:4:2} ))
52         x4=$(( 0x${h:6:2} ))
54         if [ -z "$rev" ] ; then
55                 printf "%d.%d.%d.%d\n" $x1 $x2 $x3 $x4 
56         else
57                 printf "%d.%d.%d.%d\n" $x4 $x3 $x2 $x1 
58         fi
59         return 0