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