svn commit: r335404 - head/usr.bin/sort

Kyle Evans kevans at FreeBSD.org
Wed Jun 20 03:31:21 UTC 2018


Author: kevans
Date: Wed Jun 20 03:31:19 2018
New Revision: 335404
URL: https://svnweb.freebsd.org/changeset/base/335404

Log:
  sort(1): Fix -m when only implicit stdin is used for input
  
  Observe:
  
  printf "a\nb\nc\n" > /tmp/foo
  # Next command results in no output
  cat /tmp/foo | sort -m
  # Next command results in proper output
  cat /tmp/foo | sort -m -
  # Also works:
  sort -m /tmp/foo
  
  Some const'ification was done to simplify the actual solution of adding "-"
  explicitly to the file list if we didn't have any file arguments left over.
  
  PR:		190099
  MFC after:	1 week

Modified:
  head/usr.bin/sort/file.c
  head/usr.bin/sort/file.h
  head/usr.bin/sort/sort.c

Modified: head/usr.bin/sort/file.c
==============================================================================
--- head/usr.bin/sort/file.c	Wed Jun 20 03:10:49 2018	(r335403)
+++ head/usr.bin/sort/file.c	Wed Jun 20 03:31:19 2018	(r335404)
@@ -227,7 +227,7 @@ file_list_init(struct file_list *fl, bool tmp)
  * Add a file name to the list
  */
 void
-file_list_add(struct file_list *fl, char *fn, bool allocate)
+file_list_add(struct file_list *fl, const char *fn, bool allocate)
 {
 
 	if (fl && fn) {
@@ -1115,7 +1115,7 @@ file_headers_merge(size_t fnum, struct file_header **f
  * stdout.
  */
 static void
-merge_files_array(size_t argc, char **argv, const char *fn_out)
+merge_files_array(size_t argc, const char **argv, const char *fn_out)
 {
 
 	if (argv && fn_out) {

Modified: head/usr.bin/sort/file.h
==============================================================================
--- head/usr.bin/sort/file.h	Wed Jun 20 03:10:49 2018	(r335403)
+++ head/usr.bin/sort/file.h	Wed Jun 20 03:31:19 2018	(r335404)
@@ -66,7 +66,7 @@ struct file_reader;
  */
 struct file_list
 {
-	char			**fns;
+	const char *		*fns;
 	size_t			 count;
 	size_t			 sz;
 	bool			 tmp;
@@ -108,7 +108,7 @@ char *new_tmp_file_name(void);
 void tmp_file_atexit(const char *tmp_file);
 
 void file_list_init(struct file_list *fl, bool tmp);
-void file_list_add(struct file_list *fl, char *fn, bool allocate);
+void file_list_add(struct file_list *fl, const char *fn, bool allocate);
 void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
 void file_list_clean(struct file_list *fl);
 

Modified: head/usr.bin/sort/sort.c
==============================================================================
--- head/usr.bin/sort/sort.c	Wed Jun 20 03:10:49 2018	(r335403)
+++ head/usr.bin/sort/sort.c	Wed Jun 20 03:31:19 2018	(r335404)
@@ -1299,7 +1299,11 @@ main(int argc, char **argv)
 		struct file_list fl;
 
 		file_list_init(&fl, false);
-		file_list_populate(&fl, argc, argv, true);
+		/* No file arguments remaining means "read from stdin." */
+		if (argc == 0)
+			file_list_add(&fl, "-", true);
+		else
+			file_list_populate(&fl, argc, argv, true);
 		merge_files(&fl, outfile);
 		file_list_clean(&fl);
 	}


More information about the svn-src-head mailing list