lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1992-1998, 2000 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, write to the Free |
| 16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 17 | 02111-1307 USA. |
| 18 | */ |
| 19 | |
| 20 | /* Modified for uClibc by Erik Andersen |
| 21 | */ |
| 22 | |
| 23 | #include <_lfs_64.h> |
| 24 | |
| 25 | #include <dirent.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #include "dirstream.h" |
| 32 | |
| 33 | int scandir64(const char *dir, struct dirent64 ***namelist, |
| 34 | int (*selector) (const struct dirent64 *), |
| 35 | int (*compar) (const struct dirent64 **, const struct dirent64 **)) |
| 36 | { |
| 37 | DIR *dp = opendir (dir); |
| 38 | struct dirent64 *current; |
| 39 | struct dirent64 **names = NULL; |
| 40 | size_t names_size = 0, pos; |
| 41 | int save; |
| 42 | |
| 43 | if (dp == NULL) |
| 44 | return -1; |
| 45 | |
| 46 | save = errno; |
| 47 | __set_errno (0); |
| 48 | |
| 49 | pos = 0; |
| 50 | while ((current = readdir64 (dp)) != NULL) { |
| 51 | int use_it = selector == NULL; |
| 52 | |
| 53 | if (! use_it) |
| 54 | { |
| 55 | use_it = (*selector) (current); |
| 56 | /* The selector function might have changed errno. |
| 57 | * It was zero before and it need to be again to make |
| 58 | * the latter tests work. */ |
| 59 | if (! use_it) |
| 60 | __set_errno (0); |
| 61 | } |
| 62 | if (use_it) |
| 63 | { |
| 64 | struct dirent64 *vnew; |
| 65 | size_t dsize; |
| 66 | |
| 67 | /* Ignore errors from selector or readdir64 */ |
| 68 | __set_errno (0); |
| 69 | |
| 70 | if (unlikely(pos == names_size)) |
| 71 | { |
| 72 | struct dirent64 **new; |
| 73 | if (names_size == 0) |
| 74 | names_size = 10; |
| 75 | else |
| 76 | names_size *= 2; |
| 77 | new = (struct dirent64 **) realloc (names, |
| 78 | names_size * sizeof (struct dirent64 *)); |
| 79 | if (new == NULL) |
| 80 | break; |
| 81 | names = new; |
| 82 | } |
| 83 | |
| 84 | dsize = ¤t->d_name[_D_ALLOC_NAMLEN(current)] - (char*)current; |
| 85 | vnew = (struct dirent64 *) malloc (dsize); |
| 86 | if (vnew == NULL) |
| 87 | break; |
| 88 | |
| 89 | names[pos++] = (struct dirent64 *) memcpy (vnew, current, dsize); |
| 90 | } |
| 91 | } |
| 92 | if (unlikely(errno != 0)) |
| 93 | { |
| 94 | save = errno; |
| 95 | closedir (dp); |
| 96 | while (pos > 0) |
| 97 | free (names[--pos]); |
| 98 | free (names); |
| 99 | __set_errno (save); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | closedir (dp); |
| 104 | __set_errno (save); |
| 105 | |
| 106 | /* Sort the list if we have a comparison function to sort with. */ |
| 107 | if (compar != NULL) |
| 108 | qsort (names, pos, sizeof (struct dirent64 *), (comparison_fn_t) compar); |
| 109 | *namelist = names; |
| 110 | return pos; |
| 111 | } |