Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Last active August 26, 2022 01:19
Show Gist options
  • Save stuaxo/79bcdcbaf9aa3b277207 to your computer and use it in GitHub Desktop.
Save stuaxo/79bcdcbaf9aa3b277207 to your computer and use it in GitHub Desktop.
Parse ld.so.conf and get all the directories from it
from os.path import abspath, isabs, join
from glob import glob
def parse_ldsoconf(filename="/etc/ld.so.conf"):
paths = set()
directory = dirname(abspath(filename))
with open(filename) as f:
for line in (_line.rstrip() for _line in f.readlines()):
if line.startswith("include "):
wildcard = line.partition("include ")[-1:][0].rstrip()
if not isabs(wildcard):
wildcard = join(directory, wildcard)
for filename in glob(wildcard):
paths |= parse_ldsoconf(filename)
elif not line.startswith("#"):
if line:
paths.add(line)
return paths
parse_ldsoconf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment