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