of course! (was: Anything faster than stat(S)? [...])

Maarten Litmaath maart at cs.vu.nl
Tue Nov 14 09:32:29 AEST 1989


In article <17264 at rpp386.cactus.org> jfh at rpp386.cactus.org (John F. Haugh II) writes:
\...
\	isadir (char *path)
\	{
\		char dir[PATH_MAX];
\
\		if (access (path, 0))
\			return 0;
\
\		strcpy (dir, path);
\		strcat (dir, "/x");
\
\		errno = 0;
\		access (dir, 0);
\
\		return errno == 0 || errno == ENOENT;
\	}
\
\We know all of the initial path exists because of the first access()
\call.

Can you say `race condition'?

\And with the second access() call we can discover if the
\last component of `path' isn't a directory since errno would be ENOTDIR
\rather than ENOENT.

	isadir(char *path)
	{
		char dir[PATH_MAX];

		strcpy(dir, path);
		strcat(dir, "/.");

		return access(dir, 0);
	}
-- 
"Richard Sexton is actually an AI program (or Construct, if you will) running
on some AT&T (R) 3B" (Richard Brosseau) | maart at cs.vu.nl, mcsun!botter!maart



More information about the Comp.unix.wizards mailing list