[klibc] strverscmp, scandir, alphasort and versionsort

Luciano Miguel Ferreira Rocha strange at nsk.no-ip.org
Tue Oct 3 15:25:22 PDT 2006


qsort blocks and consumes all cpu if nmemb == 0, so new scandir.c,
changing:
> 	} else {
> 		qsort(list, nl, sizeof(struct dirent *), compar);
> 		*namelist = list;
> 	}

to
        } else if (nl > 0) {
                qsort(list, nl, sizeof(struct dirent *), compar);
                *namelist = list;
        }

-- 
lfr
0/0
-------------- next part --------------
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2006 Luciano Rocha - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>

/* from the manual:
 *        The scandir() function scans the directory  dir,  calling
 *        filter()  on each  directory entry.  Entries for which filter()
 *        returns non-zero are stored in strings allocated via malloc(),
 *        sorted using qsort() with the comparison  function compar(), and
 *        collected in array namelist which is allocated via malloc().  If
 *        filter is NULL, all entries are selected.
 */
int scandir(const char *dir,
		struct dirent ***namelist,
		int(*filter)(const struct dirent *),
		int(*compar)(const void *, const void *))
{
	DIR *d;
	struct dirent *de, **list;
	int nl;

	if (!(d = opendir(dir)))
		return -1;

	list = NULL;
	nl = 0;

	while ((de = readdir(d))) {
		if (filter && !filter(de)) continue;
		if (!(list = realloc(list, sizeof(struct dirent *) * (nl+1)))
				|| !(list[nl] = malloc(de->d_reclen)))
			break;
		memcpy(list[nl], de, de->d_reclen);
		nl++;
	}

	closedir(d);

	/* error allocating memory? */
	if (de) {
		while (nl > 0) {
			free(list[--nl]);
		}
		free(list);
		nl = -1;
	} else if (nl > 0) {
		qsort(list, nl, sizeof(struct dirent *), compar);
		*namelist = list;
	}

	return nl;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.zytor.com/pipermail/klibc/attachments/20061003/21b08555/attachment.bin 


More information about the klibc mailing list