home *** CD-ROM | disk | FTP | other *** search
-
-
-
- SSSCCCAAANNNDDDIIIRRR(((333))) UUUNNNIIIXXX 555...000 SSSCCCAAANNNDDDIIIRRR(((333)))
-
-
-
- NNNAAAMMMEEE
- scandir, alphasort - scan a directory
-
- SSSYYYNNNOOOPPPSSSIIISSS
- ####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ttttyyyyppppeeeessss....hhhh>>>>
- ####iiiinnnncccclllluuuuddddeeee <<<<ssssyyyyssss////ddddiiiirrrreeeennnntttt....hhhh>>>>
-
- iiiinnnntttt
- ssssccccaaaannnnddddiiiirrrr((((nnnnaaaammmmeeee,,,, lllliiiisssstttt,,,, sssseeeelllleeeeccccttttoooorrrr,,,, ssssoooorrrrtttteeeerrrr))))
- cccchhhhaaaarrrr ****nnnnaaaammmmeeee;;;;
- ssssttttrrrruuuucccctttt ddddiiiirrrreeeennnntttt ************lllliiiisssstttt;;;;
- iiiinnnntttt ((((****sssseeeelllleeeeccccttttoooorrrr))))(((())));;;;
- iiiinnnntttt ((((****ssssoooorrrrtttteeeerrrr))))(((())));;;;
-
- iiiinnnntttt
- aaaallllpppphhhhaaaassssoooorrrrtttt((((dddd1111,,,, dddd2222))))
- ssssttttrrrruuuucccctttt ddddiiiirrrreeeennnntttt ********dddd1111;;;;
- ssssttttrrrruuuucccctttt ddddiiiirrrreeeennnntttt ********dddd2222;;;;
-
- DDDEEESSSCCCRRRIIIPPPTTTIIIOOONNN
- _S_c_a_n_d_i_r reads the directory _n_a_m_e and builds a
- NULL-terminated array of pointers to the entries found in
- that directory. This array is put into the location pointed
- to by the _l_i_s_t parameter.
-
- If the _s_e_l_e_c_t_o_r parameter is non-NULL, it is taken to be a
- pointer to a function called with each entry, to determine
- whether or not it should be included in the returned list.
- If the parameter is NULL, all entries are included.
-
- As an added feature, the entries can be sorted (with
- _q_s_o_r_t(3)) before the list is returned. If the _s_o_r_t_e_r
- parameter is non-NULL, it is passed to qsort to use as the
- comparison function. The _a_l_p_h_a_s_o_r_t routine is provided to
- sort the array alphabetically.
-
- The array pointed to by _l_i_s_t and the items it points to are
- all space obtained through _m_a_l_l_o_c(3), and their storage can
- be reclaimed as shown in the example below.
-
- EEEXXXAAAMMMPPPLLLEEE
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Page 1 (printed 8/1/88)
-
-
-
-
-
-
- SSSCCCAAANNNDDDIIIRRR(((333))) UUUNNNIIIXXX 555...000 SSSCCCAAANNNDDDIIIRRR(((333)))
-
-
-
- Here is a small _l_s(1)-like program:
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/dir.h>
-
- extern int alphasort();
-
- static int
- filesonly(e)
- struct dirent *e;
- {
- struct stat sb;
-
- return(stat(e->d_name, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFREG);
- }
-
- main(ac, av)
- int ac;
- char *av[];
- {
- register int i;
- register int j;
- struct dirent **list;
-
- if (ac != 2) {
- fprintf(stderr, "usage: %s dirname0, av[0]);
- exit(1);
- }
- if (chdir(av[1]) < 0) {
- perror(av[1]);
- exit(1);
- }
- if ((i = scandir(".", &list, filesonly, alphasort)) < 0) {
- perror("Error reading directory");
- exit(1);
- }
- for (j = 0; j < i; j++)
- printf("%s0, list[j]->d_name);
- for (j = 0; j < i; j++)
- free((char *)list[j]);
- free((char *)list);
- exit(0);
- }
-
- SSSEEEEEE AAALLLSSSOOO
- directory(3), qsort(3)
-
- DDDIIIAAAGGGNNNOOOSSSTTTIIICCCSSS
- Returns the number of entries in the ``list,'' or -1 if the
- directory could not be opened or a memory allocation failed.
-
-
-
-
- Page 2 (printed 8/1/88)
-
-
-
-
-
-
- SSSCCCAAANNNDDDIIIRRR(((333))) UUUNNNIIIXXX 555...000 SSSCCCAAANNNDDDIIIRRR(((333)))
-
-
-
- BBBUUUGGGSSS
- The routine can be slightly wasteful of space.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Page 3 (printed 8/1/88)
-
-
-
-