📄 scandir.3
字号:
.TH SCANDIR 3.\" $Header: scandir.3,v 1.1 87/12/29 21:35:54 rsalz Exp $.SH NAMEscandir, alphasort \- scan a directory.SH SYNOPSIS.nf.ft B#include <sys/types.h>#include <sys/dirent.h>intscandir(name, list, selector, sorter).in +4nchar *name;struct dirent ***list;int (*selector)();int (*sorter)();.in -4nintalphasort(d1, d2).in +4nstruct dirent **d1;struct dirent **d2;.in -4n.ft R.fi.SH DESCRIPTION.I Scandirreads the directory.I nameand builds a NULL\-terminated array of pointers to the entries foundin that directory.This array is put into the location pointed to by the.I listparameter..PPIf the.I selectorparameter is non\-NULL, it is taken to be a pointer to a function calledwith each entry, to determine whether or not it should be included inthe returned list.If the parameter is NULL, all entries are included..PPAs an added feature, the entries can be sorted (with.IR qsort (3))before the list is returned.If the.I sorterparameter is non\-NULL, it is passed to qsort to use as the comparisonfunction.The.I alphasortroutine is provided to sort the array alphabetically..PPThe array pointed to by.I listand the items it points to are all space obtained through.IR malloc (3),and their storage can be reclaimed as shown in the example below..SH "EXAMPLE"Here is a small.IR ls (1)\-likeprogram:.ne 50.RS.nf#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/dir.h>extern int alphasort();static intfilesonly(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 dirname\n", 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("%s\n", list[j]->d_name); for (j = 0; j < i; j++) free((char *)list[j]); free((char *)list); exit(0);}.fi.RE.SH "SEE ALSO"directory(3), qsort(3).SH DIAGNOSTICSReturns the number of entries in the ``list,'' or \-1 if the directorycould not be opened or a memory allocation failed..SH BUGSThe routine can be slightly wasteful of space.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -