blob: c28e6a8b99d77ad875581ef5ba4601b37c158667 [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 Andersen544891d2001-02-22 23:37:30 +000031#include <getopt.h>
Eric Andersen655584b2001-07-25 07:22:55 +000032#include <unistd.h>
Eric Andersen59443962001-08-22 05:06:29 +000033#include <stdint.h>
Eric Andersen655584b2001-07-25 07:22:55 +000034#include <fcntl.h>
35#include <sys/time.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000036#include "busybox.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000037
Manuel Novoa III cad53642003-03-19 09:13:01 +000038#define CT_AUTO 0
39#define CT_UNIX2DOS 1
40#define CT_DOS2UNIX 2
41
Eric Andersen59443962001-08-22 05:06:29 +000042/* 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. */
48typedef unsigned long int bb_uint64_t;
Eric Andersenbe0c3602001-08-02 10:55:32 +000049
Eric Andersen655584b2001-07-25 07:22:55 +000050static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
51
Glenn L McGratha6ce6702001-04-12 02:26:04 +000052// if fn is NULL then input is stdin and output is stdout
Eric Andersen655584b2001-07-25 07:22:55 +000053static int convert(char *fn, int ConvType)
54{
55 int c, fd;
56 struct timeval tv;
57 char tempFn[BUFSIZ];
Eric Andersen59443962001-08-22 05:06:29 +000058 static bb_uint64_t value=0;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000059 FILE *in = stdin, *out = stdout;
60
61 if (fn != NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 in = bb_xfopen(fn, "rw");
Eric Andersen009ae1f2002-07-03 04:24:08 +000063 safe_strncpy(tempFn, fn, sizeof(tempFn));
Eric Andersen655584b2001-07-25 07:22:55 +000064 c = strlen(tempFn);
65 tempFn[c] = '.';
66 while(1) {
67 if (c >=BUFSIZ)
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 bb_error_msg_and_die("unique name not found");
Eric Andersen655584b2001-07-25 07:22:55 +000069 /* Get some semi random stuff to try and make a
70 * random filename based (and in the same dir as)
71 * the input file... */
72 gettimeofday (&tv, NULL);
Eric Andersen59443962001-08-22 05:06:29 +000073 value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
Eric Andersen655584b2001-07-25 07:22:55 +000074 tempFn[++c] = letters[value % 62];
75 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 }
89 }
90
91 while ((c = fgetc(in)) != EOF) {
92 if (c == '\r') {
93 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
94 // file is alredy in DOS format so it is not necessery to touch it
Eric Andersen655584b2001-07-25 07:22:55 +000095 remove(tempFn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000096 if (fclose(in) < 0 || fclose(out) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 bb_perror_nomsg();
Glenn L McGratha6ce6702001-04-12 02:26:04 +000098 return -2;
99 }
100 return 0;
101 }
102 if (!ConvType)
103 ConvType = CT_DOS2UNIX;
104 break;
105 }
106 if (c == '\n') {
107 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
108 // file is alredy in UNIX format so it is not necessery to touch it
Eric Andersen655584b2001-07-25 07:22:55 +0000109 remove(tempFn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000110 if ((fclose(in) < 0) || (fclose(out) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 bb_perror_nomsg();
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000112 return -2;
113 }
114 return 0;
115 }
116 if (!ConvType) {
117 ConvType = CT_UNIX2DOS;
118 }
119 if (ConvType == CT_UNIX2DOS) {
120 fputc('\r', out);
121 }
122 fputc('\n', out);
123 break;
124 }
125 fputc(c, out);
126 }
127 if (c != EOF)
128 while ((c = fgetc(in)) != EOF) {
129 if (c == '\r')
130 continue;
131 if (c == '\n') {
132 if (ConvType == CT_UNIX2DOS)
133 fputc('\r', out);
134 fputc('\n', out);
135 continue;
136 }
137 fputc(c, out);
138 }
139
140 if (fn != NULL) {
Eric Andersen655584b2001-07-25 07:22:55 +0000141 if (fclose(in) < 0 || fclose(out) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 bb_perror_nomsg();
Eric Andersen655584b2001-07-25 07:22:55 +0000143 remove(tempFn);
144 return -2;
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000145 }
146
Eric Andersen59443962001-08-22 05:06:29 +0000147 /* Assume they are both on the same filesystem (which
148 * should be true since we put them into the same directory
149 * so we _should_ be ok, but you never know... */
Eric Andersen655584b2001-07-25 07:22:55 +0000150 if (rename(tempFn, fn) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 bb_perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
Eric Andersen655584b2001-07-25 07:22:55 +0000152 return -1;
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000153 }
154 }
155
156 return 0;
157}
158
Eric Andersen655584b2001-07-25 07:22:55 +0000159int dos2unix_main(int argc, char *argv[])
160{
Eric Andersen544891d2001-02-22 23:37:30 +0000161 int ConvType = CT_AUTO;
162 int o;
163
Eric Andersen655584b2001-07-25 07:22:55 +0000164 //See if we are supposed to be doing dos2unix or unix2dos
165 if (argv[0][0]=='d') {
166 ConvType = CT_DOS2UNIX;
167 }
168 if (argv[0][0]=='u') {
169 ConvType = CT_UNIX2DOS;
170 }
171
Eric Andersen544891d2001-02-22 23:37:30 +0000172 // process parameters
173 while ((o = getopt(argc, argv, "du")) != EOF) {
174 switch (o) {
175 case 'd':
176 ConvType = CT_UNIX2DOS;
177 break;
178 case 'u':
179 ConvType = CT_DOS2UNIX;
180 break;
181 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 bb_show_usage();
Eric Andersen544891d2001-02-22 23:37:30 +0000183 }
Eric Andersencff3fe32000-09-20 19:22:26 +0000184 }
Eric Andersen544891d2001-02-22 23:37:30 +0000185
186 if (optind < argc) {
187 while(optind < argc)
188 if ((o = convert(argv[optind++], ConvType)) < 0)
189 break;
190 }
191 else
192 o = convert(NULL, ConvType);
193
194 return o;
195}
196