scandir()

chris at mimsy.UUCP chris at mimsy.UUCP
Fri Jul 1 18:17:01 AEST 1988


In article <799 at scubed.UUCP> warner at scubed.UUCP (Ken Warner) writes:
>Here is a routine that uses scandir().  This is Sun 3-OS 3.4,5
>specific.  The trick to using scandir() is to know how many entries are
>in the directory before you make the call to scandir().  scandir()
>just stuffs the array full of the entries into the the location pointed
>to by the automatic and will blow away the stack if there isn't any
>room.  Sort of a catch 22.  

This is wrong (as was the code that called scandir).  If you had to
know the directory length first, scandir would be useless.  The manual
entry for scandir is correct but misleading (there is no array in
its declaration!).

f()
{
	struct direct **list;
	int i, n;

	/* important:      & */
	n = scandir("foo", &list, (int (*)())NULL, (int (*)())NULL);
	/* important:      & */

	if (n == -1) ... /* could not scan */

	for (i = 0; i < n; i++)
		printf("%s\n", list[i]->d_name);

	for (i = 0; i < n; i++)
		free((char *)list[i]);
	free((char *)list);
}

/*
 * The type of the second argument to scandir is `pointer to pointer
 * to pointer to struct direct', or `struct direct ***'.  As a parameter
 * this may (but never should) be written as `struct direct *(*namelist[])'.
 * The manual is misleading; namelist is neither an array nor a pointer to
 * an array.
 */
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.wizards mailing list