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.
38 /* We are making a lame pseudo-random string generator here. in
39 * convert(), each pass through the while loop will add more and more
40 * stuff into value, which is _supposed_ to wrap. We don't care about
41 * it being accurate. We care about it being messy, since we then mod
42 * it by the sizeof(letters) and then use that as an index into letters
43 * to pick a random letter to add to out temporary file. */
44 typedef unsigned long int bb_uint64_t;
46 static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
48 // if fn is NULL then input is stdin and output is stdout
49 static int convert(char *fn, int ConvType)
54 static bb_uint64_t value=0;
55 FILE *in = stdin, *out = stdout;
58 if ((in = wfopen(fn, "rw")) == NULL) {
61 safe_strncpy(tempFn, fn, sizeof(tempFn));
66 error_msg_and_die("unique name not found");
67 /* Get some semi random stuff to try and make a
68 * random filename based (and in the same dir as)
69 * the input file... */
70 gettimeofday (&tv, NULL);
71 value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
72 tempFn[++c] = letters[value % 62];
76 if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
79 out = fdopen(fd, "w+");
89 while ((c = fgetc(in)) != EOF) {
91 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
92 // file is alredy in DOS format so it is not necessery to touch it
94 if (fclose(in) < 0 || fclose(out) < 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
108 if ((fclose(in) < 0) || (fclose(out) < 0)) {
115 ConvType = CT_UNIX2DOS;
117 if (ConvType == CT_UNIX2DOS) {
126 while ((c = fgetc(in)) != EOF) {
130 if (ConvType == CT_UNIX2DOS)
139 if (fclose(in) < 0 || fclose(out) < 0) {
145 /* Assume they are both on the same filesystem (which
146 * should be true since we put them into the same directory
147 * so we _should_ be ok, but you never know... */
148 if (rename(tempFn, fn) < 0) {
149 perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
157 int dos2unix_main(int argc, char *argv[])
159 int ConvType = CT_AUTO;
162 //See if we are supposed to be doing dos2unix or unix2dos
163 if (argv[0][0]=='d') {
164 ConvType = CT_DOS2UNIX;
166 if (argv[0][0]=='u') {
167 ConvType = CT_UNIX2DOS;
170 // process parameters
171 while ((o = getopt(argc, argv, "du")) != EOF) {
174 ConvType = CT_UNIX2DOS;
177 ConvType = CT_DOS2UNIX;
186 if ((o = convert(argv[optind++], ConvType)) < 0)
190 o = convert(NULL, ConvType);