| 1224 | } |
| 1225 | |
| 1226 | static FTSENT* |
| 1227 | fts_alloc(FTS* sp, const char* name, int namelen) |
| 1228 | { |
| 1229 | FTSENT* p; |
| 1230 | size_t len; |
| 1231 | |
| 1232 | /* |
| 1233 | * The file name is a variable length array and no stat structure is |
| 1234 | * necessary if the user has set the nostat bit. Allocate the FTSENT |
| 1235 | * structure, the file name and the stat structure in one chunk, but |
| 1236 | * be careful that the stat structure is reasonably aligned. Since the |
| 1237 | * fts_name field is declared to be of size 1, the fts_name pointer is |
| 1238 | * namelen + 2 before the first possible address of the stat structure. |
| 1239 | */ |
| 1240 | len = sizeof(FTSENT) + namelen; |
| 1241 | if (!ISSET(FTS_NOSTAT)) { |
| 1242 | len += sizeof(stat_struct) + ALIGNBYTES; |
| 1243 | } |
| 1244 | if ((p = (FTSENT*)malloc(len)) == nullptr) { |
| 1245 | return nullptr; |
| 1246 | } |
| 1247 | |
| 1248 | /* Copy the name and guarantee NUL termination. */ |
| 1249 | memmove((void*)p->fts_name, (void*)name, (size_t)namelen); |
| 1250 | p->fts_name[namelen] = '\0'; |
| 1251 | |
| 1252 | if (!ISSET(FTS_NOSTAT)) { |
| 1253 | p->fts_statp = (stat_struct*)ALIGN(p->fts_name + namelen + 2); |
| 1254 | } else { |
| 1255 | p->fts_statp = nullptr; |
| 1256 | } |
| 1257 | p->fts_namelen = (u_short)namelen; |
| 1258 | p->fts_path = sp->fts_path; |
| 1259 | p->fts_errno = 0; |
| 1260 | p->fts_flags = 0; |
| 1261 | p->fts_instr = FTS_NOINSTR; |
| 1262 | p->fts_number = 0; |
| 1263 | p->fts_pointer = nullptr; |
| 1264 | p->fts_type = FTS_NSOK; |
| 1265 | return (p); |
| 1266 | } |
| 1267 | |
| 1268 | static void |
| 1269 | fts_lfree(FTSENT* head) |