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