Anything faster than stat(S)? need to determine File or Dir

John F. Haugh II jfh at rpp386.cactus.org
Sat Nov 11 02:29:30 AEST 1989


In article <15769 at bloom-beacon.MIT.EDU> jik at athena.mit.edu (Jonathan I. Kamens) writes:
>In article <2586 at unisoft.UUCP> greywolf at unisoft.UUCP (The Grey Wolf) writes:
>>I think, IMHO, you're better off going with stat().
>
>  Yup, I think so too.  That's how I do it in the code I've written.

Just for the sake of disagreeing, what about other system calls that
are able to distinguish between a file being a directory or not?

The error return entries for access(1) tell me something useful -

	A component of the path prefix is not a directory. [ENOTDIR]
	The named file does not exist. [ENOENT]

How about code like this -

	#include <errno.h>

	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.  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.

Ain't perfect either, but maybe better?
-- 
John F. Haugh II                        +-Things you didn't want to know:------
VoiceNet: (512) 832-8832   Data: -8835  | The real meaning of EMACS is ...
InterNet: jfh at rpp386.cactus.org         |   ... EMACS makes a computer slow.
UUCPNet:  {texbell|bigtex}!rpp386!jfh   +--<><--<><--<><--<><--<><--<><--<><---



More information about the Comp.unix.wizards mailing list