phantasmal_dgd_v1/
phantasmal_dgd_v1/bin/
phantasmal_dgd_v1/doc/
phantasmal_dgd_v1/mud/doc/
phantasmal_dgd_v1/mud/doc/api/
phantasmal_dgd_v1/mud/doc/kernel/
phantasmal_dgd_v1/mud/doc/kernel/hook/
phantasmal_dgd_v1/mud/doc/kernel/lfun/
phantasmal_dgd_v1/mud/include/
phantasmal_dgd_v1/mud/include/kernel/
phantasmal_dgd_v1/mud/kernel/lib/
phantasmal_dgd_v1/mud/kernel/lib/api/
phantasmal_dgd_v1/mud/kernel/obj/
phantasmal_dgd_v1/mud/kernel/sys/
phantasmal_dgd_v1/mud/tmp/
phantasmal_dgd_v1/mud/usr/System/
phantasmal_dgd_v1/mud/usr/System/keys/
phantasmal_dgd_v1/mud/usr/System/obj/
phantasmal_dgd_v1/mud/usr/System/open/lib/
phantasmal_dgd_v1/mud/usr/common/data/
phantasmal_dgd_v1/mud/usr/common/lib/parsed/
phantasmal_dgd_v1/mud/usr/common/obj/telopt/
phantasmal_dgd_v1/mud/usr/common/obj/ustate/
phantasmal_dgd_v1/mud/usr/game/
phantasmal_dgd_v1/mud/usr/game/include/
phantasmal_dgd_v1/mud/usr/game/obj/
phantasmal_dgd_v1/mud/usr/game/object/
phantasmal_dgd_v1/mud/usr/game/object/stuff/
phantasmal_dgd_v1/mud/usr/game/sys/
phantasmal_dgd_v1/mud/usr/game/text/
phantasmal_dgd_v1/mud/usr/game/users/
phantasmal_dgd_v1/src/host/
phantasmal_dgd_v1/src/host/beos/
phantasmal_dgd_v1/src/host/mac/
phantasmal_dgd_v1/src/host/unix/
phantasmal_dgd_v1/src/host/win32/res/
phantasmal_dgd_v1/src/kfun/
phantasmal_dgd_v1/src/lpc/
phantasmal_dgd_v1/src/parser/
# include <string.h>

# define IDX(a, i, n)	((void *) ((char *) (a) + (i) * (n)))

/*
 * NAME:	qsort()
 * DESCRIPTION:	sort an array
 */
void qsort(void *arr, size_t size, size_t sz,
	   int (*cmp)(const void *a, const void *b))
{
    char elt[128];
    void *val;
    int n, i, j;

    i = 1;
    for (;;) {
	if (i >= size) {
	    return;
	}
	if (cmp(IDX(arr, i - 1, sz), IDX(arr, i, sz)) > 0) {
	    break;
	}
	i++;
    }

    for (n = 1; n < size; n <<= 1) ;

    for (n >>= 1; n > 0; --n) {
	memcpy(elt, IDX(arr, n - 1, sz), sz);
	for (i = n, j = n << 1; j <= size; i = j, j <<= 1) {
	    val = IDX(arr, j - 1, sz);
	    if (j < size && cmp(IDX(arr, j, sz), val) > 0) {
		val = IDX(arr, j++, sz);
	    }
	    if (cmp(elt, val) > 0) {
		break;
	    }
	    memcpy(IDX(arr, i - 1, sz), val, sz);
	}
	memcpy(IDX(arr, i - 1, sz), elt, sz);
    }

    for (n = size - 1; n > 0; --n) {
	memcpy(elt, IDX(arr, n, sz), sz);
	memcpy(IDX(arr, n, sz), arr, sz);
	for (i = 1, j = 2; j <= n; i = j, j <<= 1) {
	    val = IDX(arr, j - 1, sz);
	    if (j < n && cmp(IDX(arr, j, sz), val) > 0) {
		val = IDX(arr, j++, sz);
	    }
	    if (cmp(elt, val) > 0) {
		break;
	    }
	    memcpy(IDX(arr, i - 1, sz), val, sz);
	}
	memcpy(IDX(arr, i - 1, sz), elt, sz);
    }
}