Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 2 | * dos2unix for BusyBox |
| 3 | * |
| 4 | * dos2unix '\n' convertor 0.5.0 |
| 5 | * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997) |
| 6 | * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>. |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * dos2unix filters reading input from stdin and writing output to stdout. |
| 10 | * Without arguments it reverts the format (e.i. if source is in UNIX format, |
| 11 | * output is in DOS format and vice versa). |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version 2 |
| 16 | * of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 26 | * |
| 27 | * See the COPYING file for license information. |
| 28 | */ |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 29 | |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 30 | #include <string.h> |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 31 | #include <getopt.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 33 | |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 34 | // if fn is NULL then input is stdin and output is stdout |
Matt Kraai | 1e04ea3 | 2001-04-12 21:38:06 +0000 | [diff] [blame] | 35 | static int convert(char *fn, int ConvType) { |
Glenn L McGrath | a6ce670 | 2001-04-12 02:26:04 +0000 | [diff] [blame] | 36 | char c; |
| 37 | char *tempFn = NULL; |
| 38 | FILE *in = stdin, *out = stdout; |
| 39 | |
| 40 | if (fn != NULL) { |
| 41 | if ((in = wfopen(fn, "r")) == NULL) { |
| 42 | return -1; |
| 43 | } |
| 44 | if ((out = tmpfile()) == NULL) { |
| 45 | perror_msg(NULL); |
| 46 | return -2; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | while ((c = fgetc(in)) != EOF) { |
| 51 | if (c == '\r') { |
| 52 | if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { |
| 53 | // file is alredy in DOS format so it is not necessery to touch it |
| 54 | if (fclose(in) < 0 || fclose(out) < 0) { |
| 55 | perror_msg(NULL); |
| 56 | return -2; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | if (!ConvType) |
| 61 | ConvType = CT_DOS2UNIX; |
| 62 | break; |
| 63 | } |
| 64 | if (c == '\n') { |
| 65 | if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { |
| 66 | // file is alredy in UNIX format so it is not necessery to touch it |
| 67 | if ((fclose(in) < 0) || (fclose(out) < 0)) { |
| 68 | perror_msg(NULL); |
| 69 | return -2; |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | if (!ConvType) { |
| 74 | ConvType = CT_UNIX2DOS; |
| 75 | } |
| 76 | if (ConvType == CT_UNIX2DOS) { |
| 77 | fputc('\r', out); |
| 78 | } |
| 79 | fputc('\n', out); |
| 80 | break; |
| 81 | } |
| 82 | fputc(c, out); |
| 83 | } |
| 84 | if (c != EOF) |
| 85 | while ((c = fgetc(in)) != EOF) { |
| 86 | if (c == '\r') |
| 87 | continue; |
| 88 | if (c == '\n') { |
| 89 | if (ConvType == CT_UNIX2DOS) |
| 90 | fputc('\r', out); |
| 91 | fputc('\n', out); |
| 92 | continue; |
| 93 | } |
| 94 | fputc(c, out); |
| 95 | } |
| 96 | |
| 97 | if (fn != NULL) { |
| 98 | if (fclose(in) < 0 || fclose(out) < 0 || |
| 99 | (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { |
| 100 | perror_msg(NULL); |
| 101 | return -2; |
| 102 | } |
| 103 | |
| 104 | while ((c = fgetc(in)) != EOF) { |
| 105 | fputc(c, out); |
| 106 | } |
| 107 | |
| 108 | if ((fclose(in) < 0) || (fclose(out) < 0)) { |
| 109 | perror_msg(NULL); |
| 110 | return -2; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 117 | int dos2unix_main(int argc, char *argv[]) { |
| 118 | int ConvType = CT_AUTO; |
| 119 | int o; |
| 120 | |
| 121 | // process parameters |
| 122 | while ((o = getopt(argc, argv, "du")) != EOF) { |
| 123 | switch (o) { |
| 124 | case 'd': |
| 125 | ConvType = CT_UNIX2DOS; |
| 126 | break; |
| 127 | case 'u': |
| 128 | ConvType = CT_DOS2UNIX; |
| 129 | break; |
| 130 | default: |
| 131 | show_usage(); |
| 132 | } |
Eric Andersen | cff3fe3 | 2000-09-20 19:22:26 +0000 | [diff] [blame] | 133 | } |
Eric Andersen | 544891d | 2001-02-22 23:37:30 +0000 | [diff] [blame] | 134 | |
| 135 | if (optind < argc) { |
| 136 | while(optind < argc) |
| 137 | if ((o = convert(argv[optind++], ConvType)) < 0) |
| 138 | break; |
| 139 | } |
| 140 | else |
| 141 | o = convert(NULL, ConvType); |
| 142 | |
| 143 | return o; |
| 144 | } |
| 145 | |