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.
26 /* if fn is NULL then input is stdin and output is stdout */
27 static int convert(char *fn)
33 in = bb_xfopen(fn, "rw");
35 The file is then created with mode read/write and
36 permissions 0666 for glibc 2.0.6 and earlier or
37 0600 for glibc 2.0.7 and later.
39 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
41 sizeof bb_common_bufsiz1 is 4096, so it should be big enough to
42 hold the full path. However if the output is truncated the
43 subsequent call to mkstemp would fail.
45 if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1
46 || chmod(bb_common_bufsiz1, 0600) == -1) {
47 bb_perror_nomsg_and_die();
49 out = fdopen(i, "w+");
52 remove(bb_common_bufsiz1);
59 while ((i = fgetc(in)) != EOF) {
63 if (ConvType == CT_UNIX2DOS)
72 if (fclose(in) < 0 || fclose(out) < 0) {
74 remove(bb_common_bufsiz1);
77 /* Assume they are both on the same filesystem (which
78 * should be true since we put them into the same directory
79 * so we _should_ be ok, but you never know... */
80 if (rename(bb_common_bufsiz1, fn) < 0) {
81 bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn);
89 int dos2unix_main(int argc, char *argv[])
93 /* See if we are supposed to be doing dos2unix or unix2dos */
94 if (bb_applet_name[0] == 'd') {
95 ConvType = CT_DOS2UNIX; /*2 */
97 ConvType = CT_UNIX2DOS; /*1 */
99 /* -u and -d are mutally exclusive */
100 bb_opt_complementally = "?:u--d:d--u";
101 /* process parameters */
102 /* -u convert to unix */
103 /* -d convert to dos */
104 o = bb_getopt_ulflags(argc, argv, "du");
106 /* Do the conversion requested by an argument else do the default
107 * conversion depending on our name. */
112 while (optind < argc)
113 if ((o = convert(argv[optind++])) < 0)