File: readdir.c

package info (click to toggle)
care 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch, trixie
  • size: 1,716 kB
  • sloc: ansic: 15,436; sh: 1,195; makefile: 6
file content (49 lines) | stat: -rw-r--r-- 974 bytes parent folder | download | duplicates (5)
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
48
49
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv[])
{
	struct dirent *dirents;
	DIR *dir;

	int status;
	int i;

	for (i = 1; i < argc; i++) {
		dir = opendir(argv[i]);
		if (dir == NULL) {
			perror("opendir(3)");
			exit(EXIT_FAILURE);
		}

		errno = 0;
		while ((dirents = readdir(dir)) != NULL) {
			printf("%s %s\n",
				dirents->d_type == DT_BLK  ? "DT_BLK " :
				dirents->d_type == DT_CHR  ? "DT_CHR " :
				dirents->d_type == DT_DIR  ? "DT_DIR " :
				dirents->d_type == DT_FIFO ? "DT_FIFO" :
				dirents->d_type == DT_LNK  ? "DT_LNK " :
				dirents->d_type == DT_REG  ? "DT_REG " :
				dirents->d_type == DT_SOCK ? "DT_SOCK" :
				"DT_UNKNOWN", dirents->d_name);
			errno = 0;
		}

		if (errno != 0) {
			perror("readdir(3)");
			exit(EXIT_FAILURE);
		}

		status = closedir(dir);
		if (status < 0) {
			perror("closedir(3)");
			exit(EXIT_FAILURE);
		}
	}

	exit(EXIT_SUCCESS);
}