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