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 int convert(char *fn, int conv_type)
27 #define name_buf bb_common_bufsiz1
32 in = xfopen(fn, "rw");
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 snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
39 i = mkstemp(&name_buf[0]);
40 if (i == -1 || chmod(name_buf, 0600) == -1) {
41 bb_perror_nomsg_and_die();
43 out = fdopen(i, "w+");
51 while ((i = fgetc(in)) != EOF) {
55 if (conv_type == CT_UNIX2DOS)
64 if (fclose(in) < 0 || fclose(out) < 0) {
69 /* Assume they are both on the same filesystem (which
70 * should be true since we put them into the same directory
71 * so we _should_ be ok, but you never know... */
72 if (rename(name_buf, fn) < 0) {
73 bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
81 int dos2unix_main(int argc, char **argv);
82 int dos2unix_main(int argc, char **argv)
86 /* See if we are supposed to be doing dos2unix or unix2dos */
87 if (applet_name[0] == 'd') {
88 conv_type = CT_DOS2UNIX; /* 2 */
90 conv_type = CT_UNIX2DOS; /* 1 */
93 /* -u convert to unix, -d convert to dos */
94 opt_complementary = "u--d:d--u"; /* mutually exclusive */
95 o = getopt32(argv, "du");
97 /* Do the conversion requested by an argument else do the default
98 * conversion depending on our name. */
103 /* might be convert(NULL) if there is no filename given */
104 o = convert(argv[optind], conv_type);
108 } while (optind < argc);