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; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 44 | int i; |
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; |
| 52 | |
| 53 | resolved_fn = xmalloc_follow_symlinks(fn); |
| 54 | if (resolved_fn == NULL) |
| 55 | bb_simple_perror_msg_and_die(fn); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 56 | in = xfopen_for_read(resolved_fn); |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 57 | fstat(fileno(in), &st); |
| 58 | |
| 59 | temp_fn = xasprintf("%sXXXXXX", resolved_fn); |
Alexander Shishkin | 6722737 | 2010-10-22 13:27:16 +0200 | [diff] [blame] | 60 | i = xmkstemp(temp_fn); |
| 61 | if (fchmod(i, st.st_mode) == -1) |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 62 | bb_simple_perror_msg_and_die(temp_fn); |
Alexander Shishkin | 6722737 | 2010-10-22 13:27:16 +0200 | [diff] [blame] | 63 | |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 64 | out = xfdopen_for_write(i); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 67 | while ((i = fgetc(in)) != EOF) { |
| 68 | if (i == '\r') |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 69 | continue; |
Denis Vlasenko | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 70 | if (i == '\n') |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 71 | if (conv_type == CT_UNIX2DOS) |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 72 | fputc('\r', out); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 73 | fputc(i, out); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (fn != NULL) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 77 | if (fclose(in) < 0 || fclose(out) < 0) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 78 | unlink(temp_fn); |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 79 | bb_perror_nomsg_and_die(); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 80 | } |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 81 | xrename(temp_fn, resolved_fn); |
| 82 | free(temp_fn); |
| 83 | free(resolved_fn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 84 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 87 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 88 | int dos2unix_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 89 | { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 90 | int o, conv_type; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 91 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 92 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 93 | conv_type = CT_UNIX2DOS; |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 94 | if (applet_name[0] == 'd') { |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 95 | conv_type = CT_DOS2UNIX; |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 96 | } |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 98 | /* -u convert to unix, -d convert to dos */ |
Denis Vlasenko | a0319ba | 2007-08-16 10:37:49 +0000 | [diff] [blame] | 99 | opt_complementary = "u--d:d--u"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 100 | o = getopt32(argv, "du"); |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 101 | |
| 102 | /* Do the conversion requested by an argument else do the default |
| 103 | * conversion depending on our name. */ |
| 104 | if (o) |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 105 | conv_type = o; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 106 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 107 | argv += optind; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 108 | do { |
| 109 | /* might be convert(NULL) if there is no filename given */ |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 110 | convert(*argv, conv_type); |
Denys Vlasenko | d1516c4 | 2010-07-17 22:43:42 +0200 | [diff] [blame] | 111 | } while (*argv && *++argv); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 113 | return 0; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 114 | } |