1 /* vi: set sw=4 ts=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>.
10 * dos2unix filters reading input from stdin and writing output to stdout.
12 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
22 /* if fn is NULL then input is stdin and output is stdout */
23 static void convert(char *fn, int conv_type)
27 char *name_buf = name_buf; /* for compiler */
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.
38 name_buf = xasprintf("%sXXXXXX", fn);
39 i = mkstemp(name_buf);
41 || fchmod(i, 0600) == -1
42 || !(out = fdopen(i, "w+"))
44 bb_perror_nomsg_and_die();
48 while ((i = fgetc(in)) != EOF) {
52 if (conv_type == CT_UNIX2DOS)
58 if (fclose(in) < 0 || fclose(out) < 0) {
60 bb_perror_nomsg_and_die();
62 // TODO: destroys symlinks. See how passwd handles this
63 xrename(name_buf, fn);
68 int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
69 int dos2unix_main(int argc, char **argv)
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;
79 /* -u convert to unix, -d convert to dos */
80 opt_complementary = "u--d:d--u"; /* mutually exclusive */
81 o = getopt32(argv, "du");
83 /* Do the conversion requested by an argument else do the default
84 * conversion depending on our name. */
89 /* might be convert(NULL) if there is no filename given */
90 convert(argv[optind], conv_type);
92 } while (optind < argc);