Deleted Added
sdiff udiff text old ( 50471 ) new ( 51208 )
full compact
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
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.
8 *

--- 26 unchanged lines hidden (view full) ---

35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
41#endif
42static const char rcsid[] =
43 "$FreeBSD: head/bin/dd/conv.c 50471 1999-08-27 23:15:48Z peter $";
44#endif /* not lint */
45
46#include <sys/param.h>
47
48#include <err.h>
49#include <string.h>
50
51#include "dd.h"
52#include "extern.h"
53
54/*
55 * def --
56 * Copy input to output. Input is buffered until reaches obs, and then
57 * output until less than obs remains. Only a single buffer is used.
58 * Worst case buffer calculation is (ibs + obs - 1).
59 */
60void
61def()
62{
63 int cnt;
64 u_char *inp, *t;
65
66 if ((t = ctab) != NULL)
67 for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
68 *inp = t[*inp];
69
70 /* Make the output buffer look right. */
71 out.dbp = in.dbp;
72 out.dbcnt = in.dbcnt;

--- 25 unchanged lines hidden (view full) ---

98 * bytes to output. Records less than cbs are padded with spaces.
99 *
100 * max in buffer: MAX(ibs, cbsz)
101 * max out buffer: obs + cbsz
102 */
103void
104block()
105{
106 static int intrunc;
107 int ch, cnt, maxlen;
108 u_char *inp, *outp, *t;
109
110 /*
111 * Record truncation can cross block boundaries. If currently in a
112 * truncation state, keep tossing characters until reach a newline.
113 * Start at the beginning of the buffer, as the input buffer is always
114 * left empty.
115 */
116 if (intrunc) {

--- 25 unchanged lines hidden (view full) ---

142 for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
143 ++cnt)
144 *outp++ = ch;
145 /*
146 * Check for short record without a newline. Reassemble the
147 * input block.
148 */
149 if (ch != '\n' && in.dbcnt < cbsz) {
150 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
151 break;
152 }
153
154 /* Adjust the input buffer numbers. */
155 in.dbcnt -= cnt;
156 if (ch == '\n')
157 --in.dbcnt;
158

--- 33 unchanged lines hidden (view full) ---

192 * Don't worry about truncation or translation, the input buffer is
193 * always empty when truncating, and no characters have been added for
194 * translation. The bottom line is that anything left in the input
195 * buffer is a truncated record. Anything left in the output buffer
196 * just wasn't big enough.
197 */
198 if (in.dbcnt) {
199 ++st.trunc;
200 memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
201 (void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
202 cbsz - in.dbcnt);
203 out.dbcnt += cbsz;
204 }
205}
206
207/*
208 * Convert fixed length (cbsz) records to variable length. Deletes any
209 * trailing blanks and appends a newline.
210 *
211 * max in buffer: MAX(ibs, cbsz) + cbsz
212 * max out buffer: obs + cbsz
213 */
214void
215unblock()
216{
217 int cnt;
218 u_char *inp, *t;
219
220 /* Translation and case conversion. */
221 if ((t = ctab) != NULL)
222 for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
223 *--inp = t[*inp];
224 /*
225 * Copy records (max cbsz size chunks) into the output buffer. The
226 * translation has to already be done or we might not recognize the
227 * spaces.
228 */
229 for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
230 for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
231 ;
232 if (t >= inp) {
233 cnt = t - inp + 1;
234 memmove(out.dbp, inp, cnt);
235 out.dbp += cnt;
236 out.dbcnt += cnt;
237 }
238 *out.dbp++ = '\n';
239 if (++out.dbcnt >= out.dbsz)
240 dd_out(0);
241 }
242 if (in.dbcnt)
243 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
244 in.dbp = in.db + in.dbcnt;
245}
246
247void
248unblock_close()
249{
250 int cnt;
251 u_char *t;
252
253 if (in.dbcnt) {
254 warnx("%s: short input record", in.name);
255 for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
256 ;
257 if (t >= in.db) {
258 cnt = t - in.db + 1;
259 memmove(out.dbp, in.db, cnt);
260 out.dbp += cnt;
261 out.dbcnt += cnt;
262 }
263 ++out.dbcnt;
264 *out.dbp++ = '\n';
265 }
266}