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
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.
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]
23 * Copyright 1998-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "initialize.h"
33 * TEXT_DOMAIN should have been set by build environment.
35 #define TEXT_DOMAIN "SUNW_OST_OSCMD"
36 #endif /* TEXT_DOMAIN */
39 * /dev/zero, output file, stdin, stdout, and stderr
41 #define N_FILES_ALREADY_OPEN 5
43 static const char *filename_stdin
= "STDIN";
44 const char *filename_stdout
= "STDOUT";
46 static sigjmp_buf signal_jmp_buf
;
47 static volatile sig_atomic_t signal_delivered
;
52 if (sigsetjmp(signal_jmp_buf
, 1))
53 exit(127 + signal_delivered
);
57 sig_handler(int signo
)
59 signal_delivered
= signo
;
60 siglongjmp(signal_jmp_buf
, 1);
64 initialize_pre(sort_t
*S
)
67 * Initialize sort structure.
69 (void) memset(S
, 0, sizeof (sort_t
));
71 S
->m_stats
= safe_realloc(NULL
, sizeof (sort_statistics_t
));
72 __S(stats_init(S
->m_stats
));
74 S
->m_default_species
= ALPHA
;
77 * Simple localization issues.
79 (void) setlocale(LC_ALL
, "");
80 (void) textdomain(TEXT_DOMAIN
);
82 #ifndef DEBUG_FORCE_WIDE
83 S
->m_c_locale
= xstreql("C", setlocale(LC_COLLATE
, NULL
));
84 S
->m_single_byte_locale
= SGN(MB_CUR_MAX
== 1);
85 #else /* DEBUG_FORCE_WIDE */
87 S
->m_single_byte_locale
= 0;
88 #endif /* DEBUG_FORCE_WIDE */
91 * We use a constant seed so that our sorts on a given file are
96 if (atexit(atexit_handler
) < 0)
97 warn(gettext("atexit() handler installation failed"));
100 * Establish signal handlers and sufficient state for clean up.
102 if (signal(SIGTERM
, sig_handler
) == SIG_ERR
)
103 die(EMSG_SIGNAL
, "SIGTERM");
104 if (signal(SIGHUP
, sig_handler
) == SIG_ERR
)
105 die(EMSG_SIGNAL
, "SIGHUP");
106 if (signal(SIGPIPE
, sig_handler
) == SIG_ERR
)
107 die(EMSG_SIGNAL
, "SIGPIPE");
113 initialize_post(sort_t
*S
)
117 S
->m_memory_available
= available_memory(S
->m_memory_limit
);
119 set_file_template(&S
->m_tmpdir_template
);
122 * Initialize locale-specific ops vectors.
126 if (S
->m_single_byte_locale
) {
127 S
->m_compare_fn
= (cmp_fcn_t
)strcoll
;
128 S
->m_coll_convert
= field_convert
;
129 F
= S
->m_fields_head
;
132 switch (F
->f_species
) {
135 (FIELD_IGNORE_NONPRINTABLES
|
136 FIELD_DICTIONARY_ORDER
|
137 FIELD_FOLD_UPPERCASE
))
138 F
->f_convert
= field_convert_alpha
;
141 field_convert_alpha_simple
;
144 F
->f_convert
= field_convert_numeric
;
147 F
->f_convert
= field_convert_month
;
150 die(EMSG_UNKN_FIELD
, F
->f_species
);
156 S
->m_compare_fn
= (cmp_fcn_t
)wcscoll
;
157 S
->m_coll_convert
= field_convert_wide
;
159 F
= S
->m_fields_head
;
161 switch (F
->f_species
) {
163 F
->f_convert
= field_convert_alpha_wide
;
167 field_convert_numeric_wide
;
170 F
->f_convert
= field_convert_month_wide
;
173 die(EMSG_UNKN_FIELD
, F
->f_species
);
181 * Validate and obtain sizes, inodes for input streams.
183 stream_stat_chain(S
->m_input_streams
);
184 __S(stats_set_input_files(stream_count_chain(S
->m_input_streams
)));
189 establish_output_guard(S
);
192 * Ready stdin for usage as stream.
194 if (S
->m_input_from_stdin
) {
197 if (S
->m_single_byte_locale
) {
198 str
= stream_new(STREAM_SINGLE
| STREAM_NOTFILE
);
199 str
->s_element_size
= sizeof (char);
201 str
= stream_new(STREAM_WIDE
| STREAM_NOTFILE
);
202 str
->s_element_size
= sizeof (wchar_t);
204 str
->s_filename
= (char *)filename_stdin
;
205 stream_push_to_chain(&S
->m_input_streams
, str
);
206 __S(stats_incr_input_files());