/
scandir.c
144 lines (116 loc) · 3.51 KB
/
scandir.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* scandir() and alphasort() implementation for OS/2 kLIBC
*
* Copyright (C) 2016-2021 KO Myung-Hun <komh@chollian.net>
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*/
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "scandir.h"
/**
* scandir()
*
* @remark OS/2 kLIBC declares scandir() differently from POSIX
*/
int os2compat_scandir( const char *dir, struct dirent ***namelist,
int ( *sel )(/* const */ struct dirent * ),
int ( *compare )( const /* struct dirent ** */ void *,
const /* struct dirent ** */ void *))
{
DIR *dp;
struct dirent **list = NULL;
size_t list_size = 0;
int list_count = 0;
struct dirent *d;
int saved_errno;
dp = opendir( dir );
if( dp == NULL )
return -1;
/* Save original errno */
saved_errno = errno;
/* Clear errno to test later */
errno = 0;
while(( d = readdir( dp )) != NULL )
{
int selected = !sel || sel( d );
/* Clear errno modified by sel() */
errno = 0;
if( selected )
{
struct dirent *d1;
size_t len;
/* List full ? */
if( list_count == list_size )
{
struct dirent **new_list;
if( list_size == 0 )
list_size = 20;
else
list_size += list_size;
new_list = ( struct dirent ** )realloc( list,
list_size *
sizeof( *list ));
if( !new_list )
{
errno = ENOMEM;
break;
}
list = new_list;
}
/* On OS/2 kLIBC, d_name is not the last member of struct dirent.
* So just allocate as many as the size of struct dirent. */
len = sizeof( struct dirent );
d1 = ( struct dirent * )malloc( len );
if( !d1 )
{
errno = ENOMEM;
break;
}
memcpy( d1, d, len );
list[ list_count++ ] = d1;
}
}
/* Error ? */
if( errno )
{
/* Store errno to use later */
saved_errno = errno;
/* Free a directory list */
while( list_count > 0 )
free( list[ --list_count ]);
free( list );
/* Indicate an error */
list_count = -1;
}
else
{
/* If compare is present, sort */
if( compare != NULL )
qsort( list, list_count, sizeof( *list ), compare );
*namelist = list;
}
/* Ignore error of closedir() */
closedir( dp );
errno = saved_errno;
return list_count;
}
/**
* alphasort()
*
* @remark OS/2 kLIBC requires parameters of compare function differently
* from POSIX
*/
int os2compat_alphasort( const /* struct dirent **d1 */ void *p1,
const /* struct dirent **d2 */ void *p2 )
{
struct dirent **d1 = ( struct dirent ** )p1;
struct dirent **d2 = ( struct dirent ** )p2;
return strcoll(( *d1 )->d_name, ( *d2 )->d_name);
}