annotate toys/other/dos2unix.c @ 877:37e668afd008

Isaac Dunham pointed out that the kernel treats - and _ as identical in module names, so modinfo should too. Made it use mmap() while I was there, and some cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Wed, 24 Apr 2013 03:04:31 -0500
parents 6cc69be43c42
children 5fac2769a159
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* dos2unix.c - convert newline format
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Rob Landley <rob@landley.net>
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
4
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 USE_DOS2UNIX(NEWTOY(dos2unix, NULL, TOYFLAG_BIN))
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 USE_DOS2UNIX(OLDTOY(unix2dos, dos2unix, NULL, TOYFLAG_BIN))
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 config DOS2UNIX
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
9 bool "dos2unix/unix2dos"
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
10 default y
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
11 help
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
12 usage: dos2unix/unix2dos [file...]
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
13
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
14 Convert newline format between dos (\r\n) and unix (just \n)
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
15 If no files listed copy from stdin, "-" is a synonym for stdin.
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 */
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
18 #define FOR_dos2unix
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 #include "toys.h"
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
20
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
21 GLOBALS(
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
22 char *tempfile;
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 )
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 static void do_dos2unix(int fd, char *name)
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
27 char c = toys.which->name[0];
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
28 int outfd = 1, catch = 0;
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
29
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
30 if (fd) outfd = copy_tempfile(fd, name, &TT.tempfile);
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
32 for (;;) {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
33 int len, in, out;
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
35 len = read(fd, toybuf+(sizeof(toybuf)/2), sizeof(toybuf)/2);
780
6cc69be43c42 Have error_msg() and friends set TT.exitval to 1 if it's still 0, clean out other places that were setting it that no longer need to.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
36 if (len<0) perror_msg("%s",name);
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
37 if (len<1) break;
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
38
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
39 for (in = out = 0; in < len; in++) {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
40 char x = toybuf[in+sizeof(toybuf)/2];
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
41
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
42 // Drop \r only if followed by \n in dos2unix mode
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
43 if (catch) {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
44 if (c == 'u' || x != '\n') toybuf[out++] = '\r';
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
45 catch = 0;
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
46 // Add \r only if \n not after \r in unix2dos mode
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
47 } else if (c == 'u' && x == '\n') toybuf[out++] = '\r';
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
48
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
49 if (x == '\r') catch++;
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
50 else toybuf[out++] = x;
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
51 }
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
52 xwrite(outfd, toybuf, out);
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
53 }
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
54 if (catch) xwrite(outfd, "\r", 1);
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
55
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
56 if (fd) replace_tempfile(-1, outfd, &TT.tempfile);
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 }
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
58
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 void dos2unix_main(void)
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 {
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.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
61 loopfiles(toys.optargs, do_dos2unix);
644
6a096902309d Add dos2unix/unix2dos, remove old wrapper versions.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 }