[Midnightbsd-cvs] src [7156] trunk/usr.bin/sort: update sort
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Sun Jul 26 13:10:43 EDT 2015
Revision: 7156
http://svnweb.midnightbsd.org/src/?rev=7156
Author: laffer1
Date: 2015-07-26 13:10:42 -0400 (Sun, 26 Jul 2015)
Log Message:
-----------
update sort
Modified Paths:
--------------
trunk/usr.bin/sort/Makefile
trunk/usr.bin/sort/bwstring.c
trunk/usr.bin/sort/bwstring.h
trunk/usr.bin/sort/coll.c
trunk/usr.bin/sort/coll.h
trunk/usr.bin/sort/file.c
trunk/usr.bin/sort/file.h
trunk/usr.bin/sort/mem.c
trunk/usr.bin/sort/mem.h
trunk/usr.bin/sort/radixsort.c
trunk/usr.bin/sort/radixsort.h
trunk/usr.bin/sort/sort.1.in
trunk/usr.bin/sort/sort.c
trunk/usr.bin/sort/sort.h
trunk/usr.bin/sort/vsort.c
trunk/usr.bin/sort/vsort.h
Modified: trunk/usr.bin/sort/Makefile
===================================================================
--- trunk/usr.bin/sort/Makefile 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/Makefile 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,5 +1,5 @@
# $MidnightBSD$
-# $FreeBSD: src/usr.bin/sort/Makefile,v 1.8 2012/06/27 05:59:01 gabor Exp $
+# $FreeBSD: stable/10/usr.bin/sort/Makefile 281834 2015-04-21 15:47:00Z garga $
.include <bsd.own.mk>
@@ -7,8 +7,6 @@
SRCS= bwstring.c coll.c file.c mem.c radixsort.c sort.c vsort.c
-WARNS= 6
-
sort.1: sort.1.in
/usr/bin/sed ${MAN_SUB} ${.ALLSRC} >${.TARGET}
@@ -16,23 +14,14 @@
.if defined(WITH_THREADS)
CFLAGS+= -DSORT_THREADS
-LDFLAGS+= -lpthread -lmd
+LDADD+= -lpthread -lmd
MAN_SUB+= -e 's|%%THREADS%%||g'
.else
-LDFLAGS+= -lmd
+LDADD+= -lmd
MAN_SUB+= -e 's|%%THREADS%%|\.\\"|g'
.endif
-#.if !defined(WITHOUT_NLS)
-#NLS+= hu_HU.ISO8859-2
-#NLSSRCFILES= ${NLS:S@$@.msg@}
-#MAN_SUB+= -e 's|%%NLS%%||g'
-#.for lang in ${NLS}
-#NLSSRCDIR_${lang}= ${.CURDIR}/nls
-#.endfor
-#.else
CFLAGS+= -DWITHOUT_NLS
MAN_SUB+= -e 's|%%NLS%%|\.\\"|g'
-#.endif
.include <bsd.prog.mk>
Modified: trunk/usr.bin/sort/bwstring.c
===================================================================
--- trunk/usr.bin/sort/bwstring.c 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/bwstring.c 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,6 +1,7 @@
+/* $MidnightBSD$ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sort/bwstring.c,v 1.3 2012/05/25 09:30:16 gabor Exp $");
+__FBSDID("$FreeBSD: stable/10/usr.bin/sort/bwstring.c 281535 2015-04-14 18:57:50Z pfg $");
#include <ctype.h>
#include <errno.h>
@@ -65,18 +66,12 @@
for (int i = 0; i < 12; i++) {
cmonths[i] = NULL;
tmp = (unsigned char *) nl_langinfo(item[i]);
- if (tmp == NULL)
- continue;
if (debug_sort)
printf("month[%d]=%s\n", i, tmp);
+ if (*tmp == '\0')
+ continue;
+ m = sort_strdup(tmp);
len = strlen(tmp);
- if (len < 1)
- continue;
- while (isblank(*tmp))
- ++tmp;
- m = sort_malloc(len + 1);
- memcpy(m, tmp, len + 1);
- m[len] = '\0';
for (unsigned int j = 0; j < len; j++)
m[j] = toupper(m[j]);
cmonths[i] = m;
@@ -91,18 +86,17 @@
for (int i = 0; i < 12; i++) {
wmonths[i] = NULL;
tmp = (unsigned char *) nl_langinfo(item[i]);
- if (tmp == NULL)
- continue;
if (debug_sort)
printf("month[%d]=%s\n", i, tmp);
+ if (*tmp == '\0')
+ continue;
len = strlen(tmp);
- if (len < 1)
- continue;
- while (isblank(*tmp))
- ++tmp;
m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
- if (mbstowcs(m, tmp, len) == ((size_t) -1))
+ if (mbstowcs(m, (char*)tmp, len) ==
+ ((size_t) - 1)) {
+ sort_free(m);
continue;
+ }
m[len] = L'\0';
for (unsigned int j = 0; j < len; j++)
m[j] = towupper(m[j]);
@@ -421,7 +415,7 @@
* The output is ended either with '\n' (nl == true)
* or '\0' (nl == false).
*/
-int
+size_t
bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended)
{
@@ -442,11 +436,11 @@
} else {
wchar_t eols;
- int printed = 0;
+ size_t printed = 0;
eols = zero_ended ? btowc('\0') : btowc('\n');
- while (printed < (int) BWSLEN(bws)) {
+ while (printed < BWSLEN(bws)) {
const wchar_t *s = bws->data.wstr + printed;
if (*s == L'\0') {
@@ -479,7 +473,7 @@
struct bwstring *
bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb)
{
- wchar_t eols;
+ wint_t eols;
eols = zero_ended ? btowc('\0') : btowc('\n');
@@ -494,7 +488,7 @@
return (NULL);
}
if (*len > 0) {
- if (ret[*len - 1] == eols)
+ if (ret[*len - 1] == (wchar_t)eols)
--(*len);
}
return (bwssbdup(ret, *len));
@@ -513,11 +507,9 @@
if (ret[*len - 1] == '\n')
--(*len);
}
- return (bwscsbdup(ret, *len));
+ return (bwscsbdup((unsigned char*)ret, *len));
} else {
- wchar_t c = 0;
-
*len = 0;
if (feof(f))
@@ -532,6 +524,8 @@
if (MB_CUR_MAX == 1)
while (!feof(f)) {
+ int c;
+
c = fgetc(f);
if (c == EOF) {
@@ -553,6 +547,8 @@
}
else
while (!feof(f)) {
+ wint_t c = 0;
+
c = fgetwc(f);
if (c == WEOF) {
@@ -750,7 +746,7 @@
} else if (s2[i] == 0)
return (+1);
- res = strcoll(s1 + i, s2 + i);
+ res = strcoll((const char*)(s1 + i), (const char*)(s2 + i));
if (res)
return (res);
@@ -872,7 +868,7 @@
return (0);
}
- ret = strtod(s, &ep);
+ ret = strtod((char*)s, &ep);
if ((unsigned char*) ep == s) {
*empty = true;
return (0);
@@ -923,11 +919,11 @@
while (isblank(*s) && s < end)
++s;
- len = strlen(s);
+ len = strlen((const char*)s);
for (int i = 11; i >= 0; --i) {
if (cmonths[i] &&
- (s == (unsigned char*)strstr(s, cmonths[i])))
+ (s == (unsigned char*)strstr((const char*)s, (char*)(cmonths[i]))))
return (i);
}
Modified: trunk/usr.bin/sort/bwstring.h
===================================================================
--- trunk/usr.bin/sort/bwstring.h 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/bwstring.h 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,8 +1,9 @@
-/* $FreeBSD: src/usr.bin/sort/bwstring.h,v 1.1 2012/05/11 12:37:16 gabor Exp $ */
+/* $MidnightBSD$ */
+/* $FreeBSD: stable/10/usr.bin/sort/bwstring.h 265160 2014-04-30 20:39:08Z pfg $ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -28,7 +29,7 @@
*/
#if !defined(__BWSTRING_H__)
-#define __BWSTRING_H__
+#define __BWSTRING_H__
#include <stdbool.h>
#include <stdio.h>
@@ -41,7 +42,7 @@
extern bool byte_sort;
/* wchar_t is of 4 bytes: */
-#define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t))
+#define SIZEOF_WCHAR_STRING(LEN) ((LEN)*sizeof(wchar_t))
/*
* Binary "wide" string
@@ -64,7 +65,7 @@
typedef void *bwstring_iterator;
-#define BWSLEN(s) ((s)->len)
+#define BWSLEN(s) ((s)->len)
struct bwstring *bwsalloc(size_t sz);
@@ -93,7 +94,7 @@
int bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
int bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len);
int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset);
-int bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended);
+size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended);
struct bwstring *bwsfgetln(FILE *file, size_t *len, bool zero_ended, struct reader_buffer *rb);
static inline bwstring_iterator
@@ -135,7 +136,7 @@
int
bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len);
-#define BWS_GET(bws, pos) ((MB_CUR_MAX == 1) ? ((bws)->data.cstr[(pos)]) : (bws)->data.wstr[(pos)])
+#define BWS_GET(bws, pos) ((MB_CUR_MAX == 1) ? ((bws)->data.cstr[(pos)]) : (bws)->data.wstr[(pos)])
void initialise_months(void);
Modified: trunk/usr.bin/sort/coll.c
===================================================================
--- trunk/usr.bin/sort/coll.c 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/coll.c 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,6 +1,7 @@
+/* $MidnightBSD$ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sort/coll.c,v 1.3 2012/06/08 19:21:49 gabor Exp $");
+__FBSDID("$FreeBSD: stable/10/usr.bin/sort/coll.c 251245 2013-06-02 09:43:48Z gabor $");
#include <sys/types.h>
@@ -47,11 +48,11 @@
struct key_specs *keys;
size_t keys_num = 0;
-wchar_t symbol_decimal_point = L'.';
+wint_t symbol_decimal_point = L'.';
/* there is no default thousands separator in collate rules: */
-wchar_t symbol_thousands_sep = 0;
-wchar_t symbol_negative_sign = L'-';
-wchar_t symbol_positive_sign = L'+';
+wint_t symbol_thousands_sep = 0;
+wint_t symbol_negative_sign = L'-';
+wint_t symbol_positive_sign = L'+';
static int wstrcoll(struct key_value *kv1, struct key_value *kv2, size_t offset);
static int gnumcoll(struct key_value*, struct key_value *, size_t offset);
@@ -260,7 +261,7 @@
while (cpos < BWSLEN(s)) {
bool isblank;
- isblank = iswblank(BWS_GET(s,cpos));
+ isblank = iswblank(BWS_GET(s, cpos));
if (isblank && !pb) {
--fields;
@@ -277,7 +278,7 @@
size_t cpos = 0;
while (cpos < BWSLEN(s)) {
- if (BWS_GET(s,cpos) == sort_opts_vals.field_sep) {
+ if (BWS_GET(s,cpos) == (wchar_t)sort_opts_vals.field_sep) {
--fields;
if (fields <= 1)
return (cpos + 1);
@@ -328,7 +329,7 @@
next_field_start = skip_fields_to_start(s, f2 + 1,
&empty_field);
if ((next_field_start > 0) && sort_opts_vals.tflag &&
- (sort_opts_vals.field_sep == BWS_GET(s,
+ ((wchar_t)sort_opts_vals.field_sep == BWS_GET(s,
next_field_start - 1)))
--next_field_start;
} else
@@ -699,7 +700,7 @@
* point is in sfrac, tail is the pointer to the remainder of the string.
*/
static int
-read_number(struct bwstring *s0, int *sign, wchar_t *smain, int *main_len, wchar_t *sfrac, int *frac_len, unsigned char *si)
+read_number(struct bwstring *s0, int *sign, wchar_t *smain, size_t *main_len, wchar_t *sfrac, size_t *frac_len, unsigned char *si)
{
bwstring_iterator s;
@@ -711,7 +712,7 @@
while (iswblank(bws_get_iter_value(s)))
s = bws_iterator_inc(s, 1);
- if (bws_get_iter_value(s) == symbol_negative_sign) {
+ if (bws_get_iter_value(s) == (wchar_t)symbol_negative_sign) {
*sign = -1;
s = bws_iterator_inc(s, 1);
}
@@ -727,7 +728,7 @@
s = bws_iterator_inc(s, 1);
*main_len += 1;
} else if (symbol_thousands_sep &&
- (bws_get_iter_value(s) == symbol_thousands_sep))
+ (bws_get_iter_value(s) == (wchar_t)symbol_thousands_sep))
s = bws_iterator_inc(s, 1);
else
break;
@@ -735,7 +736,7 @@
smain[*main_len] = 0;
- if (bws_get_iter_value(s) == symbol_decimal_point) {
+ if (bws_get_iter_value(s) == (wchar_t)symbol_decimal_point) {
s = bws_iterator_inc(s, 1);
while (iswdigit(bws_get_iter_value(s)) &&
*frac_len < MAX_NUM_SIZE) {
@@ -798,7 +799,8 @@
struct bwstring *s1, *s2;
wchar_t sfrac1[MAX_NUM_SIZE + 1], sfrac2[MAX_NUM_SIZE + 1];
wchar_t smain1[MAX_NUM_SIZE + 1], smain2[MAX_NUM_SIZE + 1];
- int cmp_res, frac1, frac2, main1, main2, sign1, sign2;
+ int cmp_res, sign1, sign2;
+ size_t frac1, frac2, main1, main2;
unsigned char SI1, SI2;
bool e1, e2, key1_read, key2_read;
Modified: trunk/usr.bin/sort/coll.h
===================================================================
--- trunk/usr.bin/sort/coll.h 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/coll.h 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,8 +1,9 @@
-/* $FreeBSD: src/usr.bin/sort/coll.h,v 1.1 2012/05/11 12:37:16 gabor Exp $ */
+/* $MidnightBSD$ */
+/* $FreeBSD: stable/10/usr.bin/sort/coll.h 265160 2014-04-30 20:39:08Z pfg $ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -28,7 +29,7 @@
*/
#if !defined(__COLL_H__)
-#define __COLL_H__
+#define __COLL_H__
#include "bwstring.h"
#include "sort.h"
@@ -133,12 +134,12 @@
extern size_t keys_num;
/*
- * Main localised symbols
+ * Main localised symbols. These must be wint_t as they may hold WEOF.
*/
-extern wchar_t symbol_decimal_point;
-extern wchar_t symbol_thousands_sep;
-extern wchar_t symbol_negative_sign;
-extern wchar_t symbol_positive_sign;
+extern wint_t symbol_decimal_point;
+extern wint_t symbol_thousands_sep;
+extern wint_t symbol_negative_sign;
+extern wint_t symbol_positive_sign;
/* funcs */
Modified: trunk/usr.bin/sort/file.c
===================================================================
--- trunk/usr.bin/sort/file.c 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/file.c 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,6 +1,7 @@
+/* $MidnightBSD$ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sort/file.c,v 1.5 2012/07/04 16:25:11 gabor Exp $");
+__FBSDID("$FreeBSD: stable/10/usr.bin/sort/file.c 281535 2015-04-14 18:57:50Z pfg $");
#include <sys/mman.h>
#include <sys/stat.h>
@@ -188,48 +189,12 @@
}
/*
- * Read zero-terminated line from a file
- */
-char *
-read_file0_line(struct file0_reader *f0r)
-{
- size_t pos = 0;
- int c;
-
- if ((f0r->f == NULL) || feof(f0r->f))
- return (NULL);
-
- if (f0r->current_line && f0r->current_sz > 0)
- f0r->current_line[0] = 0;
-
- while (!feof(f0r->f)) {
- c = fgetc(f0r->f);
- if (feof(f0r->f) || (c == -1))
- break;
- if ((pos + 1) >= f0r->current_sz) {
- size_t newsz = (f0r->current_sz + 2) * 2;
- f0r->current_line = sort_realloc(f0r->current_line,
- newsz);
- f0r->current_sz = newsz;
- }
- f0r->current_line[pos] = (char)c;
- if (c == 0)
- break;
- else
- f0r->current_line[pos + 1] = 0;
- ++pos;
- }
-
- return f0r->current_line;
-}
-
-/*
* Generate new temporary file name
*/
char *
new_tmp_file_name(void)
{
- static unsigned int tfcounter = 0;
+ static size_t tfcounter = 0;
static const char *fn = ".bsdsort.";
char *ret;
size_t sz;
@@ -237,7 +202,7 @@
sz = strlen(tmpdir) + 1 + strlen(fn) + 32 + 1;
ret = sort_malloc(sz);
- sprintf(ret, "%s/%s%d.%u", tmpdir, fn, (int) getpid(), tfcounter++);
+ sprintf(ret, "%s/%s%d.%lu", tmpdir, fn, (int) getpid(), (unsigned long)(tfcounter++));
tmp_file_atexit(ret);
return (ret);
}
@@ -300,7 +265,7 @@
if (fl) {
if (fl->fns) {
- int i;
+ size_t i;
for (i = 0; i < fl->count; i++) {
if (fl->fns[i]) {
@@ -345,7 +310,7 @@
size_t indx = l->count;
if ((l->list == NULL) || (indx >= l->size)) {
- int newsize = (l->size + 1) + 1024;
+ size_t newsize = (l->size + 1) + 1024;
l->list = sort_realloc(l->list,
sizeof(struct sort_list_item*) * newsize);
@@ -439,7 +404,8 @@
struct bwstring *s1, *s2, *s1disorder, *s2disorder;
struct file_reader *fr;
struct keys_array *ka1, *ka2;
- int res, pos, posdisorder;
+ int res;
+ size_t pos, posdisorder;
s1 = s2 = s1disorder = s2disorder = NULL;
ka1 = ka2 = NULL;
@@ -979,7 +945,7 @@
* Swap two array elements
*/
static void
-file_header_swap(struct file_header **fh, int i1, int i2)
+file_header_swap(struct file_header **fh, size_t i1, size_t i2)
{
struct file_header *tmp;
@@ -995,11 +961,11 @@
* "Raises" last element to its right place
*/
static void
-file_header_heap_swim(struct file_header **fh, int indx)
+file_header_heap_swim(struct file_header **fh, size_t indx)
{
if (indx > 0) {
- int parent_index;
+ size_t parent_index;
parent_index = (indx - 1) >> 1;
@@ -1015,16 +981,16 @@
* Sink the top element to its correct position
*/
static void
-file_header_heap_sink(struct file_header **fh, int indx, int size)
+file_header_heap_sink(struct file_header **fh, size_t indx, size_t size)
{
- int left_child_index;
- int right_child_index;
+ size_t left_child_index;
+ size_t right_child_index;
left_child_index = indx + indx + 1;
right_child_index = left_child_index + 1;
if (left_child_index < size) {
- int min_child_index;
+ size_t min_child_index;
min_child_index = left_child_index;
@@ -1045,7 +1011,7 @@
* Adds element to the "left" end
*/
static void
-file_header_list_rearrange_from_header(struct file_header **fh, int size)
+file_header_list_rearrange_from_header(struct file_header **fh, size_t size)
{
file_header_heap_sink(fh, 0, size);
@@ -1055,7 +1021,7 @@
* Adds element to the "right" end
*/
static void
-file_header_list_push(struct file_header *f, struct file_header **fh, int size)
+file_header_list_push(struct file_header *f, struct file_header **fh, size_t size)
{
fh[size++] = f;
@@ -1118,10 +1084,10 @@
* Merge array of "files headers"
*/
static void
-file_headers_merge(int fnum, struct file_header **fh, FILE *f_out)
+file_headers_merge(size_t fnum, struct file_header **fh, FILE *f_out)
{
struct last_printed lp;
- int i;
+ size_t i;
memset(&lp, 0, sizeof(lp));
@@ -1149,13 +1115,13 @@
* stdout.
*/
static void
-merge_files_array(int argc, char **argv, const char *fn_out)
+merge_files_array(size_t argc, char **argv, const char *fn_out)
{
if (argv && fn_out) {
struct file_header **fh;
FILE *f_out;
- int i;
+ size_t i;
f_out = openfile(fn_out, "w");
@@ -1189,12 +1155,12 @@
return (0);
else {
struct file_list new_fl;
- int indx = 0;
+ size_t indx = 0;
file_list_init(&new_fl, true);
while (indx < fl->count) {
char *fnew;
- int num;
+ size_t num;
num = fl->count - indx;
fnew = new_tmp_file_name();
@@ -1203,7 +1169,7 @@
num = max_open_files - 1;
merge_files_array(num, fl->fns + indx, fnew);
if (fl->tmp) {
- int i;
+ size_t i;
for (i = 0; i < num; i++)
unlink(fl->fns[indx + i]);
@@ -1379,7 +1345,7 @@
* Swap two array elements
*/
static void
-sub_list_swap(struct sort_list **sl, int i1, int i2)
+sub_list_swap(struct sort_list **sl, size_t i1, size_t i2)
{
struct sort_list *tmp;
@@ -1395,11 +1361,11 @@
* "Raises" last element to its right place
*/
static void
-sub_list_swim(struct sort_list **sl, int indx)
+sub_list_swim(struct sort_list **sl, size_t indx)
{
if (indx > 0) {
- int parent_index;
+ size_t parent_index;
parent_index = (indx - 1) >> 1;
@@ -1415,16 +1381,16 @@
* Sink the top element to its correct position
*/
static void
-sub_list_sink(struct sort_list **sl, int indx, int size)
+sub_list_sink(struct sort_list **sl, size_t indx, size_t size)
{
- int left_child_index;
- int right_child_index;
+ size_t left_child_index;
+ size_t right_child_index;
left_child_index = indx + indx + 1;
right_child_index = left_child_index + 1;
if (left_child_index < size) {
- int min_child_index;
+ size_t min_child_index;
min_child_index = left_child_index;
@@ -1445,7 +1411,7 @@
* Adds element to the "right" end
*/
static void
-sub_list_push(struct sort_list *s, struct sort_list **sl, int size)
+sub_list_push(struct sort_list *s, struct sort_list **sl, size_t size)
{
sl[size++] = s;
Modified: trunk/usr.bin/sort/file.h
===================================================================
--- trunk/usr.bin/sort/file.h 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/file.h 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,8 +1,9 @@
-/* $FreeBSD: src/usr.bin/sort/file.h,v 1.3 2012/07/04 16:25:11 gabor Exp $ */
+/* $MidnightBSD$ */
+/* $FreeBSD: stable/10/usr.bin/sort/file.h 281535 2015-04-14 18:57:50Z pfg $ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -28,7 +29,7 @@
*/
#if !defined(__SORT_FILE_H__)
-#define __SORT_FILE_H__
+#define __SORT_FILE_H__
#include "coll.h"
#include "sort.h"
@@ -39,8 +40,8 @@
#define SORT_HEAPSORT 3
#define SORT_RADIXSORT 4
-#define DEFAULT_SORT_ALGORITHM SORT_HEAPSORT
-#define DEFAULT_SORT_FUNC heapsort
+#define DEFAULT_SORT_ALGORITHM SORT_HEAPSORT
+#define DEFAULT_SORT_FUNC heapsort
/*
* List of data to be sorted.
@@ -65,21 +66,11 @@
struct file_list
{
char **fns;
- int count;
- int sz;
+ size_t count;
+ size_t sz;
bool tmp;
};
-/*
- * Structure for zero-separated file reading (for input files list)
- */
-struct file0_reader
-{
- char *current_line;
- FILE *f;
- size_t current_sz;
-};
-
/* memory */
/**/
@@ -110,8 +101,6 @@
struct bwstring *file_reader_readline(struct file_reader *fr);
void file_reader_free(struct file_reader *fr);
-char *read_file0_line(struct file0_reader *f0r);
-
void init_tmp_files(void);
void clear_tmp_files(void);
char *new_tmp_file_name(void);
Modified: trunk/usr.bin/sort/mem.c
===================================================================
--- trunk/usr.bin/sort/mem.c 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/mem.c 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,6 +1,7 @@
+/* $MidnightBSD$ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sort/mem.c,v 1.1 2012/05/11 12:37:16 gabor Exp $");
+__FBSDID("$FreeBSD: stable/10/usr.bin/sort/mem.c 251245 2013-06-02 09:43:48Z gabor $");
#include <err.h>
#include <stdint.h>
Modified: trunk/usr.bin/sort/mem.h
===================================================================
--- trunk/usr.bin/sort/mem.h 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/mem.h 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/* $FreeBSD: stable/10/usr.bin/sort/mem.h 265160 2014-04-30 20:39:08Z pfg $ */
/*-
Modified: trunk/usr.bin/sort/radixsort.c
===================================================================
--- trunk/usr.bin/sort/radixsort.c 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/radixsort.c 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,5 +1,6 @@
+/* $MidnightBSD$ */
/*-
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* Copyright (C) 2012 Gabor Kovesdan <gabor at FreeBSD.org>
* All rights reserved.
*
@@ -26,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sort/radixsort.c,v 1.4 2012/07/04 16:25:11 gabor Exp $");
+__FBSDID("$FreeBSD: stable/10/usr.bin/sort/radixsort.c 259853 2013-12-25 00:53:48Z dim $");
#include <errno.h>
#include <err.h>
@@ -171,6 +172,8 @@
return (sl);
}
+#if defined(SORT_THREADS)
+
/*
* Pop sort level from the stack (multi-threaded style)
*/
@@ -180,9 +183,7 @@
struct level_stack *saved_ls;
struct sort_level *sl;
-#if defined(SORT_THREADS)
pthread_mutex_lock(&g_ls_mutex);
-#endif
if (g_ls) {
sl = g_ls->sl;
@@ -193,9 +194,7 @@
saved_ls = NULL;
}
-#if defined(SORT_THREADS)
pthread_mutex_unlock(&g_ls_mutex);
-#endif
sort_free(saved_ls);
@@ -202,8 +201,10 @@
return (sl);
}
+#endif /* defined(SORT_THREADS) */
+
static void
-add_to_sublevel(struct sort_level *sl, struct sort_list_item *item, int indx)
+add_to_sublevel(struct sort_level *sl, struct sort_list_item *item, size_t indx)
{
struct sort_level *ssl;
Modified: trunk/usr.bin/sort/radixsort.h
===================================================================
--- trunk/usr.bin/sort/radixsort.h 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/radixsort.h 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,7 +1,8 @@
-/* $FreeBSD: src/usr.bin/sort/radixsort.h,v 1.1 2012/05/11 12:37:16 gabor Exp $ */
+/* $MidnightBSD$ */
+/* $FreeBSD: stable/10/usr.bin/sort/radixsort.h 265160 2014-04-30 20:39:08Z pfg $ */
/*-
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* Copyright (C) 2012 Gabor Kovesdan <gabor at FreeBSD.org>
* All rights reserved.
*
@@ -28,7 +29,7 @@
*/
#if !defined(__SORT_RADIX_H__)
-#define __SORT_RADIX_H__
+#define __SORT_RADIX_H__
#include "coll.h"
#include "sort.h"
Modified: trunk/usr.bin/sort/sort.1.in
===================================================================
--- trunk/usr.bin/sort/sort.1.in 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/sort.1.in 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,5 +1,6 @@
.\" $OpenBSD: sort.1,v 1.31 2007/08/21 21:22:37 millert Exp $
.\" $FreeBSD: stable/10/usr.bin/sort/sort.1.in 269498 2014-08-04 03:59:48Z gjb $
+.\" $MidnightBSD$
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
Modified: trunk/usr.bin/sort/sort.c
===================================================================
--- trunk/usr.bin/sort/sort.c 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/sort.c 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,6 +1,7 @@
+/* $MidnightBSD$ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/usr.bin/sort/sort.c,v 1.13 2012/07/04 16:25:11 gabor Exp $");
+__FBSDID("$FreeBSD: stable/10/usr.bin/sort/sort.c 281535 2015-04-14 18:57:50Z pfg $");
#include <sys/stat.h>
#include <sys/sysctl.h>
@@ -103,7 +104,7 @@
bool need_hint;
#if defined(SORT_THREADS)
-size_t ncpu = 1;
+unsigned int ncpu = 1;
size_t nthreads = 1;
#endif
@@ -117,7 +118,7 @@
/*
* Arguments from file (when file0-from option is used:
*/
-static int argc_from_file0 = -1;
+static size_t argc_from_file0 = (size_t)-1;
static char **argv_from_file0;
/*
@@ -146,7 +147,7 @@
#define NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS 6
static const char mutually_exclusive_flags[NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS] = { 'M', 'n', 'g', 'R', 'h', 'V' };
-struct option long_options[] = {
+static struct option long_options[] = {
{ "batch-size", required_argument, NULL, BS_OPT },
{ "buffer-size", required_argument, NULL, 'S' },
{ "check", optional_argument, NULL, 'c' },
@@ -229,34 +230,38 @@
static void
read_fns_from_file0(const char *fn)
{
- if (fn) {
- struct file0_reader f0r;
- FILE *f;
+ FILE *f;
+ char *line = NULL;
+ size_t linesize = 0;
+ ssize_t linelen;
- f = fopen(fn, "r");
- if (f == NULL)
- err(2, NULL);
+ if (fn == NULL)
+ return;
- memset(&f0r, 0, sizeof(f0r));
- f0r.f = f;
+ f = fopen(fn, "r");
+ if (f == NULL)
+ err(2, "%s", fn);
- while (!feof(f)) {
- char *line = read_file0_line(&f0r);
-
- if (line && *line) {
- ++argc_from_file0;
- if (argc_from_file0 < 1)
- argc_from_file0 = 1;
- argv_from_file0 = sort_realloc(argv_from_file0,
- argc_from_file0 * sizeof(char *));
- if (argv_from_file0 == NULL)
- err(2, NULL);
- argv_from_file0[argc_from_file0 - 1] =
- sort_strdup(line);
- }
+ while ((linelen = getdelim(&line, &linesize, '\0', f)) != -1) {
+ if (*line != '\0') {
+ if (argc_from_file0 == (size_t) - 1)
+ argc_from_file0 = 0;
+ ++argc_from_file0;
+ argv_from_file0 = sort_realloc(argv_from_file0,
+ argc_from_file0 * sizeof(char *));
+ if (argv_from_file0 == NULL)
+ err(2, NULL);
+ argv_from_file0[argc_from_file0 - 1] = line;
+ } else {
+ free(line);
}
- closefile(f, fn);
+ line = NULL;
+ linesize = 0;
}
+ if (ferror(f))
+ err(2, "%s: getdelim", fn);
+
+ closefile(f, fn);
}
/*
@@ -265,31 +270,27 @@
static void
set_hw_params(void)
{
-#if defined(SORT_THREADS)
- size_t ncpusz;
-#endif
- size_t pages, psize, psz, pszsz;
+ long pages, psize;
pages = psize = 0;
+
#if defined(SORT_THREADS)
ncpu = 1;
- ncpusz = sizeof(size_t);
#endif
- psz = pszsz = sizeof(size_t);
- if (sysctlbyname("vm.stats.vm.v_free_count", &pages, &psz,
- NULL, 0) < 0) {
- perror("vm.stats.vm.v_free_count");
- return;
+ pages = sysconf(_SC_PHYS_PAGES);
+ if (pages < 1) {
+ perror("sysconf pages");
+ psize = 1;
}
- if (sysctlbyname("vm.stats.vm.v_page_size", &psize, &pszsz,
- NULL, 0) < 0) {
- perror("vm.stats.vm.v_page_size");
- return;
+ psize = sysconf(_SC_PAGESIZE);
+ if (psize < 1) {
+ perror("sysconf psize");
+ psize = 4096;
}
#if defined(SORT_THREADS)
- if (sysctlbyname("hw.ncpu", &ncpu, &ncpusz,
- NULL, 0) < 0)
+ ncpu = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN);
+ if (ncpu < 1)
ncpu = 1;
else if(ncpu > 32)
ncpu = 32;
@@ -298,7 +299,10 @@
#endif
free_memory = (unsigned long long) pages * (unsigned long long) psize;
- available_free_memory = (free_memory * 9) / 10;
+ available_free_memory = free_memory / 2;
+
+ if (available_free_memory < 1024)
+ available_free_memory = 1024;
}
/*
@@ -435,8 +439,7 @@
100;
break;
default:
- fprintf(stderr, "%s: %s\n", strerror(EINVAL),
- optarg);
+ warnc(EINVAL, "%s", optarg);
membuf = available_free_memory;
}
}
@@ -659,7 +662,7 @@
errno = 0;
ks->f2 = (size_t) strtoul(f, NULL, 10);
if (errno != 0)
- errx(2, "%s: -k", strerror(errno));
+ err(2, "-k");
if (ks->f2 == 0) {
warn("%s",getstr(5));
goto end;
@@ -668,7 +671,7 @@
errno = 0;
ks->f1 = (size_t) strtoul(f, NULL, 10);
if (errno != 0)
- errx(2, "%s: -k", strerror(errno));
+ err(2, "-k");
if (ks->f1 == 0) {
warn("%s",getstr(5));
goto end;
@@ -686,12 +689,12 @@
errno = 0;
ks->c2 = (size_t) strtoul(c, NULL, 10);
if (errno != 0)
- errx(2, "%s: -k", strerror(errno));
+ err(2, "-k");
} else {
errno = 0;
ks->c1 = (size_t) strtoul(c, NULL, 10);
if (errno != 0)
- errx(2, "%s: -k", strerror(errno));
+ err(2, "-k");
if (ks->c1 == 0) {
warn("%s",getstr(6));
goto end;
@@ -899,7 +902,7 @@
}
}
}
- sprintf(sopt, "-k%d.%d", f1, c1);
+ sprintf(sopt, "-k%d.%d%s", f1, c1, sopts1);
argv[i] = sort_strdup(sopt);
}
}
@@ -988,21 +991,6 @@
set_tmpdir();
set_sort_opts();
-#if 0
- {
- static int counter = 0;
- char fn[128];
- sprintf(fn, "/var/tmp/debug.sort.%d", counter++);
- FILE* f = fopen(fn, "w");
- fprintf(f, ">>sort>>");
- for (int i = 0; i < argc; i++) {
- fprintf(f, "<%s>", argv[i]);
- }
- fprintf(f, "<<sort<<\n");
- fclose(f);
- }
-#endif
-
fix_obsolete_keys(&argc, argv);
while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL))
@@ -1042,8 +1030,7 @@
if (parse_k(optarg, &(keys[keys_num - 1]))
< 0) {
- errx(2, "%s: -k %s\n",
- strerror(EINVAL), optarg);
+ errc(2, EINVAL, "-k %s", optarg);
}
break;
@@ -1068,8 +1055,7 @@
case 't':
while (strlen(optarg) > 1) {
if (optarg[0] != '\\') {
- errx(2, "%s: %s\n",
- strerror(EINVAL), optarg);
+ errc(2, EINVAL, "%s", optarg);
}
optarg += 1;
if (*optarg == '0') {
@@ -1156,8 +1142,7 @@
errno = 0;
long mof = strtol(optarg, NULL, 10);
if (errno != 0)
- errx(2, "--batch-size: %s",
- strerror(errno));
+ err(2, "--batch-size");
if (mof >= 2)
max_open_files = (size_t) mof + 1;
}
@@ -1221,13 +1206,15 @@
ks->sm.func = get_sort_func(&(ks->sm));
}
- if (argc_from_file0 >= 0) {
+ if (argv_from_file0) {
argc = argc_from_file0;
argv = argv_from_file0;
}
if (debug_sort) {
+ printf("Memory to be used for sorting: %llu\n",available_free_memory);
#if defined(SORT_THREADS)
+ printf("Number of CPUs: %d\n",(int)ncpu);
nthreads = 1;
#endif
printf("Using collate rules of %s locale\n",
Modified: trunk/usr.bin/sort/sort.h
===================================================================
--- trunk/usr.bin/sort/sort.h 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/sort.h 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,8 +1,9 @@
-/* $FreeBSD: src/usr.bin/sort/sort.h,v 1.7 2012/06/08 19:21:49 gabor Exp $ */
+/* $MidnightBSD$ */
+/* $FreeBSD: stable/10/usr.bin/sort/sort.h 265160 2014-04-30 20:39:08Z pfg $ */
/*-
* Copyright (C) 2009 Gabor Kovesdan <gabor at FreeBSD.org>
- * Copyright (C) 2012 Oleg Moskalenko <oleg.moskalenko at citrix.com>
+ * Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -28,7 +29,7 @@
*/
#if !defined(__BSD_SORT_H__)
-#define __BSD_SORT_H__
+#define __BSD_SORT_H__
#include <errno.h>
#include <stdbool.h>
@@ -39,7 +40,7 @@
#include <sys/types.h>
#include <md5.h>
-#define VERSION "2.3-FreeBSD"
+#define VERSION "2.3-MidnightBSD"
#ifdef WITHOUT_NLS
#define getstr(n) nlsstr[n]
@@ -53,8 +54,8 @@
extern const char *nlsstr[];
#if defined(SORT_THREADS)
-#define MT_SORT_THRESHOLD (10000)
-extern size_t ncpu;
+#define MT_SORT_THRESHOLD (10000)
+extern unsigned int ncpu;
extern size_t nthreads;
#endif
@@ -77,7 +78,7 @@
*/
struct sort_opts
{
- wchar_t field_sep;
+ wint_t field_sep;
int sort_method;
bool cflag;
bool csilentflag;
Modified: trunk/usr.bin/sort/vsort.c
===================================================================
--- trunk/usr.bin/sort/vsort.c 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/vsort.c 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*-
* Copyright (C) 2012 Oleg Moskalenko <mom040267 at gmail.com>
* Copyright (C) 2012 Gabor Kovesdan <gabor at FreeBSD.org>
Modified: trunk/usr.bin/sort/vsort.h
===================================================================
--- trunk/usr.bin/sort/vsort.h 2015-07-26 17:05:25 UTC (rev 7155)
+++ trunk/usr.bin/sort/vsort.h 2015-07-26 17:10:42 UTC (rev 7156)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/* $FreeBSD: stable/10/usr.bin/sort/vsort.h 265160 2014-04-30 20:39:08Z pfg $ */
/*-
More information about the Midnightbsd-cvs
mailing list