Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!andante!princeton!udel!gatech!ncar!oddjob!mimsy!chris From: chris@mimsy.UUCP Newsgroups: comp.unix.wizards Subject: Re: scandir() Keywords: scandir catch-22 Message-ID: <12267@mimsy.UUCP> Date: 1 Jul 88 08:17:01 GMT References: <1445@uokmax.UUCP> <799@scubed.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 42 In article <799@scubed.UUCP> warner@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@mimsy.umd.edu Path: uunet!mimsy!chris