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 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 16 | |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 17 | enum ConvType { |
| 18 | CT_UNIX2DOS = 1, |
| 19 | CT_DOS2UNIX |
| 20 | } ConvType; |
"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 */ |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 23 | static int convert(char *fn) |
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; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 27 | |
| 28 | if (fn != NULL) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 29 | in = xfopen(fn, "rw"); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 30 | /* |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 31 | The file is then created with mode read/write and |
| 32 | permissions 0666 for glibc 2.0.6 and earlier or |
| 33 | 0600 for glibc 2.0.7 and later. |
| 34 | */ |
| 35 | snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 36 | /* |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 37 | sizeof bb_common_bufsiz1 is 4096, so it should be big enough to |
| 38 | hold the full path. However if the output is truncated the |
| 39 | subsequent call to mkstemp would fail. |
| 40 | */ |
| 41 | if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1 |
| 42 | || chmod(bb_common_bufsiz1, 0600) == -1) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 43 | bb_perror_nomsg_and_die(); |
| 44 | } |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 45 | out = fdopen(i, "w+"); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 46 | if (!out) { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 47 | close(i); |
| 48 | remove(bb_common_bufsiz1); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 49 | } |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 50 | } else { |
| 51 | in = stdin; |
| 52 | out = stdout; |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 55 | while ((i = fgetc(in)) != EOF) { |
| 56 | if (i == '\r') |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 57 | continue; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 58 | if (i == '\n') { |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 59 | if (ConvType == CT_UNIX2DOS) |
| 60 | fputc('\r', out); |
| 61 | fputc('\n', out); |
| 62 | continue; |
| 63 | } |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 64 | fputc(i, out); |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (fn != NULL) { |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 68 | if (fclose(in) < 0 || fclose(out) < 0) { |
| 69 | bb_perror_nomsg(); |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 70 | remove(bb_common_bufsiz1); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 71 | return -2; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 72 | } |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 73 | /* Assume they are both on the same filesystem (which |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 74 | * should be true since we put them into the same directory |
| 75 | * so we _should_ be ok, but you never know... */ |
| 76 | if (rename(bb_common_bufsiz1, fn) < 0) { |
| 77 | bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn); |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 78 | return -1; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 79 | } |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 85 | int dos2unix_main(int argc, char *argv[]) |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 86 | { |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 87 | int o; |
| 88 | |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 89 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 90 | if (applet_name[0] == 'd') { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 91 | ConvType = CT_DOS2UNIX; /*2 */ |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 92 | } else { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 93 | ConvType = CT_UNIX2DOS; /*1 */ |
Eric Andersen | 655584b | 2001-07-25 07:22:55 +0000 | [diff] [blame] | 94 | } |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 95 | /* -u and -d are mutally exclusive */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 96 | opt_complementary = "?:u--d:d--u"; |
Rob Landley | f815469 | 2005-09-01 03:11:19 +0000 | [diff] [blame] | 97 | /* process parameters */ |
Rob Landley | 330ac85 | 2006-03-14 21:49:18 +0000 | [diff] [blame] | 98 | /* -u convert to unix */ |
| 99 | /* -d convert to dos */ |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 100 | o = getopt32(argc, 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) |
| 105 | ConvType = o; |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 106 | |
| 107 | if (optind < argc) { |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 108 | while (optind < argc) |
| 109 | if ((o = convert(argv[optind++])) < 0) |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 110 | break; |
Bernhard Reutner-Fischer | b5353a2 | 2006-06-01 18:30:42 +0000 | [diff] [blame] | 111 | } else |
| 112 | o = convert(NULL); |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 113 | |
| 114 | return o; |
| 115 | } |