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 #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include "initialize.h"
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #ifndef TEXT_DOMAIN
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate * TEXT_DOMAIN should have been set by build environment.
34*0Sstevel@tonic-gate */
35*0Sstevel@tonic-gate #define TEXT_DOMAIN "SUNW_OST_OSCMD"
36*0Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate * /dev/zero, output file, stdin, stdout, and stderr
40*0Sstevel@tonic-gate */
41*0Sstevel@tonic-gate #define N_FILES_ALREADY_OPEN 5
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate static const char *filename_stdin = "STDIN";
44*0Sstevel@tonic-gate const char *filename_stdout = "STDOUT";
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate static sigjmp_buf signal_jmp_buf;
47*0Sstevel@tonic-gate static volatile sig_atomic_t signal_delivered;
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate static void
set_signal_jmp(void)50*0Sstevel@tonic-gate set_signal_jmp(void)
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate if (sigsetjmp(signal_jmp_buf, 1))
53*0Sstevel@tonic-gate exit(127 + signal_delivered);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate static void
sig_handler(int signo)57*0Sstevel@tonic-gate sig_handler(int signo)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate signal_delivered = signo;
60*0Sstevel@tonic-gate siglongjmp(signal_jmp_buf, 1);
61*0Sstevel@tonic-gate }
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate void
initialize_pre(sort_t * S)64*0Sstevel@tonic-gate initialize_pre(sort_t *S)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate /*
67*0Sstevel@tonic-gate * Initialize sort structure.
68*0Sstevel@tonic-gate */
69*0Sstevel@tonic-gate (void) memset(S, 0, sizeof (sort_t));
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate S->m_stats = safe_realloc(NULL, sizeof (sort_statistics_t));
72*0Sstevel@tonic-gate __S(stats_init(S->m_stats));
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate S->m_default_species = ALPHA;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate * Simple localization issues.
78*0Sstevel@tonic-gate */
79*0Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
80*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate #ifndef DEBUG_FORCE_WIDE
83*0Sstevel@tonic-gate S->m_c_locale = xstreql("C", setlocale(LC_COLLATE, NULL));
84*0Sstevel@tonic-gate S->m_single_byte_locale = SGN(MB_CUR_MAX == 1);
85*0Sstevel@tonic-gate #else /* DEBUG_FORCE_WIDE */
86*0Sstevel@tonic-gate S->m_c_locale = 0;
87*0Sstevel@tonic-gate S->m_single_byte_locale = 0;
88*0Sstevel@tonic-gate #endif /* DEBUG_FORCE_WIDE */
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate * We use a constant seed so that our sorts on a given file are
92*0Sstevel@tonic-gate * reproducible.
93*0Sstevel@tonic-gate */
94*0Sstevel@tonic-gate srand(3459871433U);
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate if (atexit(atexit_handler) < 0)
97*0Sstevel@tonic-gate warn(gettext("atexit() handler installation failed"));
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate * Establish signal handlers and sufficient state for clean up.
101*0Sstevel@tonic-gate */
102*0Sstevel@tonic-gate if (signal(SIGTERM, sig_handler) == SIG_ERR)
103*0Sstevel@tonic-gate die(EMSG_SIGNAL, "SIGTERM");
104*0Sstevel@tonic-gate if (signal(SIGHUP, sig_handler) == SIG_ERR)
105*0Sstevel@tonic-gate die(EMSG_SIGNAL, "SIGHUP");
106*0Sstevel@tonic-gate if (signal(SIGPIPE, sig_handler) == SIG_ERR)
107*0Sstevel@tonic-gate die(EMSG_SIGNAL, "SIGPIPE");
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate set_signal_jmp();
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate void
initialize_post(sort_t * S)113*0Sstevel@tonic-gate initialize_post(sort_t *S)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate field_t *F;
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate S->m_memory_available = available_memory(S->m_memory_limit);
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate set_file_template(&S->m_tmpdir_template);
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate /*
122*0Sstevel@tonic-gate * Initialize locale-specific ops vectors.
123*0Sstevel@tonic-gate */
124*0Sstevel@tonic-gate field_initialize(S);
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate if (S->m_single_byte_locale) {
127*0Sstevel@tonic-gate S->m_compare_fn = (cmp_fcn_t)strcoll;
128*0Sstevel@tonic-gate S->m_coll_convert = field_convert;
129*0Sstevel@tonic-gate F = S->m_fields_head;
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate while (F != NULL) {
132*0Sstevel@tonic-gate switch (F->f_species) {
133*0Sstevel@tonic-gate case ALPHA:
134*0Sstevel@tonic-gate if (F->f_options &
135*0Sstevel@tonic-gate (FIELD_IGNORE_NONPRINTABLES |
136*0Sstevel@tonic-gate FIELD_DICTIONARY_ORDER |
137*0Sstevel@tonic-gate FIELD_FOLD_UPPERCASE))
138*0Sstevel@tonic-gate F->f_convert = field_convert_alpha;
139*0Sstevel@tonic-gate else
140*0Sstevel@tonic-gate F->f_convert =
141*0Sstevel@tonic-gate field_convert_alpha_simple;
142*0Sstevel@tonic-gate break;
143*0Sstevel@tonic-gate case NUMERIC:
144*0Sstevel@tonic-gate F->f_convert = field_convert_numeric;
145*0Sstevel@tonic-gate break;
146*0Sstevel@tonic-gate case MONTH:
147*0Sstevel@tonic-gate F->f_convert = field_convert_month;
148*0Sstevel@tonic-gate break;
149*0Sstevel@tonic-gate default:
150*0Sstevel@tonic-gate die(EMSG_UNKN_FIELD, F->f_species);
151*0Sstevel@tonic-gate break;
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate F = F->f_next;
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate } else {
156*0Sstevel@tonic-gate S->m_compare_fn = (cmp_fcn_t)wcscoll;
157*0Sstevel@tonic-gate S->m_coll_convert = field_convert_wide;
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate F = S->m_fields_head;
160*0Sstevel@tonic-gate while (F != NULL) {
161*0Sstevel@tonic-gate switch (F->f_species) {
162*0Sstevel@tonic-gate case ALPHA:
163*0Sstevel@tonic-gate F->f_convert = field_convert_alpha_wide;
164*0Sstevel@tonic-gate break;
165*0Sstevel@tonic-gate case NUMERIC:
166*0Sstevel@tonic-gate F->f_convert =
167*0Sstevel@tonic-gate field_convert_numeric_wide;
168*0Sstevel@tonic-gate break;
169*0Sstevel@tonic-gate case MONTH:
170*0Sstevel@tonic-gate F->f_convert = field_convert_month_wide;
171*0Sstevel@tonic-gate break;
172*0Sstevel@tonic-gate default:
173*0Sstevel@tonic-gate die(EMSG_UNKN_FIELD, F->f_species);
174*0Sstevel@tonic-gate break;
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate F = F->f_next;
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate /*
181*0Sstevel@tonic-gate * Validate and obtain sizes, inodes for input streams.
182*0Sstevel@tonic-gate */
183*0Sstevel@tonic-gate stream_stat_chain(S->m_input_streams);
184*0Sstevel@tonic-gate __S(stats_set_input_files(stream_count_chain(S->m_input_streams)));
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate /*
187*0Sstevel@tonic-gate * Output guard.
188*0Sstevel@tonic-gate */
189*0Sstevel@tonic-gate establish_output_guard(S);
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate * Ready stdin for usage as stream.
193*0Sstevel@tonic-gate */
194*0Sstevel@tonic-gate if (S->m_input_from_stdin) {
195*0Sstevel@tonic-gate stream_t *str;
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate if (S->m_single_byte_locale) {
198*0Sstevel@tonic-gate str = stream_new(STREAM_SINGLE | STREAM_NOTFILE);
199*0Sstevel@tonic-gate str->s_element_size = sizeof (char);
200*0Sstevel@tonic-gate } else {
201*0Sstevel@tonic-gate str = stream_new(STREAM_WIDE | STREAM_NOTFILE);
202*0Sstevel@tonic-gate str->s_element_size = sizeof (wchar_t);
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate str->s_filename = (char *)filename_stdin;
205*0Sstevel@tonic-gate stream_push_to_chain(&S->m_input_streams, str);
206*0Sstevel@tonic-gate __S(stats_incr_input_files());
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate }
209