wifi: add support for /usr/bin binary path

Years ago iwconfig started its life in /sbin. Then some distributions
moved it to /usr/bin. Then in 2012 some projects started pushing for
deprecation of /usr/sbin and merging everything into /usr/bin.

We now search paths including /usr/bin for the iwconfig binary. This
is because sbin paths are not usually in non privileged user PATH, so
we help io.popen locate the binary on the file-system.

We do this search in the following way:
  - default to basename only, 'iwconfig' for the iwconfig variable
  - search paths one by one once and redeclare variable iwconfig with
    full path if binary is found in any
  - avoid searching paths on next execution if iwconfig variable
    already contains a path
This commit is contained in:
Adrian C. (anrxc) 2013-05-27 17:51:58 +02:00
parent f7fdd909ee
commit fac688ec4b
1 changed files with 20 additions and 9 deletions

View File

@ -9,11 +9,11 @@ local math = { ceil = math.ceil }
local setmetatable = setmetatable local setmetatable = setmetatable
local helpers = require("vicious.helpers") local helpers = require("vicious.helpers")
local io = { local io = {
open = io.open, open = io.open,
popen = io.popen popen = io.popen
} }
local string = { local string = {
find = string.find, find = string.find,
match = string.match match = string.match
} }
-- }}} -- }}}
@ -24,6 +24,12 @@ local string = {
local wifi = {} local wifi = {}
-- {{{ Variable definitions
local iwconfig = "iwconfig"
local iwcpaths = { "/sbin", "/usr/sbin", "/usr/local/sbin", "/usr/bin" }
-- }}}
-- {{{ Wireless widget type -- {{{ Wireless widget type
local function worker(format, warg) local function worker(format, warg)
if not warg then return end if not warg then return end
@ -39,14 +45,19 @@ local function worker(format, warg)
["{sign}"] = 0 ["{sign}"] = 0
} }
-- Get data from iwconfig where available -- Sbin paths aren't in user PATH, search for the binary
local iwconfig = "/sbin/iwconfig" if iwconfig == "iwconfig" then
local f = io.open(iwconfig, "rb") for _, p in ipairs(iwcpaths) do
if not f then local f = io.open(p .. "/iwconfig", "rb")
iwconfig = "/usr/sbin/iwconfig" if f then
else iwconfig = p .. "/iwconfig"
f:close() f:close()
break
end
end
end end
-- Get data from iwconfig where available
local f = io.popen(iwconfig .." ".. warg .. " 2>&1") local f = io.popen(iwconfig .." ".. warg .. " 2>&1")
local iw = f:read("*all") local iw = f:read("*all")
f:close() f:close()