Remove bb_ prefixes from xfuncs.c (and a few other places), consolidate
[platform/upstream/busybox.git] / coreutils / dos2unix.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * dos2unix for BusyBox
4  *
5  * dos2unix '\n' convertor 0.5.0
6  * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997)
7  * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>.
8  * All rights reserved.
9  *
10  * dos2unix filters reading input from stdin and writing output to stdout.
11  *
12  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
13 */
14
15 #include "busybox.h"
16
17 enum ConvType {
18         CT_UNIX2DOS = 1,
19         CT_DOS2UNIX
20 } ConvType;
21
22 /* if fn is NULL then input is stdin and output is stdout */
23 static int convert(char *fn)
24 {
25         FILE *in, *out;
26         int i;
27
28         if (fn != NULL) {
29                 in = xfopen(fn, "rw");
30                 /*
31                    The file is then created with mode read/write and
32                    permissions 0666 for glibc 2.0.6 and earlier or
33                    0600  for glibc  2.0.7 and  later.
34                  */
35                 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
36                 /*
37                    sizeof bb_common_bufsiz1 is 4096, so it should be big enough to
38                    hold the full path.  However if the output is truncated the
39                    subsequent call to mkstemp would fail.
40                  */
41                 if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1
42                         || chmod(bb_common_bufsiz1, 0600) == -1) {
43                         bb_perror_nomsg_and_die();
44                 }
45                 out = fdopen(i, "w+");
46                 if (!out) {
47                         close(i);
48                         remove(bb_common_bufsiz1);
49                 }
50         } else {
51                 in = stdin;
52                 out = stdout;
53         }
54
55         while ((i = fgetc(in)) != EOF) {
56                 if (i == '\r')
57                         continue;
58                 if (i == '\n') {
59                         if (ConvType == CT_UNIX2DOS)
60                                 fputc('\r', out);
61                         fputc('\n', out);
62                         continue;
63                 }
64                 fputc(i, out);
65         }
66
67         if (fn != NULL) {
68                 if (fclose(in) < 0 || fclose(out) < 0) {
69                         bb_perror_nomsg();
70                         remove(bb_common_bufsiz1);
71                         return -2;
72                 }
73                 /* Assume they are both on the same filesystem (which
74                  * should be true since we put them into the same directory
75                  * so we _should_ be ok, but you never know... */
76                 if (rename(bb_common_bufsiz1, fn) < 0) {
77                         bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn);
78                         return -1;
79                 }
80         }
81
82         return 0;
83 }
84
85 int dos2unix_main(int argc, char *argv[])
86 {
87         int o;
88
89         /* See if we are supposed to be doing dos2unix or unix2dos */
90         if (bb_applet_name[0] == 'd') {
91                 ConvType = CT_DOS2UNIX; /*2 */
92         } else {
93                 ConvType = CT_UNIX2DOS; /*1 */
94         }
95         /* -u and -d are mutally exclusive */
96         bb_opt_complementally = "?:u--d:d--u";
97         /* process parameters */
98         /* -u convert to unix */
99         /* -d convert to dos  */
100         o = bb_getopt_ulflags(argc, argv, "du");
101
102         /* Do the conversion requested by an argument else do the default
103          * conversion depending on our name.  */
104         if (o)
105                 ConvType = o;
106
107         if (optind < argc) {
108                 while (optind < argc)
109                         if ((o = convert(argv[optind++])) < 0)
110                                 break;
111         } else
112                 o = convert(NULL);
113
114         return o;
115 }