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 | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 23 | static int 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 | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 27 | #define name_buf bb_common_bufsiz1 |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 29 | in = stdin; |
| 30 | out = stdout; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 31 | if (fn != NULL) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 32 | in = xfopen(fn, "rw"); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 33 | /* |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 34 | The file is then created with mode read/write and |
| 35 | permissions 0666 for glibc 2.0.6 and earlier or |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 36 | 0600 for glibc 2.0.7 and later. |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 37 | */ |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 38 | snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn); |
| 39 | i = mkstemp(&name_buf[0]); |
| 40 | if (i == -1 || chmod(name_buf, 0600) == -1) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 41 | bb_perror_nomsg_and_die(); |
| 42 | } |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 43 | out = fdopen(i, "w+"); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 44 | if (!out) { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 45 | close(i); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 46 | remove(name_buf); |
| 47 | return -2; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 48 | } |
| 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; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +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); |
| 57 | fputc('\n', out); |
| 58 | continue; |
| 59 | } |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 60 | fputc(i, out); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | if (fn != NULL) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 64 | if (fclose(in) < 0 || fclose(out) < 0) { |
| 65 | bb_perror_nomsg(); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 66 | remove(name_buf); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 67 | return -2; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 68 | } |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 69 | /* Assume they are both on the same filesystem (which |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 70 | * should be true since we put them into the same directory |
| 71 | * so we _should_ be ok, but you never know... */ |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 72 | if (rename(name_buf, fn) < 0) { |
| 73 | bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 74 | return -1; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 75 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 81 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 82 | int dos2unix_main(int argc, char **argv) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 83 | { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 84 | int o, conv_type; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 85 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 86 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 87 | if (applet_name[0] == 'd') { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 88 | conv_type = CT_DOS2UNIX; /* 2 */ |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 89 | } else { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 90 | conv_type = CT_UNIX2DOS; /* 1 */ |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 91 | } |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 92 | |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 93 | /* -u convert to unix, -d convert to dos */ |
Denis Vlasenko | a0319ba | 2007-08-16 10:37:49 +0000 | [diff] [blame] | 94 | opt_complementary = "u--d:d--u"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 95 | o = getopt32(argv, "du"); |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 96 | |
| 97 | /* Do the conversion requested by an argument else do the default |
| 98 | * conversion depending on our name. */ |
| 99 | if (o) |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 100 | conv_type = o; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 102 | do { |
| 103 | /* might be convert(NULL) if there is no filename given */ |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 104 | o = convert(argv[optind], conv_type); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 105 | if (o < 0) |
| 106 | break; |
| 107 | optind++; |
| 108 | } while (optind < argc); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 109 | |
| 110 | return o; |
| 111 | } |