xref: /freebsd/usr.bin/sort/file.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1c66bbc91SGabor Kovesdan /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4c66bbc91SGabor Kovesdan  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5c859c6ddSGabor Kovesdan  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
6c66bbc91SGabor Kovesdan  * All rights reserved.
7c66bbc91SGabor Kovesdan  *
8c66bbc91SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
9c66bbc91SGabor Kovesdan  * modification, are permitted provided that the following conditions
10c66bbc91SGabor Kovesdan  * are met:
11c66bbc91SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
12c66bbc91SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
13c66bbc91SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
14c66bbc91SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
15c66bbc91SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
16c66bbc91SGabor Kovesdan  *
17c66bbc91SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18c66bbc91SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c66bbc91SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c66bbc91SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21c66bbc91SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c66bbc91SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c66bbc91SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c66bbc91SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c66bbc91SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c66bbc91SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c66bbc91SGabor Kovesdan  * SUCH DAMAGE.
28c66bbc91SGabor Kovesdan  */
29c66bbc91SGabor Kovesdan 
30c66bbc91SGabor Kovesdan #include <sys/cdefs.h>
31c66bbc91SGabor Kovesdan #include <sys/mman.h>
32c66bbc91SGabor Kovesdan #include <sys/stat.h>
33c66bbc91SGabor Kovesdan #include <sys/types.h>
34c66bbc91SGabor Kovesdan #include <sys/queue.h>
35c66bbc91SGabor Kovesdan 
36c66bbc91SGabor Kovesdan #include <err.h>
37c66bbc91SGabor Kovesdan #include <fcntl.h>
38c66bbc91SGabor Kovesdan #if defined(SORT_THREADS)
39c66bbc91SGabor Kovesdan #include <pthread.h>
40c66bbc91SGabor Kovesdan #endif
41c66bbc91SGabor Kovesdan #include <semaphore.h>
42c66bbc91SGabor Kovesdan #include <stdio.h>
43c66bbc91SGabor Kovesdan #include <stdlib.h>
44c66bbc91SGabor Kovesdan #include <string.h>
45c66bbc91SGabor Kovesdan #include <unistd.h>
46c66bbc91SGabor Kovesdan #include <wchar.h>
47c66bbc91SGabor Kovesdan #include <wctype.h>
48c66bbc91SGabor Kovesdan 
49c66bbc91SGabor Kovesdan #include "coll.h"
50c66bbc91SGabor Kovesdan #include "file.h"
51c66bbc91SGabor Kovesdan #include "radixsort.h"
52c66bbc91SGabor Kovesdan 
53c66bbc91SGabor Kovesdan unsigned long long free_memory = 1000000;
54c66bbc91SGabor Kovesdan unsigned long long available_free_memory = 1000000;
55c66bbc91SGabor Kovesdan 
565ca724dcSGabor Kovesdan bool use_mmap;
575ca724dcSGabor Kovesdan 
58c66bbc91SGabor Kovesdan const char *tmpdir = "/var/tmp";
59ce1e997fSGabor Kovesdan const char *compress_program;
60c66bbc91SGabor Kovesdan 
61c66bbc91SGabor Kovesdan size_t max_open_files = 16;
62c66bbc91SGabor Kovesdan 
63c66bbc91SGabor Kovesdan /*
64c66bbc91SGabor Kovesdan  * File reader structure
65c66bbc91SGabor Kovesdan  */
66c66bbc91SGabor Kovesdan struct file_reader
67c66bbc91SGabor Kovesdan {
68c66bbc91SGabor Kovesdan 	FILE			*file;
69c66bbc91SGabor Kovesdan 	char			*fname;
70b58094c0SBaptiste Daroussin 	char			*buffer;
71c66bbc91SGabor Kovesdan 	unsigned char		*mmapaddr;
72c66bbc91SGabor Kovesdan 	unsigned char		*mmapptr;
73c66bbc91SGabor Kovesdan 	size_t			 bsz;
74c66bbc91SGabor Kovesdan 	size_t			 mmapsize;
75c66bbc91SGabor Kovesdan 	int			 fd;
76c66bbc91SGabor Kovesdan 	char			 elsymb;
77c66bbc91SGabor Kovesdan };
78c66bbc91SGabor Kovesdan 
79c66bbc91SGabor Kovesdan /*
80c66bbc91SGabor Kovesdan  * Structure to be used in file merge process.
81c66bbc91SGabor Kovesdan  */
82c66bbc91SGabor Kovesdan struct file_header
83c66bbc91SGabor Kovesdan {
84c66bbc91SGabor Kovesdan 	struct file_reader		*fr;
85c66bbc91SGabor Kovesdan 	struct sort_list_item		*si; /* current top line */
86c66bbc91SGabor Kovesdan 	size_t				 file_pos;
87c66bbc91SGabor Kovesdan };
88c66bbc91SGabor Kovesdan 
89c66bbc91SGabor Kovesdan /*
90c66bbc91SGabor Kovesdan  * List elements of "cleanable" files list.
91c66bbc91SGabor Kovesdan  */
92c66bbc91SGabor Kovesdan struct CLEANABLE_FILE
93c66bbc91SGabor Kovesdan {
94c66bbc91SGabor Kovesdan 	char				*fn;
95c66bbc91SGabor Kovesdan 	LIST_ENTRY(CLEANABLE_FILE)	 files;
96c66bbc91SGabor Kovesdan };
97c66bbc91SGabor Kovesdan 
98c66bbc91SGabor Kovesdan /*
99c66bbc91SGabor Kovesdan  * List header of "cleanable" files list.
100c66bbc91SGabor Kovesdan  */
101c66bbc91SGabor Kovesdan static LIST_HEAD(CLEANABLE_FILES,CLEANABLE_FILE) tmp_files;
102c66bbc91SGabor Kovesdan 
103c66bbc91SGabor Kovesdan /*
104c66bbc91SGabor Kovesdan  * Semaphore to protect the tmp file list.
105c66bbc91SGabor Kovesdan  * We use semaphore here because it is signal-safe, according to POSIX.
106c66bbc91SGabor Kovesdan  * And semaphore does not require pthread library.
107c66bbc91SGabor Kovesdan  */
108c66bbc91SGabor Kovesdan static sem_t tmp_files_sem;
109c66bbc91SGabor Kovesdan 
110c66bbc91SGabor Kovesdan static void mt_sort(struct sort_list *list,
111c66bbc91SGabor Kovesdan     int (*sort_func)(void *, size_t, size_t,
112c66bbc91SGabor Kovesdan     int (*)(const void *, const void *)), const char* fn);
113c66bbc91SGabor Kovesdan 
114c66bbc91SGabor Kovesdan /*
115c66bbc91SGabor Kovesdan  * Init tmp files list
116c66bbc91SGabor Kovesdan  */
117c66bbc91SGabor Kovesdan void
init_tmp_files(void)118c66bbc91SGabor Kovesdan init_tmp_files(void)
119c66bbc91SGabor Kovesdan {
120e5f71a07SPedro F. Giffuni 
121c66bbc91SGabor Kovesdan 	LIST_INIT(&tmp_files);
122c66bbc91SGabor Kovesdan 	sem_init(&tmp_files_sem, 0, 1);
123c66bbc91SGabor Kovesdan }
124c66bbc91SGabor Kovesdan 
125c66bbc91SGabor Kovesdan /*
126c66bbc91SGabor Kovesdan  * Save name of a tmp file for signal cleanup
127c66bbc91SGabor Kovesdan  */
128c66bbc91SGabor Kovesdan void
tmp_file_atexit(const char * tmp_file)129c66bbc91SGabor Kovesdan tmp_file_atexit(const char *tmp_file)
130c66bbc91SGabor Kovesdan {
131e5f71a07SPedro F. Giffuni 
132c66bbc91SGabor Kovesdan 	if (tmp_file) {
133c66bbc91SGabor Kovesdan 		sem_wait(&tmp_files_sem);
134c66bbc91SGabor Kovesdan 		struct CLEANABLE_FILE *item =
135c66bbc91SGabor Kovesdan 		    sort_malloc(sizeof(struct CLEANABLE_FILE));
136c66bbc91SGabor Kovesdan 		item->fn = sort_strdup(tmp_file);
137c66bbc91SGabor Kovesdan 		LIST_INSERT_HEAD(&tmp_files, item, files);
138c66bbc91SGabor Kovesdan 		sem_post(&tmp_files_sem);
139c66bbc91SGabor Kovesdan 	}
140c66bbc91SGabor Kovesdan }
141c66bbc91SGabor Kovesdan 
142c66bbc91SGabor Kovesdan /*
143c66bbc91SGabor Kovesdan  * Clear tmp files
144c66bbc91SGabor Kovesdan  */
145c66bbc91SGabor Kovesdan void
clear_tmp_files(void)146c66bbc91SGabor Kovesdan clear_tmp_files(void)
147c66bbc91SGabor Kovesdan {
148c66bbc91SGabor Kovesdan 	struct CLEANABLE_FILE *item;
149c66bbc91SGabor Kovesdan 
150c66bbc91SGabor Kovesdan 	sem_wait(&tmp_files_sem);
151c66bbc91SGabor Kovesdan 	LIST_FOREACH(item,&tmp_files,files) {
152c66bbc91SGabor Kovesdan 		if ((item) && (item->fn))
153c66bbc91SGabor Kovesdan 			unlink(item->fn);
154c66bbc91SGabor Kovesdan 	}
155c66bbc91SGabor Kovesdan 	sem_post(&tmp_files_sem);
156c66bbc91SGabor Kovesdan }
157c66bbc91SGabor Kovesdan 
158c66bbc91SGabor Kovesdan /*
159c66bbc91SGabor Kovesdan  * Check whether a file is a temporary file
160c66bbc91SGabor Kovesdan  */
161c66bbc91SGabor Kovesdan static bool
file_is_tmp(const char * fn)162c66bbc91SGabor Kovesdan file_is_tmp(const char* fn)
163c66bbc91SGabor Kovesdan {
164c66bbc91SGabor Kovesdan 	struct CLEANABLE_FILE *item;
165c66bbc91SGabor Kovesdan 	bool ret = false;
166c66bbc91SGabor Kovesdan 
167c66bbc91SGabor Kovesdan 	if (fn) {
168c66bbc91SGabor Kovesdan 		sem_wait(&tmp_files_sem);
169c66bbc91SGabor Kovesdan 		LIST_FOREACH(item,&tmp_files,files) {
170c66bbc91SGabor Kovesdan 			if ((item) && (item->fn))
171c66bbc91SGabor Kovesdan 				if (strcmp(item->fn, fn) == 0) {
172c66bbc91SGabor Kovesdan 					ret = true;
173c66bbc91SGabor Kovesdan 					break;
174c66bbc91SGabor Kovesdan 				}
175c66bbc91SGabor Kovesdan 		}
176c66bbc91SGabor Kovesdan 		sem_post(&tmp_files_sem);
177c66bbc91SGabor Kovesdan 	}
178c66bbc91SGabor Kovesdan 
179c66bbc91SGabor Kovesdan 	return (ret);
180c66bbc91SGabor Kovesdan }
181c66bbc91SGabor Kovesdan 
182c66bbc91SGabor Kovesdan /*
183c66bbc91SGabor Kovesdan  * Generate new temporary file name
184c66bbc91SGabor Kovesdan  */
185c66bbc91SGabor Kovesdan char *
new_tmp_file_name(void)186c66bbc91SGabor Kovesdan new_tmp_file_name(void)
187c66bbc91SGabor Kovesdan {
188c66bbc91SGabor Kovesdan 	char *ret;
1893f9e5e59SBaptiste Daroussin 	int fd;
190c66bbc91SGabor Kovesdan 
1913f9e5e59SBaptiste Daroussin 	if (asprintf(&ret, "%s/.bsdsort.XXXXXXXXXX", tmpdir) == -1)
1923f9e5e59SBaptiste Daroussin 		err(2, "asprintf()");
1933f9e5e59SBaptiste Daroussin 	if ((fd = mkstemp(ret)) == -1)
1943f9e5e59SBaptiste Daroussin 		err(2, "mkstemp()");
1953f9e5e59SBaptiste Daroussin 	close(fd);
196c66bbc91SGabor Kovesdan 
197c66bbc91SGabor Kovesdan 	tmp_file_atexit(ret);
198c66bbc91SGabor Kovesdan 	return (ret);
199c66bbc91SGabor Kovesdan }
200c66bbc91SGabor Kovesdan 
201c66bbc91SGabor Kovesdan /*
202c66bbc91SGabor Kovesdan  * Initialize file list
203c66bbc91SGabor Kovesdan  */
204c66bbc91SGabor Kovesdan void
file_list_init(struct file_list * fl,bool tmp)205c66bbc91SGabor Kovesdan file_list_init(struct file_list *fl, bool tmp)
206c66bbc91SGabor Kovesdan {
207e5f71a07SPedro F. Giffuni 
208c66bbc91SGabor Kovesdan 	if (fl) {
209f02c7837SBaptiste Daroussin 		memset(fl, 0, sizeof(*fl));
210c66bbc91SGabor Kovesdan 		fl->tmp = tmp;
211c66bbc91SGabor Kovesdan 	}
212c66bbc91SGabor Kovesdan }
213c66bbc91SGabor Kovesdan 
214c66bbc91SGabor Kovesdan /*
215c66bbc91SGabor Kovesdan  * Add a file name to the list
216c66bbc91SGabor Kovesdan  */
217c66bbc91SGabor Kovesdan void
file_list_add(struct file_list * fl,const char * fn,bool allocate)2187137597eSKyle Evans file_list_add(struct file_list *fl, const char *fn, bool allocate)
219c66bbc91SGabor Kovesdan {
220e5f71a07SPedro F. Giffuni 
221c66bbc91SGabor Kovesdan 	if (fl && fn) {
222c66bbc91SGabor Kovesdan 		if (fl->count >= fl->sz || (fl->fns == NULL)) {
223c66bbc91SGabor Kovesdan 			fl->sz = (fl->sz) * 2 + 1;
224c66bbc91SGabor Kovesdan 			fl->fns = sort_realloc(fl->fns, fl->sz *
225c66bbc91SGabor Kovesdan 			    sizeof(char *));
226c66bbc91SGabor Kovesdan 		}
227c66bbc91SGabor Kovesdan 		fl->fns[fl->count] = allocate ? sort_strdup(fn) : fn;
228c66bbc91SGabor Kovesdan 		fl->count += 1;
229c66bbc91SGabor Kovesdan 	}
230c66bbc91SGabor Kovesdan }
231c66bbc91SGabor Kovesdan 
232c66bbc91SGabor Kovesdan /*
233c66bbc91SGabor Kovesdan  * Populate file list from array of file names
234c66bbc91SGabor Kovesdan  */
235c66bbc91SGabor Kovesdan void
file_list_populate(struct file_list * fl,int argc,char ** argv,bool allocate)236c66bbc91SGabor Kovesdan file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate)
237c66bbc91SGabor Kovesdan {
238e5f71a07SPedro F. Giffuni 
239c66bbc91SGabor Kovesdan 	if (fl && argv) {
240c66bbc91SGabor Kovesdan 		int i;
241c66bbc91SGabor Kovesdan 
242c66bbc91SGabor Kovesdan 		for (i = 0; i < argc; i++)
243c66bbc91SGabor Kovesdan 			file_list_add(fl, argv[i], allocate);
244c66bbc91SGabor Kovesdan 	}
245c66bbc91SGabor Kovesdan }
246c66bbc91SGabor Kovesdan 
247c66bbc91SGabor Kovesdan /*
248c66bbc91SGabor Kovesdan  * Clean file list data and delete the files,
249c66bbc91SGabor Kovesdan  * if this is a list of temporary files
250c66bbc91SGabor Kovesdan  */
251c66bbc91SGabor Kovesdan void
file_list_clean(struct file_list * fl)252c66bbc91SGabor Kovesdan file_list_clean(struct file_list *fl)
253c66bbc91SGabor Kovesdan {
254e5f71a07SPedro F. Giffuni 
255c66bbc91SGabor Kovesdan 	if (fl) {
256c66bbc91SGabor Kovesdan 		if (fl->fns) {
257e8da8c74SGabor Kovesdan 			size_t i;
258c66bbc91SGabor Kovesdan 
259c66bbc91SGabor Kovesdan 			for (i = 0; i < fl->count; i++) {
260c66bbc91SGabor Kovesdan 				if (fl->fns[i]) {
261c66bbc91SGabor Kovesdan 					if (fl->tmp)
262c66bbc91SGabor Kovesdan 						unlink(fl->fns[i]);
263c66bbc91SGabor Kovesdan 					sort_free(fl->fns[i]);
264c66bbc91SGabor Kovesdan 					fl->fns[i] = 0;
265c66bbc91SGabor Kovesdan 				}
266c66bbc91SGabor Kovesdan 			}
267c66bbc91SGabor Kovesdan 			sort_free(fl->fns);
268c66bbc91SGabor Kovesdan 			fl->fns = NULL;
269c66bbc91SGabor Kovesdan 		}
270c66bbc91SGabor Kovesdan 		fl->sz = 0;
271c66bbc91SGabor Kovesdan 		fl->count = 0;
272c66bbc91SGabor Kovesdan 		fl->tmp = false;
273c66bbc91SGabor Kovesdan 	}
274c66bbc91SGabor Kovesdan }
275c66bbc91SGabor Kovesdan 
276c66bbc91SGabor Kovesdan /*
277c66bbc91SGabor Kovesdan  * Init sort list
278c66bbc91SGabor Kovesdan  */
279c66bbc91SGabor Kovesdan void
sort_list_init(struct sort_list * l)280c66bbc91SGabor Kovesdan sort_list_init(struct sort_list *l)
281c66bbc91SGabor Kovesdan {
282e5f71a07SPedro F. Giffuni 
283c66bbc91SGabor Kovesdan 	if (l) {
284f02c7837SBaptiste Daroussin 		memset(l, 0, sizeof(*l));
285c66bbc91SGabor Kovesdan 		l->memsize = sizeof(struct sort_list);
286c66bbc91SGabor Kovesdan 	}
287c66bbc91SGabor Kovesdan }
288c66bbc91SGabor Kovesdan 
289c66bbc91SGabor Kovesdan /*
290c66bbc91SGabor Kovesdan  * Add string to sort list
291c66bbc91SGabor Kovesdan  */
292c66bbc91SGabor Kovesdan void
sort_list_add(struct sort_list * l,struct bwstring * str)293c66bbc91SGabor Kovesdan sort_list_add(struct sort_list *l, struct bwstring *str)
294c66bbc91SGabor Kovesdan {
295e5f71a07SPedro F. Giffuni 
296c66bbc91SGabor Kovesdan 	if (l && str) {
297c66bbc91SGabor Kovesdan 		size_t indx = l->count;
298c66bbc91SGabor Kovesdan 
299c66bbc91SGabor Kovesdan 		if ((l->list == NULL) || (indx >= l->size)) {
300e8da8c74SGabor Kovesdan 			size_t newsize = (l->size + 1) + 1024;
301c66bbc91SGabor Kovesdan 
302c66bbc91SGabor Kovesdan 			l->list = sort_realloc(l->list,
303c66bbc91SGabor Kovesdan 			    sizeof(struct sort_list_item*) * newsize);
304c66bbc91SGabor Kovesdan 			l->memsize += (newsize - l->size) *
305c66bbc91SGabor Kovesdan 			    sizeof(struct sort_list_item*);
306c66bbc91SGabor Kovesdan 			l->size = newsize;
307c66bbc91SGabor Kovesdan 		}
308c66bbc91SGabor Kovesdan 		l->list[indx] = sort_list_item_alloc();
309c66bbc91SGabor Kovesdan 		sort_list_item_set(l->list[indx], str);
310c66bbc91SGabor Kovesdan 		l->memsize += sort_list_item_size(l->list[indx]);
311c66bbc91SGabor Kovesdan 		l->count += 1;
312c66bbc91SGabor Kovesdan 	}
313c66bbc91SGabor Kovesdan }
314c66bbc91SGabor Kovesdan 
315c66bbc91SGabor Kovesdan /*
316c66bbc91SGabor Kovesdan  * Clean sort list data
317c66bbc91SGabor Kovesdan  */
318c66bbc91SGabor Kovesdan void
sort_list_clean(struct sort_list * l)319c66bbc91SGabor Kovesdan sort_list_clean(struct sort_list *l)
320c66bbc91SGabor Kovesdan {
321e5f71a07SPedro F. Giffuni 
322c66bbc91SGabor Kovesdan 	if (l) {
323c66bbc91SGabor Kovesdan 		if (l->list) {
324c66bbc91SGabor Kovesdan 			size_t i;
325c66bbc91SGabor Kovesdan 
326c66bbc91SGabor Kovesdan 			for (i = 0; i < l->count; i++) {
327c66bbc91SGabor Kovesdan 				struct sort_list_item *item;
328c66bbc91SGabor Kovesdan 
329c66bbc91SGabor Kovesdan 				item = l->list[i];
330c66bbc91SGabor Kovesdan 
331c66bbc91SGabor Kovesdan 				if (item) {
332c66bbc91SGabor Kovesdan 					sort_list_item_clean(item);
333c66bbc91SGabor Kovesdan 					sort_free(item);
334c66bbc91SGabor Kovesdan 					l->list[i] = NULL;
335c66bbc91SGabor Kovesdan 				}
336c66bbc91SGabor Kovesdan 			}
337c66bbc91SGabor Kovesdan 			sort_free(l->list);
338c66bbc91SGabor Kovesdan 			l->list = NULL;
339c66bbc91SGabor Kovesdan 		}
340c66bbc91SGabor Kovesdan 		l->count = 0;
341c66bbc91SGabor Kovesdan 		l->size = 0;
342c66bbc91SGabor Kovesdan 		l->memsize = sizeof(struct sort_list);
343c66bbc91SGabor Kovesdan 	}
344c66bbc91SGabor Kovesdan }
345c66bbc91SGabor Kovesdan 
346c66bbc91SGabor Kovesdan /*
347c66bbc91SGabor Kovesdan  * Write sort list to file
348c66bbc91SGabor Kovesdan  */
349c66bbc91SGabor Kovesdan void
sort_list_dump(struct sort_list * l,const char * fn)350c66bbc91SGabor Kovesdan sort_list_dump(struct sort_list *l, const char *fn)
351c66bbc91SGabor Kovesdan {
352e5f71a07SPedro F. Giffuni 
353c66bbc91SGabor Kovesdan 	if (l && fn) {
354c66bbc91SGabor Kovesdan 		FILE *f;
355c66bbc91SGabor Kovesdan 
356c66bbc91SGabor Kovesdan 		f = openfile(fn, "w");
357c66bbc91SGabor Kovesdan 		if (f == NULL)
358c66bbc91SGabor Kovesdan 			err(2, NULL);
359c66bbc91SGabor Kovesdan 
360c66bbc91SGabor Kovesdan 		if (l->list) {
361c66bbc91SGabor Kovesdan 			size_t i;
3625ca724dcSGabor Kovesdan 			if (!(sort_opts_vals.uflag)) {
3635ca724dcSGabor Kovesdan 				for (i = 0; i < l->count; ++i)
3645ca724dcSGabor Kovesdan 					bwsfwrite(l->list[i]->str, f,
365c66bbc91SGabor Kovesdan 					    sort_opts_vals.zflag);
3665ca724dcSGabor Kovesdan 			} else {
3675ca724dcSGabor Kovesdan 				struct sort_list_item *last_printed_item = NULL;
3685ca724dcSGabor Kovesdan 				struct sort_list_item *item;
3695ca724dcSGabor Kovesdan 				for (i = 0; i < l->count; ++i) {
3705ca724dcSGabor Kovesdan 					item = l->list[i];
3715ca724dcSGabor Kovesdan 					if ((last_printed_item == NULL) ||
3725ca724dcSGabor Kovesdan 					    list_coll(&last_printed_item, &item)) {
3735ca724dcSGabor Kovesdan 						bwsfwrite(item->str, f, sort_opts_vals.zflag);
374c66bbc91SGabor Kovesdan 						last_printed_item = item;
375c66bbc91SGabor Kovesdan 					}
376c66bbc91SGabor Kovesdan 				}
377c66bbc91SGabor Kovesdan 			}
3785ca724dcSGabor Kovesdan 		}
379c66bbc91SGabor Kovesdan 
380c66bbc91SGabor Kovesdan 		closefile(f, fn);
381c66bbc91SGabor Kovesdan 	}
382c66bbc91SGabor Kovesdan }
383c66bbc91SGabor Kovesdan 
384c66bbc91SGabor Kovesdan /*
385c66bbc91SGabor Kovesdan  * Checks if the given file is sorted.  Stops at the first disorder,
386c66bbc91SGabor Kovesdan  * prints the disordered line and returns 1.
387c66bbc91SGabor Kovesdan  */
388c66bbc91SGabor Kovesdan int
check(const char * fn)389c66bbc91SGabor Kovesdan check(const char *fn)
390c66bbc91SGabor Kovesdan {
391c66bbc91SGabor Kovesdan 	struct bwstring *s1, *s2, *s1disorder, *s2disorder;
392c66bbc91SGabor Kovesdan 	struct file_reader *fr;
393c66bbc91SGabor Kovesdan 	struct keys_array *ka1, *ka2;
394e8da8c74SGabor Kovesdan 	int res;
395e8da8c74SGabor Kovesdan 	size_t pos, posdisorder;
396c66bbc91SGabor Kovesdan 
397c66bbc91SGabor Kovesdan 	s1 = s2 = s1disorder = s2disorder = NULL;
398c66bbc91SGabor Kovesdan 	ka1 = ka2 = NULL;
399c66bbc91SGabor Kovesdan 
400c66bbc91SGabor Kovesdan 	fr = file_reader_init(fn);
401c66bbc91SGabor Kovesdan 
402c66bbc91SGabor Kovesdan 	res = 0;
403c66bbc91SGabor Kovesdan 	pos = 1;
404c66bbc91SGabor Kovesdan 	posdisorder = 1;
405c66bbc91SGabor Kovesdan 
406c66bbc91SGabor Kovesdan 	if (fr == NULL) {
407c66bbc91SGabor Kovesdan 		err(2, NULL);
408c66bbc91SGabor Kovesdan 		goto end;
409c66bbc91SGabor Kovesdan 	}
410c66bbc91SGabor Kovesdan 
411c66bbc91SGabor Kovesdan 	s1 = file_reader_readline(fr);
412c66bbc91SGabor Kovesdan 	if (s1 == NULL)
413c66bbc91SGabor Kovesdan 		goto end;
414c66bbc91SGabor Kovesdan 
415c66bbc91SGabor Kovesdan 	ka1 = keys_array_alloc();
416c66bbc91SGabor Kovesdan 	preproc(s1, ka1);
417c66bbc91SGabor Kovesdan 
418c66bbc91SGabor Kovesdan 	s2 = file_reader_readline(fr);
419c66bbc91SGabor Kovesdan 	if (s2 == NULL)
420c66bbc91SGabor Kovesdan 		goto end;
421c66bbc91SGabor Kovesdan 
422c66bbc91SGabor Kovesdan 	ka2 = keys_array_alloc();
423c66bbc91SGabor Kovesdan 	preproc(s2, ka2);
424c66bbc91SGabor Kovesdan 
425c66bbc91SGabor Kovesdan 	for (;;) {
426c66bbc91SGabor Kovesdan 
427c66bbc91SGabor Kovesdan 		if (debug_sort) {
428c66bbc91SGabor Kovesdan 			bwsprintf(stdout, s2, "s1=<", ">");
429c66bbc91SGabor Kovesdan 			bwsprintf(stdout, s1, "s2=<", ">");
430c66bbc91SGabor Kovesdan 		}
431c66bbc91SGabor Kovesdan 		int cmp = key_coll(ka2, ka1, 0);
432c66bbc91SGabor Kovesdan 		if (debug_sort)
433c66bbc91SGabor Kovesdan 			printf("; cmp1=%d", cmp);
434c66bbc91SGabor Kovesdan 
435c66bbc91SGabor Kovesdan 		if (!cmp && sort_opts_vals.complex_sort &&
436c66bbc91SGabor Kovesdan 		    !(sort_opts_vals.uflag) && !(sort_opts_vals.sflag)) {
437c66bbc91SGabor Kovesdan 			cmp = top_level_str_coll(s2, s1);
438c66bbc91SGabor Kovesdan 			if (debug_sort)
439c66bbc91SGabor Kovesdan 				printf("; cmp2=%d", cmp);
440c66bbc91SGabor Kovesdan 		}
441c66bbc91SGabor Kovesdan 		if (debug_sort)
442c66bbc91SGabor Kovesdan 			printf("\n");
443c66bbc91SGabor Kovesdan 
444c66bbc91SGabor Kovesdan 		if ((sort_opts_vals.uflag && (cmp <= 0)) || (cmp < 0)) {
445c66bbc91SGabor Kovesdan 			if (!(sort_opts_vals.csilentflag)) {
446c66bbc91SGabor Kovesdan 				s2disorder = bwsdup(s2);
447c66bbc91SGabor Kovesdan 				posdisorder = pos;
448c66bbc91SGabor Kovesdan 				if (debug_sort)
449c66bbc91SGabor Kovesdan 					s1disorder = bwsdup(s1);
450c66bbc91SGabor Kovesdan 			}
451c66bbc91SGabor Kovesdan 			res = 1;
452c66bbc91SGabor Kovesdan 			goto end;
453c66bbc91SGabor Kovesdan 		}
454c66bbc91SGabor Kovesdan 
455c66bbc91SGabor Kovesdan 		pos++;
456c66bbc91SGabor Kovesdan 
457c66bbc91SGabor Kovesdan 		clean_keys_array(s1, ka1);
458c66bbc91SGabor Kovesdan 		sort_free(ka1);
459c66bbc91SGabor Kovesdan 		ka1 = ka2;
460c66bbc91SGabor Kovesdan 		ka2 = NULL;
461c66bbc91SGabor Kovesdan 
462c66bbc91SGabor Kovesdan 		bwsfree(s1);
463c66bbc91SGabor Kovesdan 		s1 = s2;
464c66bbc91SGabor Kovesdan 
465c66bbc91SGabor Kovesdan 		s2 = file_reader_readline(fr);
466c66bbc91SGabor Kovesdan 		if (s2 == NULL)
467c66bbc91SGabor Kovesdan 			goto end;
468c66bbc91SGabor Kovesdan 
469c66bbc91SGabor Kovesdan 		ka2 = keys_array_alloc();
470c66bbc91SGabor Kovesdan 		preproc(s2, ka2);
471c66bbc91SGabor Kovesdan 	}
472c66bbc91SGabor Kovesdan 
473c66bbc91SGabor Kovesdan end:
474c66bbc91SGabor Kovesdan 	if (ka1) {
475c66bbc91SGabor Kovesdan 		clean_keys_array(s1, ka1);
476c66bbc91SGabor Kovesdan 		sort_free(ka1);
477c66bbc91SGabor Kovesdan 	}
478c66bbc91SGabor Kovesdan 
479c66bbc91SGabor Kovesdan 	if (s1)
480c66bbc91SGabor Kovesdan 		bwsfree(s1);
481c66bbc91SGabor Kovesdan 
482c66bbc91SGabor Kovesdan 	if (ka2) {
483c66bbc91SGabor Kovesdan 		clean_keys_array(s2, ka2);
484c66bbc91SGabor Kovesdan 		sort_free(ka2);
485c66bbc91SGabor Kovesdan 	}
486c66bbc91SGabor Kovesdan 
487c66bbc91SGabor Kovesdan 	if (s2)
488c66bbc91SGabor Kovesdan 		bwsfree(s2);
489c66bbc91SGabor Kovesdan 
490c66bbc91SGabor Kovesdan 	if ((fn == NULL) || (*fn == 0) || (strcmp(fn, "-") == 0)) {
491c66bbc91SGabor Kovesdan 		for (;;) {
492c66bbc91SGabor Kovesdan 			s2 = file_reader_readline(fr);
493c66bbc91SGabor Kovesdan 			if (s2 == NULL)
494c66bbc91SGabor Kovesdan 				break;
495c66bbc91SGabor Kovesdan 			bwsfree(s2);
496c66bbc91SGabor Kovesdan 		}
497c66bbc91SGabor Kovesdan 	}
498c66bbc91SGabor Kovesdan 
499c66bbc91SGabor Kovesdan 	file_reader_free(fr);
500c66bbc91SGabor Kovesdan 
501c66bbc91SGabor Kovesdan 	if (s2disorder) {
502c66bbc91SGabor Kovesdan 		bws_disorder_warnx(s2disorder, fn, posdisorder);
503c66bbc91SGabor Kovesdan 		if (s1disorder) {
504c66bbc91SGabor Kovesdan 			bws_disorder_warnx(s1disorder, fn, posdisorder);
505c66bbc91SGabor Kovesdan 			if (s1disorder != s2disorder)
506c66bbc91SGabor Kovesdan 				bwsfree(s1disorder);
507c66bbc91SGabor Kovesdan 		}
508c66bbc91SGabor Kovesdan 		bwsfree(s2disorder);
509c66bbc91SGabor Kovesdan 		s1disorder = NULL;
510c66bbc91SGabor Kovesdan 		s2disorder = NULL;
511c66bbc91SGabor Kovesdan 	}
512c66bbc91SGabor Kovesdan 
513c66bbc91SGabor Kovesdan 	if (res)
514c66bbc91SGabor Kovesdan 		exit(res);
515c66bbc91SGabor Kovesdan 
516c66bbc91SGabor Kovesdan 	return (0);
517c66bbc91SGabor Kovesdan }
518c66bbc91SGabor Kovesdan 
519c66bbc91SGabor Kovesdan /*
520c66bbc91SGabor Kovesdan  * Opens a file.  If the given filename is "-", stdout will be
521c66bbc91SGabor Kovesdan  * opened.
522c66bbc91SGabor Kovesdan  */
523c66bbc91SGabor Kovesdan FILE *
openfile(const char * fn,const char * mode)524c66bbc91SGabor Kovesdan openfile(const char *fn, const char *mode)
525c66bbc91SGabor Kovesdan {
526c66bbc91SGabor Kovesdan 	FILE *file;
527c66bbc91SGabor Kovesdan 
528958b0d46SBaptiste Daroussin 	if (strcmp(fn, "-") == 0)
529c66bbc91SGabor Kovesdan 		return ((mode && mode[0] == 'r') ? stdin : stdout);
530958b0d46SBaptiste Daroussin 
531c66bbc91SGabor Kovesdan 	mode_t orig_file_mask = 0;
532c66bbc91SGabor Kovesdan 	int is_tmp = file_is_tmp(fn);
533c66bbc91SGabor Kovesdan 
534c66bbc91SGabor Kovesdan 	if (is_tmp && (mode[0] == 'w'))
535c66bbc91SGabor Kovesdan 		orig_file_mask = umask(S_IWGRP | S_IWOTH |
536c66bbc91SGabor Kovesdan 		    S_IRGRP | S_IROTH);
537c66bbc91SGabor Kovesdan 
538c66bbc91SGabor Kovesdan 	if (is_tmp && (compress_program != NULL)) {
53948a53cc4SBaptiste Daroussin 		int r;
540c66bbc91SGabor Kovesdan 		char *cmd;
541c66bbc91SGabor Kovesdan 
542c66bbc91SGabor Kovesdan 		fflush(stdout);
543c66bbc91SGabor Kovesdan 
544c66bbc91SGabor Kovesdan 		if (mode[0] == 'r')
54548a53cc4SBaptiste Daroussin 			r = asprintf(&cmd, "cat %s | %s -d",
546c66bbc91SGabor Kovesdan 			    fn, compress_program);
547c66bbc91SGabor Kovesdan 		else if (mode[0] == 'w')
54848a53cc4SBaptiste Daroussin 			r = asprintf(&cmd, "%s > %s",
549c66bbc91SGabor Kovesdan 			    compress_program, fn);
550c66bbc91SGabor Kovesdan 		else
5518818aa39SGabor Kovesdan 			err(2, "%s", getstr(7));
552c66bbc91SGabor Kovesdan 
55348a53cc4SBaptiste Daroussin 		if (r == -1)
55448a53cc4SBaptiste Daroussin 			err(2, "aspritnf()");
55548a53cc4SBaptiste Daroussin 
556c66bbc91SGabor Kovesdan 		if ((file = popen(cmd, mode)) == NULL)
557c66bbc91SGabor Kovesdan 			err(2, NULL);
55848a53cc4SBaptiste Daroussin 		free(cmd);
559c66bbc91SGabor Kovesdan 	} else
560c66bbc91SGabor Kovesdan 		if ((file = fopen(fn, mode)) == NULL)
561c66bbc91SGabor Kovesdan 			err(2, NULL);
562c66bbc91SGabor Kovesdan 
563c66bbc91SGabor Kovesdan 	if (is_tmp && (mode[0] == 'w'))
564c66bbc91SGabor Kovesdan 		umask(orig_file_mask);
565c66bbc91SGabor Kovesdan 
566c66bbc91SGabor Kovesdan 	return (file);
567c66bbc91SGabor Kovesdan }
568c66bbc91SGabor Kovesdan 
569c66bbc91SGabor Kovesdan /*
570c66bbc91SGabor Kovesdan  * Close file
571c66bbc91SGabor Kovesdan  */
572c66bbc91SGabor Kovesdan void
closefile(FILE * f,const char * fn)573c66bbc91SGabor Kovesdan closefile(FILE *f, const char *fn)
574c66bbc91SGabor Kovesdan {
575ffd41d39SBaptiste Daroussin 	if (f == NULL || f == stdin)
576f9d9a7ccSBaptiste Daroussin 		return;
577f9d9a7ccSBaptiste Daroussin 	if (f == stdout) {
578c66bbc91SGabor Kovesdan 		fflush(f);
579f9d9a7ccSBaptiste Daroussin 		return;
580f9d9a7ccSBaptiste Daroussin 	}
581c66bbc91SGabor Kovesdan 	if (file_is_tmp(fn) && compress_program != NULL) {
582c66bbc91SGabor Kovesdan 		if(pclose(f)<0)
583c66bbc91SGabor Kovesdan 			err(2,NULL);
584c66bbc91SGabor Kovesdan 	} else
585c66bbc91SGabor Kovesdan 		fclose(f);
586c66bbc91SGabor Kovesdan }
587c66bbc91SGabor Kovesdan 
588c66bbc91SGabor Kovesdan /*
589c66bbc91SGabor Kovesdan  * Reads a file into the internal buffer.
590c66bbc91SGabor Kovesdan  */
591c66bbc91SGabor Kovesdan struct file_reader *
file_reader_init(const char * fsrc)592c66bbc91SGabor Kovesdan file_reader_init(const char *fsrc)
593c66bbc91SGabor Kovesdan {
594c66bbc91SGabor Kovesdan 	struct file_reader *ret;
595c66bbc91SGabor Kovesdan 
596c66bbc91SGabor Kovesdan 	if (fsrc == NULL)
597c66bbc91SGabor Kovesdan 		fsrc = "-";
598c66bbc91SGabor Kovesdan 
599ecc3c291SBaptiste Daroussin 	ret = sort_calloc(1, sizeof(struct file_reader));
600c66bbc91SGabor Kovesdan 
601f079ef8aSBaptiste Daroussin 	ret->elsymb = sort_opts_vals.zflag ? '\0' : '\n';
602c66bbc91SGabor Kovesdan 	ret->fname = sort_strdup(fsrc);
603c66bbc91SGabor Kovesdan 
6045ca724dcSGabor Kovesdan 	if (strcmp(fsrc, "-") && (compress_program == NULL) && use_mmap) {
605c66bbc91SGabor Kovesdan 
606c66bbc91SGabor Kovesdan 		do {
607c66bbc91SGabor Kovesdan 			struct stat stat_buf;
608c66bbc91SGabor Kovesdan 			void *addr;
609c66bbc91SGabor Kovesdan 			size_t sz = 0;
610c66bbc91SGabor Kovesdan 			int fd, flags;
611c66bbc91SGabor Kovesdan 
612c66bbc91SGabor Kovesdan 			flags = MAP_NOCORE | MAP_NOSYNC;
613c66bbc91SGabor Kovesdan 
614c66bbc91SGabor Kovesdan 			fd = open(fsrc, O_RDONLY);
615c66bbc91SGabor Kovesdan 			if (fd < 0)
616c66bbc91SGabor Kovesdan 				err(2, NULL);
617c66bbc91SGabor Kovesdan 
618c66bbc91SGabor Kovesdan 			if (fstat(fd, &stat_buf) < 0) {
619c66bbc91SGabor Kovesdan 				close(fd);
620c66bbc91SGabor Kovesdan 				break;
621c66bbc91SGabor Kovesdan 			}
622c66bbc91SGabor Kovesdan 
623c66bbc91SGabor Kovesdan 			sz = stat_buf.st_size;
624c66bbc91SGabor Kovesdan 
625c66bbc91SGabor Kovesdan #if defined(MAP_PREFAULT_READ)
626c66bbc91SGabor Kovesdan 			flags |= MAP_PREFAULT_READ;
627c66bbc91SGabor Kovesdan #endif
628c66bbc91SGabor Kovesdan 
629c66bbc91SGabor Kovesdan 			addr = mmap(NULL, sz, PROT_READ, flags, fd, 0);
630c66bbc91SGabor Kovesdan 			if (addr == MAP_FAILED) {
631c66bbc91SGabor Kovesdan 				close(fd);
632c66bbc91SGabor Kovesdan 				break;
633c66bbc91SGabor Kovesdan 			}
634c66bbc91SGabor Kovesdan 
635c66bbc91SGabor Kovesdan 			ret->fd = fd;
636c66bbc91SGabor Kovesdan 			ret->mmapaddr = addr;
637c66bbc91SGabor Kovesdan 			ret->mmapsize = sz;
638c66bbc91SGabor Kovesdan 			ret->mmapptr = ret->mmapaddr;
639c66bbc91SGabor Kovesdan 
640c66bbc91SGabor Kovesdan 		} while (0);
641c66bbc91SGabor Kovesdan 	}
642c66bbc91SGabor Kovesdan 
643c66bbc91SGabor Kovesdan 	if (ret->mmapaddr == NULL) {
644c66bbc91SGabor Kovesdan 		ret->file = openfile(fsrc, "r");
645c66bbc91SGabor Kovesdan 		if (ret->file == NULL)
646c66bbc91SGabor Kovesdan 			err(2, NULL);
647c66bbc91SGabor Kovesdan 	}
648c66bbc91SGabor Kovesdan 
649c66bbc91SGabor Kovesdan 	return (ret);
650c66bbc91SGabor Kovesdan }
651c66bbc91SGabor Kovesdan 
652c66bbc91SGabor Kovesdan struct bwstring *
file_reader_readline(struct file_reader * fr)653c66bbc91SGabor Kovesdan file_reader_readline(struct file_reader *fr)
654c66bbc91SGabor Kovesdan {
655c66bbc91SGabor Kovesdan 	struct bwstring *ret = NULL;
656c66bbc91SGabor Kovesdan 
657c66bbc91SGabor Kovesdan 	if (fr->mmapaddr) {
658c66bbc91SGabor Kovesdan 		unsigned char *mmapend;
659c66bbc91SGabor Kovesdan 
660c66bbc91SGabor Kovesdan 		mmapend = fr->mmapaddr + fr->mmapsize;
661c66bbc91SGabor Kovesdan 		if (fr->mmapptr >= mmapend)
662c66bbc91SGabor Kovesdan 			return (NULL);
663c66bbc91SGabor Kovesdan 		else {
664c66bbc91SGabor Kovesdan 			unsigned char *strend;
665c66bbc91SGabor Kovesdan 			size_t sz;
666c66bbc91SGabor Kovesdan 
667c66bbc91SGabor Kovesdan 			sz = mmapend - fr->mmapptr;
668c66bbc91SGabor Kovesdan 			strend = memchr(fr->mmapptr, fr->elsymb, sz);
669c66bbc91SGabor Kovesdan 
670c66bbc91SGabor Kovesdan 			if (strend == NULL) {
671c66bbc91SGabor Kovesdan 				ret = bwscsbdup(fr->mmapptr, sz);
672c66bbc91SGabor Kovesdan 				fr->mmapptr = mmapend;
673c66bbc91SGabor Kovesdan 			} else {
674c66bbc91SGabor Kovesdan 				ret = bwscsbdup(fr->mmapptr, strend -
675c66bbc91SGabor Kovesdan 				    fr->mmapptr);
676c66bbc91SGabor Kovesdan 				fr->mmapptr = strend + 1;
677c66bbc91SGabor Kovesdan 			}
678c66bbc91SGabor Kovesdan 		}
679c66bbc91SGabor Kovesdan 	} else {
680f079ef8aSBaptiste Daroussin 		ssize_t len;
681f079ef8aSBaptiste Daroussin 
682f079ef8aSBaptiste Daroussin 		len = getdelim(&fr->buffer, &fr->bsz, fr->elsymb, fr->file);
683b58094c0SBaptiste Daroussin 		if (len < 0) {
684b58094c0SBaptiste Daroussin 			if (!feof(fr->file))
685b58094c0SBaptiste Daroussin 				err(2, NULL);
686b58094c0SBaptiste Daroussin 			return (NULL);
687b58094c0SBaptiste Daroussin 		}
688f079ef8aSBaptiste Daroussin 		if (len > 0 && fr->buffer[len - 1] == fr->elsymb)
689b58094c0SBaptiste Daroussin 			len--;
690b58094c0SBaptiste Daroussin 		ret = bwscsbdup(fr->buffer, len);
691c66bbc91SGabor Kovesdan 	}
692c66bbc91SGabor Kovesdan 
693c66bbc91SGabor Kovesdan 	return (ret);
694c66bbc91SGabor Kovesdan }
695c66bbc91SGabor Kovesdan 
696c66bbc91SGabor Kovesdan static void
file_reader_clean(struct file_reader * fr)697c66bbc91SGabor Kovesdan file_reader_clean(struct file_reader *fr)
698c66bbc91SGabor Kovesdan {
699e5f71a07SPedro F. Giffuni 
700ffd41d39SBaptiste Daroussin 	if (fr == NULL)
701ffd41d39SBaptiste Daroussin 		return;
702ffd41d39SBaptiste Daroussin 
703c66bbc91SGabor Kovesdan 	if (fr->mmapaddr)
704c66bbc91SGabor Kovesdan 		munmap(fr->mmapaddr, fr->mmapsize);
705c66bbc91SGabor Kovesdan 	if (fr->fd)
706c66bbc91SGabor Kovesdan 		close(fr->fd);
707c66bbc91SGabor Kovesdan 
708ffd41d39SBaptiste Daroussin 	free(fr->buffer);
709c66bbc91SGabor Kovesdan 	closefile(fr->file, fr->fname);
710ffd41d39SBaptiste Daroussin 	free(fr->fname);
711c66bbc91SGabor Kovesdan 	memset(fr, 0, sizeof(struct file_reader));
712c66bbc91SGabor Kovesdan }
713c66bbc91SGabor Kovesdan 
714c66bbc91SGabor Kovesdan void
file_reader_free(struct file_reader * fr)715c66bbc91SGabor Kovesdan file_reader_free(struct file_reader *fr)
716c66bbc91SGabor Kovesdan {
717e5f71a07SPedro F. Giffuni 
718226e4146SBaptiste Daroussin 	if (fr == NULL)
719226e4146SBaptiste Daroussin 		return;
720c66bbc91SGabor Kovesdan 	file_reader_clean(fr);
721226e4146SBaptiste Daroussin 	free(fr);
722c66bbc91SGabor Kovesdan }
723c66bbc91SGabor Kovesdan 
724c66bbc91SGabor Kovesdan int
procfile(const char * fsrc,struct sort_list * list,struct file_list * fl)725c66bbc91SGabor Kovesdan procfile(const char *fsrc, struct sort_list *list, struct file_list *fl)
726c66bbc91SGabor Kovesdan {
727c66bbc91SGabor Kovesdan 	struct file_reader *fr;
728c66bbc91SGabor Kovesdan 
729c66bbc91SGabor Kovesdan 	fr = file_reader_init(fsrc);
730c66bbc91SGabor Kovesdan 	if (fr == NULL)
731c66bbc91SGabor Kovesdan 		err(2, NULL);
732c66bbc91SGabor Kovesdan 
733c66bbc91SGabor Kovesdan 	/* file browse cycle */
734c66bbc91SGabor Kovesdan 	for (;;) {
735c66bbc91SGabor Kovesdan 		struct bwstring *bws;
736c66bbc91SGabor Kovesdan 
737c66bbc91SGabor Kovesdan 		bws = file_reader_readline(fr);
738c66bbc91SGabor Kovesdan 
739c66bbc91SGabor Kovesdan 		if (bws == NULL)
740c66bbc91SGabor Kovesdan 			break;
741c66bbc91SGabor Kovesdan 
742c66bbc91SGabor Kovesdan 		sort_list_add(list, bws);
743c66bbc91SGabor Kovesdan 
744c66bbc91SGabor Kovesdan 		if (list->memsize >= available_free_memory) {
745c66bbc91SGabor Kovesdan 			char *fn;
746c66bbc91SGabor Kovesdan 
747c66bbc91SGabor Kovesdan 			fn = new_tmp_file_name();
748c66bbc91SGabor Kovesdan 			sort_list_to_file(list, fn);
749c66bbc91SGabor Kovesdan 			file_list_add(fl, fn, false);
750c66bbc91SGabor Kovesdan 			sort_list_clean(list);
751c66bbc91SGabor Kovesdan 		}
752c66bbc91SGabor Kovesdan 	}
753c66bbc91SGabor Kovesdan 
754c66bbc91SGabor Kovesdan 	file_reader_free(fr);
755c66bbc91SGabor Kovesdan 
756c66bbc91SGabor Kovesdan 	return (0);
757c66bbc91SGabor Kovesdan }
758c66bbc91SGabor Kovesdan 
759c66bbc91SGabor Kovesdan /*
760c66bbc91SGabor Kovesdan  * Compare file headers. Files with EOF always go to the end of the list.
761c66bbc91SGabor Kovesdan  */
762c66bbc91SGabor Kovesdan static int
file_header_cmp(struct file_header * f1,struct file_header * f2)763c66bbc91SGabor Kovesdan file_header_cmp(struct file_header *f1, struct file_header *f2)
764c66bbc91SGabor Kovesdan {
765e5f71a07SPedro F. Giffuni 
766c66bbc91SGabor Kovesdan 	if (f1 == f2)
767c66bbc91SGabor Kovesdan 		return (0);
768c66bbc91SGabor Kovesdan 	else {
769c66bbc91SGabor Kovesdan 		if (f1->fr == NULL) {
770c66bbc91SGabor Kovesdan 			return ((f2->fr == NULL) ? 0 : +1);
771c66bbc91SGabor Kovesdan 		} else if (f2->fr == NULL)
772c66bbc91SGabor Kovesdan 			return (-1);
773c66bbc91SGabor Kovesdan 		else {
774c66bbc91SGabor Kovesdan 			int ret;
775c66bbc91SGabor Kovesdan 
776c66bbc91SGabor Kovesdan 			ret = list_coll(&(f1->si), &(f2->si));
777c66bbc91SGabor Kovesdan 			if (!ret)
778c66bbc91SGabor Kovesdan 				return ((f1->file_pos < f2->file_pos) ? -1 : +1);
779c66bbc91SGabor Kovesdan 			return (ret);
780c66bbc91SGabor Kovesdan 		}
781c66bbc91SGabor Kovesdan 	}
782c66bbc91SGabor Kovesdan }
783c66bbc91SGabor Kovesdan 
784c66bbc91SGabor Kovesdan /*
785c66bbc91SGabor Kovesdan  * Allocate and init file header structure
786c66bbc91SGabor Kovesdan  */
787c66bbc91SGabor Kovesdan static void
file_header_init(struct file_header ** fh,const char * fn,size_t file_pos)788c66bbc91SGabor Kovesdan file_header_init(struct file_header **fh, const char *fn, size_t file_pos)
789c66bbc91SGabor Kovesdan {
790e5f71a07SPedro F. Giffuni 
791c66bbc91SGabor Kovesdan 	if (fh && fn) {
792c66bbc91SGabor Kovesdan 		struct bwstring *line;
793c66bbc91SGabor Kovesdan 
794c66bbc91SGabor Kovesdan 		*fh = sort_malloc(sizeof(struct file_header));
795c66bbc91SGabor Kovesdan 		(*fh)->file_pos = file_pos;
796c66bbc91SGabor Kovesdan 		(*fh)->fr = file_reader_init(fn);
797c66bbc91SGabor Kovesdan 		if ((*fh)->fr == NULL) {
798c66bbc91SGabor Kovesdan 			perror(fn);
7998818aa39SGabor Kovesdan 			err(2, "%s", getstr(8));
800c66bbc91SGabor Kovesdan 		}
801c66bbc91SGabor Kovesdan 		line = file_reader_readline((*fh)->fr);
802c66bbc91SGabor Kovesdan 		if (line == NULL) {
803c66bbc91SGabor Kovesdan 			file_reader_free((*fh)->fr);
804c66bbc91SGabor Kovesdan 			(*fh)->fr = NULL;
805c66bbc91SGabor Kovesdan 			(*fh)->si = NULL;
806c66bbc91SGabor Kovesdan 		} else {
807c66bbc91SGabor Kovesdan 			(*fh)->si = sort_list_item_alloc();
808c66bbc91SGabor Kovesdan 			sort_list_item_set((*fh)->si, line);
809c66bbc91SGabor Kovesdan 		}
810c66bbc91SGabor Kovesdan 	}
811c66bbc91SGabor Kovesdan }
812c66bbc91SGabor Kovesdan 
813c66bbc91SGabor Kovesdan /*
814c66bbc91SGabor Kovesdan  * Close file
815c66bbc91SGabor Kovesdan  */
816c66bbc91SGabor Kovesdan static void
file_header_close(struct file_header ** fh)817c66bbc91SGabor Kovesdan file_header_close(struct file_header **fh)
818c66bbc91SGabor Kovesdan {
819e5f71a07SPedro F. Giffuni 
820c66bbc91SGabor Kovesdan 	if (fh && *fh) {
821c66bbc91SGabor Kovesdan 		file_reader_free((*fh)->fr);
822c66bbc91SGabor Kovesdan 		(*fh)->fr = NULL;
823c66bbc91SGabor Kovesdan 		if ((*fh)->si) {
824c66bbc91SGabor Kovesdan 			sort_list_item_clean((*fh)->si);
825c66bbc91SGabor Kovesdan 			sort_free((*fh)->si);
826c66bbc91SGabor Kovesdan 			(*fh)->si = NULL;
827c66bbc91SGabor Kovesdan 		}
828c66bbc91SGabor Kovesdan 		sort_free(*fh);
829c66bbc91SGabor Kovesdan 		*fh = NULL;
830c66bbc91SGabor Kovesdan 	}
831c66bbc91SGabor Kovesdan }
832c66bbc91SGabor Kovesdan 
833c66bbc91SGabor Kovesdan /*
834c66bbc91SGabor Kovesdan  * Swap two array elements
835c66bbc91SGabor Kovesdan  */
836c66bbc91SGabor Kovesdan static void
file_header_swap(struct file_header ** fh,size_t i1,size_t i2)837e8da8c74SGabor Kovesdan file_header_swap(struct file_header **fh, size_t i1, size_t i2)
838c66bbc91SGabor Kovesdan {
839c66bbc91SGabor Kovesdan 	struct file_header *tmp;
840c66bbc91SGabor Kovesdan 
841c66bbc91SGabor Kovesdan 	tmp = fh[i1];
842c66bbc91SGabor Kovesdan 	fh[i1] = fh[i2];
843c66bbc91SGabor Kovesdan 	fh[i2] = tmp;
844c66bbc91SGabor Kovesdan }
845c66bbc91SGabor Kovesdan 
846c66bbc91SGabor Kovesdan /* heap algorithm ==>> */
847c66bbc91SGabor Kovesdan 
848c66bbc91SGabor Kovesdan /*
849c66bbc91SGabor Kovesdan  * See heap sort algorithm
850c66bbc91SGabor Kovesdan  * "Raises" last element to its right place
851c66bbc91SGabor Kovesdan  */
852c66bbc91SGabor Kovesdan static void
file_header_heap_swim(struct file_header ** fh,size_t indx)853e8da8c74SGabor Kovesdan file_header_heap_swim(struct file_header **fh, size_t indx)
854c66bbc91SGabor Kovesdan {
855e5f71a07SPedro F. Giffuni 
856c66bbc91SGabor Kovesdan 	if (indx > 0) {
857e8da8c74SGabor Kovesdan 		size_t parent_index;
858c66bbc91SGabor Kovesdan 
859c66bbc91SGabor Kovesdan 		parent_index = (indx - 1) >> 1;
860c66bbc91SGabor Kovesdan 
861c66bbc91SGabor Kovesdan 		if (file_header_cmp(fh[indx], fh[parent_index]) < 0) {
862c66bbc91SGabor Kovesdan 			/* swap child and parent and continue */
863c66bbc91SGabor Kovesdan 			file_header_swap(fh, indx, parent_index);
864c66bbc91SGabor Kovesdan 			file_header_heap_swim(fh, parent_index);
865c66bbc91SGabor Kovesdan 		}
866c66bbc91SGabor Kovesdan 	}
867c66bbc91SGabor Kovesdan }
868c66bbc91SGabor Kovesdan 
869c66bbc91SGabor Kovesdan /*
870c66bbc91SGabor Kovesdan  * Sink the top element to its correct position
871c66bbc91SGabor Kovesdan  */
872c66bbc91SGabor Kovesdan static void
file_header_heap_sink(struct file_header ** fh,size_t indx,size_t size)873e8da8c74SGabor Kovesdan file_header_heap_sink(struct file_header **fh, size_t indx, size_t size)
874c66bbc91SGabor Kovesdan {
875e8da8c74SGabor Kovesdan 	size_t left_child_index;
876e8da8c74SGabor Kovesdan 	size_t right_child_index;
877c66bbc91SGabor Kovesdan 
878c66bbc91SGabor Kovesdan 	left_child_index = indx + indx + 1;
879c66bbc91SGabor Kovesdan 	right_child_index = left_child_index + 1;
880c66bbc91SGabor Kovesdan 
881c66bbc91SGabor Kovesdan 	if (left_child_index < size) {
882e8da8c74SGabor Kovesdan 		size_t min_child_index;
883c66bbc91SGabor Kovesdan 
884c66bbc91SGabor Kovesdan 		min_child_index = left_child_index;
885c66bbc91SGabor Kovesdan 
886c66bbc91SGabor Kovesdan 		if ((right_child_index < size) &&
887c66bbc91SGabor Kovesdan 		    (file_header_cmp(fh[left_child_index],
888c66bbc91SGabor Kovesdan 		    fh[right_child_index]) > 0))
889c66bbc91SGabor Kovesdan 			min_child_index = right_child_index;
890c66bbc91SGabor Kovesdan 		if (file_header_cmp(fh[indx], fh[min_child_index]) > 0) {
891c66bbc91SGabor Kovesdan 			file_header_swap(fh, indx, min_child_index);
892c66bbc91SGabor Kovesdan 			file_header_heap_sink(fh, min_child_index, size);
893c66bbc91SGabor Kovesdan 		}
894c66bbc91SGabor Kovesdan 	}
895c66bbc91SGabor Kovesdan }
896c66bbc91SGabor Kovesdan 
897c66bbc91SGabor Kovesdan /* <<== heap algorithm */
898c66bbc91SGabor Kovesdan 
899c66bbc91SGabor Kovesdan /*
900c66bbc91SGabor Kovesdan  * Adds element to the "left" end
901c66bbc91SGabor Kovesdan  */
902c66bbc91SGabor Kovesdan static void
file_header_list_rearrange_from_header(struct file_header ** fh,size_t size)903e8da8c74SGabor Kovesdan file_header_list_rearrange_from_header(struct file_header **fh, size_t size)
904c66bbc91SGabor Kovesdan {
905e5f71a07SPedro F. Giffuni 
906c66bbc91SGabor Kovesdan 	file_header_heap_sink(fh, 0, size);
907c66bbc91SGabor Kovesdan }
908c66bbc91SGabor Kovesdan 
909c66bbc91SGabor Kovesdan /*
910c66bbc91SGabor Kovesdan  * Adds element to the "right" end
911c66bbc91SGabor Kovesdan  */
912c66bbc91SGabor Kovesdan static void
file_header_list_push(struct file_header * f,struct file_header ** fh,size_t size)913e8da8c74SGabor Kovesdan file_header_list_push(struct file_header *f, struct file_header **fh, size_t size)
914c66bbc91SGabor Kovesdan {
915e5f71a07SPedro F. Giffuni 
916c66bbc91SGabor Kovesdan 	fh[size++] = f;
917c66bbc91SGabor Kovesdan 	file_header_heap_swim(fh, size - 1);
918c66bbc91SGabor Kovesdan }
919c66bbc91SGabor Kovesdan 
920c66bbc91SGabor Kovesdan struct last_printed
921c66bbc91SGabor Kovesdan {
922c66bbc91SGabor Kovesdan 	struct bwstring *str;
923c66bbc91SGabor Kovesdan };
924c66bbc91SGabor Kovesdan 
925c66bbc91SGabor Kovesdan /*
926c66bbc91SGabor Kovesdan  * Prints the current line of the file
927c66bbc91SGabor Kovesdan  */
928c66bbc91SGabor Kovesdan static void
file_header_print(struct file_header * fh,FILE * f_out,struct last_printed * lp)929c66bbc91SGabor Kovesdan file_header_print(struct file_header *fh, FILE *f_out, struct last_printed *lp)
930c66bbc91SGabor Kovesdan {
931e5f71a07SPedro F. Giffuni 
932c66bbc91SGabor Kovesdan 	if (fh && fh->fr && f_out && fh->si && fh->si->str) {
933c66bbc91SGabor Kovesdan 		if (sort_opts_vals.uflag) {
934c66bbc91SGabor Kovesdan 			if ((lp->str == NULL) || (str_list_coll(lp->str, &(fh->si)))) {
935c66bbc91SGabor Kovesdan 				bwsfwrite(fh->si->str, f_out, sort_opts_vals.zflag);
936c66bbc91SGabor Kovesdan 				if (lp->str)
937c66bbc91SGabor Kovesdan 					bwsfree(lp->str);
938c66bbc91SGabor Kovesdan 				lp->str = bwsdup(fh->si->str);
939c66bbc91SGabor Kovesdan 			}
940c66bbc91SGabor Kovesdan 		} else
941c66bbc91SGabor Kovesdan 			bwsfwrite(fh->si->str, f_out, sort_opts_vals.zflag);
942c66bbc91SGabor Kovesdan 	}
943c66bbc91SGabor Kovesdan }
944c66bbc91SGabor Kovesdan 
945c66bbc91SGabor Kovesdan /*
946c66bbc91SGabor Kovesdan  * Read next line
947c66bbc91SGabor Kovesdan  */
948c66bbc91SGabor Kovesdan static void
file_header_read_next(struct file_header * fh)949c66bbc91SGabor Kovesdan file_header_read_next(struct file_header *fh)
950c66bbc91SGabor Kovesdan {
951e5f71a07SPedro F. Giffuni 
952c66bbc91SGabor Kovesdan 	if (fh && fh->fr) {
953c66bbc91SGabor Kovesdan 		struct bwstring *tmp;
954c66bbc91SGabor Kovesdan 
955c66bbc91SGabor Kovesdan 		tmp = file_reader_readline(fh->fr);
956c66bbc91SGabor Kovesdan 		if (tmp == NULL) {
957c66bbc91SGabor Kovesdan 			file_reader_free(fh->fr);
958c66bbc91SGabor Kovesdan 			fh->fr = NULL;
959c66bbc91SGabor Kovesdan 			if (fh->si) {
960c66bbc91SGabor Kovesdan 				sort_list_item_clean(fh->si);
961c66bbc91SGabor Kovesdan 				sort_free(fh->si);
962c66bbc91SGabor Kovesdan 				fh->si = NULL;
963c66bbc91SGabor Kovesdan 			}
964c66bbc91SGabor Kovesdan 		} else {
965c66bbc91SGabor Kovesdan 			if (fh->si == NULL)
966c66bbc91SGabor Kovesdan 				fh->si = sort_list_item_alloc();
967c66bbc91SGabor Kovesdan 			sort_list_item_set(fh->si, tmp);
968c66bbc91SGabor Kovesdan 		}
969c66bbc91SGabor Kovesdan 	}
970c66bbc91SGabor Kovesdan }
971c66bbc91SGabor Kovesdan 
972c66bbc91SGabor Kovesdan /*
973c66bbc91SGabor Kovesdan  * Merge array of "files headers"
974c66bbc91SGabor Kovesdan  */
975c66bbc91SGabor Kovesdan static void
file_headers_merge(size_t fnum,struct file_header ** fh,FILE * f_out)976e8da8c74SGabor Kovesdan file_headers_merge(size_t fnum, struct file_header **fh, FILE *f_out)
977c66bbc91SGabor Kovesdan {
978c66bbc91SGabor Kovesdan 	struct last_printed lp;
979e8da8c74SGabor Kovesdan 	size_t i;
980c66bbc91SGabor Kovesdan 
981c66bbc91SGabor Kovesdan 	memset(&lp, 0, sizeof(lp));
982c66bbc91SGabor Kovesdan 
983c66bbc91SGabor Kovesdan 	/*
984c66bbc91SGabor Kovesdan 	 * construct the initial sort structure
985c66bbc91SGabor Kovesdan 	 */
986c66bbc91SGabor Kovesdan 	for (i = 0; i < fnum; i++)
987c66bbc91SGabor Kovesdan 		file_header_list_push(fh[i], fh, i);
988c66bbc91SGabor Kovesdan 
989c66bbc91SGabor Kovesdan 	while (fh[0]->fr) { /* unfinished files are always in front */
990c66bbc91SGabor Kovesdan 		/* output the smallest line: */
991c66bbc91SGabor Kovesdan 		file_header_print(fh[0], f_out, &lp);
992c66bbc91SGabor Kovesdan 		/* read a new line, if possible: */
993c66bbc91SGabor Kovesdan 		file_header_read_next(fh[0]);
994c66bbc91SGabor Kovesdan 		/* re-arrange the list: */
995c66bbc91SGabor Kovesdan 		file_header_list_rearrange_from_header(fh, fnum);
996c66bbc91SGabor Kovesdan 	}
997c66bbc91SGabor Kovesdan 
998c66bbc91SGabor Kovesdan 	if (lp.str)
999c66bbc91SGabor Kovesdan 		bwsfree(lp.str);
1000c66bbc91SGabor Kovesdan }
1001c66bbc91SGabor Kovesdan 
1002c66bbc91SGabor Kovesdan /*
1003c66bbc91SGabor Kovesdan  * Merges the given files into the output file, which can be
1004c66bbc91SGabor Kovesdan  * stdout.
1005c66bbc91SGabor Kovesdan  */
1006c66bbc91SGabor Kovesdan static void
merge_files_array(size_t argc,const char ** argv,const char * fn_out)10077137597eSKyle Evans merge_files_array(size_t argc, const char **argv, const char *fn_out)
1008c66bbc91SGabor Kovesdan {
1009e5f71a07SPedro F. Giffuni 
1010c66bbc91SGabor Kovesdan 	if (argv && fn_out) {
1011c66bbc91SGabor Kovesdan 		struct file_header **fh;
1012c66bbc91SGabor Kovesdan 		FILE *f_out;
1013e8da8c74SGabor Kovesdan 		size_t i;
1014c66bbc91SGabor Kovesdan 
1015c66bbc91SGabor Kovesdan 		f_out = openfile(fn_out, "w");
1016c66bbc91SGabor Kovesdan 
1017c66bbc91SGabor Kovesdan 		if (f_out == NULL)
1018c66bbc91SGabor Kovesdan 			err(2, NULL);
1019c66bbc91SGabor Kovesdan 
1020c66bbc91SGabor Kovesdan 		fh = sort_malloc((argc + 1) * sizeof(struct file_header *));
1021c66bbc91SGabor Kovesdan 
1022c66bbc91SGabor Kovesdan 		for (i = 0; i < argc; i++)
1023c66bbc91SGabor Kovesdan 			file_header_init(fh + i, argv[i], (size_t) i);
1024c66bbc91SGabor Kovesdan 
1025c66bbc91SGabor Kovesdan 		file_headers_merge(argc, fh, f_out);
1026c66bbc91SGabor Kovesdan 
1027c66bbc91SGabor Kovesdan 		for (i = 0; i < argc; i++)
1028c66bbc91SGabor Kovesdan 			file_header_close(fh + i);
1029c66bbc91SGabor Kovesdan 
1030c66bbc91SGabor Kovesdan 		sort_free(fh);
1031c66bbc91SGabor Kovesdan 
1032c66bbc91SGabor Kovesdan 		closefile(f_out, fn_out);
1033c66bbc91SGabor Kovesdan 	}
1034c66bbc91SGabor Kovesdan }
1035c66bbc91SGabor Kovesdan 
1036c66bbc91SGabor Kovesdan /*
1037c66bbc91SGabor Kovesdan  * Shrinks the file list until its size smaller than max number of opened files
1038c66bbc91SGabor Kovesdan  */
1039c66bbc91SGabor Kovesdan static int
shrink_file_list(struct file_list * fl)1040c66bbc91SGabor Kovesdan shrink_file_list(struct file_list *fl)
1041c66bbc91SGabor Kovesdan {
1042e5f71a07SPedro F. Giffuni 
1043c66bbc91SGabor Kovesdan 	if ((fl == NULL) || (size_t) (fl->count) < max_open_files)
1044c66bbc91SGabor Kovesdan 		return (0);
1045c66bbc91SGabor Kovesdan 	else {
1046c66bbc91SGabor Kovesdan 		struct file_list new_fl;
1047e8da8c74SGabor Kovesdan 		size_t indx = 0;
1048c66bbc91SGabor Kovesdan 
1049c66bbc91SGabor Kovesdan 		file_list_init(&new_fl, true);
1050c66bbc91SGabor Kovesdan 		while (indx < fl->count) {
1051c66bbc91SGabor Kovesdan 			char *fnew;
1052e8da8c74SGabor Kovesdan 			size_t num;
1053c66bbc91SGabor Kovesdan 
1054c66bbc91SGabor Kovesdan 			num = fl->count - indx;
1055c66bbc91SGabor Kovesdan 			fnew = new_tmp_file_name();
1056c66bbc91SGabor Kovesdan 
1057c66bbc91SGabor Kovesdan 			if ((size_t) num >= max_open_files)
1058c66bbc91SGabor Kovesdan 				num = max_open_files - 1;
1059c66bbc91SGabor Kovesdan 			merge_files_array(num, fl->fns + indx, fnew);
1060c66bbc91SGabor Kovesdan 			if (fl->tmp) {
1061e8da8c74SGabor Kovesdan 				size_t i;
1062c66bbc91SGabor Kovesdan 
1063c66bbc91SGabor Kovesdan 				for (i = 0; i < num; i++)
1064c66bbc91SGabor Kovesdan 					unlink(fl->fns[indx + i]);
1065c66bbc91SGabor Kovesdan 			}
1066c66bbc91SGabor Kovesdan 			file_list_add(&new_fl, fnew, false);
1067c66bbc91SGabor Kovesdan 			indx += num;
1068c66bbc91SGabor Kovesdan 		}
1069c66bbc91SGabor Kovesdan 		fl->tmp = false; /* already taken care of */
1070c66bbc91SGabor Kovesdan 		file_list_clean(fl);
1071c66bbc91SGabor Kovesdan 
1072c66bbc91SGabor Kovesdan 		fl->count = new_fl.count;
1073c66bbc91SGabor Kovesdan 		fl->fns = new_fl.fns;
1074c66bbc91SGabor Kovesdan 		fl->sz = new_fl.sz;
1075c66bbc91SGabor Kovesdan 		fl->tmp = new_fl.tmp;
1076c66bbc91SGabor Kovesdan 
1077c66bbc91SGabor Kovesdan 		return (1);
1078c66bbc91SGabor Kovesdan 	}
1079c66bbc91SGabor Kovesdan }
1080c66bbc91SGabor Kovesdan 
1081c66bbc91SGabor Kovesdan /*
1082c66bbc91SGabor Kovesdan  * Merge list of files
1083c66bbc91SGabor Kovesdan  */
1084c66bbc91SGabor Kovesdan void
merge_files(struct file_list * fl,const char * fn_out)1085c66bbc91SGabor Kovesdan merge_files(struct file_list *fl, const char *fn_out)
1086c66bbc91SGabor Kovesdan {
1087e5f71a07SPedro F. Giffuni 
1088c66bbc91SGabor Kovesdan 	if (fl && fn_out) {
1089c66bbc91SGabor Kovesdan 		while (shrink_file_list(fl));
1090c66bbc91SGabor Kovesdan 
1091c66bbc91SGabor Kovesdan 		merge_files_array(fl->count, fl->fns, fn_out);
1092c66bbc91SGabor Kovesdan 	}
1093c66bbc91SGabor Kovesdan }
1094c66bbc91SGabor Kovesdan 
1095c66bbc91SGabor Kovesdan static const char *
get_sort_method_name(int sm)1096c66bbc91SGabor Kovesdan get_sort_method_name(int sm)
1097c66bbc91SGabor Kovesdan {
1098e5f71a07SPedro F. Giffuni 
1099c66bbc91SGabor Kovesdan 	if (sm == SORT_MERGESORT)
1100c66bbc91SGabor Kovesdan 		return "mergesort";
1101c66bbc91SGabor Kovesdan 	else if (sort_opts_vals.sort_method == SORT_RADIXSORT)
1102c66bbc91SGabor Kovesdan 		return "radixsort";
1103c66bbc91SGabor Kovesdan 	else if (sort_opts_vals.sort_method == SORT_HEAPSORT)
1104c66bbc91SGabor Kovesdan 		return "heapsort";
1105c66bbc91SGabor Kovesdan 	else
1106c66bbc91SGabor Kovesdan 		return "quicksort";
1107c66bbc91SGabor Kovesdan }
1108c66bbc91SGabor Kovesdan 
1109c66bbc91SGabor Kovesdan /*
1110c66bbc91SGabor Kovesdan  * Wrapper for qsort
1111c66bbc91SGabor Kovesdan  */
sort_qsort(void * list,size_t count,size_t elem_size,int (* cmp_func)(const void *,const void *))1112c66bbc91SGabor Kovesdan static int sort_qsort(void *list, size_t count, size_t elem_size,
1113c66bbc91SGabor Kovesdan     int (*cmp_func)(const void *, const void *))
1114c66bbc91SGabor Kovesdan {
1115c66bbc91SGabor Kovesdan 
1116c66bbc91SGabor Kovesdan 	qsort(list, count, elem_size, cmp_func);
1117c66bbc91SGabor Kovesdan 	return (0);
1118c66bbc91SGabor Kovesdan }
1119c66bbc91SGabor Kovesdan 
1120c66bbc91SGabor Kovesdan /*
1121c66bbc91SGabor Kovesdan  * Sort list of lines and writes it to the file
1122c66bbc91SGabor Kovesdan  */
1123c66bbc91SGabor Kovesdan void
sort_list_to_file(struct sort_list * list,const char * outfile)1124c66bbc91SGabor Kovesdan sort_list_to_file(struct sort_list *list, const char *outfile)
1125c66bbc91SGabor Kovesdan {
1126c66bbc91SGabor Kovesdan 	struct sort_mods *sm = &(keys[0].sm);
1127c66bbc91SGabor Kovesdan 
11282d955e41SGordon Bergling 	if (!(sm->Mflag) && !(sm->Rflag) && !(sm->Vflag) &&
1129c66bbc91SGabor Kovesdan 	    !(sm->gflag) && !(sm->hflag) && !(sm->nflag)) {
1130c66bbc91SGabor Kovesdan 		if ((sort_opts_vals.sort_method == SORT_DEFAULT) && byte_sort)
1131c66bbc91SGabor Kovesdan 			sort_opts_vals.sort_method = SORT_RADIXSORT;
1132c66bbc91SGabor Kovesdan 
1133c66bbc91SGabor Kovesdan 	} else if (sort_opts_vals.sort_method == SORT_RADIXSORT)
11348818aa39SGabor Kovesdan 		err(2, "%s", getstr(9));
1135c66bbc91SGabor Kovesdan 
1136c66bbc91SGabor Kovesdan 	/*
1137c66bbc91SGabor Kovesdan 	 * to handle stable sort and the unique cases in the
1138c66bbc91SGabor Kovesdan 	 * right order, we need stable basic algorithm
1139c66bbc91SGabor Kovesdan 	 */
1140c66bbc91SGabor Kovesdan 	if (sort_opts_vals.sflag) {
1141c66bbc91SGabor Kovesdan 		switch (sort_opts_vals.sort_method){
1142c66bbc91SGabor Kovesdan 		case SORT_MERGESORT:
1143c66bbc91SGabor Kovesdan 			break;
1144c66bbc91SGabor Kovesdan 		case SORT_RADIXSORT:
1145c66bbc91SGabor Kovesdan 			break;
1146c66bbc91SGabor Kovesdan 		case SORT_DEFAULT:
1147c66bbc91SGabor Kovesdan 			sort_opts_vals.sort_method = SORT_MERGESORT;
1148c66bbc91SGabor Kovesdan 			break;
1149c66bbc91SGabor Kovesdan 		default:
11508818aa39SGabor Kovesdan 			errx(2, "%s", getstr(10));
115180c7cc1cSPedro F. Giffuni 		}
1152c66bbc91SGabor Kovesdan 	}
1153c66bbc91SGabor Kovesdan 
1154c66bbc91SGabor Kovesdan 	if (sort_opts_vals.sort_method == SORT_DEFAULT)
11555d5151aeSGabor Kovesdan 		sort_opts_vals.sort_method = DEFAULT_SORT_ALGORITHM;
1156c66bbc91SGabor Kovesdan 
1157c66bbc91SGabor Kovesdan 	if (debug_sort)
1158c66bbc91SGabor Kovesdan 		printf("sort_method=%s\n",
1159c66bbc91SGabor Kovesdan 		    get_sort_method_name(sort_opts_vals.sort_method));
1160c66bbc91SGabor Kovesdan 
1161c66bbc91SGabor Kovesdan 	switch (sort_opts_vals.sort_method){
1162c66bbc91SGabor Kovesdan 	case SORT_RADIXSORT:
1163c66bbc91SGabor Kovesdan 		rxsort(list->list, list->count);
1164c66bbc91SGabor Kovesdan 		sort_list_dump(list, outfile);
1165c66bbc91SGabor Kovesdan 		break;
1166c66bbc91SGabor Kovesdan 	case SORT_MERGESORT:
1167c66bbc91SGabor Kovesdan 		mt_sort(list, mergesort, outfile);
1168c66bbc91SGabor Kovesdan 		break;
1169c66bbc91SGabor Kovesdan 	case SORT_HEAPSORT:
1170c66bbc91SGabor Kovesdan 		mt_sort(list, heapsort,	outfile);
1171c66bbc91SGabor Kovesdan 		break;
11725d5151aeSGabor Kovesdan 	case SORT_QSORT:
1173c66bbc91SGabor Kovesdan 		mt_sort(list, sort_qsort, outfile);
1174c66bbc91SGabor Kovesdan 		break;
11755d5151aeSGabor Kovesdan 	default:
11765d5151aeSGabor Kovesdan 		mt_sort(list, DEFAULT_SORT_FUNC, outfile);
11775d5151aeSGabor Kovesdan 		break;
1178c66bbc91SGabor Kovesdan 	}
1179c66bbc91SGabor Kovesdan }
1180c66bbc91SGabor Kovesdan 
1181c66bbc91SGabor Kovesdan /******************* MT SORT ************************/
1182c66bbc91SGabor Kovesdan 
1183c66bbc91SGabor Kovesdan #if defined(SORT_THREADS)
1184c66bbc91SGabor Kovesdan /* semaphore to count threads */
1185c66bbc91SGabor Kovesdan static sem_t mtsem;
1186c66bbc91SGabor Kovesdan 
1187c66bbc91SGabor Kovesdan /* current system sort function */
1188c66bbc91SGabor Kovesdan static int (*g_sort_func)(void *, size_t, size_t,
1189c66bbc91SGabor Kovesdan     int(*)(const void *, const void *));
1190c66bbc91SGabor Kovesdan 
1191c66bbc91SGabor Kovesdan /*
1192c66bbc91SGabor Kovesdan  * Sort cycle thread (in multi-threaded mode)
1193c66bbc91SGabor Kovesdan  */
1194c66bbc91SGabor Kovesdan static void*
mt_sort_thread(void * arg)1195c66bbc91SGabor Kovesdan mt_sort_thread(void* arg)
1196c66bbc91SGabor Kovesdan {
1197c66bbc91SGabor Kovesdan 	struct sort_list *list = arg;
1198c66bbc91SGabor Kovesdan 
1199c66bbc91SGabor Kovesdan 	g_sort_func(list->list, list->count, sizeof(struct sort_list_item *),
1200c66bbc91SGabor Kovesdan 	    (int(*)(const void *, const void *)) list_coll);
1201c66bbc91SGabor Kovesdan 
1202c66bbc91SGabor Kovesdan 	sem_post(&mtsem);
1203c66bbc91SGabor Kovesdan 
1204c66bbc91SGabor Kovesdan 	return (arg);
1205c66bbc91SGabor Kovesdan }
1206c66bbc91SGabor Kovesdan 
1207c66bbc91SGabor Kovesdan /*
1208c66bbc91SGabor Kovesdan  * Compare sub-lists. Empty sub-lists always go to the end of the list.
1209c66bbc91SGabor Kovesdan  */
1210c66bbc91SGabor Kovesdan static int
sub_list_cmp(struct sort_list * l1,struct sort_list * l2)1211c66bbc91SGabor Kovesdan sub_list_cmp(struct sort_list *l1, struct sort_list *l2)
1212c66bbc91SGabor Kovesdan {
1213c66bbc91SGabor Kovesdan 
1214c66bbc91SGabor Kovesdan 	if (l1 == l2)
1215c66bbc91SGabor Kovesdan 		return (0);
1216c66bbc91SGabor Kovesdan 	else {
1217c66bbc91SGabor Kovesdan 		if (l1->count == 0) {
1218c66bbc91SGabor Kovesdan 			return ((l2->count == 0) ? 0 : +1);
1219c66bbc91SGabor Kovesdan 		} else if (l2->count == 0) {
1220c66bbc91SGabor Kovesdan 			return (-1);
1221c66bbc91SGabor Kovesdan 		} else {
1222c66bbc91SGabor Kovesdan 			int ret;
1223c66bbc91SGabor Kovesdan 
1224c66bbc91SGabor Kovesdan 			ret = list_coll(&(l1->list[0]), &(l2->list[0]));
1225c66bbc91SGabor Kovesdan 			if (!ret)
1226c66bbc91SGabor Kovesdan 				return ((l1->sub_list_pos < l2->sub_list_pos) ?
1227c66bbc91SGabor Kovesdan 				    -1 : +1);
1228c66bbc91SGabor Kovesdan 			return (ret);
1229c66bbc91SGabor Kovesdan 		}
1230c66bbc91SGabor Kovesdan 	}
1231c66bbc91SGabor Kovesdan }
1232c66bbc91SGabor Kovesdan 
1233c66bbc91SGabor Kovesdan /*
1234c66bbc91SGabor Kovesdan  * Swap two array elements
1235c66bbc91SGabor Kovesdan  */
1236c66bbc91SGabor Kovesdan static void
sub_list_swap(struct sort_list ** sl,size_t i1,size_t i2)1237e8da8c74SGabor Kovesdan sub_list_swap(struct sort_list **sl, size_t i1, size_t i2)
1238c66bbc91SGabor Kovesdan {
1239c66bbc91SGabor Kovesdan 	struct sort_list *tmp;
1240c66bbc91SGabor Kovesdan 
1241c66bbc91SGabor Kovesdan 	tmp = sl[i1];
1242c66bbc91SGabor Kovesdan 	sl[i1] = sl[i2];
1243c66bbc91SGabor Kovesdan 	sl[i2] = tmp;
1244c66bbc91SGabor Kovesdan }
1245c66bbc91SGabor Kovesdan 
1246c66bbc91SGabor Kovesdan /* heap algorithm ==>> */
1247c66bbc91SGabor Kovesdan 
1248c66bbc91SGabor Kovesdan /*
1249c66bbc91SGabor Kovesdan  * See heap sort algorithm
1250c66bbc91SGabor Kovesdan  * "Raises" last element to its right place
1251c66bbc91SGabor Kovesdan  */
1252c66bbc91SGabor Kovesdan static void
sub_list_swim(struct sort_list ** sl,size_t indx)1253e8da8c74SGabor Kovesdan sub_list_swim(struct sort_list **sl, size_t indx)
1254c66bbc91SGabor Kovesdan {
1255c66bbc91SGabor Kovesdan 
1256c66bbc91SGabor Kovesdan 	if (indx > 0) {
1257e8da8c74SGabor Kovesdan 		size_t parent_index;
1258c66bbc91SGabor Kovesdan 
1259c66bbc91SGabor Kovesdan 		parent_index = (indx - 1) >> 1;
1260c66bbc91SGabor Kovesdan 
1261c66bbc91SGabor Kovesdan 		if (sub_list_cmp(sl[indx], sl[parent_index]) < 0) {
1262c66bbc91SGabor Kovesdan 			/* swap child and parent and continue */
1263c66bbc91SGabor Kovesdan 			sub_list_swap(sl, indx, parent_index);
1264c66bbc91SGabor Kovesdan 			sub_list_swim(sl, parent_index);
1265c66bbc91SGabor Kovesdan 		}
1266c66bbc91SGabor Kovesdan 	}
1267c66bbc91SGabor Kovesdan }
1268c66bbc91SGabor Kovesdan 
1269c66bbc91SGabor Kovesdan /*
1270c66bbc91SGabor Kovesdan  * Sink the top element to its correct position
1271c66bbc91SGabor Kovesdan  */
1272c66bbc91SGabor Kovesdan static void
sub_list_sink(struct sort_list ** sl,size_t indx,size_t size)1273e8da8c74SGabor Kovesdan sub_list_sink(struct sort_list **sl, size_t indx, size_t size)
1274c66bbc91SGabor Kovesdan {
1275e8da8c74SGabor Kovesdan 	size_t left_child_index;
1276e8da8c74SGabor Kovesdan 	size_t right_child_index;
1277c66bbc91SGabor Kovesdan 
1278c66bbc91SGabor Kovesdan 	left_child_index = indx + indx + 1;
1279c66bbc91SGabor Kovesdan 	right_child_index = left_child_index + 1;
1280c66bbc91SGabor Kovesdan 
1281c66bbc91SGabor Kovesdan 	if (left_child_index < size) {
1282e8da8c74SGabor Kovesdan 		size_t min_child_index;
1283c66bbc91SGabor Kovesdan 
1284c66bbc91SGabor Kovesdan 		min_child_index = left_child_index;
1285c66bbc91SGabor Kovesdan 
1286c66bbc91SGabor Kovesdan 		if ((right_child_index < size) &&
1287c66bbc91SGabor Kovesdan 		    (sub_list_cmp(sl[left_child_index],
1288c66bbc91SGabor Kovesdan 		    sl[right_child_index]) > 0))
1289c66bbc91SGabor Kovesdan 			min_child_index = right_child_index;
1290c66bbc91SGabor Kovesdan 		if (sub_list_cmp(sl[indx], sl[min_child_index]) > 0) {
1291c66bbc91SGabor Kovesdan 			sub_list_swap(sl, indx, min_child_index);
1292c66bbc91SGabor Kovesdan 			sub_list_sink(sl, min_child_index, size);
1293c66bbc91SGabor Kovesdan 		}
1294c66bbc91SGabor Kovesdan 	}
1295c66bbc91SGabor Kovesdan }
1296c66bbc91SGabor Kovesdan 
1297c66bbc91SGabor Kovesdan /* <<== heap algorithm */
1298c66bbc91SGabor Kovesdan 
1299c66bbc91SGabor Kovesdan /*
1300c66bbc91SGabor Kovesdan  * Adds element to the "right" end
1301c66bbc91SGabor Kovesdan  */
1302c66bbc91SGabor Kovesdan static void
sub_list_push(struct sort_list * s,struct sort_list ** sl,size_t size)1303e8da8c74SGabor Kovesdan sub_list_push(struct sort_list *s, struct sort_list **sl, size_t size)
1304c66bbc91SGabor Kovesdan {
1305c66bbc91SGabor Kovesdan 
1306c66bbc91SGabor Kovesdan 	sl[size++] = s;
1307c66bbc91SGabor Kovesdan 	sub_list_swim(sl, size - 1);
1308c66bbc91SGabor Kovesdan }
1309c66bbc91SGabor Kovesdan 
1310c66bbc91SGabor Kovesdan struct last_printed_item
1311c66bbc91SGabor Kovesdan {
1312c66bbc91SGabor Kovesdan 	struct sort_list_item *item;
1313c66bbc91SGabor Kovesdan };
1314c66bbc91SGabor Kovesdan 
1315c66bbc91SGabor Kovesdan /*
1316c66bbc91SGabor Kovesdan  * Prints the current line of the file
1317c66bbc91SGabor Kovesdan  */
1318c66bbc91SGabor Kovesdan static void
sub_list_header_print(struct sort_list * sl,FILE * f_out,struct last_printed_item * lp)1319c66bbc91SGabor Kovesdan sub_list_header_print(struct sort_list *sl, FILE *f_out,
1320c66bbc91SGabor Kovesdan     struct last_printed_item *lp)
1321c66bbc91SGabor Kovesdan {
1322c66bbc91SGabor Kovesdan 
1323c66bbc91SGabor Kovesdan 	if (sl && sl->count && f_out && sl->list[0]->str) {
1324c66bbc91SGabor Kovesdan 		if (sort_opts_vals.uflag) {
1325c66bbc91SGabor Kovesdan 			if ((lp->item == NULL) || (list_coll(&(lp->item),
1326c66bbc91SGabor Kovesdan 			    &(sl->list[0])))) {
1327c66bbc91SGabor Kovesdan 				bwsfwrite(sl->list[0]->str, f_out,
1328c66bbc91SGabor Kovesdan 				    sort_opts_vals.zflag);
1329c66bbc91SGabor Kovesdan 				lp->item = sl->list[0];
1330c66bbc91SGabor Kovesdan 			}
1331c66bbc91SGabor Kovesdan 		} else
1332c66bbc91SGabor Kovesdan 			bwsfwrite(sl->list[0]->str, f_out,
1333c66bbc91SGabor Kovesdan 			    sort_opts_vals.zflag);
1334c66bbc91SGabor Kovesdan 	}
1335c66bbc91SGabor Kovesdan }
1336c66bbc91SGabor Kovesdan 
1337c66bbc91SGabor Kovesdan /*
1338c66bbc91SGabor Kovesdan  * Read next line
1339c66bbc91SGabor Kovesdan  */
1340c66bbc91SGabor Kovesdan static void
sub_list_next(struct sort_list * sl)1341c66bbc91SGabor Kovesdan sub_list_next(struct sort_list *sl)
1342c66bbc91SGabor Kovesdan {
1343c66bbc91SGabor Kovesdan 
1344c66bbc91SGabor Kovesdan 	if (sl && sl->count) {
1345c66bbc91SGabor Kovesdan 		sl->list += 1;
1346c66bbc91SGabor Kovesdan 		sl->count -= 1;
1347c66bbc91SGabor Kovesdan 	}
1348c66bbc91SGabor Kovesdan }
1349c66bbc91SGabor Kovesdan 
1350c66bbc91SGabor Kovesdan /*
1351c66bbc91SGabor Kovesdan  * Merge sub-lists to a file
1352c66bbc91SGabor Kovesdan  */
1353c66bbc91SGabor Kovesdan static void
merge_sub_lists(struct sort_list ** sl,size_t n,FILE * f_out)1354c66bbc91SGabor Kovesdan merge_sub_lists(struct sort_list **sl, size_t n, FILE* f_out)
1355c66bbc91SGabor Kovesdan {
1356c66bbc91SGabor Kovesdan 	struct last_printed_item lp;
1357c66bbc91SGabor Kovesdan 	size_t i;
1358c66bbc91SGabor Kovesdan 
1359c66bbc91SGabor Kovesdan 	memset(&lp,0,sizeof(lp));
1360c66bbc91SGabor Kovesdan 
1361c66bbc91SGabor Kovesdan 	/* construct the initial list: */
1362c66bbc91SGabor Kovesdan 	for (i = 0; i < n; i++)
1363c66bbc91SGabor Kovesdan 		sub_list_push(sl[i], sl, i);
1364c66bbc91SGabor Kovesdan 
1365c66bbc91SGabor Kovesdan 	while (sl[0]->count) { /* unfinished lists are always in front */
1366c66bbc91SGabor Kovesdan 		/* output the smallest line: */
1367c66bbc91SGabor Kovesdan 		sub_list_header_print(sl[0], f_out, &lp);
1368c66bbc91SGabor Kovesdan 		/* move to a new line, if possible: */
1369c66bbc91SGabor Kovesdan 		sub_list_next(sl[0]);
1370c66bbc91SGabor Kovesdan 		/* re-arrange the list: */
1371c66bbc91SGabor Kovesdan 		sub_list_sink(sl, 0, n);
1372c66bbc91SGabor Kovesdan 	}
1373c66bbc91SGabor Kovesdan }
1374c66bbc91SGabor Kovesdan 
1375c66bbc91SGabor Kovesdan /*
1376c66bbc91SGabor Kovesdan  * Merge sub-lists to a file
1377c66bbc91SGabor Kovesdan  */
1378c66bbc91SGabor Kovesdan static void
merge_list_parts(struct sort_list ** parts,size_t n,const char * fn)1379c66bbc91SGabor Kovesdan merge_list_parts(struct sort_list **parts, size_t n, const char *fn)
1380c66bbc91SGabor Kovesdan {
1381c66bbc91SGabor Kovesdan 	FILE* f_out;
1382c66bbc91SGabor Kovesdan 
1383c66bbc91SGabor Kovesdan 	f_out = openfile(fn,"w");
1384c66bbc91SGabor Kovesdan 
1385c66bbc91SGabor Kovesdan 	merge_sub_lists(parts, n, f_out);
1386c66bbc91SGabor Kovesdan 
1387c66bbc91SGabor Kovesdan 	closefile(f_out, fn);
1388c66bbc91SGabor Kovesdan }
1389c66bbc91SGabor Kovesdan 
1390c66bbc91SGabor Kovesdan #endif /* defined(SORT_THREADS) */
1391c66bbc91SGabor Kovesdan /*
1392c66bbc91SGabor Kovesdan  * Multi-threaded sort algorithm "driver"
1393c66bbc91SGabor Kovesdan  */
1394c66bbc91SGabor Kovesdan static void
mt_sort(struct sort_list * list,int (* sort_func)(void *,size_t,size_t,int (*)(const void *,const void *)),const char * fn)1395c66bbc91SGabor Kovesdan mt_sort(struct sort_list *list,
1396c66bbc91SGabor Kovesdan     int(*sort_func)(void *, size_t, size_t, int(*)(const void *, const void *)),
1397c66bbc91SGabor Kovesdan     const char* fn)
1398c66bbc91SGabor Kovesdan {
1399c66bbc91SGabor Kovesdan #if defined(SORT_THREADS)
14005ca724dcSGabor Kovesdan 	if (nthreads < 2 || list->count < MT_SORT_THRESHOLD) {
14015ca724dcSGabor Kovesdan 		size_t nthreads_save = nthreads;
14025ca724dcSGabor Kovesdan 		nthreads = 1;
1403c66bbc91SGabor Kovesdan #endif
1404c66bbc91SGabor Kovesdan 		/* if single thread or small data, do simple sort */
1405c66bbc91SGabor Kovesdan 		sort_func(list->list, list->count,
1406c66bbc91SGabor Kovesdan 		    sizeof(struct sort_list_item *),
1407c66bbc91SGabor Kovesdan 		    (int(*)(const void *, const void *)) list_coll);
1408c66bbc91SGabor Kovesdan 		sort_list_dump(list, fn);
1409c66bbc91SGabor Kovesdan #if defined(SORT_THREADS)
14105ca724dcSGabor Kovesdan 		nthreads = nthreads_save;
1411c66bbc91SGabor Kovesdan 	} else {
1412c66bbc91SGabor Kovesdan 		/* multi-threaded sort */
1413c66bbc91SGabor Kovesdan 		struct sort_list **parts;
1414c66bbc91SGabor Kovesdan 		size_t avgsize, cstart, i;
1415c66bbc91SGabor Kovesdan 
1416c66bbc91SGabor Kovesdan 		/* array of sub-lists */
1417c66bbc91SGabor Kovesdan 		parts = sort_malloc(sizeof(struct sort_list*) * nthreads);
1418c66bbc91SGabor Kovesdan 		cstart = 0;
1419c66bbc91SGabor Kovesdan 		avgsize = list->count / nthreads;
1420c66bbc91SGabor Kovesdan 
1421c66bbc91SGabor Kovesdan 		/* set global system sort function */
1422c66bbc91SGabor Kovesdan 		g_sort_func = sort_func;
1423c66bbc91SGabor Kovesdan 
1424c66bbc91SGabor Kovesdan 		/* set sublists */
1425c66bbc91SGabor Kovesdan 		for (i = 0; i < nthreads; ++i) {
1426c66bbc91SGabor Kovesdan 			size_t sz = 0;
1427c66bbc91SGabor Kovesdan 
1428c66bbc91SGabor Kovesdan 			parts[i] = sort_malloc(sizeof(struct sort_list));
1429c66bbc91SGabor Kovesdan 			parts[i]->list = list->list + cstart;
1430c66bbc91SGabor Kovesdan 			parts[i]->memsize = 0;
1431c66bbc91SGabor Kovesdan 			parts[i]->sub_list_pos = i;
1432c66bbc91SGabor Kovesdan 
1433c66bbc91SGabor Kovesdan 			sz = (i == nthreads - 1) ? list->count - cstart :
1434c66bbc91SGabor Kovesdan 			    avgsize;
1435c66bbc91SGabor Kovesdan 
1436c66bbc91SGabor Kovesdan 			parts[i]->count = sz;
1437c66bbc91SGabor Kovesdan 
1438c66bbc91SGabor Kovesdan 			parts[i]->size = parts[i]->count;
1439c66bbc91SGabor Kovesdan 
1440c66bbc91SGabor Kovesdan 			cstart += sz;
1441c66bbc91SGabor Kovesdan 		}
1442c66bbc91SGabor Kovesdan 
1443c66bbc91SGabor Kovesdan 		/* init threads counting semaphore */
1444c66bbc91SGabor Kovesdan 		sem_init(&mtsem, 0, 0);
1445c66bbc91SGabor Kovesdan 
1446c66bbc91SGabor Kovesdan 		/* start threads */
1447c66bbc91SGabor Kovesdan 		for (i = 0; i < nthreads; ++i) {
1448c66bbc91SGabor Kovesdan 			pthread_t pth;
1449c66bbc91SGabor Kovesdan 			pthread_attr_t attr;
1450c66bbc91SGabor Kovesdan 
1451c66bbc91SGabor Kovesdan 			pthread_attr_init(&attr);
1452c66bbc91SGabor Kovesdan 			pthread_attr_setdetachstate(&attr, PTHREAD_DETACHED);
1453c66bbc91SGabor Kovesdan 
14545ca724dcSGabor Kovesdan 			for (;;) {
14555ca724dcSGabor Kovesdan 				int res = pthread_create(&pth, &attr,
14565ca724dcSGabor Kovesdan 				    mt_sort_thread, parts[i]);
14575ca724dcSGabor Kovesdan 
14585ca724dcSGabor Kovesdan 				if (res >= 0)
14595ca724dcSGabor Kovesdan 					break;
14605ca724dcSGabor Kovesdan 				if (errno == EAGAIN) {
14615ca724dcSGabor Kovesdan 					pthread_yield();
14625ca724dcSGabor Kovesdan 					continue;
14635ca724dcSGabor Kovesdan 				}
14645ca724dcSGabor Kovesdan 				err(2, NULL);
14655ca724dcSGabor Kovesdan 			}
1466c66bbc91SGabor Kovesdan 
1467c66bbc91SGabor Kovesdan 			pthread_attr_destroy(&attr);
1468c66bbc91SGabor Kovesdan 		}
1469c66bbc91SGabor Kovesdan 
1470c66bbc91SGabor Kovesdan 		/* wait for threads completion */
1471c66bbc91SGabor Kovesdan 		for (i = 0; i < nthreads; ++i) {
1472c66bbc91SGabor Kovesdan 			sem_wait(&mtsem);
1473c66bbc91SGabor Kovesdan 		}
1474c66bbc91SGabor Kovesdan 		/* destroy the semaphore - we do not need it anymore */
1475c66bbc91SGabor Kovesdan 		sem_destroy(&mtsem);
1476c66bbc91SGabor Kovesdan 
1477c66bbc91SGabor Kovesdan 		/* merge sorted sub-lists to the file */
1478c66bbc91SGabor Kovesdan 		merge_list_parts(parts, nthreads, fn);
1479c66bbc91SGabor Kovesdan 
1480c66bbc91SGabor Kovesdan 		/* free sub-lists data */
1481c66bbc91SGabor Kovesdan 		for (i = 0; i < nthreads; ++i) {
1482c66bbc91SGabor Kovesdan 			sort_free(parts[i]);
1483c66bbc91SGabor Kovesdan 		}
1484c66bbc91SGabor Kovesdan 		sort_free(parts);
1485c66bbc91SGabor Kovesdan 	}
1486c66bbc91SGabor Kovesdan #endif /* defined(SORT_THREADS) */
1487c66bbc91SGabor Kovesdan }
1488