xref: /openbsd-src/usr.bin/sort/file.h (revision 7f9ccaab7fc72de1006ac289d076f23fd0d2af73)
1*7f9ccaabSderaadt /*	$OpenBSD: file.h,v 1.4 2015/04/02 22:14:51 deraadt Exp $	*/
279428148Smillert 
379428148Smillert /*-
479428148Smillert  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
579428148Smillert  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
679428148Smillert  * All rights reserved.
779428148Smillert  *
879428148Smillert  * Redistribution and use in source and binary forms, with or without
979428148Smillert  * modification, are permitted provided that the following conditions
1079428148Smillert  * are met:
1179428148Smillert  * 1. Redistributions of source code must retain the above copyright
1279428148Smillert  *    notice, this list of conditions and the following disclaimer.
1379428148Smillert  * 2. Redistributions in binary form must reproduce the above copyright
1479428148Smillert  *    notice, this list of conditions and the following disclaimer in the
1579428148Smillert  *    documentation and/or other materials provided with the distribution.
1679428148Smillert  *
1779428148Smillert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1879428148Smillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1979428148Smillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2079428148Smillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2179428148Smillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2279428148Smillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2379428148Smillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2479428148Smillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2579428148Smillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2679428148Smillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2779428148Smillert  * SUCH DAMAGE.
2879428148Smillert  */
2979428148Smillert 
3079428148Smillert #if !defined(__SORT_FILE_H__)
3179428148Smillert #define	__SORT_FILE_H__
3279428148Smillert 
3379428148Smillert #include "coll.h"
3479428148Smillert #include "sort.h"
3579428148Smillert 
3679428148Smillert #define	SORT_DEFAULT	0
3779428148Smillert #define	SORT_QSORT	1
3879428148Smillert #define	SORT_MERGESORT	2
3979428148Smillert #define	SORT_HEAPSORT	3
4079428148Smillert #define	SORT_RADIXSORT  4
4179428148Smillert 
4279428148Smillert #define	DEFAULT_SORT_ALGORITHM SORT_HEAPSORT
4379428148Smillert #define	DEFAULT_SORT_FUNC heapsort
4479428148Smillert 
4579428148Smillert /*
4679428148Smillert  * List of data to be sorted.
4779428148Smillert  */
4879428148Smillert struct sort_list {
4979428148Smillert 	struct sort_list_item	**list;
5079428148Smillert 	unsigned long long	 memsize;
5179428148Smillert 	size_t			 count;
5279428148Smillert 	size_t			 size;
5379428148Smillert 	size_t			 sub_list_pos;
5479428148Smillert };
5579428148Smillert 
5679428148Smillert /*
5779428148Smillert  * File reader object
5879428148Smillert  */
5979428148Smillert struct file_reader;
6079428148Smillert 
6179428148Smillert /*
6279428148Smillert  * List of files to be sorted
6379428148Smillert  */
6479428148Smillert struct file_list {
6579428148Smillert 	char			**fns;
6679428148Smillert 	size_t			 count;
6779428148Smillert 	size_t			 sz;
6879428148Smillert 	bool			 tmp;
6979428148Smillert };
7079428148Smillert 
7179428148Smillert /* memory */
7279428148Smillert 
7379428148Smillert /**/
7479428148Smillert 
7579428148Smillert extern unsigned long long available_free_memory;
7679428148Smillert 
7779428148Smillert /* Are we using mmap ? */
7879428148Smillert extern bool use_mmap;
7979428148Smillert 
8079428148Smillert /* temporary file dir */
8179428148Smillert 
8279428148Smillert extern const char *tmpdir;
8379428148Smillert 
8479428148Smillert /*
8579428148Smillert  * Max number of simultaneously open files (including the output file).
8679428148Smillert  */
8779428148Smillert extern size_t max_open_files;
8879428148Smillert 
8979428148Smillert /*
9079428148Smillert  * Compress program
9179428148Smillert  */
9279428148Smillert extern const char* compress_program;
9379428148Smillert 
9479428148Smillert /* funcs */
9579428148Smillert 
9679428148Smillert struct file_reader *file_reader_init(const char *fsrc);
9779428148Smillert struct bwstring *file_reader_readline(struct file_reader *fr);
9879428148Smillert void file_reader_free(struct file_reader *fr);
9979428148Smillert 
10079428148Smillert void init_tmp_files(void);
10179428148Smillert void clear_tmp_files(void);
10279428148Smillert char *new_tmp_file_name(void);
10379428148Smillert void tmp_file_atexit(const char *tmp_file);
10479428148Smillert 
10579428148Smillert void file_list_init(struct file_list *fl, bool tmp);
10679428148Smillert void file_list_add(struct file_list *fl, char *fn, bool allocate);
10779428148Smillert void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate);
10879428148Smillert void file_list_clean(struct file_list *fl);
10979428148Smillert 
11079428148Smillert int check(const char *);
11179428148Smillert void merge_files(struct file_list *fl, const char *fn_out);
11279428148Smillert FILE *openfile(const char *, const char *);
11379428148Smillert void closefile(FILE *, const char *);
11479428148Smillert int procfile(const char *fn, struct sort_list *list, struct file_list *fl);
11579428148Smillert 
11679428148Smillert void sort_list_init(struct sort_list *l);
11779428148Smillert void sort_list_add(struct sort_list *l, struct bwstring *str);
11879428148Smillert void sort_list_clean(struct sort_list *l);
11979428148Smillert void sort_list_dump(struct sort_list *l, const char *fn);
12079428148Smillert 
12179428148Smillert void sort_list_to_file(struct sort_list *list, const char *outfile);
12279428148Smillert 
12379428148Smillert #endif /* __SORT_FILE_H__ */
124