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

dirent.c « support - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc1b3e238e16730ad5113258b05eb174ec214b6f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * <dirent.h> wrapper functions.
 *
 * Authors:
 *   Jonathan Pryor (jonpryor@vt.edu)
 *
 * Copyright (C) 2004-2005 Jonathan Pryor
 */

#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>

#include "map.h"
#include "mph.h"

#if defined (PATH_MAX) && defined (NAME_MAX)
	#define MPH_PATH_MAX MAX(PATH_MAX, NAME_MAX)
#elif defined (PATH_MAX)
	#define MPH_PATH_MAX PATH_MAX
#elif defined (NAME_MAX)
	#define MPH_PATH_MAX NAME_MAX
#else /* !defined PATH_MAX && !defined NAME_MAX */
	#define MPH_PATH_MAX 2048
#endif

G_BEGIN_DECLS

#if HAVE_SEEKDIR
gint32
Mono_Posix_Syscall_seekdir (void *dir, mph_off_t offset)
{
	mph_return_if_off_t_overflow (offset);

	seekdir ((DIR*) dir, (off_t) offset);

	return 0;
}
#endif  /* def HAVE_SEEKDIR */

#if HAVE_TELLDIR
mph_off_t
Mono_Posix_Syscall_telldir (void *dir)
{
	return telldir ((DIR*) dir);
}
#endif  /* def HAVE_TELLDIR */

static void
copy_dirent (struct Mono_Posix_Syscall__Dirent *to, struct dirent *from)
{
	memset (to, 0, sizeof(*to));

	to->d_ino    = from->d_ino;
	to->d_name   = strdup (from->d_name);

#ifdef HAVE_STRUCT_DIRENT_D_OFF
	to->d_off    = from->d_off;
#endif
#ifdef HAVE_STRUCT_DIRENT_D_RECLEN
	to->d_reclen = from->d_reclen;
#endif
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
	to->d_type   = from->d_type;
#endif
}

gint32
Mono_Posix_Syscall_readdir (void *dirp, struct Mono_Posix_Syscall__Dirent *entry)
{
	struct dirent *d;

	if (entry == NULL) {
		errno = EFAULT;
		return -1;
	}

	errno = 0;
	d = readdir (dirp);

	if (d == NULL) {
		return -1;
	}

	copy_dirent (entry, d);

	return 0;
}

gint32
Mono_Posix_Syscall_readdir_r (void *dirp, struct Mono_Posix_Syscall__Dirent *entry, void **result)
{
	struct dirent *_entry = malloc (sizeof (struct dirent) + MPH_PATH_MAX + 1);
	int r;

	r = readdir_r (dirp, _entry, (struct dirent**) result);

	if (r == 0 && *result != NULL) {
		copy_dirent (entry, _entry);
	}

	free (_entry);

	return r;
}

int
Mono_Posix_Syscall_rewinddir (void* dir)
{
	rewinddir (dir);
	return 0;
}

G_END_DECLS

/*
 * vim: noexpandtab
 */