comparison toys/other/dos2unix.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children 6cc69be43c42
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* dos2unix.c - convert newline format
2 *
3 * dos2unix.c - convert newline format
4 * 2 *
5 * Copyright 2012 Rob Landley <rob@landley.net> 3 * Copyright 2012 Rob Landley <rob@landley.net>
6 4
7 USE_DOS2UNIX(NEWTOY(dos2unix, NULL, TOYFLAG_BIN)) 5 USE_DOS2UNIX(NEWTOY(dos2unix, NULL, TOYFLAG_BIN))
8 USE_DOS2UNIX(OLDTOY(unix2dos, dos2unix, NULL, TOYFLAG_BIN)) 6 USE_DOS2UNIX(OLDTOY(unix2dos, dos2unix, NULL, TOYFLAG_BIN))
9 7
10 config DOS2UNIX 8 config DOS2UNIX
11 bool "dos2unix/unix2dos" 9 bool "dos2unix/unix2dos"
12 default y 10 default y
13 help 11 help
14 usage: dos2unix/unix2dos [file...] 12 usage: dos2unix/unix2dos [file...]
15 13
16 Convert newline format between dos (\r\n) and unix (just \n) 14 Convert newline format between dos (\r\n) and unix (just \n)
17 If no files listed copy from stdin, "-" is a synonym for stdin. 15 If no files listed copy from stdin, "-" is a synonym for stdin.
18 */ 16 */
19 17
20 #define FOR_dos2unix 18 #define FOR_dos2unix
21 #include "toys.h" 19 #include "toys.h"
22 20
23 GLOBALS( 21 GLOBALS(
24 char *tempfile; 22 char *tempfile;
25 ) 23 )
26 24
27 static void do_dos2unix(int fd, char *name) 25 static void do_dos2unix(int fd, char *name)
28 { 26 {
29 char c = toys.which->name[0]; 27 char c = toys.which->name[0];
30 int outfd = 1, catch = 0; 28 int outfd = 1, catch = 0;
31 29
32 if (fd) outfd = copy_tempfile(fd, name, &TT.tempfile); 30 if (fd) outfd = copy_tempfile(fd, name, &TT.tempfile);
33 31
34 for (;;) { 32 for (;;) {
35 int len, in, out; 33 int len, in, out;
36 34
37 len = read(fd, toybuf+(sizeof(toybuf)/2), sizeof(toybuf)/2); 35 len = read(fd, toybuf+(sizeof(toybuf)/2), sizeof(toybuf)/2);
38 if (len<0) { 36 if (len<0) {
39 perror_msg("%s",name); 37 perror_msg("%s",name);
40 toys.exitval = 1; 38 toys.exitval = 1;
41 } 39 }
42 if (len<1) break; 40 if (len<1) break;
43 41
44 for (in = out = 0; in < len; in++) { 42 for (in = out = 0; in < len; in++) {
45 char x = toybuf[in+sizeof(toybuf)/2]; 43 char x = toybuf[in+sizeof(toybuf)/2];
46 44
47 // Drop \r only if followed by \n in dos2unix mode 45 // Drop \r only if followed by \n in dos2unix mode
48 if (catch) { 46 if (catch) {
49 if (c == 'u' || x != '\n') toybuf[out++] = '\r'; 47 if (c == 'u' || x != '\n') toybuf[out++] = '\r';
50 catch = 0; 48 catch = 0;
51 // Add \r only if \n not after \r in unix2dos mode 49 // Add \r only if \n not after \r in unix2dos mode
52 } else if (c == 'u' && x == '\n') toybuf[out++] = '\r'; 50 } else if (c == 'u' && x == '\n') toybuf[out++] = '\r';
53 51
54 if (x == '\r') catch++; 52 if (x == '\r') catch++;
55 else toybuf[out++] = x; 53 else toybuf[out++] = x;
56 } 54 }
57 xwrite(outfd, toybuf, out); 55 xwrite(outfd, toybuf, out);
58 } 56 }
59 if (catch) xwrite(outfd, "\r", 1); 57 if (catch) xwrite(outfd, "\r", 1);
60 58
61 if (fd) replace_tempfile(-1, outfd, &TT.tempfile); 59 if (fd) replace_tempfile(-1, outfd, &TT.tempfile);
62 } 60 }
63 61
64 void dos2unix_main(void) 62 void dos2unix_main(void)
65 { 63 {
66 loopfiles(toys.optargs, do_dos2unix); 64 loopfiles(toys.optargs, do_dos2unix);
67 } 65 }