aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/sort
diff options
context:
space:
mode:
authorSimon J. Gerraty <sjg@FreeBSD.org>2015-05-27 01:19:58 +0000
committerSimon J. Gerraty <sjg@FreeBSD.org>2015-05-27 01:19:58 +0000
commit98e0ffaefb0f241cda3a72395d3be04192ae0d47 (patch)
tree55c065b6730aaac2afb6c29933ee6ec5fa4c4249 /usr.bin/sort
parentb17ff922d4072ae132ece458f5b5d74a236880ac (diff)
parente81032ad243db32b8fd615b2d55ee94b9f6a5b6a (diff)
downloadsrc-98e0ffaefb0f241cda3a72395d3be04192ae0d47.tar.gz
src-98e0ffaefb0f241cda3a72395d3be04192ae0d47.zip
Merge sync of head
Notes
Notes: svn path=/projects/bmake/; revision=283595
Diffstat (limited to 'usr.bin/sort')
-rw-r--r--usr.bin/sort/Makefile6
-rw-r--r--usr.bin/sort/bwstring.c27
-rw-r--r--usr.bin/sort/coll.c14
-rw-r--r--usr.bin/sort/file.c38
-rw-r--r--usr.bin/sort/file.h14
-rw-r--r--usr.bin/sort/mem.c1
-rw-r--r--usr.bin/sort/radixsort.c4
-rw-r--r--usr.bin/sort/sort.1.in22
-rw-r--r--usr.bin/sort/sort.c54
9 files changed, 63 insertions, 117 deletions
diff --git a/usr.bin/sort/Makefile b/usr.bin/sort/Makefile
index 532e9307caab..d097beb370a5 100644
--- a/usr.bin/sort/Makefile
+++ b/usr.bin/sort/Makefile
@@ -13,12 +13,10 @@ CLEANFILES+= sort.1
.if ${MK_SORT_THREADS} != "no"
CFLAGS+= -DSORT_THREADS
-LDADD= -lpthread -lmd
-DPADD= ${LIBPTHREAD} ${LIBMD}
+LIBADD= pthread md
MAN_SUB+= -e 's|%%THREADS%%||g'
.else
-LDADD= -lmd
-DPADD= ${LIBMD}
+LIBADD= md
MAN_SUB+= -e 's|%%THREADS%%|\.\\"|g'
.endif
diff --git a/usr.bin/sort/bwstring.c b/usr.bin/sort/bwstring.c
index 3def9d07dbdc..fc30b56ae885 100644
--- a/usr.bin/sort/bwstring.c
+++ b/usr.bin/sort/bwstring.c
@@ -65,18 +65,12 @@ initialise_months(void)
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);
- len = strlen((char*)tmp);
- if (len < 1)
+ if (*tmp == '\0')
continue;
- while (isblank(*tmp))
- ++tmp;
- m = sort_malloc(len + 1);
- memcpy(m, tmp, len + 1);
- m[len] = '\0';
+ m = sort_strdup(tmp);
+ len = strlen(tmp);
for (unsigned int j = 0; j < len; j++)
m[j] = toupper(m[j]);
cmonths[i] = m;
@@ -91,18 +85,17 @@ initialise_months(void)
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);
- len = strlen((char*)tmp);
- if (len < 1)
+ if (*tmp == '\0')
continue;
- while (isblank(*tmp))
- ++tmp;
+ len = strlen(tmp);
m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
- if (mbstowcs(m, (char*)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]);
@@ -234,7 +227,7 @@ bwsdup(const struct bwstring *s)
}
/*
- * Create a new binary string from a raw binary buffer.
+ * Create a new binary string from a wide character buffer.
*/
struct bwstring *
bwssbdup(const wchar_t *str, size_t len)
diff --git a/usr.bin/sort/coll.c b/usr.bin/sort/coll.c
index b3c80f64b3e8..0b7eb571b8f5 100644
--- a/usr.bin/sort/coll.c
+++ b/usr.bin/sort/coll.c
@@ -78,7 +78,7 @@ keys_array_alloc(void)
}
/*
- * Calculate whether we need key hint space
+ * Calculate whether we need key hint space
*/
static size_t
key_hint_size(void)
@@ -595,12 +595,12 @@ list_coll(struct sort_list_item **ss1, struct sort_list_item **ss2)
return (list_coll_offset(ss1, ss2, 0));
}
-#define LSCDEF(N) \
-static int \
-list_coll_##N(struct sort_list_item **ss1, struct sort_list_item **ss2) \
-{ \
- \
- return (list_coll_offset(ss1, ss2, N)); \
+#define LSCDEF(N) \
+static int \
+list_coll_##N(struct sort_list_item **ss1, struct sort_list_item **ss2) \
+{ \
+ \
+ return (list_coll_offset(ss1, ss2, N)); \
}
LSCDEF(1)
diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c
index d989008dcc29..0a0088947dee 100644
--- a/usr.bin/sort/file.c
+++ b/usr.bin/sort/file.c
@@ -188,42 +188,6 @@ file_is_tmp(const char* fn)
}
/*
- * 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 *
@@ -1127,7 +1091,7 @@ file_headers_merge(size_t fnum, struct file_header **fh, FILE *f_out)
memset(&lp, 0, sizeof(lp));
/*
- * construct the initial sort structure
+ * construct the initial sort structure
*/
for (i = 0; i < fnum; i++)
file_header_list_push(fh[i], fh, i);
diff --git a/usr.bin/sort/file.h b/usr.bin/sort/file.h
index 7037d87486f8..e87fafda1c85 100644
--- a/usr.bin/sort/file.h
+++ b/usr.bin/sort/file.h
@@ -55,7 +55,7 @@ struct sort_list
};
/*
- * File reader object
+ * File reader object
*/
struct file_reader;
@@ -70,16 +70,6 @@ struct file_list
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 +100,6 @@ struct file_reader *file_reader_init(const char *fsrc);
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);
diff --git a/usr.bin/sort/mem.c b/usr.bin/sort/mem.c
index 93cb019646f6..c6e9a53c3017 100644
--- a/usr.bin/sort/mem.c
+++ b/usr.bin/sort/mem.c
@@ -29,7 +29,6 @@
__FBSDID("$FreeBSD$");
#include <err.h>
-#include <stdint.h>
#include <stdlib.h>
#include <string.h>
diff --git a/usr.bin/sort/radixsort.c b/usr.bin/sort/radixsort.c
index f6cf6ac1a040..1fa260b7cc04 100644
--- a/usr.bin/sort/radixsort.c
+++ b/usr.bin/sort/radixsort.c
@@ -129,7 +129,7 @@ have_sort_left(void)
* Push sort level to the stack
*/
static inline void
-push_ls(struct sort_level* sl)
+push_ls(struct sort_level *sl)
{
struct level_stack *new_ls;
@@ -308,7 +308,7 @@ run_sort_level_next(struct sort_level *sl)
sl->sublevels = NULL;
}
- switch (sl->tosort_num){
+ switch (sl->tosort_num) {
case 0:
goto end;
case (1):
diff --git a/usr.bin/sort/sort.1.in b/usr.bin/sort/sort.1.in
index 1a836a893553..75593067ed0c 100644
--- a/usr.bin/sort/sort.1.in
+++ b/usr.bin/sort/sort.1.in
@@ -1,4 +1,4 @@
-.\" $OpenBSD: sort.1,v 1.31 2007/08/21 21:22:37 millert Exp $
+.\" $OpenBSD: sort.1,v 1.45 2015/03/19 13:51:10 jmc Exp $
.\" $FreeBSD$
.\"
.\" Copyright (c) 1991, 1993
@@ -33,7 +33,7 @@
.\"
.\" @(#)sort.1 8.1 (Berkeley) 6/6/93
.\"
-.Dd July 3, 2012
+.Dd March 19 2015
.Dt SORT 1
.Os
.Sh NAME
@@ -160,9 +160,10 @@ before comparison, that is, perform case-independent sorting.
Sort by general numerical value.
As opposed to
.Fl n ,
-this option handles general floating points, which have a much
-permissive format than those allowed by
-. Fl n ,
+this option handles general floating points.
+It has a more
+permissive format than that allowed by
+.Fl n
but it has a significant performance drawback.
.It Fl h, Fl Fl human-numeric-sort, Fl Fl sort=human-numeric
Sort by numerical value, but take into account the SI suffix,
@@ -247,9 +248,8 @@ can be attached independently to each
argument of the key specifications.
.Fl b .
.It Xo
-.Sm off
-.Fl k\ \& Ar field1 Op , Ar field2 , Fl Fl key Ns = Ns Ar field1 Op , Ar field2
-.Sm on
+.Fl k Ar field1 Ns Op , Ns Ar field2 ,
+.Fl Fl key Ns = Ns Ar field1 Ns Op , Ns Ar field2
.Xc
Define a restricted sort key that has the starting position
.Ar field1 ,
@@ -334,7 +334,7 @@ standard output.
%%THREADS%%Default number equals to the number of CPUs.
.It Fl Fl files0-from Ns = Ns Ar filename
Take the input file list from the file
-.Ar filename.
+.Ar filename .
The file names must be separated by NUL
(like the output produced by the command "find ... -print0").
.It Fl Fl radixsort
@@ -610,9 +610,9 @@ A
command first appeared in
.At v3 .
.Sh AUTHORS
-Gabor Kovesdan <gabor@FreeBSD.org>,
+.An Gabor Kovesdan Aq Mt gabor@FreeBSD.org ,
.Pp
-Oleg Moskalenko <mom040267@gmail.com>
+.An Oleg Moskalenko Aq Mt mom040267@gmail.com
.Sh NOTES
This implementation of
.Nm
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index 372c661d40ee..43ae43ce4650 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -229,34 +229,38 @@ usage(bool opt_err)
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);
-
- memset(&f0r, 0, sizeof(f0r));
- f0r.f = f;
-
- while (!feof(f)) {
- char *line = read_file0_line(&f0r);
+ if (fn == NULL)
+ return;
- if (line && *line) {
- 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] =
- sort_strdup(line);
- }
+ f = fopen(fn, "r");
+ if (f == NULL)
+ err(2, "%s", fn);
+
+ 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);
}
/*
@@ -523,7 +527,7 @@ check_mutually_exclusive_flags(char c, bool *mef_flags)
int fo_index, mec;
bool found_others, found_this;
- found_others = found_this =false;
+ found_others = found_this = false;
fo_index = 0;
for (int i = 0; i < NUMBER_OF_MUTUALLY_EXCLUSIVE_FLAGS; i++) {