Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
lspci emulation for Busybox lspci
#!/bin/sh
# use option -nn to show both textual and numeric ID's (names & numbers)
i=/usr/share/misc/pci.ids.gz
if [ ! -s $i ]; then
busybox lspci
else
pciids=$(mktemp)
zcat $i | sed '/^#/d; /^\t\t/d' > $pciids
classes=$(mktemp)
sed -n '/^C /,$p' $pciids > $classes
busybox lspci | \
while read a b c id; do
echo -n "$a"
awk -va=${c:0:2} -vb=${c:2:2} -vo="$1" '
{
if ($1 == "C" && $2 == a) class = substr($0, 5)
if (class != "" && $1 == b) { class = substr($0, 5); exit }
}
END {
if (o == "-nn")
printf("%s [%s%s]: ", class, a, b)
else
printf("%s: ", class)
}' $classes
sed -n "/${id:0:4}/,/^[^\t]/p" $pciids | \
awk -vh=${id:0:4} -vl=${id:5:4} -vo="$1" '
{
if ($1 == h)
{ m = substr($0, 7); next }
else if ($1 == l)
{ d = substr($0, 7); exit }
}
END {
if (o == "-nn") nn = " [" h ":" l "]";
printf("%s%s%s\n", m, d, nn);
}'
done
rm $pciids $classes
fi
#!/bin/sh
busybox lspci | while read a b c id ; do
echo -n "$a "
if [ $b != "Class" ] || [ ! -s /usr/share/misc/pci.ids.gz ]; then
echo "$b $c $id"
continue
fi
zcat /usr/share/misc/pci.ids.gz | \
awk -va=${c:0:2} -vb=${c:2:2} -vh=${id:0:4} -vl=${id:5:4} '{
if ($1 == "C" && $2 == a) class=substr($0,5)
if (class != "" && $1 == b) { class=substr($0,5); exit }
if (substr($0,1,4) == h) m=substr($0,7)
else if (m == "") next
else if (substr($0,2,4) == l) { name=m substr($0,7); m="" }
else if ($1 == h && $2 == l) { name=m substr($0,14); m="" }
} END { print class ": " name }'
done
lspci --version
lspci version 3.2.1
time lspci
real 0m 0.03s
user 0m 0.02s
sys 0m 0.00s
time lspci-emul-orig.sh
real 0m 3.50s
user 0m 3.43s
sys 0m 0.17s
time lspci-emul-opt.sh
real 0m 0.73s
user 0m 0.73s
sys 0m 0.11s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment