21 int main(int argc
, char **argv
);
25 void statistics(void);
35 register char *ps
= pch
;
37 while (*ps
++ == *pc
++)
45 #define BIGNUM 2147483647
54 while ((*pc
>= '0') && (*pc
<= '9'))
55 ans
= (long) ((*pc
++ - '0') + (ans
* 10));
56 while (TRUE
) switch (*pc
++) {
70 if ((ans
>= BIGNUM
) || (ans
< 0)) {
71 fprintf(stderr
, "dd: argument %s out of range\n",
82 #define NOERROR 0x0008
85 #define NOTRUNC 0x0040
89 unsigned cbs
, bs
, skip
, nseek
, count
;
91 unsigned ibs
= DEFAULT
;
92 unsigned obs
= DEFAULT
;
94 char *ifilename
= NULL
;
95 char *ofilename
= NULL
;
100 char *ibuf
, *obuf
, *op
;
101 unsigned nifull
, nipartial
, nofull
, nopartial
;
105 char mlen
[] = {64, 45, 82, 45, 83, 96, 109, 100, 109, 97, 96, 116, 108, 9};
111 if (obc
== 0) return;
116 if ((n
= write(ofd
, obuf
, obc
)) != obc
) {
118 fprintf(stderr
, "dd: Write error: %s\n", strerror(errno
));
120 fprintf(stderr
, "dd: Short write, %d instead of %d\n", n
, obc
);
129 if (convflag
& SILENT
) return;
130 fprintf(stderr
, "%u+%u records in\n", nifull
, nipartial
);
131 fprintf(stderr
, "%u+%u records out\n", nofull
, nopartial
);
132 if (ntr
) fprintf(stderr
, "%d truncated records\n", ntr
);
141 void (*convert
) (int);
199 for (j
= 0; j
< 13; j
++) mlen
[j
]++;
204 while (*pch
!= EOS
) {
233 if (is(",")) continue;
234 fprintf(stderr
, "dd: bad argument: %s\n",
238 if (*pch
== EOS
) continue;
240 fprintf(stderr
, "dd: bad argument: %s\n", pch
);
243 if ((convert
== null
) && (convflag
& (UCASE
| LCASE
))) convert
= cnull
;
244 if ((ifd
= ((ifilename
) ? open(ifilename
, O_RDONLY
) : dup(0))) < 0) {
245 fprintf(stderr
, "dd: Can't open %s: %s\n",
246 (ifilename
) ? ifilename
: "stdin", strerror(errno
));
249 oflags
= O_WRONLY
| O_CREAT
;
250 if (!seekseen
&& (convflag
& NOTRUNC
) != NOTRUNC
)
252 if ((ofd
= ((ofilename
) ? open(ofilename
, oflags
, 0666)
254 fprintf(stderr
, "dd: Can't open %s: %s\n",
255 (ofilename
) ? ofilename
: "stdout", strerror(errno
));
260 if (convert
== null
) flag
++;
263 fprintf(stderr
, "dd: ibs cannot be zero\n");
267 fprintf(stderr
, "dd: obs cannot be zero\n");
270 if ((ibuf
= sbrk(ibs
)) == (char *) -1) {
271 fprintf(stderr
, "dd: not enough memory\n");
274 if ((obuf
= (flag
) ? ibuf
: sbrk(obs
)) == (char *) -1) {
275 fprintf(stderr
, "dd: not enough memory\n");
280 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
) signal(SIGINT
, over
);
283 if (fstat(ifd
,&st
) < 0 || !(S_ISREG(st
.st_mode
) || S_ISBLK(st
.st_mode
))
284 || lseek(ifd
, (off_t
) ibs
* (off_t
) skip
, SEEK_SET
) == (off_t
) -1) {
286 if (read(ifd
, ibuf
, ibs
) == -1) {
288 "dd: Error skipping input: %s\n",
292 } while (--skip
!= 0);
296 if (lseek(ofd
, (off_t
) obs
* (off_t
) nseek
, SEEK_SET
) == (off_t
) -1) {
297 fprintf(stderr
, "dd: Seeking on output failed: %s\n",
306 if ((count
== 0) || ((nifull
+ nipartial
) != count
)) {
307 if (convflag
& (NOERROR
| SYNC
))
308 for (iptr
= ibuf
+ ibs
; iptr
> ibuf
;) *--iptr
= 0;
309 ibc
= read(ifd
, ibuf
, ibs
);
312 fprintf(stderr
, "dd: Read error: %s\n", strerror(errno
));
313 if ((convflag
& NOERROR
) == 0) {
318 for (i
= 0; i
< ibs
; i
++)
319 if (ibuf
[i
] != 0) ibc
= i
;
322 if ((ibc
== 0) && (--files
<= 0)) {
328 if (convflag
& SYNC
) ibc
= ibs
;
333 if ((convflag
& SWAB
) && i
) do {
357 if ((convflag
& UCASE
) && (c
>= 'a') &&
360 if ((convflag
& LCASE
) && (c
>= 'A') &&
397 signal(sig
, SIG_DFL
);