blob: 0464df76f5395cec4a86b3bd68942206b47d7233 [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 Andersen655584b2001-07-25 07:22:55 +000031#include <unistd.h>
Eric Andersen59443962001-08-22 05:06:29 +000032#include <stdint.h>
Eric Andersen655584b2001-07-25 07:22:55 +000033#include <fcntl.h>
34#include <sys/time.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include "busybox.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000036
Manuel Novoa III cad53642003-03-19 09:13:01 +000037#define CT_UNIX2DOS 1
38#define CT_DOS2UNIX 2
39
Eric Andersen59443962001-08-22 05:06:29 +000040/* We are making a lame pseudo-random string generator here. in
41 * convert(), each pass through the while loop will add more and more
42 * stuff into value, which is _supposed_ to wrap. We don't care about
Rob Landleyf8154692005-09-01 03:11:19 +000043 * it being accurate. We care about it being messy, since we use it
Eric Andersen59443962001-08-22 05:06:29 +000044 * to pick a random letter to add to out temporary file. */
45typedef unsigned long int bb_uint64_t;
Eric Andersenbe0c3602001-08-02 10:55:32 +000046
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000047#define tempFn bb_common_bufsiz1
48
Rob Landleyf8154692005-09-01 03:11:19 +000049/* if fn is NULL then input is stdin and output is stdout */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050static int convert(char *fn, int ConvType)
Eric Andersen655584b2001-07-25 07:22:55 +000051{
52 int c, fd;
53 struct timeval tv;
Eric Andersen59443962001-08-22 05:06:29 +000054 static bb_uint64_t value=0;
Rob Landleyf8154692005-09-01 03:11:19 +000055 FILE *in, *out;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000056
57 if (fn != NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 in = bb_xfopen(fn, "rw");
Eric Andersen009ae1f2002-07-03 04:24:08 +000059 safe_strncpy(tempFn, fn, sizeof(tempFn));
Eric Andersen655584b2001-07-25 07:22:55 +000060 c = strlen(tempFn);
61 tempFn[c] = '.';
62 while(1) {
Eric Andersen6c0396b2004-05-05 19:39:21 +000063 /* tempFn is BUFSIZ so the last addressable spot it BUFSIZ-1.
64 * The loop increments by 2. So this must check for BUFSIZ-3. */
65 if (c >=BUFSIZ-3)
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_error_msg_and_die("unique name not found");
Eric Andersen655584b2001-07-25 07:22:55 +000067 /* 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);
Eric Andersen59443962001-08-22 05:06:29 +000071 value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
Rob Landleyf8154692005-09-01 03:11:19 +000072 tempFn[++c] = ((value%62) < 26)?(value%62)+97:
73 ((value%62) < 52)?(value%62)+39:
74 (value%62)-4;
Eric Andersen655584b2001-07-25 07:22:55 +000075 tempFn[c+1] = '\0';
76 value /= 62;
77
78 if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
79 continue;
80 }
81 out = fdopen(fd, "w+");
82 if (!out) {
83 close(fd);
84 remove(tempFn);
85 continue;
86 }
87 break;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000088 }
Rob Landleyf8154692005-09-01 03:11:19 +000089 } else {
90 in = stdin;
91 out = stdout;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000092 }
93
94 while ((c = fgetc(in)) != EOF) {
95 if (c == '\r') {
96 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
Rob Landleyf8154692005-09-01 03:11:19 +000097 /* file is already in DOS format so it is
98 * not necessary to touch it. */
Eric Andersen655584b2001-07-25 07:22:55 +000099 remove(tempFn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000100 if (fclose(in) < 0 || fclose(out) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_perror_nomsg();
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000102 return -2;
103 }
104 return 0;
105 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000106 break;
107 }
108 if (c == '\n') {
109 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
Rob Landleyf8154692005-09-01 03:11:19 +0000110 /* file is already in DOS format so it is
111 * not necessary to touch it. */
Eric Andersen655584b2001-07-25 07:22:55 +0000112 remove(tempFn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000113 if ((fclose(in) < 0) || (fclose(out) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 bb_perror_nomsg();
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000115 return -2;
116 }
117 return 0;
118 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000119 if (ConvType == CT_UNIX2DOS) {
120 fputc('\r', out);
121 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000122 }
123 fputc(c, out);
124 }
Rob Landleyf8154692005-09-01 03:11:19 +0000125 while (c != EOF && (c = fgetc(in)) != EOF) {
126 if (c == '\r')
127 continue;
128 if (c == '\n') {
129 if (ConvType == CT_UNIX2DOS)
130 fputc('\r', out);
131 fputc('\n', out);
132 continue;
133 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000134 fputc(c, out);
135 }
136
137 if (fn != NULL) {
Eric Andersen655584b2001-07-25 07:22:55 +0000138 if (fclose(in) < 0 || fclose(out) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 bb_perror_nomsg();
Eric Andersen655584b2001-07-25 07:22:55 +0000140 remove(tempFn);
141 return -2;
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000142 }
143
Eric Andersen59443962001-08-22 05:06:29 +0000144 /* Assume they are both on the same filesystem (which
145 * should be true since we put them into the same directory
146 * so we _should_ be ok, but you never know... */
Eric Andersen655584b2001-07-25 07:22:55 +0000147 if (rename(tempFn, fn) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 bb_perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
Eric Andersen655584b2001-07-25 07:22:55 +0000149 return -1;
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000150 }
151 }
152
153 return 0;
154}
155
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000156int dos2unix_main(int argc, char *argv[])
Eric Andersen655584b2001-07-25 07:22:55 +0000157{
Rob Landleyf8154692005-09-01 03:11:19 +0000158 int ConvType;
Eric Andersen544891d2001-02-22 23:37:30 +0000159 int o;
160
Rob Landleyf8154692005-09-01 03:11:19 +0000161 /* See if we are supposed to be doing dos2unix or unix2dos */
Eric Andersen655584b2001-07-25 07:22:55 +0000162 if (argv[0][0]=='d') {
163 ConvType = CT_DOS2UNIX;
Rob Landleyf8154692005-09-01 03:11:19 +0000164 } else {
Eric Andersen655584b2001-07-25 07:22:55 +0000165 ConvType = CT_UNIX2DOS;
166 }
167
Rob Landleyf8154692005-09-01 03:11:19 +0000168 /* process parameters */
169 o = bb_getopt_ulflags(argc, argv, "ud");
170
171 /* Do the conversion requested by an argument else do the default
172 * conversion depending on our name. */
173 if (o)
174 ConvType = o;
Eric Andersen544891d2001-02-22 23:37:30 +0000175
176 if (optind < argc) {
177 while(optind < argc)
178 if ((o = convert(argv[optind++], ConvType)) < 0)
179 break;
180 }
181 else
182 o = convert(NULL, ConvType);
183
184 return o;
185}
186