Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 3 | * dos2unix for BusyBox |
| 4 | * |
| 5 | * dos2unix '\n' convertor 0.5.0 |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 6 | * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997) |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 7 | * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>. |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * dos2unix filters reading input from stdin and writing output to stdout. |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 11 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 12 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 13 | */ |
| 14 | //config:config DOS2UNIX |
Denys Vlasenko | 7caf136 | 2016-11-23 14:52:19 +0100 | [diff] [blame] | 15 | //config: bool "dos2unix" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 16 | //config: default y |
| 17 | //config: help |
| 18 | //config: dos2unix is used to convert a text file from DOS format to |
| 19 | //config: UNIX format, and vice versa. |
| 20 | //config: |
| 21 | //config:config UNIX2DOS |
Denys Vlasenko | 7caf136 | 2016-11-23 14:52:19 +0100 | [diff] [blame] | 22 | //config: bool "unix2dos" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 23 | //config: default y |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 24 | //config: help |
| 25 | //config: unix2dos is used to convert a text file from UNIX format to |
| 26 | //config: DOS format, and vice versa. |
| 27 | |
| 28 | //applet:IF_DOS2UNIX(APPLET_NOEXEC(dos2unix, dos2unix, BB_DIR_USR_BIN, BB_SUID_DROP, dos2unix)) |
| 29 | //applet:IF_UNIX2DOS(APPLET_NOEXEC(unix2dos, dos2unix, BB_DIR_USR_BIN, BB_SUID_DROP, unix2dos)) |
| 30 | |
| 31 | //kbuild:lib-$(CONFIG_DOS2UNIX) += dos2unix.o |
| 32 | //kbuild:lib-$(CONFIG_UNIX2DOS) += dos2unix.o |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 33 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 34 | //usage:#define dos2unix_trivial_usage |
| 35 | //usage: "[-ud] [FILE]" |
| 36 | //usage:#define dos2unix_full_usage "\n\n" |
| 37 | //usage: "Convert FILE in-place from DOS to Unix format.\n" |
| 38 | //usage: "When no file is given, use stdin/stdout.\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 39 | //usage: "\n -u dos2unix" |
| 40 | //usage: "\n -d unix2dos" |
| 41 | //usage: |
| 42 | //usage:#define unix2dos_trivial_usage |
| 43 | //usage: "[-ud] [FILE]" |
| 44 | //usage:#define unix2dos_full_usage "\n\n" |
| 45 | //usage: "Convert FILE in-place from Unix to DOS format.\n" |
| 46 | //usage: "When no file is given, use stdin/stdout.\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 47 | //usage: "\n -u dos2unix" |
| 48 | //usage: "\n -d unix2dos" |
| 49 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 50 | #include "libbb.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 51 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 52 | /* This is a NOEXEC applet. Be very careful! */ |
| 53 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 54 | enum { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 55 | CT_UNIX2DOS = 1, |
| 56 | CT_DOS2UNIX |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 57 | }; |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 58 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 59 | /* if fn is NULL then input is stdin and output is stdout */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 60 | static void convert(char *fn, int conv_type) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 61 | { |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 62 | FILE *in, *out; |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 63 | int ch; |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 64 | char *temp_fn = temp_fn; /* for compiler */ |
| 65 | char *resolved_fn = resolved_fn; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 67 | in = stdin; |
| 68 | out = stdout; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 69 | if (fn != NULL) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 70 | struct stat st; |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 71 | int fd; |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 72 | |
| 73 | resolved_fn = xmalloc_follow_symlinks(fn); |
| 74 | if (resolved_fn == NULL) |
| 75 | bb_simple_perror_msg_and_die(fn); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 76 | in = xfopen_for_read(resolved_fn); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 77 | xfstat(fileno(in), &st, resolved_fn); |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 78 | |
| 79 | temp_fn = xasprintf("%sXXXXXX", resolved_fn); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 80 | fd = xmkstemp(temp_fn); |
| 81 | if (fchmod(fd, st.st_mode) == -1) |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 82 | bb_simple_perror_msg_and_die(temp_fn); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 83 | fchown(fd, st.st_uid, st.st_gid); |
Alexander Shishkin | 6722737 | 2010-10-22 13:27:16 +0200 | [diff] [blame] | 84 | |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 85 | out = xfdopen_for_write(fd); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 88 | while ((ch = fgetc(in)) != EOF) { |
| 89 | if (ch == '\r') |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 90 | continue; |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 91 | if (ch == '\n') |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 92 | if (conv_type == CT_UNIX2DOS) |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 93 | fputc('\r', out); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 94 | fputc(ch, out); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (fn != NULL) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 98 | if (fclose(in) < 0 || fclose(out) < 0) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 99 | unlink(temp_fn); |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 100 | bb_perror_nomsg_and_die(); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 101 | } |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 102 | xrename(temp_fn, resolved_fn); |
| 103 | free(temp_fn); |
| 104 | free(resolved_fn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 105 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 108 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 109 | int dos2unix_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 110 | { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 111 | int o, conv_type; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 112 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 113 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
Denys Vlasenko | 7caf136 | 2016-11-23 14:52:19 +0100 | [diff] [blame] | 114 | if (ENABLE_DOS2UNIX |
| 115 | && (!ENABLE_UNIX2DOS || applet_name[0] == 'd') |
| 116 | ) { |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 117 | conv_type = CT_DOS2UNIX; |
Denys Vlasenko | 7caf136 | 2016-11-23 14:52:19 +0100 | [diff] [blame] | 118 | } else { |
| 119 | conv_type = CT_UNIX2DOS; |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 120 | } |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 122 | /* -u convert to unix, -d convert to dos */ |
Denis Vlasenko | a0319ba | 2007-08-16 10:37:49 +0000 | [diff] [blame] | 123 | opt_complementary = "u--d:d--u"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 124 | o = getopt32(argv, "du"); |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 125 | |
| 126 | /* Do the conversion requested by an argument else do the default |
| 127 | * conversion depending on our name. */ |
| 128 | if (o) |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 129 | conv_type = o; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 130 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 131 | argv += optind; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 132 | do { |
| 133 | /* might be convert(NULL) if there is no filename given */ |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 134 | convert(*argv, conv_type); |
Denys Vlasenko | d1516c4 | 2010-07-17 22:43:42 +0200 | [diff] [blame] | 135 | } while (*argv && *++argv); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 137 | return 0; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 138 | } |