Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!hplabs!hpfcso!hpfcdc!rer From: rer@hpfcdc.HP.COM (Rob Robason) Newsgroups: comp.sys.hp Subject: Re: DIRECTORY (3C) strangeness Message-ID: <5570336@hpfcdc.HP.COM> Date: 29 Nov 89 20:40:25 GMT References: <616@mmlai.UUCP> Organization: HP Ft. Collins, Co. Lines: 92 > System: HP-UX 6.5 B 9000/370 > Has anybody else used this code and been able to extract file names in > alphabetical order? The 6.5 release has an undocumented function in libc called scandir and another called alphasort. These derive from BSD and will read an entire directory and sort it alphabetically. Note that use of scandir and alphasort is not yet supported on the 800, not for any particular reason -- just that it got into the 300 more by accident than plan. Attached is the reference page for scandir. HP hasn't tested the scandir function in 6.5, and since it's not documented it is officially "unsupported", but the sample application shown at the bottom of this response does seem to work. > I really need the filenames in order... Thanks in advance. > Tony Burzio * SNOW! On Thanksgiving? Ski time! Rob Robason * Went skiing yesterday at Keystone -- GREAT!! Not speaking for HP, just trying to be helpful ######################################################################## SCANDIR(3) (September 17, 1985) SCANDIR(3) NAME scandir, alphasort - scan a directory SYNOPSIS #include #include scandir(dirname, namelist, select, compar) char *dirname; struct direct *(*namelist[]); int (*select)(); int (*compar)(); alphasort(d1, d2) struct direct **d1, **d2; DESCRIPTION Scandir reads the directory dirname and builds an array of pointers to directory entries using malloc(3). It returns the number of entries in the array and a pointer to the array through namelist. The select parameter is a pointer to a user supplied subroutine which is called by scandir to select which entries are to be included in the array. The select routine is passed a pointer to a directory entry and should return a non-zero value if the directory entry is to be included in the array. If select is null, then all the directory entries will be included. The compar parameter is a pointer to a user supplied subroutine which is passed to qsort(3) to sort the completed array. If this pointer is null, the array is not sorted. Alphasort is a routine which can be used for the compar parameter to sort the array alphabetically. The memory allocated for the array can be deallocated with free (see malloc(3)) by freeing each pointer in the array and the array itself. SEE ALSO directory(3), malloc(3), qsort(3), dir(5) DIAGNOSTICS Returns -1 if the directory cannot be opened for reading or if malloc(3) cannot allocate enough memory to hold all the data structures. Hewlett-Packard Company - 1 - Nov 29, 1989 ########################################################################## #include #include int alphasort(); main() { struct direct **namelist; scandir(".", &namelist, 0, alphasort); while (*namelist) { printf("%s\n", (*namelist)->d_name); namelist++; } }