add an initscript
[openwrt.git] / openwrt / package / base-files / default / etc / functions.sh
1 #!/bin/ash
2
3 alias debug=${DEBUG:-:}
4
5 # allow env to override nvram
6 nvram () {
7 if [ -x /usr/sbin/nvram ]; then
8 case $1 in
9 get) eval "echo \${$2:-\$(command nvram get $2)}";;
10 *) command nvram $*;;
11 esac
12 else
13 case $1 in
14 get) eval "echo \${$2:-\${DEFAULT_$2}}";;
15 *);;
16 esac
17 fi
18 }
19
20 # valid interface?
21 if_valid () (
22 ifconfig "$1" >&- 2>&- ||
23 [ "${1%%[0-9]}" = "br" ] ||
24 {
25 [ "${1%%[0-9]}" = "vlan" ] && (
26 i=${1#vlan}
27 hwname=$(nvram get vlan${i}hwname)
28 hwaddr=$(nvram get ${hwname}macaddr)
29 [ -z "$hwaddr" ] && return 1
30
31 vif=$(ifconfig -a | awk '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
32 debug "# vlan$i => $vif"
33
34 $DEBUG ifconfig $vif up
35 $DEBUG vconfig add $vif $i 2>&-
36 )
37 } ||
38 { debug "# missing interface '$1' ignored"; false; }
39 )
40
41 bitcount () {
42 local c=$1
43 echo $((
44 c=((c>> 1)&0x55555555)+(c&0x55555555),
45 c=((c>> 2)&0x33333333)+(c&0x33333333),
46 c=((c>> 4)&0x0f0f0f0f)+(c&0x0f0f0f0f),
47 c=((c>> 8)&0x00ff00ff)+(c&0x00ff00ff),
48 c=((c>>16)&0x0000ffff)+(c&0x0000ffff)
49 ))
50 }
51
52 valid_netmask () {
53 return $((-($1)&~$1))
54 }
55
56 ip2int () (
57 set $(echo $1 | tr '\.' ' ')
58 echo $(($1<<24|$2<<16|$3<<8|$4))
59 )
60
61 int2ip () {
62 echo $(($1>>24&255)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))
63 }
This page took 0.041372 seconds and 5 git commands to generate.