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 GPLv2 or later, see file LICENSE in this source tree.
14 //config:config DOS2UNIX
15 //config: bool "dos2unix/unix2dos"
18 //config: dos2unix is used to convert a text file from DOS format to
19 //config: UNIX format, and vice versa.
21 //config:config UNIX2DOS
24 //config: depends on DOS2UNIX
26 //config: unix2dos is used to convert a text file from UNIX format to
27 //config: DOS format, and vice versa.
29 //applet:IF_DOS2UNIX(APPLET_NOEXEC(dos2unix, dos2unix, BB_DIR_USR_BIN, BB_SUID_DROP, dos2unix))
30 //applet:IF_UNIX2DOS(APPLET_NOEXEC(unix2dos, dos2unix, BB_DIR_USR_BIN, BB_SUID_DROP, unix2dos))
32 //kbuild:lib-$(CONFIG_DOS2UNIX) += dos2unix.o
33 //kbuild:lib-$(CONFIG_UNIX2DOS) += dos2unix.o
35 //usage:#define dos2unix_trivial_usage
36 //usage: "[-ud] [FILE]"
37 //usage:#define dos2unix_full_usage "\n\n"
38 //usage: "Convert FILE in-place from DOS to Unix format.\n"
39 //usage: "When no file is given, use stdin/stdout.\n"
40 //usage: "\n -u dos2unix"
41 //usage: "\n -d unix2dos"
43 //usage:#define unix2dos_trivial_usage
44 //usage: "[-ud] [FILE]"
45 //usage:#define unix2dos_full_usage "\n\n"
46 //usage: "Convert FILE in-place from Unix to DOS format.\n"
47 //usage: "When no file is given, use stdin/stdout.\n"
48 //usage: "\n -u dos2unix"
49 //usage: "\n -d unix2dos"
53 /* This is a NOEXEC applet. Be very careful! */
60 /* if fn is NULL then input is stdin and output is stdout */
61 static void convert(char *fn, int conv_type)
65 char *temp_fn = temp_fn; /* for compiler */
66 char *resolved_fn = resolved_fn;
74 resolved_fn = xmalloc_follow_symlinks(fn);
75 if (resolved_fn == NULL)
76 bb_simple_perror_msg_and_die(fn);
77 in = xfopen_for_read(resolved_fn);
78 xfstat(fileno(in), &st, resolved_fn);
80 temp_fn = xasprintf("%sXXXXXX", resolved_fn);
81 fd = xmkstemp(temp_fn);
82 if (fchmod(fd, st.st_mode) == -1)
83 bb_simple_perror_msg_and_die(temp_fn);
84 fchown(fd, st.st_uid, st.st_gid);
86 out = xfdopen_for_write(fd);
89 while ((ch = fgetc(in)) != EOF) {
93 if (conv_type == CT_UNIX2DOS)
99 if (fclose(in) < 0 || fclose(out) < 0) {
101 bb_perror_nomsg_and_die();
103 xrename(temp_fn, resolved_fn);
109 int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
110 int dos2unix_main(int argc UNUSED_PARAM, char **argv)
114 /* See if we are supposed to be doing dos2unix or unix2dos */
115 conv_type = CT_UNIX2DOS;
116 if (applet_name[0] == 'd') {
117 conv_type = CT_DOS2UNIX;
120 /* -u convert to unix, -d convert to dos */
121 opt_complementary = "u--d:d--u"; /* mutually exclusive */
122 o = getopt32(argv, "du");
124 /* Do the conversion requested by an argument else do the default
125 * conversion depending on our name. */
131 /* might be convert(NULL) if there is no filename given */
132 convert(*argv, conv_type);
133 } while (*argv && *++argv);