/
main.c
1774 lines (1484 loc) · 43.4 KB
/
main.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* main.c,v 1.75 2010/03/03 01:12:45 lacos Exp */
#include <unistd.h> /* unlink() */
#include <signal.h> /* kill() */
#include <stdlib.h> /* strtol() */
#include <stdarg.h> /* va_list */
#include <stdio.h> /* vfprintf() */
#include <string.h> /* strcpy() */
#include <errno.h> /* errno */
#include <assert.h> /* assert() */
#include <sys/stat.h> /* lstat() */
#include <fcntl.h> /* open() */
#include <utime.h> /* utime() */
#include "main.h" /* pname */
#include "lbunzip2.h" /* lbunzip2_wrap() */
#include "lbzip2.h" /* lbzip2_wrap() */
#define EX_OK 0
#define EX_FAIL 1
#define EX_WARN 4
/* Utilities that can be called/accessed from multiple threads. */
/* (I) The treatment for fatal errors. */
static pthread_t main_thread;
/* Pathname of the current regular file being written to. */
static char *opathn;
static pid_t pid;
static void
bailout(void)
{
sigset_t tmp_set;
if (pthread_equal(pthread_self(), main_thread)) {
if (0 != opathn) {
(void)unlink(opathn);
}
if (0 == sigemptyset(&tmp_set) && 0 == sigaddset(&tmp_set, SIGPIPE)
&& 0 == sigaddset(&tmp_set, SIGXFSZ)) {
(void)pthread_sigmask(SIG_UNBLOCK, &tmp_set, 0);
}
}
else {
if (0 == sigemptyset(&tmp_set) && 0 == sigpending(&tmp_set)) {
int chk;
chk = sigismember(&tmp_set, SIGPIPE);
if (0 == chk || (1 == chk && 0 == kill(pid, SIGPIPE))) {
chk = sigismember(&tmp_set, SIGXFSZ);
if ((0 == chk || (1 == chk && 0 == kill(pid, SIGXFSZ)))
&& 0 == kill(pid, SIGUSR1)) {
pthread_exit(0);
}
}
}
}
_exit(EX_FAIL);
}
/* (II) Logging utilities. */
const char *pname;
struct errstr
{
int err;
const char *str;
};
static const struct errstr errstr[] = {
#ifdef E2BIG
{ E2BIG, "Argument list too long" },
#endif
#ifdef EACCES
{ EACCES, "Permission denied" },
#endif
#ifdef EADDRINUSE
{ EADDRINUSE, "Address in use" },
#endif
#ifdef EADDRNOTAVAIL
{ EADDRNOTAVAIL, "Address not available" },
#endif
#ifdef EAFNOSUPPORT
{ EAFNOSUPPORT, "Address family not supported" },
#endif
#ifdef EAGAIN
{ EAGAIN, "Resource temporarily unavailable" },
#endif
#ifdef EALREADY
{ EALREADY, "Connection already in progress" },
#endif
#ifdef EBADF
{ EBADF, "Bad file descriptor" },
#endif
#ifdef EBADMSG
{ EBADMSG, "Bad message" },
#endif
#ifdef EBUSY
{ EBUSY, "Device or resource busy" },
#endif
#ifdef ECANCELED
{ ECANCELED, "Operation canceled" },
#endif
#ifdef ECHILD
{ ECHILD, "No child processes" },
#endif
#ifdef ECONNABORTED
{ ECONNABORTED, "Connection aborted" },
#endif
#ifdef ECONNREFUSED
{ ECONNREFUSED, "Connection refused" },
#endif
#ifdef ECONNRESET
{ ECONNRESET, "Connection reset" },
#endif
#ifdef EDEADLK
{ EDEADLK, "Resource deadlock would occur" },
#endif
#ifdef EDESTADDRREQ
{ EDESTADDRREQ, "Destination address required" },
#endif
#ifdef EDOM
{ EDOM, "Domain error" },
#endif
#ifdef EDQUOT
{ EDQUOT, "EDQUOT" },
#endif
#ifdef EEXIST
{ EEXIST, "File exists" },
#endif
#ifdef EFAULT
{ EFAULT, "Bad address" },
#endif
#ifdef EFBIG
{ EFBIG, "File too large" },
#endif
#ifdef EHOSTUNREACH
{ EHOSTUNREACH, "Host is unreachable" },
#endif
#ifdef EIDRM
{ EIDRM, "Identifier removed" },
#endif
#ifdef EILSEQ
{ EILSEQ, "Illegal byte sequence" },
#endif
#ifdef EINPROGRESS
{ EINPROGRESS, "Operation in progress" },
#endif
#ifdef EINVAL
{ EINVAL, "Invalid argument" },
#endif
#ifdef EIO
{ EIO, "Input/output error" },
#endif
#ifdef EISCONN
{ EISCONN, "Socket is connected" },
#endif
#ifdef EISDIR
{ EISDIR, "Is a directory" },
#endif
#ifdef ELOOP
{ ELOOP, "Too many levels of symbolic links" },
#endif
#ifdef EMFILE
{ EMFILE, "Too many open files" },
#endif
#ifdef EMLINK
{ EMLINK, "Too many links" },
#endif
#ifdef EMSGSIZE
{ EMSGSIZE, "Message too large / Inappropriate message buffer length" },
#endif
#ifdef EMULTIHOP
{ EMULTIHOP, "EMULTIHOP" },
#endif
#ifdef ENAMETOOLONG
{ ENAMETOOLONG, "Filename too long" },
#endif
#ifdef ENETDOWN
{ ENETDOWN, "Network is down" },
#endif
#ifdef ENETUNREACH
{ ENETUNREACH, "Network unreachable" },
#endif
#ifdef ENFILE
{ ENFILE, "Too many files open in system" },
#endif
#ifdef ENOBUFS
{ ENOBUFS, "No buffer space available" },
#endif
#ifdef ENODATA
{ ENODATA, "No message available" },
#endif
#ifdef ENODEV
{ ENODEV, "No such device" },
#endif
#ifdef ENOEXEC
{ ENOEXEC, "Executable file format error" },
#endif
#ifdef ENOLCK
{ ENOLCK, "No locks available" },
#endif
#ifdef ENOLINK
{ ENOLINK, "ENOLINK" },
#endif
#ifdef ENOMEM
{ ENOMEM, "Not enough space" },
#endif
#ifdef ENOMSG
{ ENOMSG, "No message of the desired type" },
#endif
#ifdef ENOPROTOOPT
{ ENOPROTOOPT, "Protocol not available" },
#endif
#ifdef ENOSPC
{ ENOSPC, "No space left on device" },
#endif
#ifdef ENOSR
{ ENOSR, "No STREAM resources" },
#endif
#ifdef ENOSTR
{ ENOSTR, "Not a STREAM" },
#endif
#ifdef ENOSYS
{ ENOSYS, "Function not implemented" },
#endif
#ifdef ENOTCONN
{ ENOTCONN, "Socket not connected" },
#endif
#ifdef ENOTDIR
{ ENOTDIR, "Not a directory" },
#endif
#ifdef ENOTEMPTY
{ ENOTEMPTY, "Directory not empty" },
#endif
#ifdef ENOTSOCK
{ ENOTSOCK, "Not a socket" },
#endif
#ifdef ENOTSUP
{ ENOTSUP, "Not supported" },
#endif
#ifdef ENOTTY
{ ENOTTY, "Inappropriate I/O control operation" },
#endif
#ifdef ENXIO
{ ENXIO, "No such device or address" },
#endif
#ifdef EOPNOTSUPP
{ EOPNOTSUPP, "Operation not supported on socket" },
#endif
#ifdef EOVERFLOW
{ EOVERFLOW, "Value too large to be stored in data type" },
#endif
#ifdef EPERM
{ EPERM, "Operation not permitted" },
#endif
#ifdef EPIPE
{ EPIPE, "Broken pipe" },
#endif
#ifdef EPROTO
{ EPROTO, "Protocol error" },
#endif
#ifdef EPROTONOSUPPORT
{ EPROTONOSUPPORT, "Protocol not supported" },
#endif
#ifdef EPROTOTYPE
{ EPROTOTYPE, "Socket type not supported" },
#endif
#ifdef ERANGE
{ ERANGE, "Numerical result out of range" },
#endif
#ifdef EROFS
{ EROFS, "Read-only file system" },
#endif
#ifdef ESPIPE
{ ESPIPE, "Invalid seek" },
#endif
#ifdef ESRCH
{ ESRCH, "No such process" },
#endif
#ifdef ESTALE
{ ESTALE, "ESTALE" },
#endif
#ifdef ETIME
{ ETIME, "STREAM ioctl() timeout" },
#endif
#ifdef ETIMEDOUT
{ ETIMEDOUT, "Connection timed out / Operation timed out" },
#endif
#ifdef ETXTBSY
{ ETXTBSY, "Text file busy" },
#endif
#ifdef EWOULDBLOCK
{ EWOULDBLOCK, "Operation would block" },
#endif
#ifdef EXDEV
{ EXDEV, "Cross-device link" },
#endif
{ EINTR, "Interrupted function call" },
{ ENOENT, "No such file or directory" }
};
const char *
err2str(int err)
{
size_t idx;
for (idx = 0u; idx < sizeof errstr / sizeof errstr[0]; ++idx) {
if (errstr[idx].err == err) {
return errstr[idx].str;
}
}
return "Unknown error";
}
void
log_info(const char *fmt, ...)
{
va_list args;
int ret;
va_start(args, fmt);
ret = vfprintf(stderr, fmt, args);
va_end(args);
if (0 > ret) {
/* stderr is never fully buffered originally. */
bailout();
}
}
void
log_fatal(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
(void)vfprintf(stderr, fmt, args);
va_end(args);
/* stderr is never fully buffered originally. */
bailout();
}
/* (III) Memory allocation. */
static void *(*mallocf)(size_t size);
static void *
trace_malloc(size_t size)
{
void *ret;
int save_errno;
assert(0u < size);
ret = malloc(size);
if (0 == ret) {
save_errno = errno;
}
log_info("%lu: malloc(%lu) == %p\n", (long unsigned)pid, (long unsigned)size,
ret);
if (0 == ret) {
errno = save_errno;
}
return ret;
}
void *
xalloc(size_t size)
{
void *ret;
ret = (*mallocf)(size);
if (0 == ret) {
log_fatal("%s: (*mallocf)(): %s\n", pname, err2str(errno));
}
return ret;
}
void (*freef)(void *ptr);
static void
trace_free(void *ptr)
{
log_info("%lu: free(%p)\n", (long unsigned)pid, ptr);
free(ptr);
}
/* (IV) Threading utilities. */
void
xinit(struct cond *cond)
{
int ret;
pthread_mutexattr_t attr;
ret = pthread_mutexattr_init(&attr);
if (0 != ret) {
log_fatal("%s: pthread_mutexattr_init(): %s\n", pname, err2str(ret));
}
ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (0 != ret) {
log_fatal("%s: pthread_mutexattr_settype(): %s\n", pname, err2str(ret));
}
ret = pthread_mutex_init(&cond->lock, &attr);
if (0 != ret) {
log_fatal("%s: pthread_mutex_init(): %s\n", pname, err2str(ret));
}
ret = pthread_mutexattr_destroy(&attr);
if (0 != ret) {
log_fatal("%s: pthread_mutexattr_destroy(): %s\n", pname, err2str(ret));
}
ret = pthread_cond_init(&cond->cond, 0);
if (0 != ret) {
log_fatal("%s: pthread_cond_init(): %s\n", pname, err2str(ret));
}
cond->ccount = 0lu;
cond->wcount = 0lu;
}
void
xdestroy(struct cond *cond)
{
int ret;
ret = pthread_cond_destroy(&cond->cond);
if (0 != ret) {
log_fatal("%s: pthread_cond_destroy(): %s\n", pname, err2str(ret));
}
ret = pthread_mutex_destroy(&cond->lock);
if (0 != ret) {
log_fatal("%s: pthread_mutex_destroy(): %s\n", pname, err2str(ret));
}
}
void
xlock(struct cond *cond)
{
int ret;
ret = pthread_mutex_lock(&cond->lock);
if (0 != ret) {
log_fatal("%s: pthread_mutex_lock(): %s\n", pname, err2str(ret));
}
}
void
xlock_pred(struct cond *cond)
{
xlock(cond);
++cond->ccount;
}
void
xunlock(struct cond *cond)
{
int ret;
ret = pthread_mutex_unlock(&cond->lock);
if (0 != ret) {
log_fatal("%s: pthread_mutex_unlock(): %s\n", pname, err2str(ret));
}
}
void
xwait(struct cond *cond)
{
int ret;
++cond->wcount;
ret = pthread_cond_wait(&cond->cond, &cond->lock);
if (0 != ret) {
log_fatal("%s: pthread_cond_wait(): %s\n", pname, err2str(ret));
}
++cond->ccount;
}
void
xsignal(struct cond *cond)
{
int ret;
ret = pthread_cond_signal(&cond->cond);
if (0 != ret) {
log_fatal("%s: pthread_cond_signal(): %s\n", pname, err2str(ret));
}
}
void
xbroadcast(struct cond *cond)
{
int ret;
ret = pthread_cond_broadcast(&cond->cond);
if (0 != ret) {
log_fatal("%s: pthread_cond_broadcast(): %s\n", pname, err2str(ret));
}
}
void
xcreate(pthread_t *thread, void *(*routine)(void *), void *arg)
{
int ret;
ret = pthread_create(thread, 0, routine, arg);
if (0 != ret) {
log_fatal("%s: pthread_create(): %s\n", pname, err2str(ret));
}
}
void
xjoin(pthread_t thread)
{
int ret;
ret = pthread_join(thread, 0);
if (0 != ret) {
log_fatal("%s: pthread_join(): %s\n", pname, err2str(ret));
}
}
void
xraise(int sig)
{
if (-1 == kill(pid, sig)) {
log_fatal("%s: kill(): %s\n", pname, err2str(errno));
}
}
/* (V) File I/O utilities. */
void
xread(struct filespec *ispec, char unsigned *buffer, size_t *vacant)
{
assert(0 < *vacant);
do {
ssize_t rd;
rd = read(ispec->fd, buffer, *vacant > (size_t)SSIZE_MAX ?
(size_t)SSIZE_MAX : *vacant);
/* End of file. */
if (0 == rd)
break;
/* Read error. */
if (-1 == rd) {
log_fatal("%s: read(%s%s%s): %s\n", pname, ispec->sep, ispec->fmt,
ispec->sep, err2str(errno));
}
*vacant -= (size_t)rd;
buffer += (size_t)rd;
ispec->total += (size_t)rd;
} while (0 < *vacant);
}
void
xwrite(struct filespec *ospec, const char unsigned *buffer, size_t size)
{
assert(0 < size);
ospec->total += size;
if (-1 != ospec->fd) {
do {
ssize_t wr;
wr = write(ospec->fd, buffer, size > (size_t)SSIZE_MAX ?
(size_t)SSIZE_MAX : size);
/* Write error. */
if (-1 == wr) {
log_fatal("%s: write(%s%s%s): %s\n", pname, ospec->sep, ospec->fmt,
ospec->sep, err2str(errno));
}
size -= (size_t)wr;
buffer += (size_t)wr;
} while (0 < size);
}
}
/* Private stuff, only callable/accessible for the main thread. */
/* Name of environtment variable that sets allocation tracing. */
static const char ev_trace[] = "LBZIP2_TRACE_ALLOC";
enum outmode
{
OM_STDOUT, /* Write all output to stdout, -c. */
OM_DISCARD, /* Discard output, -t. */
OM_REGF /* Write output to files; neither of -t/-c present. */
};
struct opts
{
unsigned num_worker; /* Start this many worker threads, -n. */
enum outmode outmode; /* How to store output, -c/-t. */
int decompress, /* Run in bunzip2 mode, -d/-z. */
bs100k, /* Blocksize switch for compression, -1 .. -9. */
exponential, /* Use an alternative block-sorting algorithm. */
force, /* Open anything / break links / remove output, -f. */
keep, /* Don't rm FILE oprnds / open rf with >1 links, -k. */
verbose, /* Print a msg. each time when starting a muxer, -v. */
print_cctrs; /* Print condition variable counters, -S. */
};
static const char version[] = "0.23";
/* Backlog factor for all workers together. */
static const unsigned blf = 4u;
/* Names of other recognized environment variables. */
static const char * const ev_name[] = { "LBZIP2", "BZIP2", "BZIP" };
/* Separator characters in environment variable values. No escaping. */
static const char envsep[] = " \t";
static long
xstrtol(const char *str, const char *source, long lower, long upper)
{
long tmp;
char *endptr;
errno = 0;
tmp = strtol(str, &endptr, 10);
if ('\0' == *str || '\0' != *endptr || 0 != errno
|| tmp < lower || tmp > upper) {
log_fatal("%s: failed to parse \"%s\" from %s as a long in [%ld..%ld],"
" specify \"-h\" for help\n", pname, str, source, lower, upper);
}
return tmp;
}
static void
usage(unsigned mx_worker)
{
log_info(
"lbzip2: Parallel bzip2 utility.\n"
"Copyright (C) 2008, 2009, 2010 Laszlo Ersek.\n"
"Copyright (C) 2011 Mikolaj Izdebski.\n"
"Released under the GNU GPLv3+.\n"
"Version %s.\n"
"\n"
"Usage:\n"
"1. PROG [-n WTHRS] [-k|-c|-t] [-d|-z] [-1 .. -9] [-f] [-v] [-S] {FILE}\n"
"2. PROG -h\n"
"\n"
"Recognized PROG names:\n"
" bunzip2, lbunzip2 : Decompress. Forceable with \"-d\".\n"
" bzcat, lbzcat : Decompress to stdout. Forceable with \"-cd\".\n"
" <otherwise> : Compress. Forceable with \"-z\".\n"
"\n"
"Environment variables:\n"
" LBZIP2, BZIP2,\n", version);
log_info(
" BZIP : Insert arguments betwen PROG and the rest of the\n"
" command line. Tokens are separated by spaces and\n"
" tabs; no escaping.\n"
" LBZIP2_TRACE_ALLOC : If set to a non-empty value, print a memory\n"
" allocation trace to stderr. Check trace with\n"
" \"malloc_trace.pl\".\n"
"\n"
"Options:\n"
" -n WTHRS : Set the number of (de)compressor threads to\n"
" WTHRS. WTHRS must be in [1, %u].\n", mx_worker);
log_info(
#ifdef _SC_NPROCESSORS_ONLN
" If this option is not specified, lbzip2 queries\n"
" the system for the number of online processors.\n"
#else
" If this option is not specified, lbzip2 exits\n"
" with an error.\n"
#endif
" -k, --keep : Don't remove FILE operands. Open regular input\n"
" files with more than one links.\n"
" -c, --stdout : Write output to stdout even with FILE operands.\n"
" Implies \"-k\". Incompatible with \"-t\".\n"
" -t, --test : Test decompression; discard output instead of\n");
log_info(
" writing it to files or stdout. Implies \"-k\".\n"
" Incompatible with \"-c\".\n"
" -d, --decompress : Force decompression over the selection by PROG.\n"
" -z, --compress : Force compression over the selection by PROG.\n"
" -1 .. -9 : Set the compression block size to 100K .. 900K.\n"
" --fast : Alias for \"-1\".\n"
" --best : Alias for \"-9\". This is the default.\n"
" -f, --force : Open non-regular input files. Open input files\n");
log_info(
" with more than one links. Try to remove each\n"
" output file before opening it.\n"
" -v, --verbose : Log each (de)compression start to stderr.\n"
" -S : Print condition variable statistics to stderr.\n"
" --exponential : Use an alternative block-sorting algorithm.\n"
" -s, --small,\n"
" -q, --quiet,\n"
" --repetitive-fast,\n"
" --repetitive-best : Accepted for compatibility, otherwise ignored.\n"
" -h, --help,\n"
" -L, --license,\n");
log_info(
" -V, --version : Print this help and exit successfully.\n"
"\n"
"Operands:\n"
" FILE : Specify files to compress or decompress. If no\n"
" FILE is given, work as a filter. FILEs with\n"
" \".bz2\", \".tbz\", \".tbz2\" and \".tz2\" name\n"
" suffixes will be skipped when compressing. When\n"
" decompressing, \".bz2\" suffixes will be removed\n"
" in output filenames; \".tbz\", \".tbz2\" and\n");
log_info(
" \".tz2\" suffixes will be replaced by \".tar\";\n"
" other filenames will be suffixed with \".out\".\n"
);
_exit(EX_OK);
}
struct arg
{
struct arg *next;
const char *val;
};
static void
opts_outmode(struct opts *opts, char ch)
{
assert('c' == ch || 't' == ch);
if (('c' == ch ? OM_DISCARD : OM_STDOUT) == opts->outmode) {
log_fatal("%s: \"-c\" and \"-t\" are incompatible, specify \"-h\" for"
" help\n", pname);
}
if ('c' == ch) {
opts->outmode = OM_STDOUT;
}
else {
opts->outmode = OM_DISCARD;
opts->decompress = 1;
}
}
static void
opts_decompress(struct opts *opts, char ch)
{
assert('d' == ch || 'z' == ch);
opts->decompress = ('d' == ch);
if (OM_DISCARD == opts->outmode) {
opts->outmode = OM_REGF;
}
}
static void
opts_setup(struct opts *opts, struct arg **operands, size_t argc, char **argv)
{
struct arg **link_at;
long mx_worker;
/*
Create a homogeneous argument list from the recognized environment
variables and from the command line.
*/
*operands = 0;
link_at = operands;
{
size_t ofs;
for (ofs = 0u; ofs < sizeof ev_name / sizeof ev_name[0]; ++ofs) {
char *ev_val;
ev_val = getenv(ev_name[ofs]);
if (0 != ev_val) {
char *tok;
for (tok = strtok(ev_val, envsep); 0 != tok; tok = strtok(0, envsep)) {
struct arg *arg;
arg = xalloc(sizeof *arg);
arg->next = 0;
arg->val = tok;
*link_at = arg;
link_at = &arg->next;
}
}
}
for (ofs = 1u; ofs < argc; ++ofs) {
struct arg *arg;
arg = xalloc(sizeof *arg);
arg->next = 0;
arg->val = argv[ofs];
*link_at = arg;
link_at = &arg->next;
}
}
/* Effectuate option defaults. */
mx_worker = sysconf(_SC_THREAD_THREADS_MAX);
if (-1L == mx_worker) {
mx_worker = LONG_MAX;
}
if (UINT_MAX < (long unsigned)mx_worker) {
mx_worker = UINT_MAX;
}
if (UINT_MAX / blf < (unsigned)mx_worker) {
mx_worker = UINT_MAX / blf;
}
if ((size_t)-1 / sizeof(pthread_t) < (unsigned)mx_worker) {
mx_worker = (size_t)-1 / sizeof(pthread_t);
}
opts->num_worker = 0u;
opts->outmode = OM_REGF;
opts->decompress = 0;
if (0 == strcmp("bunzip2", pname) || 0 == strcmp("lbunzip2", pname)) {
opts->decompress = 1;
}
else {
if (0 == strcmp("bzcat", pname) || 0 == strcmp("lbzcat", pname)) {
opts->outmode = OM_STDOUT;
opts->decompress = 1;
}
}
opts->bs100k = 9;
opts->exponential = 0;
opts->force = 0;
opts->keep = 0;
opts->verbose = 0;
opts->print_cctrs = 0;
/*
Process and remove all arguments that are options or option arguments. The
remaining arguments are the operands.
*/
link_at = operands;
{
enum
{
AS_CONTINUE, /* Continue argument processing. */
AS_STOP, /* Processing finished because of "--". */
AS_USAGE /* Processing finished because user asked for help. */
} args_state;
struct arg *arg,
*next;
args_state = AS_CONTINUE;
for (arg = *operands; 0 != arg && AS_CONTINUE == args_state; arg = next) {
const char *argscan;
argscan = arg->val;
if ('-' != *argscan) {
/* This is an operand, keep it. */
link_at = &arg->next;
next = arg->next;
}
else {
/* This argument holds options and possibly an option argument. */
++argscan;
if ('-' == *argscan) {
++argscan;
if ('\0' == *argscan) {
args_state = AS_STOP;
}
else if (0 == strcmp("stdout", argscan)) {
opts_outmode(opts, 'c');
}
else if (0 == strcmp("test", argscan)) {
opts_outmode(opts, 't');
}
else if (0 == strcmp("decompress", argscan)) {
opts_decompress(opts, 'd');
}
else if (0 == strcmp("compress", argscan)) {
opts_decompress(opts, 'z');
}
else if (0 == strcmp("fast", argscan)) {
opts->bs100k = 1;
}
else if (0 == strcmp("exponential", argscan)) {
opts->exponential = 1;
}
else if (0 == strcmp("best", argscan)) {
opts->bs100k = 9;
}
else if (0 == strcmp("force", argscan)) {
opts->force = 1;
}
else if (0 == strcmp("keep", argscan)) {
opts->keep = 1;
}
else if (0 == strcmp("verbose", argscan)) {
opts->verbose = 1;
}
else if (0 == strcmp("help", argscan)
|| 0 == strcmp("license", argscan)
|| 0 == strcmp("version", argscan)) {
args_state = AS_USAGE;
}
else if (0 != strcmp("small", argscan)
&& 0 != strcmp("quiet", argscan)
&& 0 != strcmp("repetitive-fast", argscan)
&& 0 != strcmp("repetitive-best", argscan)) {
log_fatal("%s: unknown option \"%s\", specify \"-h\" for help\n",
pname, arg->val);
}
} /* long option */
else {
int cont;
cont = 1;
do {
switch (*argscan) {
case '\0': cont = 0; break;
case 'c': case 't':
opts_outmode(opts, *argscan);
break;
case 'd': case 'z':
opts_decompress(opts, *argscan);
break;
case '1': case '2': case '3': case '4': case '5': case '6':
case '7': case '8': case '9':
opts->bs100k = *argscan - '0';
break;
case 'f': opts->force = 1; break;
case 'k': opts->keep = 1; break;
case 'v': opts->verbose = 1; break;
case 'S': opts->print_cctrs = 1; break;
case 's': case 'q':
break;