Index: fields.c =================================================================== RCS file: /cvs/src/usr.bin/sort/fields.c,v retrieving revision 1.11 diff -u -r1.11 fields.c --- fields.c 2006/02/14 14:45:36 1.11 +++ fields.c 2007/07/28 12:51:23 @@ -118,7 +118,7 @@ fieldtable->flags)) == NULL) return (1); - if (UNIQUE) + if (UNIQUE || STABLE) *(keypos-1) = REC_D; keybuf->offset = keypos - keybuf->data; keybuf->length = keybuf->offset + line->size; @@ -196,7 +196,7 @@ * To avoid confusing the exponent and the mantissa, use a field delimiter * if the exponent is exactly 61, 61+252, etc--this is ok, since it's the * only time a field delimiter can come in that position. - * Reverse order is done analagously. + * Reverse order is done analogously. */ u_char * Index: fsort.c =================================================================== RCS file: /cvs/src/usr.bin/sort/fsort.c,v retrieving revision 1.18 diff -u -r1.18 fsort.c --- fsort.c 2007/03/13 17:33:58 1.18 +++ fsort.c 2007/07/28 12:51:24 @@ -173,8 +173,11 @@ } get = getnext; if (!ntfiles && !mfct) { /* everything in memory--pop */ - if (nelem > 1 && radixsort((const u_char **)keylist, - nelem, weights, REC_D)) + if (nelem > 1 && (STABLE ? + sradixsort((const u_char **)keylist, + nelem, weights, REC_D) : + radixsort((const u_char **)keylist, + nelem, weights, REC_D))) err(2, NULL); append(keylist, nelem, depth, outfp, putline, ftbl); break; /* pop */ Index: msort.c =================================================================== RCS file: /cvs/src/usr.bin/sort/msort.c,v retrieving revision 1.20 diff -u -r1.20 msort.c --- msort.c 2007/03/13 17:33:58 1.20 +++ msort.c 2007/07/28 12:51:24 @@ -295,7 +295,7 @@ for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) { pos1 = rec1->data; pos2 = rec2->data; - if (!SINGL_FLD && UNIQUE) + if (!SINGL_FLD && UNIQUE || STABLE) end = pos1 + min(rec1->offset, rec2->offset); else end = pos1 + min(rec1->length, rec2->length); Index: sort.1 =================================================================== RCS file: /cvs/src/usr.bin/sort/sort.1,v retrieving revision 1.29 diff -u -r1.29 sort.1 --- sort.1 2007/05/31 19:20:16 1.29 +++ sort.1 2007/07/28 12:51:24 @@ -136,6 +136,10 @@ option.) .It Fl r Reverse the sense of comparisons. +.It Fl s +Enable stable sort. Use additional resources (see sradixsort(3)). +.It Fl S +Disable stable sort. This is the default. Provided for compatibility only. .El .Pp The treatment of field separators can be altered using these options: Index: sort.c =================================================================== RCS file: /cvs/src/usr.bin/sort/sort.c,v retrieving revision 1.34 diff -u -r1.34 sort.c --- sort.c 2007/03/13 17:33:58 1.34 +++ sort.c 2007/07/28 12:51:24 @@ -80,7 +80,7 @@ * masks of ignored characters. Alltable is 256 ones */ u_char dtable[NBINS], itable[NBINS], alltable[NBINS]; -int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0; +int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0, STABLE = 0; struct coldesc *clist; int ncols = 0; int ND = 10; /* limit on number of -k options. */ @@ -125,7 +125,7 @@ fixit(&argc, argv); if (!issetugid() && (outfile = getenv("TMPDIR"))) tmpdir = outfile; - while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:z")) != -1) { + while ((ch = getopt(argc, argv, "bcdfik:mHno:rR:t:T:uy:zsS")) != -1) { switch (ch) { case 'b': fldtab->flags |= BI | BT; break; @@ -192,6 +192,12 @@ d_mask['\n'] = d_mask[' ']; d_mask[REC_D] = REC_D_F; break; + case 's': + STABLE = 1; + break; + case 'S': + STABLE = 0; + break; case '?': default: usage(NULL); @@ -340,7 +346,7 @@ if (msg != NULL) warnx("%s", msg); - (void)fprintf(stderr, "usage: %s [-bcdfHimnruz] " + (void)fprintf(stderr, "usage: %s [-bcdfHimnruzsS] " "[-k field1[,field2]] [-o output] [-R char]\n" "\t[-T dir] [-t char] [file ...]\n", __progname); exit(2); Index: sort.h =================================================================== RCS file: /cvs/src/usr.bin/sort/sort.h,v retrieving revision 1.6 diff -u -r1.6 sort.h --- sort.h 2003/06/03 02:56:16 1.6 +++ sort.h 2007/07/28 12:51:24 @@ -133,7 +133,7 @@ extern u_char ascii[NBINS], Rascii[NBINS], Ftable[NBINS], RFtable[NBINS]; extern u_char alltable[NBINS], dtable[NBINS], itable[NBINS]; extern u_char d_mask[NBINS]; -extern int SINGL_FLD, SEP_FLAG, UNIQUE; +extern int SINGL_FLD, SEP_FLAG, UNIQUE, STABLE; extern int REC_D; extern char *tmpdir; extern int ND; /* limit on number of -k options. */