blob: 8308c417996ae000a25d9af10d264f58330eaaa5 [file] [log] [blame]
Eric Andersencff3fe32000-09-20 19:22:26 +00001/*
Eric Andersen544891d2001-02-22 23:37:30 +00002 * 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 Andersencff3fe32000-09-20 19:22:26 +000029
Glenn L McGratha6ce6702001-04-12 02:26:04 +000030#include <string.h>
Eric Andersen544891d2001-02-22 23:37:30 +000031#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000033
Glenn L McGratha6ce6702001-04-12 02:26:04 +000034// if fn is NULL then input is stdin and output is stdout
35extern int convert(char *fn, int ConvType) {
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 Andersen544891d2001-02-22 23:37:30 +0000117int 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 Andersencff3fe32000-09-20 19:22:26 +0000133 }
Eric Andersen544891d2001-02-22 23:37:30 +0000134
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