Initial public busybox maemo commit, 3:1.10.2.legal-1osso12
[busybox4maemo] / 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 "libbb.h"
16
17 enum {
18         CT_UNIX2DOS = 1,
19         CT_DOS2UNIX
20 };
21
22 /* if fn is NULL then input is stdin and output is stdout */
23 static void convert(char *fn, int conv_type)
24 {
25         FILE *in, *out;
26         int i;
27         char *name_buf = name_buf; /* for compiler */
28
29         in = stdin;
30         out = stdout;
31         if (fn != NULL) {
32                 in = xfopen(fn, "r");
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                 name_buf = xasprintf("%sXXXXXX", fn);
39                 i = mkstemp(name_buf);
40                 if (i == -1
41                  || fchmod(i, 0600) == -1
42                  || !(out = fdopen(i, "w+"))
43                 ) {
44                         bb_perror_nomsg_and_die();
45                 }
46         }
47
48         while ((i = fgetc(in)) != EOF) {
49                 if (i == '\r')
50                         continue;
51                 if (i == '\n')
52                         if (conv_type == CT_UNIX2DOS)
53                                 fputc('\r', out);
54                 fputc(i, out);
55         }
56
57         if (fn != NULL) {
58                 if (fclose(in) < 0 || fclose(out) < 0) {
59                         unlink(name_buf);
60                         bb_perror_nomsg_and_die();
61                 }
62 // TODO: destroys symlinks. See how passwd handles this
63                 xrename(name_buf, fn);
64                 free(name_buf);
65         }
66 }
67
68 int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
69 int dos2unix_main(int argc, char **argv)
70 {
71         int o, conv_type;
72
73         /* See if we are supposed to be doing dos2unix or unix2dos */
74         conv_type = CT_UNIX2DOS;
75         if (applet_name[0] == 'd') {
76                 conv_type = CT_DOS2UNIX;
77         }
78
79         /* -u convert to unix, -d convert to dos */
80         opt_complementary = "u--d:d--u"; /* mutually exclusive */
81         o = getopt32(argv, "du");
82
83         /* Do the conversion requested by an argument else do the default
84          * conversion depending on our name.  */
85         if (o)
86                 conv_type = o;
87
88         do {
89                 /* might be convert(NULL) if there is no filename given */
90                 convert(argv[optind], conv_type);
91                 optind++;
92         } while (optind < argc);
93
94         return 0;
95 }