> Usually whitespace diffs are not that well liked, they make for
> annoying differences, introduce noise and require more effort to review
> for questionable gain.
>
> Otoh this file is quite bad. It makes my vim light up all red.
That's why you should stick to vi and not the emacs-in-a-vi-disguise
bloatware known as vim.
> I'm still torn on this. Anyone else care to comment?
If this file is to be changed, then PLEASE fix the logic to get rid of
the gotos.
I did this more than 10 years ago (see diff below), but never dared to
expose it to enough real-word testcases to be confident in this.
Reviewers welcome. Be warned it might not apply cleanly anymore...
Index: merge.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/merge.c,v
retrieving revision 1.5
diff -u -r1.5 merge.c
--- merge.c 2002/02/17 19:42:24 1.5
+++ merge.c 2002/12/19 02:12:41
@@ -1,3 +1,4 @@
+/* $OpenBSD$ */
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
@@ -52,7 +53,7 @@
* (The default is pairwise merging.)
*/
-#include <sys/types.h>
+#include <sys/param.h>
#include <errno.h>
#include <stdlib.h>
@@ -63,33 +64,30 @@
#define ISIZE sizeof(int)
#define PSIZE sizeof(u_char *)
-#define ICOPY_LIST(src, dst, last) \
- do \
- *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
- while(src < last)
-#define ICOPY_ELT(src, dst, i) \
- do \
- *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
- while (i -= ISIZE)
+#define ICOPY_LIST(src, dst, last) \
+ do { \
+ *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
+ } while(src < last)
+#define ICOPY_ELT(src, dst, i) \
+ do { \
+ *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
+ } while (i -= ISIZE)
#define CCOPY_LIST(src, dst, last) \
- do \
+ do { \
*dst++ = *src++; \
- while (src < last)
+ } while (src < last)
#define CCOPY_ELT(src, dst, i) \
- do \
+ for (; i; i--) { \
*dst++ = *src++; \
- while (i -= 1)
+ }
/*
* Find the next possible pointer head. (Trickery for forcing an array
* to do double duty as a linked list when objects do not align with word
* boundaries.
*/
-/* Assumption: PSIZE is a power of 2. */
-#define EVAL(p) (u_char **) \
- ((u_char *)0 + \
- (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
+#define EVAL(p) (u_char **)roundup((long)p, PSIZE)
/*
* Arguments are as for qsort.
@@ -165,7 +163,14 @@
t = p;
if (i == size)
big = 0;
- goto FASTCASE;
+
+ while (i > size)
+ if ((*cmp)(q,
+ p = b + (i >>= 1))
+ <= sense)
+ t = p;
+ else
+ b = p;
} else
b = p;
while (t > b+size) {
@@ -175,14 +180,7 @@
else
b = p;
}
- goto COPY;
-FASTCASE: while (i > size)
- if ((*cmp)(q,
- p = b + (i >>= 1)) <= sense)
- t = p;
- else
- b = p;
-COPY: b = t;
+ b = t;
}
i = size;
if (q == f1) {
@@ -231,21 +229,19 @@
#define swap(a, b) { \
s = b; \
- i = size; \
- do { \
+ for (i = size; i; i--) { \
tmp = *a; *a++ = *s; *s++ = tmp; \
- } while (--i); \
+ } \
a -= size; \
}
#define reverse(bot, top) { \
s = top; \
do { \
- i = size; \
- do { \
+ for (i = size; i; i--) { \
tmp = *bot; *bot++ = *s; *s++ = tmp; \
- } while (--i); \
+ } \
s -= size2; \
- } while(bot < s); \
+ } while (bot < s); \
}
/*
Index: qsort.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/qsort.c,v
retrieving revision 1.7
diff -u -r1.7 qsort.c
--- qsort.c 2002/02/17 19:42:24 1.7
+++ qsort.c 2002/12/19 02:12:42
@@ -1,3 +1,4 @@
+/* $OpenBSD$ */
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
@@ -35,14 +36,12 @@
static char *rcsid = "$OpenBSD: qsort.c,v 1.7 2002/02/17 19:42:24 millert Exp
$";
#endif /* LIBC_SCCS and not lint */
-#include <sys/types.h>
+#include <sys/param.h>
#include <stdlib.h>
static __inline char *med3(char *, char *, char *, int (*)());
static __inline void swapfunc(char *, char *, int, int);
-#define min(a, b) (a) < (b) ? a : b
-
/*
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
*/
@@ -84,7 +83,7 @@
static __inline char *
med3(a, b, c, cmp)
char *a, *b, *c;
- int (*cmp)();
+ int (*cmp)(const void *, const void *);
{
return cmp(a, b) < 0 ?
(cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
@@ -159,9 +158,9 @@
}
pn = (char *)a + n * es;
- r = min(pa - (char *)a, pb - pa);
+ r = MIN(pa - (char *)a, pb - pa);
vecswap(a, pb - r, r);
- r = min(pd - pc, pn - pd - es);
+ r = MIN(pd - pc, pn - pd - es);
vecswap(pb, pn - r, r);
if ((r = pb - pa) > es)
qsort(a, r / es, es, cmp);