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