- patch from Denis Vlasenko to add and use bb_xopen3()
[oweals/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  *
11  *  Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
12 */
13
14 #include <string.h>
15 #include <unistd.h>
16 #include <stdint.h>
17 #include <fcntl.h>
18 #include <sys/time.h>
19 #include "busybox.h"
20
21 #define CT_UNIX2DOS     1
22 #define CT_DOS2UNIX     2
23 #define tempFn bb_common_bufsiz1
24
25 /* if fn is NULL then input is stdin and output is stdout */
26 static int convert(char *fn, int ConvType)
27 {
28         int c, fd;
29         FILE *in, *out;
30
31         if (fn != NULL) {
32                 in = bb_xfopen(fn, "rw");
33                 /*
34                 The file is then created with mode read/write and
35                 permissions 0666 for glibc 2.0.6 and earlier or
36                 0600  for glibc  2.0.7 and  later. 
37                 */
38                 snprintf(tempFn, sizeof(tempFn), "%sXXXXXX", fn);
39                 /*
40                 sizeof tempFn is 4096, so it should be big enough to hold the full
41                 path. however if the output is truncated the subsequent call to
42                 mkstemp would fail.
43                 */
44                 if ((fd = mkstemp(&tempFn[0])) == -1 || chmod(tempFn, 0600) == -1) {
45                         bb_perror_nomsg_and_die();
46                 }
47                 out = fdopen(fd, "w+");
48                 if (!out) {
49                         close(fd);
50                         remove(tempFn);
51                 }
52         } else {
53                 in = stdin;
54                 out = stdout;
55         }
56
57         while ((c = fgetc(in)) != EOF) {
58                 if (c == '\r')
59                         continue;
60                 if (c == '\n') {
61                         if (ConvType == CT_UNIX2DOS)
62                                 fputc('\r', out);
63                         fputc('\n', out);
64                         continue;
65                 }
66                 fputc(c, out);
67         }
68
69         if (fn != NULL) {
70                 if (fclose(in) < 0 || fclose(out) < 0) {
71                         bb_perror_nomsg();
72                         remove(tempFn);
73                         return -2;
74             }
75         
76                 /* Assume they are both on the same filesystem (which
77                 * should be true since we put them into the same directory
78                 * so we _should_ be ok, but you never know... */
79                 if (rename(tempFn, fn) < 0) {
80                         bb_perror_msg("cannot rename '%s' as '%s'", tempFn, fn);
81                         return -1;
82             }
83         }
84
85         return 0;
86 }
87
88 int dos2unix_main(int argc, char *argv[])
89 {
90         int ConvType;
91         int o;
92
93         /* See if we are supposed to be doing dos2unix or unix2dos */
94         if (argv[0][0]=='d') {
95             ConvType = CT_DOS2UNIX;  /*2*/
96         } else {
97             ConvType = CT_UNIX2DOS;  /*1*/
98         }
99         /* -u and -d are mutally exclusive */
100         bb_opt_complementally = "?:u--d:d--u";
101         /* process parameters */
102         /* -u convert to unix */
103         /* -d convert to dos  */
104         o = bb_getopt_ulflags(argc, argv, "du");
105
106         /* Do the conversion requested by an argument else do the default
107          * conversion depending on our name.  */
108         if (o)
109                 ConvType = o;
110
111         if (optind < argc) {
112                 while(optind < argc)
113                         if ((o = convert(argv[optind++], ConvType)) < 0)
114                                 break;
115         }
116         else
117                 o = convert(NULL, ConvType);
118
119         return o;
120 }