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 | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 27 | char *name_buf = name_buf; /* for compiler */ |
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) { |
Denis Vlasenko | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 32 | in = xfopen(fn, "r"); |
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 | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 38 | name_buf = xasprintf("%sXXXXXX", fn); |
| 39 | i = mkstemp(name_buf); |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 40 | if (i == -1 |
| 41 | || fchmod(i, 0600) == -1 |
| 42 | || !(out = fdopen(i, "w+")) |
| 43 | ) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 44 | bb_perror_nomsg_and_die(); |
| 45 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 48 | while ((i = fgetc(in)) != EOF) { |
| 49 | if (i == '\r') |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 50 | continue; |
Denis Vlasenko | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 51 | if (i == '\n') |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 52 | if (conv_type == CT_UNIX2DOS) |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 53 | fputc('\r', out); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 54 | fputc(i, out); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (fn != NULL) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 58 | if (fclose(in) < 0 || fclose(out) < 0) { |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 59 | unlink(name_buf); |
| 60 | bb_perror_nomsg_and_die(); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 61 | } |
Denis Vlasenko | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 62 | // TODO: destroys symlinks. See how passwd handles this |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 63 | xrename(name_buf, fn); |
Denis Vlasenko | cdf6277 | 2008-03-17 08:42:43 +0000 | [diff] [blame] | 64 | free(name_buf); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 65 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 68 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 69 | int dos2unix_main(int argc, char **argv) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 70 | { |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 71 | int o, conv_type; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 72 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 73 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 74 | conv_type = CT_UNIX2DOS; |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 75 | if (applet_name[0] == 'd') { |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 76 | conv_type = CT_DOS2UNIX; |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 77 | } |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 79 | /* -u convert to unix, -d convert to dos */ |
Denis Vlasenko | a0319ba | 2007-08-16 10:37:49 +0000 | [diff] [blame] | 80 | opt_complementary = "u--d:d--u"; /* mutually exclusive */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 81 | o = getopt32(argv, "du"); |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 82 | |
| 83 | /* Do the conversion requested by an argument else do the default |
| 84 | * conversion depending on our name. */ |
| 85 | if (o) |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 86 | conv_type = o; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 88 | do { |
| 89 | /* might be convert(NULL) if there is no filename given */ |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 90 | convert(argv[optind], conv_type); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 91 | optind++; |
| 92 | } while (optind < argc); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 5429636 | 2008-02-17 14:31:50 +0000 | [diff] [blame] | 94 | return 0; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 95 | } |