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.
42 /* We are making a lame pseudo-random string generator here. in
43 * convert(), each pass through the while loop will add more and more
44 * stuff into value, which is _supposed_ to wrap. We don't care about
45 * it being accurate. We care about it being messy, since we then mod
46 * it by the sizeof(letters) and then use that as an index into letters
47 * to pick a random letter to add to out temporary file. */
48 typedef unsigned long int bb_uint64_t;
50 static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
52 // if fn is NULL then input is stdin and output is stdout
53 static int convert(char *fn, int ConvType)
58 static bb_uint64_t value=0;
59 FILE *in = stdin, *out = stdout;
62 in = bb_xfopen(fn, "rw");
63 safe_strncpy(tempFn, fn, sizeof(tempFn));
67 /* tempFn is BUFSIZ so the last addressable spot it BUFSIZ-1.
68 * The loop increments by 2. So this must check for BUFSIZ-3. */
70 bb_error_msg_and_die("unique name not found");
71 /* Get some semi random stuff to try and make a
72 * random filename based (and in the same dir as)
73 * the input file... */
74 gettimeofday (&tv, NULL);
75 value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
76 tempFn[++c] = letters[value % 62];
80 if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
83 out = fdopen(fd, "w+");
93 while ((c = fgetc(in)) != EOF) {
95 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
96 // file is alredy in DOS format so it is not necessery to touch it
98 if (fclose(in) < 0 || fclose(out) < 0) {
105 ConvType = CT_DOS2UNIX;
109 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
110 // file is alredy in UNIX format so it is not necessery to touch it
112 if ((fclose(in) < 0) || (fclose(out) < 0)) {
119 ConvType = CT_UNIX2DOS;
121 if (ConvType == CT_UNIX2DOS) {
130 while ((c = fgetc(in)) != EOF) {
134 if (ConvType == CT_UNIX2DOS)
143 if (fclose(in) < 0 || fclose(out) < 0) {
149 /* Assume they are both on the same filesystem (which
150 * should be true since we put them into the same directory
151 * so we _should_ be ok, but you never know... */
152 if (rename(tempFn, fn) < 0) {
153 bb_perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
161 int dos2unix_main(int argc, char *argv[])
163 int ConvType = CT_AUTO;
166 //See if we are supposed to be doing dos2unix or unix2dos
167 if (argv[0][0]=='d') {
168 ConvType = CT_DOS2UNIX;
170 if (argv[0][0]=='u') {
171 ConvType = CT_UNIX2DOS;
174 // process parameters
175 while ((o = getopt(argc, argv, "du")) != EOF) {
178 ConvType = CT_UNIX2DOS;
181 ConvType = CT_DOS2UNIX;
190 if ((o = convert(argv[optind++], ConvType)) < 0)
194 o = convert(NULL, ConvType);