@@ -890,33 +890,38 @@ dir_s_rmdir(VALUE obj, VALUE dir)
890
890
return INT2FIX (0 );
891
891
}
892
892
893
+ #define GLOB_VERBOSE (1 << (sizeof(int) * CHAR_BIT - 1))
894
+ #define sys_warning (val ) \
895
+ ((flags & GLOB_VERBOSE) && rb_protect((VALUE (*)(VALUE))rb_sys_warning, (VALUE)(val), 0))
896
+
893
897
/* System call with warning */
894
898
static int
895
- do_stat (const char * path , struct stat * pst )
899
+ do_stat (const char * path , struct stat * pst , int flags )
900
+
896
901
{
897
902
int ret = stat (path , pst );
898
903
if (ret < 0 && errno != ENOENT )
899
- rb_protect (( VALUE ( * )( VALUE )) rb_sys_warning , ( VALUE ) path , 0 );
904
+ sys_warning ( path );
900
905
901
906
return ret ;
902
907
}
903
908
904
909
static int
905
- do_lstat (const char * path , struct stat * pst )
910
+ do_lstat (const char * path , struct stat * pst , int flags )
906
911
{
907
912
int ret = lstat (path , pst );
908
913
if (ret < 0 && errno != ENOENT )
909
- rb_protect (( VALUE ( * )( VALUE )) rb_sys_warning , ( VALUE ) path , 0 );
914
+ sys_warning ( path );
910
915
911
916
return ret ;
912
917
}
913
918
914
919
static DIR *
915
- do_opendir (const char * path )
920
+ do_opendir (const char * path , int flags )
916
921
{
917
922
DIR * dirp = opendir (path );
918
923
if (dirp == NULL && errno != ENOENT && errno != ENOTDIR )
919
- rb_protect (( VALUE ( * )( VALUE )) rb_sys_warning , ( VALUE ) path , 0 );
924
+ sys_warning ( path );
920
925
921
926
return dirp ;
922
927
}
@@ -1118,19 +1123,7 @@ glob_func_caller(VALUE val)
1118
1123
return Qnil ;
1119
1124
}
1120
1125
1121
- static int
1122
- glob_call_func (void (* func ) (const char * , VALUE ), const char * path , VALUE arg )
1123
- {
1124
- int status ;
1125
- struct glob_args args ;
1126
-
1127
- args .func = func ;
1128
- args .c = path ;
1129
- args .v = arg ;
1130
-
1131
- rb_protect (glob_func_caller , (VALUE )& args , & status );
1132
- return status ;
1133
- }
1126
+ #define glob_call_func (func , path , arg ) (*func)(path, arg)
1134
1127
1135
1128
static int
1136
1129
glob_helper (
@@ -1141,7 +1134,7 @@ glob_helper(
1141
1134
struct glob_pattern * * beg ,
1142
1135
struct glob_pattern * * end ,
1143
1136
int flags ,
1144
- void (* func ) (const char * , VALUE ),
1137
+ int (* func )(const char * , VALUE ),
1145
1138
VALUE arg )
1146
1139
{
1147
1140
struct stat st ;
@@ -1176,7 +1169,7 @@ glob_helper(
1176
1169
1177
1170
if (* path ) {
1178
1171
if (match_all && exist == UNKNOWN ) {
1179
- if (do_lstat (path , & st ) == 0 ) {
1172
+ if (do_lstat (path , & st , flags ) == 0 ) {
1180
1173
exist = YES ;
1181
1174
isdir = S_ISDIR (st .st_mode ) ? YES : S_ISLNK (st .st_mode ) ? UNKNOWN : NO ;
1182
1175
}
@@ -1186,7 +1179,7 @@ glob_helper(
1186
1179
}
1187
1180
}
1188
1181
if (match_dir && isdir == UNKNOWN ) {
1189
- if (do_stat (path , & st ) == 0 ) {
1182
+ if (do_stat (path , & st , flags ) == 0 ) {
1190
1183
exist = YES ;
1191
1184
isdir = S_ISDIR (st .st_mode ) ? YES : NO ;
1192
1185
}
@@ -1211,7 +1204,7 @@ glob_helper(
1211
1204
1212
1205
if (magical || recursive ) {
1213
1206
struct dirent * dp ;
1214
- DIR * dirp = do_opendir (* path ? path : "." );
1207
+ DIR * dirp = do_opendir (* path ? path : "." , flags );
1215
1208
if (dirp == NULL ) return 0 ;
1216
1209
1217
1210
for (dp = readdir (dirp ); dp != NULL ; dp = readdir (dirp )) {
@@ -1221,7 +1214,7 @@ glob_helper(
1221
1214
if (recursive && strcmp (dp -> d_name , "." ) != 0 && strcmp (dp -> d_name , ".." ) != 0
1222
1215
&& fnmatch ("*" , dp -> d_name , flags ) == 0 ) {
1223
1216
#ifndef _WIN32
1224
- if (do_lstat (buf , & st ) == 0 )
1217
+ if (do_lstat (buf , & st , flags ) == 0 )
1225
1218
new_isdir = S_ISDIR (st .st_mode ) ? YES : S_ISLNK (st .st_mode ) ? UNKNOWN : NO ;
1226
1219
else
1227
1220
new_isdir = NO ;
@@ -1293,18 +1286,14 @@ glob_helper(
1293
1286
}
1294
1287
1295
1288
static int
1296
- rb_glob2 (const char * path , int flags , void (* func ) (const char * , VALUE ), VALUE arg )
1289
+ ruby_glob0 (const char * path , int flags , int (* func )(const char * , VALUE ), VALUE arg )
1297
1290
{
1298
1291
struct glob_pattern * list ;
1299
1292
const char * root , * start ;
1300
1293
char * buf ;
1301
1294
int n ;
1302
1295
int status ;
1303
1296
1304
- if (flags & FNM_CASEFOLD ) {
1305
- rb_warn ("Dir.glob() ignores File::FNM_CASEFOLD" );
1306
- }
1307
-
1308
1297
start = root = path ;
1309
1298
#if defined DOSISH
1310
1299
flags |= FNM_CASEFOLD ;
@@ -1328,28 +1317,42 @@ rb_glob2(const char *path, int flags, void (*func) (const char *, VALUE), VALUE
1328
1317
return status ;
1329
1318
}
1330
1319
1331
- struct rb_glob_args {
1332
- void (* func )(const char * , VALUE );
1333
- VALUE arg ;
1334
- };
1320
+ int
1321
+ ruby_glob (const char * path , int flags , int (* func )(const char * , VALUE ), VALUE arg )
1322
+ {
1323
+ return ruby_glob0 (path , flags & ~GLOB_VERBOSE , func , arg );
1324
+ }
1335
1325
1336
- static void
1326
+ static int
1337
1327
rb_glob_caller (const char * path , VALUE a )
1338
1328
{
1339
- struct rb_glob_args * args = (struct rb_glob_args * )a ;
1340
- (* args -> func )(path , args -> arg );
1329
+ int status ;
1330
+ struct glob_args * args = (struct glob_args * )a ;
1331
+
1332
+ args -> c = path ;
1333
+ rb_protect (glob_func_caller , a , & status );
1334
+ return status ;
1341
1335
}
1342
1336
1343
- void
1344
- rb_glob (const char * path , void (* func ) (const char * , VALUE ), VALUE arg )
1337
+ static int
1338
+ rb_glob2 (const char * path , int flags , void (* func )(const char * , VALUE ), VALUE arg )
1345
1339
{
1346
- struct rb_glob_args args ;
1347
- int status ;
1340
+ struct glob_args args ;
1348
1341
1349
1342
args .func = func ;
1350
- args .arg = arg ;
1351
- status = rb_glob2 (path , 0 , rb_glob_caller , (VALUE )& args );
1343
+ args .v = arg ;
1352
1344
1345
+ if (flags & FNM_CASEFOLD ) {
1346
+ rb_warn ("Dir.glob() ignores File::FNM_CASEFOLD" );
1347
+ }
1348
+
1349
+ return ruby_glob0 (path , flags | GLOB_VERBOSE , rb_glob_caller , (VALUE )& args );
1350
+ }
1351
+
1352
+ void
1353
+ rb_glob (const char * path , void (* func ) (const char * , VALUE ), VALUE arg )
1354
+ {
1355
+ int status = rb_glob2 (path , 0 , func , arg );
1353
1356
if (status ) rb_jump_tag (status );
1354
1357
}
1355
1358
0 commit comments