move target/default/target_skeleton into package/base-files, put all the default...
[openwrt/svn-archive/archive.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 case $1 in
8 get) eval "echo \${NVRAM_$2:-\$(command nvram get $2)}";;
9 *) command nvram $*;;
10 esac
11 }
12 . /etc/nvram.overrides
13
14 # valid interface?
15 if_valid () (
16 ifconfig "$1" >&- 2>&- ||
17 [ "${1%%[0-9]}" = "br" ] ||
18 {
19 [ "${1%%[0-9]}" = "vlan" ] && (
20 i=${1#vlan}
21 hwname=$(nvram get vlan${i}hwname)
22 hwaddr=$(nvram get ${hwname}macaddr)
23 [ -z "$hwaddr" ] && return 1
24
25 vif=$(ifconfig -a | awk '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
26 debug "# vlan$i => $vif"
27
28 $DEBUG ifconfig $vif up
29 $DEBUG vconfig add $vif $i 2>&-
30 )
31 } ||
32 { debug "# missing interface '$1' ignored"; false; }
33 )
34
35 bitcount () {
36 local c=$1
37 echo $((
38 c=((c>> 1)&0x55555555)+(c&0x55555555),
39 c=((c>> 2)&0x33333333)+(c&0x33333333),
40 c=((c>> 4)&0x0f0f0f0f)+(c&0x0f0f0f0f),
41 c=((c>> 8)&0x00ff00ff)+(c&0x00ff00ff),
42 c=((c>>16)&0x0000ffff)+(c&0x0000ffff)
43 ))
44 }
45
46 valid_netmask () {
47 return $((-($1)&~$1))
48 }
49
50 ip2int () (
51 set $(echo $1 | tr '\.' ' ')
52 echo $(($1<<24|$2<<16|$3<<8|$4))
53 )
54
55 int2ip () {
56 echo $(($1>>24&255)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))
57 }