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