@@ -928,8 +928,7 @@ int file_read_key_value_r(const char *path, const char *key,
928
928
* @param farg The second arg of filter.
929
929
* @param compar See scandir.
930
930
*
931
- * @return the count of scanned files if successful, or a negative
932
- * errno-style value if not.
931
+ * @return the count of scanned files on success, or -1 on error.
933
932
*/
934
933
int ac_scandir (const char * dirp , struct dirent * * * namelist ,
935
934
int (* filter )(const struct dirent * , const void * ),
@@ -941,18 +940,21 @@ int ac_scandir(const char *dirp, struct dirent ***namelist,
941
940
int count = 0 ;
942
941
int index = 0 ;
943
942
struct dirent * * _filelist ;
943
+ struct dirent * * _outlist ;
944
944
945
945
if (!dirp || !namelist )
946
- return - EINVAL ;
946
+ return -1 ;
947
947
948
948
const int res = scandir (dirp , & _filelist , NULL , compar );
949
949
950
950
if (!filter ) {
951
951
* namelist = _filelist ;
952
952
return res ;
953
953
}
954
- if (res == -1 )
955
- return - errno ;
954
+ if (res == -1 ) {
955
+ LOGE ("failed to scandir, error (%s)\n" , strerror (errno ));
956
+ return -1 ;
957
+ }
956
958
957
959
/* overwrite filter */
958
960
/* calculate the matched files, free unneeded files and mark them */
@@ -972,25 +974,28 @@ int ac_scandir(const char *dirp, struct dirent ***namelist,
972
974
}
973
975
974
976
/* construct the out array */
975
- * namelist = malloc (count * sizeof (struct dirent * ));
976
- if (!(* namelist ))
977
+ _outlist = malloc (count * sizeof (struct dirent * ));
978
+ if (!_outlist ) {
979
+ LOGE ("failed to malloc\n" );
977
980
goto e_free ;
981
+ }
978
982
979
983
for (i = 0 ; i < res ; i ++ ) {
980
984
if (_filelist [i ])
981
- ( * namelist ) [index ++ ] = _filelist [i ];
985
+ _outlist [index ++ ] = _filelist [i ];
982
986
}
983
987
984
988
free (_filelist );
985
989
990
+ * namelist = _outlist ;
986
991
return count ;
987
992
988
993
e_free :
989
994
for (i = 0 ; i < res ; i ++ )
990
995
if (_filelist [i ])
991
996
free (_filelist [i ]);
992
997
free (_filelist );
993
- return - errno ;
998
+ return -1 ;
994
999
}
995
1000
996
1001
/* filters return zero if the match is successful */
@@ -1023,7 +1028,7 @@ int dir_contains(const char *dir, const char *filename, const int exact)
1023
1028
struct dirent * * filelist ;
1024
1029
1025
1030
if (!dir || !filename )
1026
- return - EINVAL ;
1031
+ return -1 ;
1027
1032
1028
1033
if (exact )
1029
1034
ret = ac_scandir (dir , & filelist , filter_filename_exactly ,
@@ -1150,8 +1155,7 @@ static void expand_dir(char *_dirs[], int *count, int depth, int max_depth)
1150
1155
* @param path[out] Searched file path in given dir.
1151
1156
* @param limit The number of files uplayer want to get.
1152
1157
*
1153
- * @return the count of searched files if successful, or a negative
1154
- * errno-style value if not.
1158
+ * @return the count of searched files on success, or -1 on error.
1155
1159
*/
1156
1160
int find_file (char * dir , char * target_file , int depth , char * path [], int limit )
1157
1161
{
@@ -1161,12 +1165,12 @@ int find_file(char *dir, char *target_file, int depth, char *path[], int limit)
1161
1165
int dirs ;
1162
1166
1163
1167
if (depth < 1 || !dir || !target_file || !path || limit <= 0 )
1164
- return - EINVAL ;
1168
+ return -1 ;
1165
1169
1166
1170
ret = asprintf (& _dirs [0 ], "%s" , dir );
1167
1171
if (ret < 0 ) {
1168
1172
LOGE ("compute string failed, out of memory\n" );
1169
- return - ENOMEM ;
1173
+ return -1 ;
1170
1174
}
1171
1175
dirs = 1 ;
1172
1176
@@ -1182,12 +1186,13 @@ int find_file(char *dir, char *target_file, int depth, char *path[], int limit)
1182
1186
_dirs [i ], target_file );
1183
1187
if (ret < 0 ) {
1184
1188
LOGE ("compute string failed, out of memory\n" );
1185
- ret = - ENOMEM ;
1189
+ ret = -1 ;
1186
1190
goto fail ;
1187
1191
}
1188
1192
} else if (ret < 0 ) {
1189
- LOGE ("dir_contains failed, error (%s)\n" ,
1190
- strerror (- ret ));
1193
+ LOGE ("dir_contains failed\n" );
1194
+ ret = -1 ;
1195
+ goto fail ;
1191
1196
}
1192
1197
}
1193
1198
@@ -1280,8 +1285,7 @@ int is_ac_filefmt(const char *file_fmt)
1280
1285
* @param file_fmt A string pointer of a file format.
1281
1286
* @param out Files were found.
1282
1287
*
1283
- * @return the count of searched files if successful, or a negative
1284
- * errno-style value if not.
1288
+ * @return the count of searched files on success, or -1 on error.
1285
1289
*/
1286
1290
int config_fmt_to_files (const char * file_fmt , char * * * out )
1287
1291
{
@@ -1298,11 +1302,13 @@ int config_fmt_to_files(const char *file_fmt, char ***out)
1298
1302
char * * out_array ;
1299
1303
1300
1304
if (!file_fmt || !out )
1301
- return - EINVAL ;
1305
+ return -1 ;
1302
1306
1303
1307
dir = strdup (file_fmt );
1304
- if (!dir )
1305
- return - ENOMEM ;
1308
+ if (!dir ) {
1309
+ LOGE ("failed to strdup\n" );
1310
+ return -1 ;
1311
+ }
1306
1312
1307
1313
if (!is_ac_filefmt (file_fmt )) {
1308
1314
/* It's an regular file as default */
@@ -1320,37 +1326,58 @@ int config_fmt_to_files(const char *file_fmt, char ***out)
1320
1326
/* get dir and file prefix from format */
1321
1327
p = strrchr (dir , '/' );
1322
1328
if (!p ) {
1323
- ret = - EINVAL ;
1329
+ LOGE ("only support abs path, dir (%s)\n" , dir );
1330
+ ret = -1 ;
1324
1331
goto free_dir ;
1325
1332
}
1326
1333
* p = '\0' ;
1327
1334
file_prefix = p + 1 ;
1328
- * strrchr (file_prefix , '[' ) = '\0' ;
1335
+ p = strrchr (file_prefix , '[' );
1336
+ if (p ) {
1337
+ * p = '\0' ;
1338
+ } else {
1339
+ ret = -1 ;
1340
+ LOGE ("unsupported formats (%s)\n" , dir );
1341
+ goto free_dir ;
1342
+ }
1343
+
1329
1344
1330
1345
if (!directory_exists (dir )) {
1331
1346
ret = 0 ;
1332
1347
goto free_dir ;
1333
1348
}
1334
1349
/* get format type */
1335
1350
subfix = strrchr (file_fmt , '[' );
1351
+ if (!subfix ) {
1352
+ ret = -1 ;
1353
+ LOGE ("unsupported formats (%s)\n" , dir );
1354
+ goto free_dir ;
1355
+ }
1336
1356
res = sscanf (subfix , "[%2[01-*]]" , type );
1337
1357
if (res != 1 ) {
1338
- ret = - EINVAL ;
1358
+ ret = -1 ;
1359
+ LOGE ("unsupported formats (%s)\n" , dir );
1339
1360
goto free_dir ;
1340
1361
}
1341
1362
1342
1363
/* get all files which start with prefix */
1343
1364
count = ac_scandir (dir , & filelist , filter_filename_startswith ,
1344
1365
file_prefix , alphasort );
1345
- if (count <= 0 ) {
1346
- ret = count ;
1366
+ if (count < 0 ) {
1367
+ ret = -1 ;
1368
+ LOGE ("failed to ac_scandir\n" );
1369
+ goto free_dir ;
1370
+ }
1371
+ if (!count ) {
1372
+ ret = 0 ;
1347
1373
goto free_dir ;
1348
1374
}
1349
1375
1350
1376
/* construct output */
1351
1377
out_array = (char * * )malloc (count * sizeof (char * ));
1352
1378
if (!out_array ) {
1353
- ret = - errno ;
1379
+ ret = -1 ;
1380
+ LOGE ("failed to malloc\n" );
1354
1381
goto free_filelist ;
1355
1382
}
1356
1383
0 commit comments