summaryrefslogtreecommitdiff
path: root/test/misc/dirent.c
blob: c8db2c83d13391fce36d70a0aca433fb242cd189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

#define _DTIFY(DT) [DT] #DT
const char const *types[] = {
	_DTIFY(DT_UNKNOWN),
	_DTIFY(DT_FIFO),
	_DTIFY(DT_CHR),
	_DTIFY(DT_DIR),
	_DTIFY(DT_BLK),
	_DTIFY(DT_REG),
	_DTIFY(DT_LNK),
	_DTIFY(DT_SOCK),
	_DTIFY(DT_WHT)
};

int main(int argc, char *argv[])
{
	DIR *dirh;
	struct dirent *de;
	struct dirent64 *de64;
	const char *mydir = (argc == 1 ? "/" : argv[1]);

	if ((dirh = opendir(mydir)) == NULL) {
		perror("opendir");
		return 1;
	}

	printf("readdir() says:\n");
	while ((de = readdir(dirh)) != NULL)
		printf("\tdir entry %s: %s\n", types[de->d_type], de->d_name);

	rewinddir(dirh);

	printf("readdir64() says:\n");
	while ((de64 = readdir64(dirh)) != NULL)
		printf("\tdir entry %s: %s\n", types[de64->d_type], de64->d_name);

	closedir(dirh);


	return 0;
}