xref: /onnv-gate/usr/src/cmd/sort/common/utility.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef	_SORT_UTILITY_H
28*0Sstevel@tonic-gate #define	_SORT_UTILITY_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #ifdef	__cplusplus
33*0Sstevel@tonic-gate extern "C" {
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/mman.h>
37*0Sstevel@tonic-gate #include <sys/stat.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <assert.h>
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <fcntl.h>
42*0Sstevel@tonic-gate #include <libintl.h>
43*0Sstevel@tonic-gate #include <signal.h>
44*0Sstevel@tonic-gate #include <stdarg.h>
45*0Sstevel@tonic-gate #include <stdio.h>
46*0Sstevel@tonic-gate #include <stdlib.h>
47*0Sstevel@tonic-gate #include <string.h>
48*0Sstevel@tonic-gate #include <unistd.h>
49*0Sstevel@tonic-gate #include <wchar.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include "types.h"
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #define	CMDNAME	"sort"
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #ifndef TRUE
56*0Sstevel@tonic-gate #define	TRUE	1
57*0Sstevel@tonic-gate #endif /* TRUE */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #ifndef FALSE
60*0Sstevel@tonic-gate #define	FALSE	0
61*0Sstevel@tonic-gate #endif /* FALSE */
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #define	SGN(x)		(((x) == 0 ? 0 : ((x) > 0 ? 1 : -1)))
64*0Sstevel@tonic-gate #define	MIN(x, y)	(((x) < (y)) ? (x) : (y))
65*0Sstevel@tonic-gate #define	MAX(x, y)	(((x) > (y)) ? (x) : (y))
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #define	SE_BAD_FIELD			1
68*0Sstevel@tonic-gate #define	SE_BAD_SPECIFIER		2
69*0Sstevel@tonic-gate #define	SE_BAD_STREAM			3
70*0Sstevel@tonic-gate #define	SE_CANT_MMAP_FILE		4
71*0Sstevel@tonic-gate #define	SE_CANT_OPEN_FILE		5
72*0Sstevel@tonic-gate #define	SE_CANT_SET_SIGNAL		6
73*0Sstevel@tonic-gate #define	SE_CAUGHT_SIGNAL		7
74*0Sstevel@tonic-gate #define	SE_CHECK_ERROR			8
75*0Sstevel@tonic-gate #define	SE_CHECK_FAILED			9
76*0Sstevel@tonic-gate #define	SE_CHECK_SUCCEED		10
77*0Sstevel@tonic-gate #define	SE_ILLEGAL_CHARACTER		11
78*0Sstevel@tonic-gate #define	SE_INSUFFICIENT_DESCRIPTORS	12
79*0Sstevel@tonic-gate #define	SE_INSUFFICIENT_MEMORY		13
80*0Sstevel@tonic-gate #define	SE_MMAP_FAILED			14
81*0Sstevel@tonic-gate #define	SE_MUNMAP_FAILED		15
82*0Sstevel@tonic-gate #define	SE_READ_FAILED			16
83*0Sstevel@tonic-gate #define	SE_REALLOCATE_BUFFER		17
84*0Sstevel@tonic-gate #define	SE_STAT_FAILED			18
85*0Sstevel@tonic-gate #define	SE_TOO_MANY_TEMPFILES		19
86*0Sstevel@tonic-gate #define	SE_UNLINK_FAILED		20
87*0Sstevel@tonic-gate #define	SE_USAGE			21
88*0Sstevel@tonic-gate #define	SE_WRITE_FAILED			22
89*0Sstevel@tonic-gate #define	SE_CLOSE_FAILED			23
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate #define	KILOBYTE			1024
92*0Sstevel@tonic-gate #define	MEGABYTE			(1024 * KILOBYTE)
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate #define	AV_MEM_MULTIPLIER		3
95*0Sstevel@tonic-gate #define	AV_MEM_DIVISOR			4
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate #define	OUTPUT_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | \
98*0Sstevel@tonic-gate     S_IWOTH)
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate #define	E_SUCCESS	0	/* Exit status for successful run */
101*0Sstevel@tonic-gate #define	E_FAILED_CHECK	1	/* Exit status for failed check */
102*0Sstevel@tonic-gate #define	E_ERROR		2	/* Exit status for other error */
103*0Sstevel@tonic-gate #define	E_USAGE		E_ERROR	/* Exit status for usage error */
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate #define	EMSG_CHECK	\
106*0Sstevel@tonic-gate     gettext("check option (-c) only for use with a single file\n")
107*0Sstevel@tonic-gate #define	EMSG_MMAP	gettext("can't mmap %s")
108*0Sstevel@tonic-gate #define	EMSG_MUNMAP	gettext("can't munmap %s")
109*0Sstevel@tonic-gate #define	EMSG_REALLOC	gettext("unable to reallocate buffer")
110*0Sstevel@tonic-gate #define	EMSG_ALLOC	gettext("unable to allocate memory")
111*0Sstevel@tonic-gate #define	EMSG_OPEN	gettext("can't open %s")
112*0Sstevel@tonic-gate #define	EMSG_READ	gettext("can't read %s")
113*0Sstevel@tonic-gate #define	EMSG_WRITE	gettext("can't write %s")
114*0Sstevel@tonic-gate #define	EMSG_STAT	gettext("can't stat %s")
115*0Sstevel@tonic-gate #define	EMSG_CLOSE	gettext("can't close %s")
116*0Sstevel@tonic-gate #define	EMSG_UNLINK	gettext("can't unlink %s")
117*0Sstevel@tonic-gate #define	EMSG_ILLEGAL_CHAR gettext("can't translate illegal wide character\n")
118*0Sstevel@tonic-gate #define	EMSG_TEMPORARY	gettext("temporary file template exhausted\n")
119*0Sstevel@tonic-gate #define	EMSG_MEMORY	\
120*0Sstevel@tonic-gate     gettext("insufficient memory; use -S option to increase allocation\n")
121*0Sstevel@tonic-gate #define	EMSG_DESCRIPTORS gettext("insufficient available file descriptors\n")
122*0Sstevel@tonic-gate #define	EMSG_SIGNAL	gettext("can't set signal handler for %s")
123*0Sstevel@tonic-gate #define	EMSG_BADPRIME	gettext("internal file state corrupted\n")
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate #define	EMSG_UNKN_STREAM gettext("INTERNAL: stream of type %d seen\n")
126*0Sstevel@tonic-gate #define	EMSG_UNKN_FIELD	gettext("INTERNAL: field of type %d seen\n")
127*0Sstevel@tonic-gate #define	EMSG_UNKN_OPTION gettext("INTERNAL: option parser error\n")
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate #define	WMSG_NEWLINE_ADDED \
130*0Sstevel@tonic-gate     gettext("missing NEWLINE added at end of input file %s\n")
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate extern void warn(const char *, ...);
133*0Sstevel@tonic-gate extern void die(const char *, ...);
134*0Sstevel@tonic-gate extern void usage(void);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate extern void swap(void **, void **);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate extern int bump_file_template();
139*0Sstevel@tonic-gate extern char *get_file_template();
140*0Sstevel@tonic-gate extern void set_file_template(char **);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate extern void set_cleanup_chain(stream_t **);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate extern void set_output_file(char *);
145*0Sstevel@tonic-gate extern void set_output_guard(stream_t *);
146*0Sstevel@tonic-gate extern void clear_output_guard(void);
147*0Sstevel@tonic-gate extern void establish_output_guard(sort_t *);
148*0Sstevel@tonic-gate extern void remove_output_guard();
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate extern void atexit_handler(void);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate extern size_t available_memory(size_t);
153*0Sstevel@tonic-gate extern void set_memory_ratio(sort_t *, int *, int *);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate extern size_t strtomem(char *);
156*0Sstevel@tonic-gate extern void *safe_realloc(void *, size_t);
157*0Sstevel@tonic-gate extern void safe_free(void *);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate extern void *xzmap(void *, size_t, int, int, off_t);
160*0Sstevel@tonic-gate extern void hold_file_descriptor(void);
161*0Sstevel@tonic-gate extern void release_file_descriptor(void);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate extern void copy_line_rec(const line_rec_t *, line_rec_t *);
164*0Sstevel@tonic-gate extern void trip_eof(FILE *f);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate extern int cxwrite(int, char *, size_t);
167*0Sstevel@tonic-gate extern int wxwrite(int, wchar_t *);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate extern int xstreql(const char *, const char *);
170*0Sstevel@tonic-gate extern int xstrneql(const char *, const char *, const size_t);
171*0Sstevel@tonic-gate extern char *xstrnchr(const char *, const int, const size_t);
172*0Sstevel@tonic-gate extern void xstrninv(char *, ssize_t, ssize_t);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate extern int xwcsneql(const wchar_t *, const wchar_t *, const size_t);
175*0Sstevel@tonic-gate extern wchar_t *xwsnchr(const wchar_t *, const wint_t, const size_t);
176*0Sstevel@tonic-gate extern void xwcsninv(wchar_t *, ssize_t, ssize_t);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
179*0Sstevel@tonic-gate extern void xwcsntomsb(wchar_t *, ssize_t);
180*0Sstevel@tonic-gate #endif /* _LITTLE_ENDIAN */
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate extern wchar_t *xmemwchar(wchar_t *, wchar_t, ssize_t);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate extern void xcp(char *, char *, off_t);
185*0Sstevel@tonic-gate extern void xdump(FILE *, uchar_t *, size_t, int);
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate #ifdef DEBUG
188*0Sstevel@tonic-gate #define	ASSERT(x) assert(x)
189*0Sstevel@tonic-gate #else
190*0Sstevel@tonic-gate #define	ASSERT(x)
191*0Sstevel@tonic-gate #endif
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate #ifdef STATS
194*0Sstevel@tonic-gate #define	__S(x) x
195*0Sstevel@tonic-gate #else
196*0Sstevel@tonic-gate #define	__S(x)
197*0Sstevel@tonic-gate #endif
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate #ifdef	__cplusplus
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate #endif
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate #endif	/* _SORT_UTILITY_H */
204