| 1186 | } |
| 1187 | |
| 1188 | static FTSENT* |
| 1189 | fts_sort(FTS* sp, FTSENT* head, int nitems) |
| 1190 | { |
| 1191 | FTSENT **ap, *p; |
| 1192 | |
| 1193 | /* |
| 1194 | * Construct an array of pointers to the structures and call qsort(3). |
| 1195 | * Reassemble the array in the order returned by qsort. If unable to |
| 1196 | * sort for memory reasons, return the directory entries in their |
| 1197 | * current order. Allocate enough space for the current needs plus |
| 1198 | * 40 so don't realloc one entry at a time. |
| 1199 | */ |
| 1200 | if (nitems > sp->fts_nitems) { |
| 1201 | struct _ftsent** a; |
| 1202 | |
| 1203 | sp->fts_nitems = nitems + 40; |
| 1204 | if ((a = (struct _ftsent**)realloc(sp->fts_array, |
| 1205 | sp->fts_nitems * sizeof(FTSENT*))) == nullptr) { |
| 1206 | if (sp->fts_array) { |
| 1207 | free(sp->fts_array); |
| 1208 | } |
| 1209 | sp->fts_array = nullptr; |
| 1210 | sp->fts_nitems = 0; |
| 1211 | return (head); |
| 1212 | } |
| 1213 | sp->fts_array = a; |
| 1214 | } |
| 1215 | for (ap = sp->fts_array, p = head; p; p = p->fts_link) { |
| 1216 | *ap++ = p; |
| 1217 | } |
| 1218 | qsort((void*)sp->fts_array, (size_t)nitems, sizeof(FTSENT*), (int (*)(const void*, const void*))sp->fts_compar); |
| 1219 | for (head = *(ap = sp->fts_array); --nitems; ++ap) { |
| 1220 | ap[0]->fts_link = ap[1]; |
| 1221 | } |
| 1222 | ap[0]->fts_link = nullptr; |
| 1223 | return (head); |
| 1224 | } |
| 1225 | |
| 1226 | static FTSENT* |
| 1227 | fts_alloc(FTS* sp, const char* name, int namelen) |