Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test.c « dirent « samples « mingw « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ff4267ee53d6c60863f59cb87412c898a684a87 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * A test which demonstrates the use of opendir and related functions
 * declared in dirent.h.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <dirent.h>

int
main (int argc, char* argv[])
{
	int 		i;
	struct dirent*	de;
	DIR*		dir;
	long		lPos;

	if (argc == 2)
	{
		printf ("Opening directory \"%s\"\n", argv[1]);
		dir = opendir(argv[1]);
	}
	else
	{
		printf ("Opening \".\"\n");
		dir = opendir(".");
	}

	if (!dir)
	{
		printf ("Directory open failed!\n");
		if (errno)
		{
			printf ("Error : %s\n", strerror(errno));
		}
		return 1;
	}

	i = 0;
	lPos = -1;

	while (de = readdir (dir))
	{
		i++;
		printf ("%d : \"%s\" (tell %ld)\n", i, de->d_name,
			telldir(dir));

		if (i == 3)
		{
			printf ("We will seek here later.\n");
			lPos = telldir (dir);
		}
	}

	printf ("Rewind directory.\n");
	rewinddir (dir);

	if (de = readdir (dir))
	{
		printf ("First entry : \"%s\"\n", de->d_name);
	}
	else
	{
		printf ("Empty directory.\n");
	}

	if (lPos != -1)
	{
		printf ("Seeking to fourth entry.\n");
		seekdir (dir, lPos);

		if (de = readdir (dir))
		{
			printf ("Fourth entry : \"%s\"\n", de->d_name);
		}
		else
		{
			printf ("No fourth entry.\n");
		}
	}
	else
	{
		printf ("Seek position is past end of directory.\n");
	}

	printf ("Closing directory.\n");
	closedir (dir);
}