I'd seen some ancient code that simplifed Unix paths for comparison by doing something like the following pseudocode:
- strip off the last path component to get a directory only part: /foo/bar -> /foo
- getcwd and remember original path
- chdir /foo
- getcwd and remember to return to caller
- chdir old original path
Is there a standard Unix system function that does all this without the thread unsafe current directory manipulation?
A mutex could make that code sequence less thread unsafe, but isn't really ideal (you'd have to know that all other code using getcwd or other functions dependent on the process cwd including system and vendor code protects with this same mutex).