xref: /freebsd/usr.bin/sort/file.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
7  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if !defined(__SORT_FILE_H__)
33 #define	__SORT_FILE_H__
34 
35 #include "coll.h"
36 #include "sort.h"
37 
38 #define	SORT_DEFAULT	0
39 #define	SORT_QSORT	1
40 #define	SORT_MERGESORT	2
41 #define	SORT_HEAPSORT	3
42 #define	SORT_RADIXSORT  4
43 
44 #define	DEFAULT_SORT_ALGORITHM SORT_HEAPSORT
45 #define	DEFAULT_SORT_FUNC heapsort
46 
47 /*
48  * List of data to be sorted.
49  */
50 struct sort_list
51 {
52 	struct sort_list_item	**list;
53 	unsigned long long	 memsize;
54 	size_t			 count;
55 	size_t			 size;
56 	size_t			 sub_list_pos;
57 };
58 
59 /*
60  * File reader object
61  */
62 struct file_reader;
63 
64 /*
65  * List of files to be sorted
66  */
67 struct file_list
68 {
69 	const char *		*fns;
70 	size_t			 count;
71 	size_t			 sz;
72 	bool			 tmp;
73 };
74 
75 /* memory */
76 
77 /**/
78 
79 extern unsigned long long free_memory;
80 extern unsigned long long available_free_memory;
81 
82 /* Are we using mmap ? */
83 extern bool use_mmap;
84 
85 /* temporary file dir */
86 
87 extern const char *tmpdir;
88 
89 /*
90  * Max number of simultaneously open files (including the output file).
91  */
92 extern size_t max_open_files;
93 
94 /*
95  * Compress program
96  */
97 extern const char* compress_program;
98 
99 /* funcs */
100 
101 struct file_reader *file_reader_init(const char *fsrc);
102 struct bwstring *file_reader_readline(struct file_reader *fr);
103 void file_reader_free(struct file_reader *fr);
104 
105 void init_tmp_files(void);
106 void clear_tmp_files(void);
107 char *new_tmp_file_name(void);
108 void tmp_file_atexit(const char *tmp_file);
109 
110 void file_list_init(struct file_list *fl, bool tmp);
111 void file_list_add(struct file_list *fl, const char *fn, bool allocate);
112 void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
113 void file_list_clean(struct file_list *fl);
114 
115 int check(const char *);
116 void merge_files(struct file_list *fl, const char *fn_out);
117 FILE *openfile(const char *, const char *);
118 void closefile(FILE *, const char *);
119 int procfile(const char *fn, struct sort_list *list, struct file_list *fl);
120 
121 void sort_list_init(struct sort_list *l);
122 void sort_list_add(struct sort_list *l, struct bwstring *str);
123 void sort_list_clean(struct sort_list *l);
124 void sort_list_dump(struct sort_list *l, const char *fn);
125 
126 void sort_list_to_file(struct sort_list *list, const char *outfile);
127 
128 #endif /* __SORT_FILE_H__ */
129