Re: Fatal error when attempting to execute bear.ex from the EuGtk package
- Posted by srb623 Sep 08, 2019
- 1349 views
I had a go at writing a Euphoria function to search path variables for a specified filename with the idea being that it could be used to locate ifconfig more generically rather than hard coding /sbin or whatever in your code. Have given it some testing and it looks OK to me but I will let you be the final judge of that. The code is as follows:
--
-- Next we need to declare the include files that we require.
--
include std/filesys.e
include std/sequence.e
--
-- Function to return the fully qualified name of the file if the filename FILENAME can
-- be found in any of the paths listed in the variable PATH or the additional paths to
-- search EXTRA_PATHS. The paths in EXTRA_PATHS will be searched before those
-- in PATH. If the filename FILENAME cannot be found then an empty string will be
-- returned.
--
function find_in_path(sequence filename, sequence path, sequence extra_paths = "")
sequence path_filename
sequence path_list
sequence path_list1
sequence path_list2
--
-- FIrst we must ensure that the filename FILENAME contains some data and if not we
-- return the empty string to the caller.
--
if length(filename) = 0 then
return ""
end if
--
-- Next we must check to see if the filename FILENAME is a fully qualified name
-- and if so we must exit with the current name as it will not be located in any
-- of the paths.
--
if filename[1] = filesys:SLASH then
return filename
end if
--
-- Next we must split the path variable PATH on the path separator and create
-- a sequence of strings of the results.
--
path_list1 = stdseq:split_any(path, filesys:PATHSEP, , 1)
--
-- Next we must split the additional paths EXTRA_PATHS on the path separator
-- and create a sequence of strings of the results.
--
path_list2 = stdseq:split_any(extra_paths, filesys:PATHSEP, , 1)
--
-- Next we must create the combined list of the two path variables. As we
-- wish to search the extra paths first it must come first in the below.
--
path_list = path_list2 & path_list1
--
-- Now we need to loop for all paths in the above list and for each of them
-- we create a filename and then check for its existence. If it does then we
-- return 'true' to the caller.
--
for ix = 1 to length(path_list) do
--
-- Now we need to form the fully qualified name of the file given the IX'th
-- path entry, the file system separator and the name of the file passed to
-- this routine for checking.
--
path_filename = path_list[ix] & filesys:SLASH & filename
--
-- Now we need to check to see if the above exists and if so we return
-- the fully qualified name of the file to the caller.
--
if filesys:file_exists(path_filename) then
return path_filename
end if
end for
--
-- Finally, if we reach this point, then the file was not found in any of the
-- paths specified, and thus we return an empty string so that the caller
-- can determine that the file was not located.
return ""
end function
printf(1, "%s\n", { find_in_path("ldconfig", getenv("PATH"), "/sbin:/usr/sbin") } )
printf(1, "%s\n", { find_in_path("ls", getenv("PATH")) } )
printf(1, "%s\n", { find_in_path("xxx", getenv("PATH")) } )
On my Debian system, the first prints /sbin/ldconfig, the second /bin/ls and the third an empty string as it is (correctly) not located. Hope this can be of some help to you.

