use qsort(3) instead of rolling our own

saves 284 in .text, no added import since we already use qsort(3) once
This commit is contained in:
tg 2006-11-10 03:23:50 +00:00
parent f103c6b2a0
commit f8e7fdbb71
5 changed files with 22 additions and 115 deletions

5
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.65 2006/11/10 01:24:38 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.66 2006/11/10 03:23:48 tg Exp $");
/* tty driver characters we are interested in */
typedef struct {
@ -508,8 +508,7 @@ x_command_glob(int flags, const char *str, int slen, char ***wordsp)
char **words = (char **)XPptrv(w);
int i, j;
qsortp(XPptrv(w), (size_t)nwords, xstrcmp);
qsort(words, nwords, sizeof (void *), xstrcmp);
for (i = j = 0; i < nwords - 1; i++) {
if (strcmp(words[i], words[i + 1]))
words[j++] = words[i];

6
eval.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.15 2006/11/10 00:09:27 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.16 2006/11/10 03:23:49 tg Exp $");
/*
* string expansion
@ -929,8 +929,8 @@ glob(char *cp, XPtrV *wp, int markdirs)
if (glob_str(cp, wp, markdirs) == 0)
XPput(*wp, debunk(cp, cp, strlen(cp) + 1));
else
qsortp(XPptrv(*wp) + oldsize, (size_t)(XPsize(*wp) - oldsize),
xstrcmp);
qsort(XPptrv(*wp) + oldsize, XPsize(*wp) - oldsize,
sizeof (void *), xstrcmp);
}
#define GF_NONE 0

18
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.53 2006/11/10 01:13:51 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.54 2006/11/10 03:23:49 tg Exp $");
extern char **environ;
@ -1098,7 +1098,7 @@ maketemp(Area *ap, Temp_type type, struct temp **tlist)
#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
hash(const char *n)
@ -1241,26 +1241,28 @@ 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);
const struct tbl *a = *((const struct tbl **)p1);
const struct tbl *b = *((const struct tbl **)p2);
return (strcmp(a->name, b->name));
}
struct tbl **
ktsort(struct table *tp)
{
int i;
size_t i;
struct tbl **p, **sp, **dp;
p = (struct tbl **)alloc(sizeofN(struct tbl *, tp->size + 1), ATEMP);
sp = tp->tbls; /* source */
dp = p; /* dest */
for (i = 0; i < tp->size; i++)
for (i = 0; i < (size_t)tp->size; i++)
if ((*dp = *sp++) != NULL && (((*dp)->flag & DEFINED) ||
((*dp)->flag & ARRAY)))
dp++;
i = dp - p;
qsortp((void **)p, (size_t)i, tnamecmp);
qsort(p, (i = dp - p), sizeof (void *), tnamecmp);
p[i] = NULL;
return p;
}

103
misc.c
View File

@ -3,7 +3,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.28 2006/11/10 02:10:45 tg Exp $\t"
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.29 2006/11/10 03:23:50 tg Exp $\t"
MKSH_SH_H_ID);
unsigned char chtypes[UCHAR_MAX + 1]; /* type bits for unsigned char */
@ -400,7 +400,7 @@ parse_args(char **argv,
if (sortargs) {
for (i = go.optind; argv[i]; i++)
;
qsortp((void **) &argv[go.optind], (size_t) (i - go.optind),
qsort(&argv[go.optind], i - go.optind, sizeof (void *),
xstrcmp);
}
if (arrayset) {
@ -600,8 +600,7 @@ do_gmatch(const unsigned char *s, const unsigned char *se,
/*
* [*+?@!](pattern|pattern|..)
*
* Not ifdef'd KSH as this is needed for ${..%..}, etc.
* This is also needed for ${..%..}, etc.
*/
case 0x80|'+': /* matches one or more times */
case 0x80|'*': /* matches zero or more times */
@ -742,102 +741,10 @@ pat_scan(const unsigned char *p, const unsigned char *pe, int match_sep)
return NULL;
}
/* -------- 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 */
{
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;
}
}
int
xstrcmp(void *p1, void *p2)
xstrcmp(const void *p1, const void *p2)
{
return (strcmp((char *)p1, (char *)p2));
return (strcmp(*(const char **)p1, *(const char **)p2));
}
/* Initialize a Getopt structure */

5
sh.h
View File

@ -8,7 +8,7 @@
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.71 2006/11/10 01:44:40 tg Exp $"
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.72 2006/11/10 03:23:50 tg Exp $"
#define MKSH_VERSION "R29 2006/11/10"
#if HAVE_SYS_PARAM_H
@ -1231,8 +1231,7 @@ int bi_getn(const char *, int *);
int gmatchx(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 *);
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 *);