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>.
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).
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.
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.
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.
27 * See the COPYING file for license information.
41 int convert(char *fn, int ConvType);
43 int dos2unix_main(int argc, char *argv[]) {
44 int ConvType = CT_AUTO;
48 while ((o = getopt(argc, argv, "du")) != EOF) {
51 ConvType = CT_UNIX2DOS;
54 ConvType = CT_DOS2UNIX;
63 if ((o = convert(argv[optind++], ConvType)) < 0)
67 o = convert(NULL, ConvType);
72 // if fn is NULL then input is stdin and output is stdout
73 int convert(char *fn, int ConvType) {
76 FILE *in = stdin, *out = stdout;
79 if ((in = fopen(fn, "r")) == NULL) {
83 tempFn = tmpnam(NULL);
84 if (tempFn == NULL || (out = fopen(tempFn, "w")) == NULL) {
90 while ((c = fgetc(in)) != EOF) {
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) {
101 ConvType = CT_DOS2UNIX;
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) {
114 ConvType = CT_UNIX2DOS;
115 if (ConvType == CT_UNIX2DOS)
123 while ((c = fgetc(in)) != EOF) {
127 if (ConvType == CT_UNIX2DOS)
136 if (fclose(in) < 0 || fclose(out) < 0 ||
137 (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) {
142 while ((c = fgetc(in)) != EOF)
145 if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) {