diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2009-01-17 22:06:45 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2009-01-17 22:06:45 +0000 |
commit | 7fa4ddd339f6387f22f417663d62a3a100ca2565 (patch) | |
tree | 41498c09ad455de1cd41dc2c87004829c19280db | |
parent | e53a83f95418337d04f6cc3057f52e2053e34bab (diff) |
Use libc qsort instead of private version. Tested by several people.
-rw-r--r-- | bin/ksh/misc.c | 91 | ||||
-rw-r--r-- | bin/ksh/proto.h | 6 | ||||
-rw-r--r-- | bin/ksh/table.c | 10 |
3 files changed, 14 insertions, 93 deletions
diff --git a/bin/ksh/misc.c b/bin/ksh/misc.c index 48f45fedc27..874a88e89ff 100644 --- a/bin/ksh/misc.c +++ b/bin/ksh/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.34 2008/07/12 12:33:42 miod Exp $ */ +/* $OpenBSD: misc.c,v 1.35 2009/01/17 22:06:44 millert Exp $ */ /* * Miscellaneous functions @@ -764,102 +764,21 @@ pat_scan(const unsigned char *p, const unsigned char *pe, int match_sep) return (const unsigned char *) 0; } - -/* -------- qsort.c -------- */ - /* * quick sort of array of generic pointers to objects. */ -static void qsort1(void **base, void **lim, int (*f)(void *, void *)); - void qsortp(void **base, /* base address */ size_t n, /* elements */ - int (*f) (void *, void *)) /* compare function */ + int (*f) (const void *, const void *)) /* compare function */ { - qsort1(base, base + n, f); -} - -#define swap2(a, b) {\ - void *t; t = *(a); *(a) = *(b); *(b) = t;\ -} -#define swap3(a, b, c) {\ - void *t; t = *(a); *(a) = *(c); *(c) = *(b); *(b) = t;\ -} - -static void -qsort1(void **base, void **lim, int (*f) (void *, void *)) -{ - void **i, **j; - void **lptr, **hptr; - size_t n; - int c; - - top: - n = (lim - base) / 2; - if (n == 0) - return; - hptr = lptr = base+n; - i = base; - j = lim - 1; - - for (;;) { - if (i < lptr) { - if ((c = (*f)(*i, *lptr)) == 0) { - lptr --; - swap2(i, lptr); - continue; - } - if (c < 0) { - i += 1; - continue; - } - } - - begin: - if (j > hptr) { - if ((c = (*f)(*hptr, *j)) == 0) { - hptr ++; - swap2(hptr, j); - goto begin; - } - if (c > 0) { - if (i == lptr) { - hptr ++; - swap3(i, hptr, j); - i = lptr += 1; - goto begin; - } - swap2(i, j); - j -= 1; - i += 1; - continue; - } - j -= 1; - goto begin; - } - - if (i == lptr) { - if (lptr-base >= lim-hptr) { - qsort1(hptr+1, lim, f); - lim = lptr; - } else { - qsort1(base, lptr, f); - base = hptr+1; - } - goto top; - } - - lptr -= 1; - swap3(j, lptr, i); - j = hptr -= 1; - } + qsort(base, n, sizeof(char *), f); } int -xstrcmp(void *p1, void *p2) +xstrcmp(const void *p1, const void *p2) { - return (strcmp((char *)p1, (char *)p2)); + return (strcmp(*(char **)p1, *(char **)p2)); } /* Initialize a Getopt structure */ diff --git a/bin/ksh/proto.h b/bin/ksh/proto.h index 2547e2122d3..95c0c12531a 100644 --- a/bin/ksh/proto.h +++ b/bin/ksh/proto.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proto.h,v 1.30 2006/03/17 16:30:13 millert Exp $ */ +/* $OpenBSD: proto.h,v 1.31 2009/01/17 22:06:44 millert Exp $ */ /* * prototypes for PD-KSH @@ -182,8 +182,8 @@ int gmatch(const char *, const char *, int); int has_globbing(const char *, const char *); const unsigned char *pat_scan(const unsigned char *, const unsigned char *, int); -void qsortp(void **, size_t, int (*)(void *, void *)); -int xstrcmp(void *, void *); +void qsortp(void **, size_t, int (*)(const void *, const void *)); +int xstrcmp(const void *, const void *); void ksh_getopt_reset(Getopt *, int); int ksh_getopt(char **, Getopt *, const char *); void print_value_quoted(const char *); diff --git a/bin/ksh/table.c b/bin/ksh/table.c index 7de3091feba..5f052c9bafc 100644 --- a/bin/ksh/table.c +++ b/bin/ksh/table.c @@ -1,4 +1,4 @@ -/* $OpenBSD: table.c,v 1.12 2005/12/11 20:31:21 otto Exp $ */ +/* $OpenBSD: table.c,v 1.13 2009/01/17 22:06:44 millert Exp $ */ /* * dynamic hashed associative table for commands and variables @@ -9,7 +9,7 @@ #define INIT_TBLS 8 /* initial table size (power of 2) */ static void texpand(struct table *, int); -static int tnamecmp(void *, void *); +static int tnamecmp(const void *, const void *); unsigned int @@ -154,9 +154,11 @@ ktnext(struct tstate *ts) } static int -tnamecmp(void *p1, void *p2) +tnamecmp(const void *p1, const void *p2) { - return strcmp(((struct tbl *)p1)->name, ((struct tbl *)p2)->name); + char *name1 = (*(struct tbl **)p1)->name; + char *name2 = (*(struct tbl **)p2)->name; + return strcmp(name1, name2); } struct tbl ** |