Cleanup patch by Bernhard Fischer, removing unnecessary includes of
[platform/upstream/busybox.git] / coreutils / dos2unix.c
1 /*
2  * 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  */
29
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdint.h>
33 #include <fcntl.h>
34 #include <sys/time.h>
35 #include "busybox.h"
36
37 #define CT_UNIX2DOS     1
38 #define CT_DOS2UNIX     2
39
40 /* 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
43  * it being accurate.  We care about it being messy, since we use it
44  * to pick a random letter to add to out temporary file. */
45 typedef unsigned long int bb_uint64_t;
46
47 /* if fn is NULL then input is stdin and output is stdout */
48 static int convert(char *fn, int ConvType)
49 {
50         int c, fd;
51         struct timeval tv;
52         RESERVE_CONFIG_BUFFER(tempFn, BUFSIZ);
53         static bb_uint64_t value=0;
54         FILE *in, *out;
55
56         if (fn != NULL) {
57                 in = bb_xfopen(fn, "rw");
58                 safe_strncpy(tempFn, fn, sizeof(tempFn));
59                 c = strlen(tempFn);
60                 tempFn[c] = '.';
61                 while(1) {
62                     /* tempFn is BUFSIZ so the last addressable spot it BUFSIZ-1.
63                      * The loop increments by 2. So this must check for BUFSIZ-3. */
64                     if (c >=BUFSIZ-3)
65                         bb_error_msg_and_die("unique name not found");
66                     /* Get some semi random stuff to try and make a
67                      * random filename based (and in the same dir as)
68                      * the input file... */
69                     gettimeofday (&tv, NULL);
70                     value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
71                     tempFn[++c] = ((value%62) < 26)?(value%62)+97:
72                                    ((value%62) < 52)?(value%62)+39:
73                                     (value%62)-4;
74                     tempFn[c+1] = '\0';
75                     value /= 62;
76
77                     if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
78                         continue;
79                     }
80                     out = fdopen(fd, "w+");
81                     if (!out) {
82                         close(fd);
83                         remove(tempFn);
84                         continue;
85                     }
86                     break;
87                 }
88         } else {
89                 in = stdin;
90                 out = stdout;
91         }
92
93         while ((c = fgetc(in)) != EOF) {
94                 if (c == '\r') {
95                         if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
96                                 /* file is already in DOS format so it is
97                                  * not necessary to touch it.  */
98                                 remove(tempFn);
99                                 if (fclose(in) < 0 || fclose(out) < 0) {
100                                         bb_perror_nomsg();
101                                         return -2;
102                                 }
103                                 return 0;
104                         }
105                         break;
106                 }
107                 if (c == '\n') {
108                         if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
109                                 /* file is already in DOS format so it is
110                                  * not necessary to touch it.  */
111                                 remove(tempFn);
112                                 if ((fclose(in) < 0) || (fclose(out) < 0)) {
113                                         bb_perror_nomsg();
114                                         return -2;
115                                 }
116                                 return 0;
117                         }
118                         if (ConvType == CT_UNIX2DOS) {
119                                 fputc('\r', out);
120                         }
121                 }
122                 fputc(c, out);
123         }
124         while (c != EOF && (c = fgetc(in)) != EOF) {
125                 if (c == '\r')
126                         continue;
127                 if (c == '\n') {
128                         if (ConvType == CT_UNIX2DOS)
129                                 fputc('\r', out);
130                         fputc('\n', out);
131                         continue;
132                 }
133                 fputc(c, out);
134         }
135
136         if (fn != NULL) {
137             if (fclose(in) < 0 || fclose(out) < 0) {
138                 bb_perror_nomsg();
139                 remove(tempFn);
140                 return -2;
141             }
142
143             /* Assume they are both on the same filesystem (which
144              * should be true since we put them into the same directory
145              * so we _should_ be ok, but you never know... */
146             if (rename(tempFn, fn) < 0) {
147                 bb_perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
148                 return -1;
149             }
150         }
151
152         return 0;
153 }
154
155 int dos2unix_main(int argc, char *argv[])
156 {
157         int ConvType;
158         int o;
159
160         /* See if we are supposed to be doing dos2unix or unix2dos */
161         if (argv[0][0]=='d') {
162             ConvType = CT_DOS2UNIX;
163         } else {
164             ConvType = CT_UNIX2DOS;
165         }
166
167         /* process parameters */
168         o = bb_getopt_ulflags(argc, argv, "ud");
169
170         /* Do the conversion requested by an argument else do the default
171          * conversion depending on our name.  */
172         if (o)
173                 ConvType = o;
174
175         if (optind < argc) {
176                 while(optind < argc)
177                         if ((o = convert(argv[optind++], ConvType)) < 0)
178                                 break;
179         }
180         else
181                 o = convert(NULL, ConvType);
182
183         return o;
184 }
185