summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sort/common/utility.h
blob: bac0d94b680a77837676ccc779245427adf9cdf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1998-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SORT_UTILITY_H
#define	_SORT_UTILITY_H

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/mman.h>
#include <sys/stat.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <libintl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>

#include "types.h"

#define	CMDNAME	"sort"

#ifndef TRUE
#define	TRUE	1
#endif /* TRUE */

#ifndef FALSE
#define	FALSE	0
#endif /* FALSE */

#define	SGN(x)		(((x) == 0 ? 0 : ((x) > 0 ? 1 : -1)))
#define	MIN(x, y)	(((x) < (y)) ? (x) : (y))
#define	MAX(x, y)	(((x) > (y)) ? (x) : (y))

#define	SE_BAD_FIELD			1
#define	SE_BAD_SPECIFIER		2
#define	SE_BAD_STREAM			3
#define	SE_CANT_MMAP_FILE		4
#define	SE_CANT_OPEN_FILE		5
#define	SE_CANT_SET_SIGNAL		6
#define	SE_CAUGHT_SIGNAL		7
#define	SE_CHECK_ERROR			8
#define	SE_CHECK_FAILED			9
#define	SE_CHECK_SUCCEED		10
#define	SE_ILLEGAL_CHARACTER		11
#define	SE_INSUFFICIENT_DESCRIPTORS	12
#define	SE_INSUFFICIENT_MEMORY		13
#define	SE_MMAP_FAILED			14
#define	SE_MUNMAP_FAILED		15
#define	SE_READ_FAILED			16
#define	SE_REALLOCATE_BUFFER		17
#define	SE_STAT_FAILED			18
#define	SE_TOO_MANY_TEMPFILES		19
#define	SE_UNLINK_FAILED		20
#define	SE_USAGE			21
#define	SE_WRITE_FAILED			22
#define	SE_CLOSE_FAILED			23

#define	KILOBYTE			1024
#define	MEGABYTE			(1024 * KILOBYTE)

#define	AV_MEM_MULTIPLIER		3
#define	AV_MEM_DIVISOR			4

#define	OUTPUT_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | \
    S_IWOTH)

#define	E_SUCCESS	0	/* Exit status for successful run */
#define	E_FAILED_CHECK	1	/* Exit status for failed check */
#define	E_ERROR		2	/* Exit status for other error */
#define	E_USAGE		E_ERROR	/* Exit status for usage error */

#define	EMSG_CHECK	\
    gettext("check option (-c) only for use with a single file\n")
#define	EMSG_MMAP	gettext("can't mmap %s")
#define	EMSG_MUNMAP	gettext("can't munmap %s")
#define	EMSG_REALLOC	gettext("unable to reallocate buffer")
#define	EMSG_ALLOC	gettext("unable to allocate memory")
#define	EMSG_OPEN	gettext("can't open %s")
#define	EMSG_READ	gettext("can't read %s")
#define	EMSG_WRITE	gettext("can't write %s")
#define	EMSG_STAT	gettext("can't stat %s")
#define	EMSG_CLOSE	gettext("can't close %s")
#define	EMSG_UNLINK	gettext("can't unlink %s")
#define	EMSG_ILLEGAL_CHAR gettext("can't translate illegal wide character\n")
#define	EMSG_TEMPORARY	gettext("temporary file template exhausted\n")
#define	EMSG_MEMORY	\
    gettext("insufficient memory; use -S option to increase allocation\n")
#define	EMSG_DESCRIPTORS gettext("insufficient available file descriptors\n")
#define	EMSG_SIGNAL	gettext("can't set signal handler for %s")
#define	EMSG_BADPRIME	gettext("internal file state corrupted\n")

#define	EMSG_UNKN_STREAM gettext("INTERNAL: stream of type %d seen\n")
#define	EMSG_UNKN_FIELD	gettext("INTERNAL: field of type %d seen\n")
#define	EMSG_UNKN_OPTION gettext("INTERNAL: option parser error\n")

#define	WMSG_NEWLINE_ADDED \
    gettext("missing NEWLINE added at end of input file %s\n")

extern void warn(const char *, ...);
extern void die(const char *, ...);
extern void usage(void);

extern void swap(void **, void **);

extern int bump_file_template();
extern char *get_file_template();
extern void set_file_template(char **);

extern void set_cleanup_chain(stream_t **);

extern void set_output_file(char *);
extern void set_output_guard(stream_t *);
extern void clear_output_guard(void);
extern void establish_output_guard(sort_t *);
extern void remove_output_guard();

extern void atexit_handler(void);

extern size_t available_memory(size_t);
extern void set_memory_ratio(sort_t *, int *, int *);

extern size_t strtomem(char *);
extern void *safe_realloc(void *, size_t);
extern void safe_free(void *);

extern void *xzmap(void *, size_t, int, int, off_t);
extern void hold_file_descriptor(void);
extern void release_file_descriptor(void);

extern void copy_line_rec(const line_rec_t *, line_rec_t *);
extern void trip_eof(FILE *f);

extern int cxwrite(int, char *, size_t);
extern int wxwrite(int, wchar_t *);

extern int xstreql(const char *, const char *);
extern int xstrneql(const char *, const char *, const size_t);
extern char *xstrnchr(const char *, const int, const size_t);
extern void xstrninv(char *, ssize_t, ssize_t);

extern int xwcsneql(const wchar_t *, const wchar_t *, const size_t);
extern wchar_t *xwsnchr(const wchar_t *, const wint_t, const size_t);
extern void xwcsninv(wchar_t *, ssize_t, ssize_t);

#ifdef _LITTLE_ENDIAN
extern void xwcsntomsb(wchar_t *, ssize_t);
#endif /* _LITTLE_ENDIAN */

extern wchar_t *xmemwchar(wchar_t *, wchar_t, ssize_t);

extern void xcp(char *, char *, off_t);
extern void xdump(FILE *, uchar_t *, size_t, int);

#ifdef DEBUG
#define	ASSERT(x) assert(x)
#else
#define	ASSERT(x)
#endif

#ifdef STATS
#define	__S(x) x
#else
#define	__S(x)
#endif

#ifdef	__cplusplus
}
#endif

#endif	/* _SORT_UTILITY_H */