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. |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 13 | */ |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 14 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 15 | //usage:#define dos2unix_trivial_usage |
| 16 | //usage: "[-ud] [FILE]" |
| 17 | //usage:#define dos2unix_full_usage "\n\n" |
| 18 | //usage: "Convert FILE in-place from DOS to Unix format.\n" |
| 19 | //usage: "When no file is given, use stdin/stdout.\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 20 | //usage: "\n -u dos2unix" |
| 21 | //usage: "\n -d unix2dos" |
| 22 | //usage: |
| 23 | //usage:#define unix2dos_trivial_usage |
| 24 | //usage: "[-ud] [FILE]" |
| 25 | //usage:#define unix2dos_full_usage "\n\n" |
| 26 | //usage: "Convert FILE in-place from Unix to DOS format.\n" |
| 27 | //usage: "When no file is given, use stdin/stdout.\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 28 | //usage: "\n -u dos2unix" |
| 29 | //usage: "\n -d unix2dos" |
| 30 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 31 | #include "libbb.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 32 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 33 | /* This is a NOEXEC applet. Be very careful! */ |
| 34 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 35 | enum { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 36 | CT_UNIX2DOS = 1, |
| 37 | CT_DOS2UNIX |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 38 | }; |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 39 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 40 | /* if fn is NULL then input is stdin and output is stdout */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 41 | static void convert(char *fn, int conv_type) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 42 | { |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 43 | FILE *in, *out; |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 44 | int ch; |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 45 | char *temp_fn = temp_fn; /* for compiler */ |
| 46 | char *resolved_fn = resolved_fn; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 48 | in = stdin; |
| 49 | out = stdout; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 50 | if (fn != NULL) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 51 | struct stat st; |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 52 | int fd; |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 53 | |
| 54 | resolved_fn = xmalloc_follow_symlinks(fn); |
| 55 | if (resolved_fn == NULL) |
| 56 | bb_simple_perror_msg_and_die(fn); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 57 | in = xfopen_for_read(resolved_fn); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 58 | xfstat(fileno(in), &st, resolved_fn); |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 59 | |
| 60 | temp_fn = xasprintf("%sXXXXXX", resolved_fn); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 61 | fd = xmkstemp(temp_fn); |
| 62 | if (fchmod(fd, st.st_mode) == -1) |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 63 | bb_simple_perror_msg_and_die(temp_fn); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 64 | fchown(fd, st.st_uid, st.st_gid); |
Alexander Shishkin | 6722737 | 2010-10-22 13:27:16 +0200 | [diff] [blame] | 65 | |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 66 | out = xfdopen_for_write(fd); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 69 | while ((ch = fgetc(in)) != EOF) { |
| 70 | if (ch == '\r') |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 71 | continue; |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 72 | if (ch == '\n') |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 73 | if (conv_type == CT_UNIX2DOS) |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 74 | fputc('\r', out); |
Denys Vlasenko | c7e47cf | 2016-01-17 03:50:36 +0100 | [diff] [blame] | 75 | fputc(ch, out); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (fn != NULL) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 79 | if (fclose(in) < 0 || fclose(out) < 0) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 80 | unlink(temp_fn); |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 81 | bb_perror_nomsg_and_die(); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 82 | } |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 83 | xrename(temp_fn, resolved_fn); |
| 84 | free(temp_fn); |
| 85 | free(resolved_fn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 86 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 89 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 90 | int dos2unix_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 91 | { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 92 | int o, conv_type; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 93 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 94 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 95 | conv_type = CT_UNIX2DOS; |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 96 | if (applet_name[0] == 'd') { |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 97 | conv_type = CT_DOS2UNIX; |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 98 | } |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 100 | /* -u convert to unix, -d convert to dos */ |
Denis Vlasenko | a0319ba | 2007-08-16 10:37:49 +0000 | [diff] [blame] | 101 | opt_complementary = "u--d:d--u"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 102 | o = getopt32(argv, "du"); |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 103 | |
| 104 | /* Do the conversion requested by an argument else do the default |
| 105 | * conversion depending on our name. */ |
| 106 | if (o) |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 107 | conv_type = o; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 108 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 109 | argv += optind; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 110 | do { |
| 111 | /* might be convert(NULL) if there is no filename given */ |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 112 | convert(*argv, conv_type); |
Denys Vlasenko | d1516c4 | 2010-07-17 22:43:42 +0200 | [diff] [blame] | 113 | } while (*argv && *++argv); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 114 | |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 115 | return 0; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 116 | } |