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 | * |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 12 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 17 | enum { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 18 | CT_UNIX2DOS = 1, |
| 19 | CT_DOS2UNIX |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 20 | }; |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 21 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 22 | /* if fn is NULL then input is stdin and output is stdout */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 23 | static void convert(char *fn, int conv_type) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 24 | { |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 25 | FILE *in, *out; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 26 | int i; |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 27 | char *temp_fn = temp_fn; /* for compiler */ |
| 28 | char *resolved_fn = resolved_fn; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 30 | in = stdin; |
| 31 | out = stdout; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 32 | if (fn != NULL) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 33 | struct stat st; |
| 34 | |
| 35 | resolved_fn = xmalloc_follow_symlinks(fn); |
| 36 | if (resolved_fn == NULL) |
| 37 | bb_simple_perror_msg_and_die(fn); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 38 | in = xfopen_for_read(resolved_fn); |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 39 | fstat(fileno(in), &st); |
| 40 | |
| 41 | temp_fn = xasprintf("%sXXXXXX", resolved_fn); |
| 42 | i = mkstemp(temp_fn); |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 43 | if (i == -1 |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 44 | || fchmod(i, st.st_mode) == -1 |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 45 | ) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 46 | bb_simple_perror_msg_and_die(temp_fn); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 47 | } |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 48 | out = xfdopen_for_write(i); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 51 | while ((i = fgetc(in)) != EOF) { |
| 52 | if (i == '\r') |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 53 | continue; |
Denis Vlasenko | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 54 | if (i == '\n') |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 55 | if (conv_type == CT_UNIX2DOS) |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 56 | fputc('\r', out); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 57 | fputc(i, out); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (fn != NULL) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 61 | if (fclose(in) < 0 || fclose(out) < 0) { |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 62 | unlink(temp_fn); |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 63 | bb_perror_nomsg_and_die(); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 64 | } |
Denis Vlasenko | a1767a1 | 2008-06-14 04:28:41 +0000 | [diff] [blame] | 65 | xrename(temp_fn, resolved_fn); |
| 66 | free(temp_fn); |
| 67 | free(resolved_fn); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 68 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 71 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 72 | int dos2unix_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 73 | { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 74 | int o, conv_type; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 75 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 76 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 77 | conv_type = CT_UNIX2DOS; |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 78 | if (applet_name[0] == 'd') { |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 79 | conv_type = CT_DOS2UNIX; |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 80 | } |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 82 | /* -u convert to unix, -d convert to dos */ |
Denis Vlasenko | a0319ba | 2007-08-16 10:37:49 +0000 | [diff] [blame] | 83 | opt_complementary = "u--d:d--u"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 84 | o = getopt32(argv, "du"); |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 85 | |
| 86 | /* Do the conversion requested by an argument else do the default |
| 87 | * conversion depending on our name. */ |
| 88 | if (o) |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 89 | conv_type = o; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 91 | argv += optind; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 92 | do { |
| 93 | /* might be convert(NULL) if there is no filename given */ |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 94 | convert(*argv, conv_type); |
| 95 | } while (*++argv); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 97 | return 0; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 98 | } |