2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
41 static char sccsid
[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
43 __used
static const char rcsid
[] =
44 "$FreeBSD: src/bin/dd/conv.c,v 1.16 2002/02/02 06:24:12 imp Exp $";
47 #include <sys/param.h>
57 * Copy input to output. Input is buffered until reaches obs, and then
58 * output until less than obs remains. Only a single buffer is used.
59 * Worst case buffer calculation is (ibs + obs - 1).
68 if ((t
= ctab
) != NULL
)
69 for (inp
= in
.dbp
- (cnt
= in
.dbrcnt
); cnt
--; ++inp
)
72 /* Make the output buffer look right. */
76 if (in
.dbcnt
>= out
.dbsz
) {
77 /* If the output buffer is full, write it. */
81 * Ddout copies the leftover output to the beginning of
82 * the buffer and resets the output buffer. Reset the
83 * input buffer to match it.
93 /* Just update the count, everything is already in the buffer. */
99 * Copy variable length newline terminated records with a max size cbsz
100 * bytes to output. Records less than cbs are padded with spaces.
102 * max in buffer: MAX(ibs, cbsz)
103 * max out buffer: obs + cbsz
115 * Record truncation can cross block boundaries. If currently in a
116 * truncation state, keep tossing characters until reach a newline.
117 * Start at the beginning of the buffer, as the input buffer is always
121 for (inp
= in
.db
, cnt
= in
.dbrcnt
; cnt
&& *inp
++ != '\n'; --cnt
)
129 /* Adjust the input buffer numbers. */
131 in
.dbp
= inp
+ cnt
- 1;
135 * Copy records (max cbsz size chunks) into the output buffer. The
136 * translation is done as we copy into the output buffer.
139 for (inp
= in
.dbp
- in
.dbcnt
, outp
= out
.dbp
; in
.dbcnt
;) {
140 maxlen
= MIN(cbsz
, in
.dbcnt
);
141 if ((t
= ctab
) != NULL
)
142 for (cnt
= 0; cnt
< maxlen
&& (ch
= *inp
++) != '\n';
146 for (cnt
= 0; cnt
< maxlen
&& (ch
= *inp
++) != '\n';
150 * Check for short record without a newline. Reassemble the
153 if (ch
!= '\n' && in
.dbcnt
< cbsz
) {
154 (void)memmove(in
.db
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
158 /* Adjust the input buffer numbers. */
163 /* Pad short records with spaces. */
165 (void)memset(outp
, ctab ? ctab
[' '] : ' ', cbsz
- cnt
);
168 * If the next character wouldn't have ended the
169 * block, it's a truncation.
171 if (!in
.dbcnt
|| *inp
!= '\n')
174 /* Toss characters to a newline. */
175 for (; in
.dbcnt
&& *inp
++ != '\n'; --in
.dbcnt
);
182 /* Adjust output buffer numbers. */
184 if ((out
.dbcnt
+= cbsz
) >= out
.dbsz
)
188 in
.dbp
= in
.db
+ in
.dbcnt
;
195 * Copy any remaining data into the output buffer and pad to a record.
196 * Don't worry about truncation or translation, the input buffer is
197 * always empty when truncating, and no characters have been added for
198 * translation. The bottom line is that anything left in the input
199 * buffer is a truncated record. Anything left in the output buffer
200 * just wasn't big enough.
204 (void)memmove(out
.dbp
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
205 (void)memset(out
.dbp
+ in
.dbcnt
, ctab ? ctab
[' '] : ' ',
212 * Convert fixed length (cbsz) records to variable length. Deletes any
213 * trailing blanks and appends a newline.
215 * max in buffer: MAX(ibs, cbsz) + cbsz
216 * max out buffer: obs + cbsz
225 /* Translation and case conversion. */
226 if ((t
= ctab
) != NULL
)
227 for (cnt
= in
.dbrcnt
, inp
= in
.dbp
; cnt
--;) {
232 * Copy records (max cbsz size chunks) into the output buffer. The
233 * translation has to already be done or we might not recognize the
236 for (inp
= in
.db
; in
.dbcnt
>= cbsz
; inp
+= cbsz
, in
.dbcnt
-= cbsz
) {
237 for (t
= inp
+ cbsz
- 1; t
>= inp
&& *t
== ' '; --t
)
241 (void)memmove(out
.dbp
, inp
, cnt
);
246 if (++out
.dbcnt
>= out
.dbsz
)
250 (void)memmove(in
.db
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
251 in
.dbp
= in
.db
+ in
.dbcnt
;
261 warnx("%s: short input record", in
.name
);
262 for (t
= in
.db
+ in
.dbcnt
- 1; t
>= in
.db
&& *t
== ' '; --t
)
266 (void)memmove(out
.dbp
, in
.db
, cnt
);