forked from illumos/illumos-gate
/
dd.c
1829 lines (1631 loc) · 42.1 KB
/
dd.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012, Josef 'Jeff' Sipek <jeffpc@31bits.net>. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* convert and copy
*/
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
/* The BIG parameter is machine dependent. It should be a long integer */
/* constant that can be used by the number parser to check the validity */
/* of numeric parameters. On 16-bit machines, it should probably be */
/* the maximum unsigned integer, 0177777L. On 32-bit machines where */
/* longs are the same size as ints, the maximum signed integer is more */
/* appropriate. This value is 017777777777L. In 64 bit environments, */
/* the maximum signed integer value is 0777777777777777777777LL */
#define BIG 0777777777777777777777LL
#define BSIZE 512
/* Option parameters */
#define COPY 0 /* file copy, preserve input block size */
#define REBLOCK 1 /* file copy, change block size */
#define LCREBLOCK 2 /* file copy, convert to lower case */
#define UCREBLOCK 3 /* file copy, convert to upper case */
#define NBASCII 4 /* file copy, convert from EBCDIC to ASCII */
#define LCNBASCII 5 /* file copy, EBCDIC to lower case ASCII */
#define UCNBASCII 6 /* file copy, EBCDIC to upper case ASCII */
#define NBEBCDIC 7 /* file copy, convert from ASCII to EBCDIC */
#define LCNBEBCDIC 8 /* file copy, ASCII to lower case EBCDIC */
#define UCNBEBCDIC 9 /* file copy, ASCII to upper case EBCDIC */
#define NBIBM 10 /* file copy, convert from ASCII to IBM */
#define LCNBIBM 11 /* file copy, ASCII to lower case IBM */
#define UCNBIBM 12 /* file copy, ASCII to upper case IBM */
#define UNBLOCK 13 /* convert blocked ASCII to ASCII */
#define LCUNBLOCK 14 /* convert blocked ASCII to lower case ASCII */
#define UCUNBLOCK 15 /* convert blocked ASCII to upper case ASCII */
#define ASCII 16 /* convert blocked EBCDIC to ASCII */
#define LCASCII 17 /* convert blocked EBCDIC to lower case ASCII */
#define UCASCII 18 /* convert blocked EBCDIC to upper case ASCII */
#define BLOCK 19 /* convert ASCII to blocked ASCII */
#define LCBLOCK 20 /* convert ASCII to lower case blocked ASCII */
#define UCBLOCK 21 /* convert ASCII to upper case blocked ASCII */
#define EBCDIC 22 /* convert ASCII to blocked EBCDIC */
#define LCEBCDIC 23 /* convert ASCII to lower case blocked EBCDIC */
#define UCEBCDIC 24 /* convert ASCII to upper case blocked EBCDIC */
#define IBM 25 /* convert ASCII to blocked IBM */
#define LCIBM 26 /* convert ASCII to lower case blocked IBM */
#define UCIBM 27 /* convert ASCII to upper case blocked IBM */
#define LCASE 01 /* flag - convert to lower case */
#define UCASE 02 /* flag - convert to upper case */
#define SWAB 04 /* flag - swap bytes before conversion */
#define NERR 010 /* flag - proceed on input errors */
#define SYNC 020 /* flag - pad short input blocks with nulls */
#define BADLIMIT 5 /* give up if no progress after BADLIMIT trys */
#define SVR4XLATE 0 /* use default EBCDIC translation */
#define BSDXLATE 1 /* use BSD-compatible EBCDIC translation */
#define USAGE\
"usage: dd [if=file] [of=file] [ibs=n|nk|nb|nxm] [obs=n|nk|nb|nxm]\n"\
" [bs=n|nk|nb|nxm] [cbs=n|nk|nb|nxm] [files=n] [skip=n]\n"\
" [iseek=n] [oseek=n] [seek=n] [count=n] [conv=[ascii]\n"\
" [,ebcdic][,ibm][,asciib][,ebcdicb][,ibmb]\n"\
" [,block|unblock][,lcase|ucase][,swab]\n"\
" [,noerror][,notrunc][,sync]]\n"
/* Global references */
/* Local routine declarations */
static int match(char *);
static void term();
static unsigned long long number();
static unsigned char *flsh();
static void stats();
/* Local data definitions */
static unsigned ibs; /* input buffer size */
static unsigned obs; /* output buffer size */
static unsigned bs; /* buffer size, overrules ibs and obs */
static unsigned cbs; /* conversion buffer size, used for block conversions */
static unsigned ibc; /* number of bytes still in the input buffer */
static unsigned obc; /* number of bytes in the output buffer */
static unsigned cbc; /* number of bytes in the conversion buffer */
static int ibf; /* input file descriptor */
static int obf; /* output file descriptor */
static int cflag; /* conversion option flags */
static int skipf; /* if skipf == 1, skip rest of input line */
static unsigned long long nifr; /* count of full input records */
static unsigned long long nipr; /* count of partial input records */
static unsigned long long nofr; /* count of full output records */
static unsigned long long nopr; /* count of partial output records */
static unsigned long long ntrunc; /* count of truncated input lines */
static unsigned long long nbad; /* count of bad records since last */
/* good one */
static int files; /* number of input files to concatenate (tape only) */
static off_t skip; /* number of input records to skip */
static off_t iseekn; /* number of input records to seek past */
static off_t oseekn; /* number of output records to seek past */
static unsigned long long count; /* number of input records to copy */
/* (0 = all) */
static int trantype; /* BSD or SVr4 compatible EBCDIC */
static char *string; /* command arg pointer */
static char *ifile; /* input file name pointer */
static char *ofile; /* output file name pointer */
static unsigned char *ibuf; /* input buffer pointer */
static unsigned char *obuf; /* output buffer pointer */
/* This is an EBCDIC to ASCII conversion table */
/* from a proposed BTL standard April 16, 1979 */
static unsigned char svr4_etoa [] =
{
0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
0247, 0250, 0325, 0056, 0074, 0050, 0053, 0174,
0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0041, 0044, 0052, 0051, 0073, 0176,
0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0313, 0054, 0045, 0137, 0076, 0077,
0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
0161, 0162, 0136, 0314, 0315, 0316, 0317, 0320,
0321, 0345, 0163, 0164, 0165, 0166, 0167, 0170,
0171, 0172, 0322, 0323, 0324, 0133, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0340, 0341, 0342, 0343, 0344, 0135, 0346, 0347,
0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* This is an ASCII to EBCDIC conversion table */
/* from a proposed BTL standard April 16, 1979 */
static unsigned char svr4_atoe [] =
{
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
0347, 0350, 0351, 0255, 0340, 0275, 0232, 0155,
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
0247, 0250, 0251, 0300, 0117, 0320, 0137, 0007,
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
0216, 0217, 0220, 0152, 0233, 0234, 0235, 0236,
0237, 0240, 0252, 0253, 0254, 0112, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0241, 0276, 0277,
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* Table for ASCII to IBM (alternate EBCDIC) code conversion */
static unsigned char svr4_atoibm[] =
{
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* Table for conversion of ASCII to lower case ASCII */
static unsigned char utol[] =
{
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
0100, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
0170, 0171, 0172, 0133, 0134, 0135, 0136, 0137,
0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157,
0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167,
0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* Table for conversion of ASCII to upper case ASCII */
static unsigned char ltou[] =
{
0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007,
0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027,
0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037,
0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047,
0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077,
0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* BSD-compatible EBCDIC to ASCII translate table */
static unsigned char bsd_etoa[] =
{
0000, 0001, 0002, 0003, 0234, 0011, 0206, 0177,
0227, 0215, 0216, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0235, 0205, 0010, 0207,
0030, 0031, 0222, 0217, 0034, 0035, 0036, 0037,
0200, 0201, 0202, 0203, 0204, 0012, 0027, 0033,
0210, 0211, 0212, 0213, 0214, 0005, 0006, 0007,
0220, 0221, 0026, 0223, 0224, 0225, 0226, 0004,
0230, 0231, 0232, 0233, 0024, 0025, 0236, 0032,
0040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
0247, 0250, 0133, 0056, 0074, 0050, 0053, 0041,
0046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0135, 0044, 0052, 0051, 0073, 0136,
0055, 0057, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0174, 0054, 0045, 0137, 0076, 0077,
0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
0302, 0140, 0072, 0043, 0100, 0047, 0075, 0042,
0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067,
0070, 0071, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* BSD-compatible ASCII to EBCDIC translate table */
static unsigned char bsd_atoe[] =
{
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
0247, 0250, 0251, 0300, 0152, 0320, 0241, 0007,
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* BSD-compatible ASCII to IBM translate table */
static unsigned char bsd_atoibm[] =
{
0000, 0001, 0002, 0003, 0067, 0055, 0056, 0057,
0026, 0005, 0045, 0013, 0014, 0015, 0016, 0017,
0020, 0021, 0022, 0023, 0074, 0075, 0062, 0046,
0030, 0031, 0077, 0047, 0034, 0035, 0036, 0037,
0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
0247, 0250, 0251, 0300, 0117, 0320, 0241, 0007,
0040, 0041, 0042, 0043, 0044, 0025, 0006, 0027,
0050, 0051, 0052, 0053, 0054, 0011, 0012, 0033,
0060, 0061, 0032, 0063, 0064, 0065, 0066, 0010,
0070, 0071, 0072, 0073, 0004, 0024, 0076, 0341,
0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377,
};
/* set up to use SVr4 ascii-ebcdic translation by default */
static unsigned char *atoe = svr4_atoe;
static unsigned char *etoa = svr4_etoa;
static unsigned char *atoibm = svr4_atoibm;
int
main(int argc, char **argv)
{
unsigned char *ip, *op; /* input and output buffer pointers */
int c; /* character counter */
int ic; /* input character */
int conv; /* conversion option code */
int trunc; /* whether output file is truncated */
struct stat file_stat;
/* Set option defaults */
ibs = BSIZE;
obs = BSIZE;
files = 1;
conv = COPY;
trunc = 1; /* default: truncate output file */
trantype = SVR4XLATE; /* use SVR4 EBCDIC by default */
/* Parse command options */
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "")) != EOF)
switch (c) {
case '?':
(void) fprintf(stderr, USAGE);
exit(2);
}
/* not getopt()'ed because dd has no options but only operand(s) */
for (c = optind; c < argc; c++)
{
string = argv[c];
if (match("ibs="))
{
ibs = (unsigned)number(BIG);
continue;
}
if (match("obs="))
{
obs = (unsigned)number(BIG);
continue;
}
if (match("cbs="))
{
cbs = (unsigned)number(BIG);
continue;
}
if (match("bs="))
{
bs = (unsigned)number(BIG);
continue;
}
if (match("if="))
{
ifile = string;
continue;
}
if (match("of="))
{
ofile = string;
continue;
}
if (match("skip="))
{
skip = number(BIG);
continue;
}
if (match("iseek="))
{
iseekn = number(BIG);
continue;
}
if (match("oseek="))
{
oseekn = number(BIG);
continue;
}
if (match("seek=")) /* retained for compatibility */
{
oseekn = number(BIG);
continue;
}
if (match("count="))
{
count = number(BIG);
continue;
}
if (match("files="))
{
files = (int)number(BIG);
continue;
}
if (match("conv="))
{
for (;;)
{
if (match(","))
{
continue;
}
if (*string == '\0')
{
break;
}
if (match("block"))
{
conv = BLOCK;
continue;
}
if (match("unblock"))
{
conv = UNBLOCK;
continue;
}
/* ebcdicb, ibmb, and asciib must precede */
/* ebcdic, ibm, and ascii in this test */
if (match("ebcdicb"))
{
conv = EBCDIC;
trantype = BSDXLATE;
continue;
}
if (match("ibmb"))
{
conv = IBM;
trantype = BSDXLATE;
continue;
}
if (match("asciib"))
{
conv = ASCII;
trantype = BSDXLATE;
continue;
}
if (match("ebcdic"))
{
conv = EBCDIC;
trantype = SVR4XLATE;
continue;
}
if (match("ibm"))
{
conv = IBM;
trantype = SVR4XLATE;
continue;
}
if (match("ascii"))
{
conv = ASCII;
trantype = SVR4XLATE;
continue;
}
if (match("lcase"))
{
cflag |= LCASE;
continue;
}
if (match("ucase"))
{
cflag |= UCASE;
continue;
}
if (match("swab"))
{
cflag |= SWAB;
continue;
}
if (match("noerror"))
{
cflag |= NERR;
continue;
}
if (match("notrunc"))
{
trunc = 0;
continue;
}
if (match("sync"))
{
cflag |= SYNC;
continue;
}
goto badarg;
}
continue;
}
badarg:
(void) fprintf(stderr, "dd: %s \"%s\"\n",
gettext("bad argument:"), string);
exit(2);
}
/* Perform consistency checks on options, decode strange conventions */
if (bs)
{
ibs = obs = bs;
}
if ((ibs == 0) || (obs == 0))
{
(void) fprintf(stderr, "dd: %s\n",
gettext("buffer sizes cannot be zero"));
exit(2);
}
if (conv == COPY)
{
if ((bs == 0) || (cflag&(LCASE|UCASE)))
{
conv = REBLOCK;
}
}
if (cbs == 0)
{
switch (conv)
{
case BLOCK:
case UNBLOCK:
conv = REBLOCK;
break;
case ASCII:
conv = NBASCII;
break;
case EBCDIC:
conv = NBEBCDIC;
break;
case IBM:
conv = NBIBM;
break;
}
}
/* Expand options into lower and upper case versions if necessary */
switch (conv)
{
case REBLOCK:
if (cflag&LCASE)
conv = LCREBLOCK;
else if (cflag&UCASE)
conv = UCREBLOCK;
break;
case UNBLOCK:
if (cflag&LCASE)
conv = LCUNBLOCK;
else if (cflag&UCASE)
conv = UCUNBLOCK;
break;
case BLOCK:
if (cflag&LCASE)
conv = LCBLOCK;
else if (cflag&UCASE)
conv = UCBLOCK;
break;
case ASCII:
if (cflag&LCASE)
conv = LCASCII;
else if (cflag&UCASE)
conv = UCASCII;
break;
case NBASCII:
if (cflag&LCASE)
conv = LCNBASCII;
else if (cflag&UCASE)
conv = UCNBASCII;
break;
case EBCDIC:
if (cflag&LCASE)
conv = LCEBCDIC;
else if (cflag&UCASE)
conv = UCEBCDIC;
break;
case NBEBCDIC:
if (cflag&LCASE)
conv = LCNBEBCDIC;
else if (cflag&UCASE)
conv = UCNBEBCDIC;
break;
case IBM:
if (cflag&LCASE)
conv = LCIBM;
else if (cflag&UCASE)
conv = UCIBM;
break;
case NBIBM:
if (cflag&LCASE)
conv = LCNBIBM;
else if (cflag&UCASE)
conv = UCNBIBM;
break;
}
/* If BSD-compatible translation is selected, change the tables */
if (trantype == BSDXLATE) {
atoe = bsd_atoe;
atoibm = bsd_atoibm;
etoa = bsd_etoa;
}
/* Open the input file, or duplicate standard input */
ibf = -1;
if (ifile)
{
ibf = open(ifile, 0);
}
#ifndef STANDALONE
else
{
ifile = "";
ibf = dup(0);
}
#endif
if (ibf == -1)
{
(void) fprintf(stderr, "dd: %s: ", ifile);
perror("open");
exit(2);
}
/* Open the output file, or duplicate standard output */
obf = -1;
if (ofile)
{
if (trunc == 0) /* do not truncate output file */
obf = open(ofile, (O_WRONLY|O_CREAT),
(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH));
else if (oseekn && (trunc == 1))
{
obf = open(ofile, O_WRONLY|O_CREAT,
(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH));
if (obf == -1)
{
(void) fprintf(stderr, "dd: %s: ", ofile);
perror("open");
exit(2);
}
(void) fstat(obf, &file_stat);
if (((file_stat.st_mode & S_IFMT) == S_IFREG) &&
(ftruncate(obf, (((off_t)oseekn) * ((off_t)obs)))
== -1))
{
perror("ftruncate");
exit(2);
}
}
else
obf = creat(ofile,
(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH));
}
#ifndef STANDALONE
else
{
ofile = "";
obf = dup(1);
}
#endif
if (obf == -1)
{
(void) fprintf(stderr, "dd: %s: ", ofile);
perror("open");
exit(2);
}
/* Expand memory to get an input buffer */
ibuf = (unsigned char *)valloc(ibs + 10);
/* If no conversions, the input buffer is the output buffer */
if (conv == COPY)
{
obuf = ibuf;
}
/* Expand memory to get an output buffer. Leave enough room at the */
/* end to convert a logical record when doing block conversions. */
else
{
obuf = (unsigned char *)valloc(obs + cbs + 10);
}
if ((ibuf == (unsigned char *)NULL) || (obuf == (unsigned char *)NULL))
{
(void) fprintf(stderr,
"dd: %s\n", gettext("not enough memory"));
exit(2);
}
/* Enable a statistics message on SIGINT */
#ifndef STANDALONE
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
{
(void) signal(SIGINT, term);
}
#endif
/* Skip input blocks */
while (skip)
{
ibc = read(ibf, (char *)ibuf, ibs);
if (ibc == (unsigned)-1)
{
if (++nbad > BADLIMIT)
{
(void) fprintf(stderr, "dd: %s\n",
gettext("skip failed"));
exit(2);
}
else
{
perror("read");
}
}
else
{
if (ibc == 0)
{
(void) fprintf(stderr, "dd: %s\n",
gettext("cannot skip past end-of-file"));
exit(3);
}
else
{
nbad = 0;
}
}
skip--;
}
/* Seek past input blocks */
if (iseekn && lseek(ibf, (((off_t)iseekn) * ((off_t)ibs)), 1) == -1)
{
perror("lseek");
exit(2);
}
/* Seek past output blocks */
if (oseekn && lseek(obf, (((off_t)oseekn) * ((off_t)obs)), 1) == -1)
{
perror("lseek");
exit(2);
}
/* Initialize all buffer pointers */
skipf = 0; /* not skipping an input line */
ibc = 0; /* no input characters yet */
obc = 0; /* no output characters yet */
cbc = 0; /* the conversion buffer is empty */
op = obuf; /* point to the output buffer */
/* Read and convert input blocks until end of file(s) */
for (;;)
{
if ((count == 0) || (nifr+nipr < count))
{
/* If proceed on error is enabled, zero the input buffer */
if (cflag&NERR)
{
ip = ibuf + ibs;
c = ibs;
if (c & 1) /* if the size is odd, */
{
*--ip = 0; /* clear the odd byte */
}
if (c >>= 1) /* divide by two */
{
do { /* clear two at a time */
*--ip = 0;
*--ip = 0;
} while (--c);
}
}
/* Read the next input block */
ibc = read(ibf, (char *)ibuf, ibs);
/* Process input errors */
if (ibc == (unsigned)-1)
{
perror("read");
if (((cflag&NERR) == 0) || (++nbad > BADLIMIT))
{
while (obc)
{
(void) flsh();
}
term(2);
}
else
{
stats();
ibc = ibs; /* assume a full block */
}
}
else
{
nbad = 0;
}
}
/* Record count satisfied, simulate end of file */
else
{
ibc = 0;
files = 1;
}