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.
37 /* Teach libc5 what a uint64_t is */
38 #if (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 1)
39 typedef unsigned long int uint64_t;
42 static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
44 // if fn is NULL then input is stdin and output is stdout
45 static int convert(char *fn, int ConvType)
50 static uint64_t value=0;
51 FILE *in = stdin, *out = stdout;
54 if ((in = wfopen(fn, "rw")) == NULL) {
62 error_msg_and_die("unique name not found");
63 /* Get some semi random stuff to try and make a
64 * random filename based (and in the same dir as)
65 * the input file... */
66 gettimeofday (&tv, NULL);
67 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
68 tempFn[++c] = letters[value % 62];
72 if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
75 out = fdopen(fd, "w+");
85 while ((c = fgetc(in)) != EOF) {
87 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
88 // file is alredy in DOS format so it is not necessery to touch it
90 if (fclose(in) < 0 || fclose(out) < 0) {
97 ConvType = CT_DOS2UNIX;
101 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
102 // file is alredy in UNIX format so it is not necessery to touch it
104 if ((fclose(in) < 0) || (fclose(out) < 0)) {
111 ConvType = CT_UNIX2DOS;
113 if (ConvType == CT_UNIX2DOS) {
122 while ((c = fgetc(in)) != EOF) {
126 if (ConvType == CT_UNIX2DOS)
135 if (fclose(in) < 0 || fclose(out) < 0) {
141 /* Assume they are both on the same filesystem */
142 if (rename(tempFn, fn) < 0) {
143 perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
151 int dos2unix_main(int argc, char *argv[])
153 int ConvType = CT_AUTO;
156 //See if we are supposed to be doing dos2unix or unix2dos
157 if (argv[0][0]=='d') {
158 ConvType = CT_DOS2UNIX;
160 if (argv[0][0]=='u') {
161 ConvType = CT_UNIX2DOS;
164 // process parameters
165 while ((o = getopt(argc, argv, "du")) != EOF) {
168 ConvType = CT_UNIX2DOS;
171 ConvType = CT_DOS2UNIX;
180 if ((o = convert(argv[optind++], ConvType)) < 0)
184 o = convert(NULL, ConvType);