file.h revision 330449
1/*	$FreeBSD: stable/11/usr.bin/sort/file.h 330449 2018-03-05 07:26:05Z eadler $	*/
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 */
50struct 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 */
62struct file_reader;
63
64/*
65 * List of files to be sorted
66 */
67struct file_list
68{
69	char			**fns;
70	size_t			 count;
71	size_t			 sz;
72	bool			 tmp;
73};
74
75/* memory */
76
77/**/
78
79extern unsigned long long free_memory;
80extern unsigned long long available_free_memory;
81
82/* Are we using mmap ? */
83extern bool use_mmap;
84
85/* temporary file dir */
86
87extern const char *tmpdir;
88
89/*
90 * Max number of simultaneously open files (including the output file).
91 */
92extern size_t max_open_files;
93
94/*
95 * Compress program
96 */
97extern const char* compress_program;
98
99/* funcs */
100
101struct file_reader *file_reader_init(const char *fsrc);
102struct bwstring *file_reader_readline(struct file_reader *fr);
103void file_reader_free(struct file_reader *fr);
104
105void init_tmp_files(void);
106void clear_tmp_files(void);
107char *new_tmp_file_name(void);
108void tmp_file_atexit(const char *tmp_file);
109
110void file_list_init(struct file_list *fl, bool tmp);
111void file_list_add(struct file_list *fl, char *fn, bool allocate);
112void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
113void file_list_clean(struct file_list *fl);
114
115int check(const char *);
116void merge_files(struct file_list *fl, const char *fn_out);
117FILE *openfile(const char *, const char *);
118void closefile(FILE *, const char *);
119int procfile(const char *fn, struct sort_list *list, struct file_list *fl);
120
121void sort_list_init(struct sort_list *l);
122void sort_list_add(struct sort_list *l, struct bwstring *str);
123void sort_list_clean(struct sort_list *l);
124void sort_list_dump(struct sort_list *l, const char *fn);
125
126void sort_list_to_file(struct sort_list *list, const char *outfile);
127
128#endif /* __SORT_FILE_H__ */
129