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#define FOR_dos2unix
28#include "toys.h"
29
30GLOBALS(
31 char *tempfile;
32)
33
34static void do_dos2unix(int fd, char *name)
35{
36 char c = toys.which->name[0];
37 int outfd = 1, catch = 0;
38
39 if (fd) outfd = copy_tempfile(fd, name, &TT.tempfile);
40
41 for (;;) {
42 int len, in, out;
43
44 len = read(fd, toybuf+(sizeof(toybuf)/2), sizeof(toybuf)/2);
45 if (len<0) perror_msg_raw(name);
46 if (len<1) break;
47
48 for (in = out = 0; in < len; in++) {
49 char x = toybuf[in+sizeof(toybuf)/2];
50
51
52 if (catch) {
53 if (c == 'u' || x != '\n') toybuf[out++] = '\r';
54 catch = 0;
55
56 } else if (c == 'u' && x == '\n') toybuf[out++] = '\r';
57
58 if (x == '\r') catch++;
59 else toybuf[out++] = x;
60 }
61 xwrite(outfd, toybuf, out);
62 }
63 if (catch) xwrite(outfd, "\r", 1);
64
65 if (fd) replace_tempfile(-1, outfd, &TT.tempfile);
66}
67
68void dos2unix_main(void)
69{
70 loopfiles(toys.optargs, do_dos2unix);
71}
72
73void unix2dos_main(void)
74{
75 dos2unix_main();
76}
77