-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutil.c
337 lines (290 loc) · 6.5 KB
/
util.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* util.c: miscellaneous useful functions for FLASH
*/
/*
* Copyright (C) 2012 Tanja Magoc
* Copyright (C) 2012, 2013, 2014 Eric Biggers
*
* This file is part of FLASH, a fast tool to merge overlapping paired-end
* reads.
*
* FLASH is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* FLASH is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with FLASH; if not, see http://www.gnu.org/licenses/.
*/
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "util.h"
#ifdef __WIN32__
/* Get the pthread mutex declarations as a replacement for flockfile() and
* funlockfile(). */
# include <pthread.h>
/* Get the GetSystemInfo() declaration as replacement for
* sysconf(_SC_NPROCESSORS_ONLN). */
# include <windows.h>
#endif
#ifdef __WIN32__
static pthread_mutex_t infofile_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t stderr_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
/* File to which to write informational messages. */
FILE *infofile;
static void
lock_infofile(void)
{
#ifdef __WIN32__
pthread_mutex_lock(&infofile_lock);
#else
flockfile(infofile);
#endif
}
static void
lock_stderr(void)
{
#ifdef __WIN32__
pthread_mutex_lock(&stderr_lock);
#else
flockfile(stderr);
#endif
}
static void
unlock_infofile(void)
{
#ifdef __WIN32__
pthread_mutex_unlock(&infofile_lock);
#else
funlockfile(infofile);
#endif
}
static void
unlock_stderr(void)
{
#ifdef __WIN32__
pthread_mutex_unlock(&stderr_lock);
#else
funlockfile(stderr);
#endif
}
#define PROGRAM_TAG "[FLASH] "
static void __noreturn
fatal(void)
{
info("FLASH did not complete successfully; exiting with failure status (1)");
exit(1);
}
/* Prints an error message and exits the program with failure status. */
void
fatal_error(const char *msg, ...)
{
va_list va;
lock_stderr();
va_start(va, msg);
fflush(stdout);
fputs(PROGRAM_TAG "ERROR: ", stderr);
vfprintf(stderr, msg, va);
putc('\n', stderr);
va_end(va);
unlock_stderr();
fatal();
}
/* Prints an error message, with added text for errno if it is nonzero, and
* exits the program with failure status. */
void
fatal_error_with_errno(const char *msg, ...)
{
va_list va;
lock_stderr();
va_start(va, msg);
fflush(stdout);
fputs(PROGRAM_TAG "ERROR: ", stderr);
vfprintf(stderr, msg, va);
if (errno)
fprintf(stderr, ": %s\n", strerror(errno));
else
putc('\n', stderr);
va_end(va);
unlock_stderr();
fatal();
}
unsigned long warning_count = 0;
/* Prints a warning message. */
void
warning(const char *msg, ...)
{
va_list va;
lock_stderr();
warning_count++;
va_start(va, msg);
fputs(PROGRAM_TAG "WARNING: ", stderr);
vfprintf(stderr, msg, va);
putc('\n', stderr);
va_end(va);
unlock_stderr();
}
/* Prints an informational message. */
void
info(const char *msg, ...)
{
va_list va;
lock_infofile();
va_start(va, msg);
fputs(PROGRAM_TAG, infofile);
vfprintf(infofile, msg, va);
putc('\n', infofile);
fflush(infofile);
va_end(va);
unlock_infofile();
}
/* Like malloc(), but aborts if out of memory, and always returns non-NULL, even
* if 0 bytes were requested. */
void *
xmalloc(size_t size)
{
void *p = malloc(size);
if (p)
return p;
if (!size) {
p = malloc(1);
if (p)
return p;
}
fatal_error("Out of memory: tried to allocate %zu bytes", size);
}
void *
xzalloc(size_t size)
{
return memset(xmalloc(size), 0, size);
}
/* Like strdup(), but aborts if out of memory. */
char *
xstrdup(const char *str)
{
return strcpy(xmalloc(strlen(str) + 1), str);
}
/* Like realloc(), but aborts if out of memory, and always returns non-NULL,
* even if 0 bytes were requested. */
void *
xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
if (p)
return p;
if (!size) {
p = malloc(1);
if (p)
return p;
}
fatal_error("Out of memory: tried to reallocate %zu bytes", size);
}
#ifndef NDEBUG
void
xfree(void *p, size_t size)
{
if (p) {
memset(p, 0xfd, size);
free(p);
}
}
#endif
/* Returns the number of available processors if it can be determined.
* Otherwise returns 1. */
unsigned
get_default_num_threads(void)
{
#ifdef __WIN32__
SYSTEM_INFO si;
GetSystemInfo(&si);
if (si.dwNumberOfProcessors > 0 && si.dwNumberOfProcessors <= UINT_MAX)
return si.dwNumberOfProcessors;
#else
long nproc = sysconf(_SC_NPROCESSORS_ONLN);
if (nproc > 0 && nproc <= UINT_MAX)
return nproc;
#endif
warning("Could not determine number of processors! Assuming 1");
return 1;
}
/* Returns true if the specified character is a path separator on the current
* platform. */
static bool
is_path_separator(char c)
{
#ifdef __WIN32__
return (c == '/') || (c == '\\');
#else
return (c == '/');
#endif
}
/* mkdir() on Windows doesn't take a mode argument. */
#ifdef __WIN32__
# define mkdir(path, mode) mkdir(path)
#endif
/* Like `mkdir -p': create the specified directory, and all parent directories,
* as needed, failing only if a needed directory cannot be created. */
void
mkdir_p(const char *dir)
{
size_t len = strlen(dir);
char dir_copy[len + 1];
char *p = dir_copy;
/* Copy the directory name to the @dir_copy array, squashing together
* consecutive path separators. */
for (size_t i = 0; i < len; i++) {
if (!is_path_separator(dir[i]) ||
!is_path_separator(dir[i + 1]))
*p++ = dir[i];
}
*p = '\0';
p = dir_copy;
do {
if (p != dir_copy && (*p == '\0' || is_path_separator(*p))) {
char orig_char = *p;
*p = '\0';
if (mkdir(dir_copy, 0755) != 0 && errno != EEXIST) {
fatal_error_with_errno("Failed to create "
"directory \"%s\"",
dir_copy);
}
*p = orig_char;
}
} while (*p++ != '\0');
}
pthread_t
create_thread(void *(*proc)(void *), void *params)
{
int result;
pthread_t t;
result = pthread_create(&t, NULL, proc, params);
if (result) {
errno = result;
fatal_error_with_errno("Failed to create new thread");
}
return t;
}
void
join_thread(pthread_t t)
{
int result = pthread_join(t, NULL);
if (result) {
errno = result;
fatal_error_with_errno("Failed to join thread");
}
}