libc: small cleanups.

Rename nitems to numitems: it shares the anme with an existing macro in
sys/params.h. Also initialize the value later which avoids asigning the
value if we exit early.

Reviewed by:	ngie
MFC after:	3 days
This commit is contained in:
pfg 2017-03-12 03:22:18 +00:00
parent f5809ea627
commit d2ff5eb11a

View File

@ -82,7 +82,7 @@ scandir(const char *dirname, struct dirent ***namelist,
#endif
{
struct dirent *d, *p, **names = NULL;
size_t nitems = 0;
size_t numitems;
long arraysz;
DIR *dirp;
@ -94,6 +94,7 @@ scandir(const char *dirname, struct dirent ***namelist,
if (names == NULL)
goto fail;
numitems = 0;
while ((d = readdir(dirp)) != NULL) {
if (select != NULL && !SELECT(d))
continue; /* just selected names */
@ -112,7 +113,7 @@ scandir(const char *dirname, struct dirent ***namelist,
* Check to make sure the array has space left and
* realloc the maximum size.
*/
if (nitems >= arraysz) {
if (numitems >= arraysz) {
struct dirent **names2;
names2 = (struct dirent **)realloc((char *)names,
@ -124,22 +125,22 @@ scandir(const char *dirname, struct dirent ***namelist,
names = names2;
arraysz *= 2;
}
names[nitems++] = p;
names[numitems++] = p;
}
closedir(dirp);
if (nitems && dcomp != NULL)
if (numitems && dcomp != NULL)
#ifdef I_AM_SCANDIR_B
qsort_b(names, nitems, sizeof(struct dirent *), (void*)dcomp);
qsort_b(names, numitems, sizeof(struct dirent *), (void*)dcomp);
#else
qsort_r(names, nitems, sizeof(struct dirent *),
qsort_r(names, numitems, sizeof(struct dirent *),
&dcomp, alphasort_thunk);
#endif
*namelist = names;
return (nitems);
return (numitems);
fail:
while (nitems > 0)
free(names[--nitems]);
while (numitems > 0)
free(names[--numitems]);
free(names);
closedir(dirp);
return (-1);