Unix Programming - looking for searchpath function

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > looking for searchpath function





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author looking for searchpath function
Mohun Biswas

2004-01-23, 5:21 pm

Is there a standard C function, either in the classic Unix API or freely
available elsewhere, to search $PATH for a given executable file? This
seems like a really basic thing and not hard to do - in fact I've just
written my own - but I can't shake the feeling that there must be a more
standard, robust, and general-purpose solution out there.

I'm looking for basically the shell "whence" (or "which") function at
the C-library level. The ideal tool would let you specify the path (so
you could search MANPATH or LD_LIBRARY_PATH) and a filter (so you could
say "only find executable files" or "only find readable files").

Thanks,
MB

Mohun Biswas

2004-01-23, 5:21 pm

Maurizio Loreti wrote:
quote:

> You may homebrew your own; "getenv" to get the environment variable,
> "strtok" to get every component, "access" to test for the file.



I *did* say, in the sentence right after the one you quoted, that I've
just finished writing my own. The point is, though it's easy to write
it's not so easy to be sure you have a robust, portable implementation
till a few years and platforms have gone by and a few bugs found. There
are special cases involving empty path entries (":" appearing at
beginning or end, or consecutively) and potential issues with symlinks
and permissions and special files and NFS and ....

By analogy: I could write my own getcwd() function. It's not hard, in
concept. In practice it's much better to use a standard, well-tested,
builtin function. That's why we have libraries.

MB

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com