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)
29 in = xfopen(fn, "rw");
31 The file is then created with mode read/write and
32 permissions 0666 for glibc 2.0.6 and earlier or
33 0600 for glibc 2.0.7 and later.
35 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
37 sizeof bb_common_bufsiz1 is 4096, so it should be big enough to
38 hold the full path. However if the output is truncated the
39 subsequent call to mkstemp would fail.
41 if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1
42 || chmod(bb_common_bufsiz1, 0600) == -1) {
43 bb_perror_nomsg_and_die();
45 out = fdopen(i, "w+");
48 remove(bb_common_bufsiz1);
55 while ((i = fgetc(in)) != EOF) {
59 if (ConvType == CT_UNIX2DOS)
68 if (fclose(in) < 0 || fclose(out) < 0) {
70 remove(bb_common_bufsiz1);
73 /* Assume they are both on the same filesystem (which
74 * should be true since we put them into the same directory
75 * so we _should_ be ok, but you never know... */
76 if (rename(bb_common_bufsiz1, fn) < 0) {
77 bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn);
85 int dos2unix_main(int argc, char *argv[])
89 /* See if we are supposed to be doing dos2unix or unix2dos */
90 if (applet_name[0] == 'd') {
91 ConvType = CT_DOS2UNIX; /*2 */
93 ConvType = CT_UNIX2DOS; /*1 */
95 /* -u and -d are mutally exclusive */
96 opt_complementary = "?:u--d:d--u";
97 /* process parameters */
98 /* -u convert to unix */
99 /* -d convert to dos */
100 o = getopt32(argc, argv, "du");
102 /* Do the conversion requested by an argument else do the default
103 * conversion depending on our name. */
108 while (optind < argc)
109 if ((o = convert(argv[optind++])) < 0)