File physmem.c added (mode: 100644) (index 0000000..0fcb5e9) |
|
1 |
|
/* Calculate the size of physical memory. |
|
2 |
|
|
|
3 |
|
Copyright (C) 2000, 2001, 2003, 2005, 2006 Free Software |
|
4 |
|
Foundation, Inc. |
|
5 |
|
|
|
6 |
|
This program is free software: you can redistribute it and/or modify |
|
7 |
|
it under the terms of the GNU General Public License as published by |
|
8 |
|
the Free Software Foundation; either version 3 of the License, or |
|
9 |
|
(at your option) any later version. |
|
10 |
|
|
|
11 |
|
This program is distributed in the hope that it will be useful, |
|
12 |
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
|
GNU General Public License for more details. |
|
15 |
|
|
|
16 |
|
You should have received a copy of the GNU General Public License |
|
17 |
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
18 |
|
|
|
19 |
|
/* Written by Paul Eggert. */ |
|
20 |
|
|
|
21 |
|
#include <unistd.h> |
|
22 |
|
|
|
23 |
|
#if HAVE_SYS_PSTAT_H |
|
24 |
|
# include <sys/pstat.h> |
|
25 |
|
#endif |
|
26 |
|
|
|
27 |
|
#if HAVE_SYS_SYSMP_H |
|
28 |
|
# include <sys/sysmp.h> |
|
29 |
|
#endif |
|
30 |
|
|
|
31 |
|
#if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H |
|
32 |
|
# include <sys/sysinfo.h> |
|
33 |
|
# include <machine/hal_sysinfo.h> |
|
34 |
|
#endif |
|
35 |
|
|
|
36 |
|
#if HAVE_SYS_TABLE_H |
|
37 |
|
# include <sys/table.h> |
|
38 |
|
#endif |
|
39 |
|
|
|
40 |
|
#include <sys/types.h> |
|
41 |
|
|
|
42 |
|
#if HAVE_SYS_PARAM_H |
|
43 |
|
# include <sys/param.h> |
|
44 |
|
#endif |
|
45 |
|
|
|
46 |
|
#if HAVE_SYS_SYSCTL_H |
|
47 |
|
# include <sys/sysctl.h> |
|
48 |
|
#endif |
|
49 |
|
|
|
50 |
|
#if HAVE_SYS_SYSTEMCFG_H |
|
51 |
|
# include <sys/systemcfg.h> |
|
52 |
|
#endif |
|
53 |
|
|
|
54 |
|
#ifdef _WIN32 |
|
55 |
|
# define WIN32_LEAN_AND_MEAN |
|
56 |
|
# include <windows.h> |
|
57 |
|
/* MEMORYSTATUSEX is missing from older windows headers, so define |
|
58 |
|
a local replacement. */ |
|
59 |
|
typedef struct |
|
60 |
|
{ |
|
61 |
|
DWORD dwLength; |
|
62 |
|
DWORD dwMemoryLoad; |
|
63 |
|
DWORDLONG ullTotalPhys; |
|
64 |
|
DWORDLONG ullAvailPhys; |
|
65 |
|
DWORDLONG ullTotalPageFile; |
|
66 |
|
DWORDLONG ullAvailPageFile; |
|
67 |
|
DWORDLONG ullTotalVirtual; |
|
68 |
|
DWORDLONG ullAvailVirtual; |
|
69 |
|
DWORDLONG ullAvailExtendedVirtual; |
|
70 |
|
} lMEMORYSTATUSEX; |
|
71 |
|
typedef WINBOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*); |
|
72 |
|
#endif |
|
73 |
|
|
|
74 |
|
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0])) |
|
75 |
|
|
|
76 |
|
/* Return the total amount of physical memory. */ |
|
77 |
|
double |
|
78 |
|
physmem_total (void) |
|
79 |
|
{ |
|
80 |
|
#if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE |
|
81 |
|
{ /* This works on linux-gnu, solaris2 and cygwin. */ |
|
82 |
|
double pages = sysconf (_SC_PHYS_PAGES); |
|
83 |
|
double pagesize = sysconf (_SC_PAGESIZE); |
|
84 |
|
if (0 <= pages && 0 <= pagesize) |
|
85 |
|
return pages * pagesize; |
|
86 |
|
} |
|
87 |
|
#endif |
|
88 |
|
|
|
89 |
|
#if HAVE_PSTAT_GETSTATIC |
|
90 |
|
{ /* This works on hpux11. */ |
|
91 |
|
struct pst_static pss; |
|
92 |
|
if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)) |
|
93 |
|
{ |
|
94 |
|
double pages = pss.physical_memory; |
|
95 |
|
double pagesize = pss.page_size; |
|
96 |
|
if (0 <= pages && 0 <= pagesize) |
|
97 |
|
return pages * pagesize; |
|
98 |
|
} |
|
99 |
|
} |
|
100 |
|
#endif |
|
101 |
|
|
|
102 |
|
#if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE |
|
103 |
|
{ /* This works on irix6. */ |
|
104 |
|
struct rminfo realmem; |
|
105 |
|
if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) |
|
106 |
|
{ |
|
107 |
|
double pagesize = sysconf (_SC_PAGESIZE); |
|
108 |
|
double pages = realmem.physmem; |
|
109 |
|
if (0 <= pages && 0 <= pagesize) |
|
110 |
|
return pages * pagesize; |
|
111 |
|
} |
|
112 |
|
} |
|
113 |
|
#endif |
|
114 |
|
|
|
115 |
|
#if HAVE_GETSYSINFO && defined GSI_PHYSMEM |
|
116 |
|
{ /* This works on Tru64 UNIX V4/5. */ |
|
117 |
|
int physmem; |
|
118 |
|
|
|
119 |
|
if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem), |
|
120 |
|
NULL, NULL, NULL) == 1) |
|
121 |
|
{ |
|
122 |
|
double kbytes = physmem; |
|
123 |
|
|
|
124 |
|
if (0 <= kbytes) |
|
125 |
|
return kbytes * 1024.0; |
|
126 |
|
} |
|
127 |
|
} |
|
128 |
|
#endif |
|
129 |
|
|
|
130 |
|
#if HAVE_SYSCTL && defined HW_PHYSMEM |
|
131 |
|
{ /* This works on *bsd and darwin. */ |
|
132 |
|
unsigned int physmem; |
|
133 |
|
size_t len = sizeof physmem; |
|
134 |
|
static int mib[2] = { CTL_HW, HW_PHYSMEM }; |
|
135 |
|
|
|
136 |
|
if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0 |
|
137 |
|
&& len == sizeof (physmem)) |
|
138 |
|
return (double) physmem; |
|
139 |
|
} |
|
140 |
|
#endif |
|
141 |
|
|
|
142 |
|
#if HAVE__SYSTEM_CONFIGURATION |
|
143 |
|
/* This works on AIX. */ |
|
144 |
|
return _system_configuration.physmem; |
|
145 |
|
#endif |
|
146 |
|
|
|
147 |
|
#if defined _WIN32 |
|
148 |
|
{ /* this works on windows */ |
|
149 |
|
PFN_MS_EX pfnex; |
|
150 |
|
HMODULE h = GetModuleHandle ("kernel32.dll"); |
|
151 |
|
|
|
152 |
|
if (!h) |
|
153 |
|
return 0.0; |
|
154 |
|
|
|
155 |
|
/* Use GlobalMemoryStatusEx if available. */ |
|
156 |
|
if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) |
|
157 |
|
{ |
|
158 |
|
lMEMORYSTATUSEX lms_ex; |
|
159 |
|
lms_ex.dwLength = sizeof lms_ex; |
|
160 |
|
if (!pfnex (&lms_ex)) |
|
161 |
|
return 0.0; |
|
162 |
|
return (double) lms_ex.ullTotalPhys; |
|
163 |
|
} |
|
164 |
|
|
|
165 |
|
/* Fall back to GlobalMemoryStatus which is always available. |
|
166 |
|
but returns wrong results for physical memory > 4GB. */ |
|
167 |
|
else |
|
168 |
|
{ |
|
169 |
|
MEMORYSTATUS ms; |
|
170 |
|
GlobalMemoryStatus (&ms); |
|
171 |
|
return (double) ms.dwTotalPhys; |
|
172 |
|
} |
|
173 |
|
} |
|
174 |
|
#endif |
|
175 |
|
|
|
176 |
|
/* Guess 64 MB. It's probably an older host, so guess small. */ |
|
177 |
|
return 64 * 1024 * 1024; |
|
178 |
|
} |
|
179 |
|
|
|
180 |
|
/* Return the amount of physical memory available. */ |
|
181 |
|
double |
|
182 |
|
physmem_available (void) |
|
183 |
|
{ |
|
184 |
|
#if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE |
|
185 |
|
{ /* This works on linux-gnu, solaris2 and cygwin. */ |
|
186 |
|
double pages = sysconf (_SC_AVPHYS_PAGES); |
|
187 |
|
double pagesize = sysconf (_SC_PAGESIZE); |
|
188 |
|
if (0 <= pages && 0 <= pagesize) |
|
189 |
|
return pages * pagesize; |
|
190 |
|
} |
|
191 |
|
#endif |
|
192 |
|
|
|
193 |
|
#if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC |
|
194 |
|
{ /* This works on hpux11. */ |
|
195 |
|
struct pst_static pss; |
|
196 |
|
struct pst_dynamic psd; |
|
197 |
|
if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0) |
|
198 |
|
&& 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)) |
|
199 |
|
{ |
|
200 |
|
double pages = psd.psd_free; |
|
201 |
|
double pagesize = pss.page_size; |
|
202 |
|
if (0 <= pages && 0 <= pagesize) |
|
203 |
|
return pages * pagesize; |
|
204 |
|
} |
|
205 |
|
} |
|
206 |
|
#endif |
|
207 |
|
|
|
208 |
|
#if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE |
|
209 |
|
{ /* This works on irix6. */ |
|
210 |
|
struct rminfo realmem; |
|
211 |
|
if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) |
|
212 |
|
{ |
|
213 |
|
double pagesize = sysconf (_SC_PAGESIZE); |
|
214 |
|
double pages = realmem.availrmem; |
|
215 |
|
if (0 <= pages && 0 <= pagesize) |
|
216 |
|
return pages * pagesize; |
|
217 |
|
} |
|
218 |
|
} |
|
219 |
|
#endif |
|
220 |
|
|
|
221 |
|
#if HAVE_TABLE && defined TBL_VMSTATS |
|
222 |
|
{ /* This works on Tru64 UNIX V4/5. */ |
|
223 |
|
struct tbl_vmstats vmstats; |
|
224 |
|
|
|
225 |
|
if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1) |
|
226 |
|
{ |
|
227 |
|
double pages = vmstats.free_count; |
|
228 |
|
double pagesize = vmstats.pagesize; |
|
229 |
|
|
|
230 |
|
if (0 <= pages && 0 <= pagesize) |
|
231 |
|
return pages * pagesize; |
|
232 |
|
} |
|
233 |
|
} |
|
234 |
|
#endif |
|
235 |
|
|
|
236 |
|
#if HAVE_SYSCTL && defined HW_USERMEM |
|
237 |
|
{ /* This works on *bsd and darwin. */ |
|
238 |
|
unsigned int usermem; |
|
239 |
|
size_t len = sizeof usermem; |
|
240 |
|
static int mib[2] = { CTL_HW, HW_USERMEM }; |
|
241 |
|
|
|
242 |
|
if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0 |
|
243 |
|
&& len == sizeof (usermem)) |
|
244 |
|
return (double) usermem; |
|
245 |
|
} |
|
246 |
|
#endif |
|
247 |
|
|
|
248 |
|
#if defined _WIN32 |
|
249 |
|
{ /* this works on windows */ |
|
250 |
|
PFN_MS_EX pfnex; |
|
251 |
|
HMODULE h = GetModuleHandle ("kernel32.dll"); |
|
252 |
|
|
|
253 |
|
if (!h) |
|
254 |
|
return 0.0; |
|
255 |
|
|
|
256 |
|
/* Use GlobalMemoryStatusEx if available. */ |
|
257 |
|
if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) |
|
258 |
|
{ |
|
259 |
|
lMEMORYSTATUSEX lms_ex; |
|
260 |
|
lms_ex.dwLength = sizeof lms_ex; |
|
261 |
|
if (!pfnex (&lms_ex)) |
|
262 |
|
return 0.0; |
|
263 |
|
return (double) lms_ex.ullAvailPhys; |
|
264 |
|
} |
|
265 |
|
|
|
266 |
|
/* Fall back to GlobalMemoryStatus which is always available. |
|
267 |
|
but returns wrong results for physical memory > 4GB */ |
|
268 |
|
else |
|
269 |
|
{ |
|
270 |
|
MEMORYSTATUS ms; |
|
271 |
|
GlobalMemoryStatus (&ms); |
|
272 |
|
return (double) ms.dwAvailPhys; |
|
273 |
|
} |
|
274 |
|
} |
|
275 |
|
#endif |
|
276 |
|
|
|
277 |
|
/* Guess 25% of physical memory. */ |
|
278 |
|
return physmem_total () / 4; |
|
279 |
|
} |
|
280 |
|
|
|
281 |
|
|
|
282 |
|
#if DEBUG |
|
283 |
|
|
|
284 |
|
# include <stdio.h> |
|
285 |
|
# include <stdlib.h> |
|
286 |
|
|
|
287 |
|
int |
|
288 |
|
main (void) |
|
289 |
|
{ |
|
290 |
|
printf ("%12.f %12.f\n", physmem_total (), physmem_available ()); |
|
291 |
|
exit (0); |
|
292 |
|
} |
|
293 |
|
|
|
294 |
|
#endif /* DEBUG */ |
|
295 |
|
|
|
296 |
|
/* |
|
297 |
|
Local Variables: |
|
298 |
|
compile-command: "gcc -DDEBUG -g -O -Wall -W physmem.c" |
|
299 |
|
End: |
|
300 |
|
*/ |
File sponge.c changed (mode: 100644) (index 3fd3ab4..ae32718) |
25 |
25 |
#include <unistd.h> |
#include <unistd.h> |
26 |
26 |
#include <sys/types.h> |
#include <sys/types.h> |
27 |
27 |
#include <sys/stat.h> |
#include <sys/stat.h> |
|
28 |
|
/* MAX() */ |
|
29 |
|
#include <sys/param.h> |
28 |
30 |
#include <fcntl.h> |
#include <fcntl.h> |
29 |
31 |
#include <errno.h> |
#include <errno.h> |
30 |
32 |
#include <string.h> |
#include <string.h> |
|
33 |
|
#include <sys/resource.h> |
|
34 |
|
/* SIZE_MAX */ |
|
35 |
|
#include <stdint.h> |
|
36 |
|
#include <signal.h> |
|
37 |
|
|
|
38 |
|
#include "physmem.c" |
|
39 |
|
|
|
40 |
|
#define MIN_SPONGE_SIZE sizeof(8192) |
|
41 |
|
#define BUFF_SIZE 8192 |
|
42 |
|
#define DEFAULT_TMP_NAME "/tmp/sponge.XXXXXX" |
|
43 |
|
char tmpname[] = DEFAULT_TMP_NAME; |
31 |
44 |
|
|
32 |
45 |
void usage() { |
void usage() { |
33 |
46 |
printf("sponge <file>: suck in all input from stdin and write it to <file>\n"); |
printf("sponge <file>: suck in all input from stdin and write it to <file>\n"); |
34 |
47 |
exit(0); |
exit(0); |
35 |
48 |
} |
} |
36 |
49 |
|
|
|
50 |
|
/* all the signal stuff copied from gnu sort */ |
|
51 |
|
|
|
52 |
|
/* The set of signals that are caught. */ |
|
53 |
|
static sigset_t caught_signals; |
|
54 |
|
|
|
55 |
|
/* Critical section status. */ |
|
56 |
|
struct cs_status |
|
57 |
|
{ |
|
58 |
|
int valid; // was bool |
|
59 |
|
sigset_t sigs; |
|
60 |
|
}; |
|
61 |
|
|
|
62 |
|
/* Enter a critical section. */ |
|
63 |
|
static struct cs_status |
|
64 |
|
cs_enter (void) |
|
65 |
|
{ |
|
66 |
|
struct cs_status status; |
|
67 |
|
status.valid = (sigprocmask (SIG_BLOCK, &caught_signals, &status.sigs) == 0); |
|
68 |
|
return status; |
|
69 |
|
} |
|
70 |
|
|
|
71 |
|
/* Leave a critical section. */ |
|
72 |
|
static void |
|
73 |
|
cs_leave (struct cs_status status) |
|
74 |
|
{ |
|
75 |
|
if (status.valid) |
|
76 |
|
{ |
|
77 |
|
/* Ignore failure when restoring the signal mask. */ |
|
78 |
|
sigprocmask (SIG_SETMASK, &status.sigs, NULL); |
|
79 |
|
} |
|
80 |
|
} |
|
81 |
|
|
|
82 |
|
|
|
83 |
|
static void cleanup() { |
|
84 |
|
unlink(tmpname); |
|
85 |
|
} |
|
86 |
|
|
|
87 |
|
static void |
|
88 |
|
onexit_cleanup (void) |
|
89 |
|
{ |
|
90 |
|
struct cs_status cs = cs_enter (); |
|
91 |
|
cleanup (); |
|
92 |
|
cs_leave (cs); |
|
93 |
|
} |
|
94 |
|
|
|
95 |
|
static void |
|
96 |
|
sighandler (int sig) |
|
97 |
|
{ |
|
98 |
|
if (! SA_NOCLDSTOP) |
|
99 |
|
signal (sig, SIG_IGN); |
|
100 |
|
|
|
101 |
|
cleanup (); |
|
102 |
|
|
|
103 |
|
signal (sig, SIG_DFL); |
|
104 |
|
raise (sig); |
|
105 |
|
} |
|
106 |
|
|
|
107 |
|
|
|
108 |
|
/* taken from coreutils sort */ |
|
109 |
|
static size_t |
|
110 |
|
default_sponge_size (void) |
|
111 |
|
{ |
|
112 |
|
/* Let MEM be available memory or 1/8 of total memory, whichever |
|
113 |
|
is greater. */ |
|
114 |
|
double avail = physmem_available (); |
|
115 |
|
double total = physmem_total (); |
|
116 |
|
double mem = MAX (avail, total / 8); |
|
117 |
|
struct rlimit rlimit; |
|
118 |
|
|
|
119 |
|
/* Let SIZE be MEM, but no more than the maximum object size or |
|
120 |
|
system resource limits. Avoid the MIN macro here, as it is not |
|
121 |
|
quite right when only one argument is floating point. Don't |
|
122 |
|
bother to check for values like RLIM_INFINITY since in practice |
|
123 |
|
they are not much less than SIZE_MAX. */ |
|
124 |
|
size_t size = SIZE_MAX; |
|
125 |
|
if (mem < size) |
|
126 |
|
size = mem; |
|
127 |
|
if (getrlimit (RLIMIT_DATA, &rlimit) == 0 && rlimit.rlim_cur < size) |
|
128 |
|
size = rlimit.rlim_cur; |
|
129 |
|
#ifdef RLIMIT_AS |
|
130 |
|
if (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur < size) |
|
131 |
|
size = rlimit.rlim_cur; |
|
132 |
|
#endif |
|
133 |
|
|
|
134 |
|
/* Leave a large safety margin for the above limits, as failure can |
|
135 |
|
occur when they are exceeded. */ |
|
136 |
|
size /= 2; |
|
137 |
|
|
|
138 |
|
#ifdef RLIMIT_RSS |
|
139 |
|
/* Leave a 1/16 margin for RSS to leave room for code, stack, etc. |
|
140 |
|
Exceeding RSS is not fatal, but can be quite slow. */ |
|
141 |
|
if (getrlimit (RLIMIT_RSS, &rlimit) == 0 && rlimit.rlim_cur / 16 * 15 < size) |
|
142 |
|
size = rlimit.rlim_cur / 16 * 15; |
|
143 |
|
#endif |
|
144 |
|
|
|
145 |
|
/* Use no less than the minimum. */ |
|
146 |
|
return MAX (size, MIN_SPONGE_SIZE); |
|
147 |
|
} |
|
148 |
|
|
37 |
149 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
38 |
|
char *buf, *bufstart; |
|
39 |
|
size_t bufsize = 8192; |
|
|
150 |
|
char *buf, *bufstart, *outname = NULL; |
|
151 |
|
size_t bufsize = BUFF_SIZE; |
40 |
152 |
size_t bufused = 0; |
size_t bufused = 0; |
41 |
153 |
ssize_t i = 0; |
ssize_t i = 0; |
42 |
|
FILE *outf; |
|
43 |
|
|
|
|
154 |
|
FILE *tmpfile = 0; |
44 |
155 |
if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) { |
if (argc > 2 || (argc == 2 && strcmp(argv[1], "-h") == 0)) { |
45 |
156 |
usage(); |
usage(); |
46 |
157 |
} |
} |
47 |
|
|
|
48 |
158 |
bufstart = buf = malloc(bufsize); |
bufstart = buf = malloc(bufsize); |
49 |
159 |
if (!buf) { |
if (!buf) { |
50 |
|
perror("malloc"); |
|
|
160 |
|
perror("failed to allocate memory"); |
51 |
161 |
exit(1); |
exit(1); |
52 |
162 |
} |
} |
53 |
163 |
|
|
|
164 |
|
{ |
|
165 |
|
static int const sig[] = |
|
166 |
|
{ |
|
167 |
|
/* The usual suspects. */ |
|
168 |
|
SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM, |
|
169 |
|
#ifdef SIGPOLL |
|
170 |
|
SIGPOLL, |
|
171 |
|
#endif |
|
172 |
|
#ifdef SIGPROF |
|
173 |
|
SIGPROF, |
|
174 |
|
#endif |
|
175 |
|
#ifdef SIGVTALRM |
|
176 |
|
SIGVTALRM, |
|
177 |
|
#endif |
|
178 |
|
#ifdef SIGXCPU |
|
179 |
|
SIGXCPU, |
|
180 |
|
#endif |
|
181 |
|
#ifdef SIGXFSZ |
|
182 |
|
SIGXFSZ, |
|
183 |
|
#endif |
|
184 |
|
}; |
|
185 |
|
enum { nsigs = sizeof sig / sizeof sig[0] }; |
|
186 |
|
|
|
187 |
|
#if SA_NOCLDSTOP |
|
188 |
|
struct sigaction act; |
|
189 |
|
|
|
190 |
|
sigemptyset (&caught_signals); |
|
191 |
|
for (i = 0; i < nsigs; i++) |
|
192 |
|
{ |
|
193 |
|
sigaction (sig[i], NULL, &act); |
|
194 |
|
if (act.sa_handler != SIG_IGN) |
|
195 |
|
sigaddset (&caught_signals, sig[i]); |
|
196 |
|
} |
|
197 |
|
|
|
198 |
|
act.sa_handler = sighandler; |
|
199 |
|
act.sa_mask = caught_signals; |
|
200 |
|
act.sa_flags = 0; |
|
201 |
|
|
|
202 |
|
for (i = 0; i < nsigs; i++) |
|
203 |
|
if (sigismember (&caught_signals, sig[i])) |
|
204 |
|
sigaction (sig[i], &act, NULL); |
|
205 |
|
#else |
|
206 |
|
for (i = 0; i < nsigs; i++) |
|
207 |
|
if (signal (sig[i], SIG_IGN) != SIG_IGN) |
|
208 |
|
{ |
|
209 |
|
signal (sig[i], sighandler); |
|
210 |
|
siginterrupt (sig[i], 1); |
|
211 |
|
} |
|
212 |
|
#endif |
|
213 |
|
} |
|
214 |
|
|
|
215 |
|
|
|
216 |
|
size_t mem_available = default_sponge_size(); |
54 |
217 |
while ((i = read(0, buf, bufsize - bufused)) > 0) { |
while ((i = read(0, buf, bufsize - bufused)) > 0) { |
55 |
218 |
bufused = bufused+i; |
bufused = bufused+i; |
56 |
219 |
if (bufused == bufsize) { |
if (bufused == bufsize) { |
|
220 |
|
if(bufsize >= mem_available) { |
|
221 |
|
if(!tmpfile) { |
|
222 |
|
umask(077); |
|
223 |
|
struct cs_status cs = cs_enter (); |
|
224 |
|
int tmpfd = mkstemp(tmpname); |
|
225 |
|
atexit(onexit_cleanup); // if solaris on_exit(onexit_cleanup, 0); |
|
226 |
|
cs_leave (cs); |
|
227 |
|
if(tmpfd < 0) { |
|
228 |
|
perror("mkstemp failed to open a temporary file"); |
|
229 |
|
exit(1); |
|
230 |
|
} |
|
231 |
|
tmpfile = fdopen(tmpfd, "w+"); |
|
232 |
|
} |
|
233 |
|
if(fwrite(bufstart, bufsize, 1, tmpfile) < 1) { |
|
234 |
|
perror("writing to tempory file failed"); |
|
235 |
|
fclose(tmpfile); |
|
236 |
|
exit(1); |
|
237 |
|
} |
|
238 |
|
bufused = 0; |
|
239 |
|
} else { |
57 |
240 |
bufsize *= 2; |
bufsize *= 2; |
58 |
241 |
bufstart = realloc(bufstart, bufsize); |
bufstart = realloc(bufstart, bufsize); |
59 |
242 |
if (!bufstart) { |
if (!bufstart) { |
60 |
|
perror("realloc"); |
|
|
243 |
|
perror("failed to realloc memory"); |
61 |
244 |
exit(1); |
exit(1); |
62 |
245 |
} |
} |
63 |
246 |
} |
} |
|
247 |
|
} |
64 |
248 |
buf = bufstart + bufused; |
buf = bufstart + bufused; |
65 |
249 |
} |
} |
66 |
|
if (i == -1) { |
|
67 |
|
perror("read"); |
|
|
250 |
|
if (i < 0) { |
|
251 |
|
perror("failed to read from stdin"); |
68 |
252 |
exit(1); |
exit(1); |
69 |
253 |
} |
} |
70 |
|
|
|
71 |
254 |
if (argc == 2) { |
if (argc == 2) { |
72 |
|
outf = fopen(argv[1], "w"); |
|
73 |
|
if (! outf) { |
|
74 |
|
fprintf(stderr, "Can't open %s: %s\n", argv[1], strerror(errno)); |
|
|
255 |
|
outname = argv[1]; |
|
256 |
|
} |
|
257 |
|
if(tmpfile) { |
|
258 |
|
if(fwrite(bufstart, bufused, 1, tmpfile) < 1) { |
|
259 |
|
perror("write tmpfile"); |
|
260 |
|
fclose(tmpfile); |
|
261 |
|
exit(1); |
|
262 |
|
} |
|
263 |
|
if(outname) { |
|
264 |
|
fclose(tmpfile); |
|
265 |
|
if(rename(tmpname, outname)) { |
|
266 |
|
perror("error renaming temporary file to output file"); |
75 |
267 |
exit(1); |
exit(1); |
76 |
268 |
} |
} |
77 |
269 |
} |
} |
78 |
270 |
else { |
else { |
79 |
|
outf = stdout; |
|
|
271 |
|
if(fseek(tmpfile, 0, SEEK_SET)) { |
|
272 |
|
perror("could to seek to start of temporary file"); |
|
273 |
|
fclose(tmpfile); |
|
274 |
|
exit(1); |
80 |
275 |
} |
} |
81 |
|
|
|
82 |
|
if (fwrite(bufstart, bufused, 1, outf) < 1) { |
|
83 |
|
perror("fwrite"); |
|
|
276 |
|
while (fread( buf, BUFF_SIZE, 1, tmpfile) < 1) { |
|
277 |
|
if(fwrite(buf, BUFF_SIZE, 1, stdout) < 1) { |
|
278 |
|
perror("error writing out merged file"); |
84 |
279 |
exit(1); |
exit(1); |
85 |
280 |
} |
} |
86 |
|
|
|
87 |
|
if (fclose(outf) != 0) { |
|
88 |
|
perror("fclose"); |
|
|
281 |
|
} |
|
282 |
|
fclose(tmpfile); |
|
283 |
|
unlink(tmpname); |
|
284 |
|
} |
|
285 |
|
} |
|
286 |
|
else { |
|
287 |
|
if(outname) { |
|
288 |
|
FILE *outfd = fopen(outname, "w"); |
|
289 |
|
if(outfd < 0) { |
|
290 |
|
perror("error opening output file"); |
89 |
291 |
exit(1); |
exit(1); |
90 |
292 |
} |
} |
91 |
|
|
|
|
293 |
|
if(fwrite(bufstart, bufused, 1, outfd) < 1) { |
|
294 |
|
perror("error writing out merged file"); |
|
295 |
|
exit(1); |
|
296 |
|
} |
|
297 |
|
} |
|
298 |
|
else if(fwrite(bufstart, bufused, 1, stdout) < 1) { |
|
299 |
|
perror("error writing out merged file"); |
|
300 |
|
exit(1); |
|
301 |
|
} |
|
302 |
|
} |
92 |
303 |
return 0; |
return 0; |
93 |
304 |
} |
} |