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