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

wtest.c « dirent « samples « mingw « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 798544374f877ed6eb4d709616cf6c3e1ae29039 (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
92
93
94
95
96
97
98
/*
 * A test which demonstrates the use of _wopendir and related
 * wide	char functions declared in dirent.h. 
 *
 * TODO: Make this _UNICODE neutral using tchar.h mappings.
 */

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

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

	if (argc == 2)
	{
		size_t len = strlen(argv[1]) + 1;
	        wchar_t* wpath = (wchar_t*) malloc(len *sizeof(wchar_t));
		mbstowcs(wpath, argv[1], len); 
		wprintf (L"Opening directory \"%s\"\n", wpath);
		dir = _wopendir(wpath);
		free (wpath);
	}
	else
	{
		wprintf (L"Opening \".\"\n");
		dir = _wopendir(L".");
	}

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

	i = 0;
	lPos = -1;

	while ((de = _wreaddir (dir)))
	{
		i++;
		wprintf (L"%d : \"%s\" (tell %ld)\n", i, de->d_name,
			_wtelldir(dir));

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

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

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

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

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

	wprintf (L"Closing directory.\n");
	_wclosedir (dir);
return 0;
}