diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-02-22 23:37:30 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-02-22 23:37:30 +0000 |
commit | 544891dd2606ad3beb0aed65feaacc7a100c9dd2 (patch) | |
tree | b6b975d748efd73b474be62572b79f3c4b132738 /coreutils | |
parent | adea7a6101ca46d83c4c2327d349c3ca3a5c7601 (diff) | |
download | busybox-w32-544891dd2606ad3beb0aed65feaacc7a100c9dd2.tar.gz busybox-w32-544891dd2606ad3beb0aed65feaacc7a100c9dd2.tar.bz2 busybox-w32-544891dd2606ad3beb0aed65feaacc7a100c9dd2.zip |
Add in kent robotti's updated dos2unix.c
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/dos2unix.c | 182 |
1 files changed, 144 insertions, 38 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index 222c8f6a4..7f1c5617d 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c | |||
@@ -1,46 +1,152 @@ | |||
1 | /* | 1 | /* |
2 | Mini dos2unix implementation for busybox | 2 | * dos2unix for BusyBox |
3 | 3 | * | |
4 | Copyright 1994,1995 Patrick Volkerding, Moorhead, Minnesota USA | 4 | * dos2unix '\n' convertor 0.5.0 |
5 | All rights reserved. | 5 | * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997) |
6 | 6 | * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>. | |
7 | Redistribution and use of this source code, with or without modification, is | 7 | * All rights reserved. |
8 | permitted provided that the following condition is met: | 8 | * |
9 | 9 | * dos2unix filters reading input from stdin and writing output to stdout. | |
10 | 1. Redistributions of this source code must retain the above copyright | 10 | * Without arguments it reverts the format (e.i. if source is in UNIX format, |
11 | notice, this condition, and the following disclaimer. | 11 | * output is in DOS format and vice versa). |
12 | 12 | * | |
13 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 13 | * This program is free software; you can redistribute it and/or |
14 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | 14 | * modify it under the terms of the GNU General Public License |
15 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 15 | * as published by the Free Software Foundation; either version 2 |
16 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 16 | * of the License, or (at your option) any later version. |
17 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 17 | * |
18 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 18 | * This program is distributed in the hope that it will be useful, |
19 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 21 | * GNU General Public License for more details. |
22 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 | * |
23 | */ | 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 | */ | ||
24 | 29 | ||
25 | #include <stdio.h> | 30 | #include <stdio.h> |
26 | #include <stdlib.h> | 31 | #include <stdlib.h> |
32 | #include <string.h> | ||
33 | #include <errno.h> | ||
34 | #include <getopt.h> | ||
27 | #include "busybox.h" | 35 | #include "busybox.h" |
28 | 36 | ||
29 | int dos2unix_main( int argc, char **argv ) { | 37 | #define CT_AUTO 0 |
30 | int c; | 38 | #define CT_UNIX2DOS 1 |
31 | if (argc > 1) { | 39 | #define CT_DOS2UNIX 2 |
32 | c = *argv[1]; | 40 | |
33 | if (c == '-') { | 41 | int convert(char *fn, int ConvType); |
34 | show_usage(); | 42 | |
35 | } | 43 | int dos2unix_main(int argc, char *argv[]) { |
44 | int ConvType = CT_AUTO; | ||
45 | int o; | ||
46 | |||
47 | // process parameters | ||
48 | while ((o = getopt(argc, argv, "du")) != EOF) { | ||
49 | switch (o) { | ||
50 | case 'd': | ||
51 | ConvType = CT_UNIX2DOS; | ||
52 | break; | ||
53 | case 'u': | ||
54 | ConvType = CT_DOS2UNIX; | ||
55 | break; | ||
56 | default: | ||
57 | show_usage(); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | if (optind < argc) { | ||
62 | while(optind < argc) | ||
63 | if ((o = convert(argv[optind++], ConvType)) < 0) | ||
64 | break; | ||
65 | } | ||
66 | else | ||
67 | o = convert(NULL, ConvType); | ||
68 | |||
69 | return o; | ||
70 | } | ||
71 | |||
72 | // if fn is NULL then input is stdin and output is stdout | ||
73 | int convert(char *fn, int ConvType) { | ||
74 | char c; | ||
75 | char *tempFn = NULL; | ||
76 | FILE *in = stdin, *out = stdout; | ||
77 | |||
78 | if (fn != NULL) { | ||
79 | if ((in = fopen(fn, "r")) == NULL) { | ||
80 | perror_msg(fn); | ||
81 | return -1; | ||
82 | } | ||
83 | tempFn = tmpnam(NULL); | ||
84 | if (tempFn == NULL || (out = fopen(tempFn, "w")) == NULL) { | ||
85 | perror_msg(NULL); | ||
86 | return -2; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | while ((c = fgetc(in)) != EOF) { | ||
91 | if (c == '\r') { | ||
92 | if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { | ||
93 | // file is alredy in DOS format so it is not necessery to touch it | ||
94 | if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) { | ||
95 | perror_msg(NULL); | ||
96 | return -2; | ||
97 | } | ||
98 | return 0; | ||
99 | } | ||
100 | if (!ConvType) | ||
101 | ConvType = CT_DOS2UNIX; | ||
102 | break; | ||
103 | } | ||
104 | if (c == '\n') { | ||
105 | if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { | ||
106 | // file is alredy in UNIX format so it is not necessery to touch it | ||
107 | if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) { | ||
108 | perror_msg(NULL); | ||
109 | return -2; | ||
110 | } | ||
111 | return 0; | ||
112 | } | ||
113 | if (!ConvType) | ||
114 | ConvType = CT_UNIX2DOS; | ||
115 | if (ConvType == CT_UNIX2DOS) | ||
116 | fputc('\r', out); | ||
117 | fputc('\n', out); | ||
118 | break; | ||
119 | } | ||
120 | fputc(c, out); | ||
36 | } | 121 | } |
37 | c = getchar(); | 122 | if (c != EOF) |
38 | while (c != EOF) { | 123 | while ((c = fgetc(in)) != EOF) { |
39 | /* Eat any \r's... they shouldn't be here */ | 124 | if (c == '\r') |
40 | while (c == '\r') c = getchar(); | 125 | continue; |
41 | if (c == EOF) break; | 126 | if (c == '\n') { |
42 | putchar(c); | 127 | if (ConvType == CT_UNIX2DOS) |
43 | c = getchar(); | 128 | fputc('\r', out); |
44 | } | 129 | fputc('\n', out); |
45 | return EXIT_SUCCESS; | 130 | continue; |
131 | } | ||
132 | fputc(c, out); | ||
133 | } | ||
134 | |||
135 | if (fn != NULL) { | ||
136 | if (fclose(in) < 0 || fclose(out) < 0 || | ||
137 | (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { | ||
138 | perror_msg(NULL); | ||
139 | return -2; | ||
140 | } | ||
141 | |||
142 | while ((c = fgetc(in)) != EOF) | ||
143 | fputc(c, out); | ||
144 | |||
145 | if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) { | ||
146 | perror_msg(NULL); | ||
147 | return -2; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | return 0; | ||
46 | } | 152 | } |