4 * dos2unix '\n' convertor 0.5.0
5 * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997)
6 * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>.
9 * dos2unix filters reading input from stdin and writing output to stdout.
10 * Without arguments it reverts the format (e.i. if source is in UNIX format,
11 * output is in DOS format and vice versa).
13 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
25 #define tempFn bb_common_bufsiz1
27 /* if fn is NULL then input is stdin and output is stdout */
28 static int convert(char *fn, int ConvType)
34 in = bb_xfopen(fn, "rw");
36 The file is then created with mode read/write and
37 permissions 0666 for glibc 2.0.6 and earlier or
38 0600 for glibc 2.0.7 and later.
40 snprintf(tempFn, sizeof(tempFn), "%sXXXXXX", fn);
42 sizeof tempFn is 4096, so it should be big enough to hold the full
43 path. however if the output is truncated the subsequent call to
46 if ((fd = mkstemp(&tempFn[0])) == -1 || chmod(tempFn, 0600) == -1) {
47 bb_perror_nomsg_and_die();
49 out = fdopen(fd, "w+");
59 while ((c = fgetc(in)) != EOF) {
63 if (ConvType == CT_UNIX2DOS)
72 if (fclose(in) < 0 || fclose(out) < 0) {
78 /* Assume they are both on the same filesystem (which
79 * should be true since we put them into the same directory
80 * so we _should_ be ok, but you never know... */
81 if (rename(tempFn, fn) < 0) {
82 bb_perror_msg("cannot rename '%s' as '%s'", tempFn, fn);
90 int dos2unix_main(int argc, char *argv[])
95 /* See if we are supposed to be doing dos2unix or unix2dos */
96 if (argv[0][0]=='d') {
97 ConvType = CT_DOS2UNIX; /*1*/
99 ConvType = CT_UNIX2DOS; /*2*/
101 /* -u and -d are mutally exclusive */
102 bb_opt_complementally = "?:u--d:d--u";
103 /* process parameters */
104 /* -u convert to unix */
105 /* -d convert to dos */
106 o = bb_getopt_ulflags(argc, argv, "du");
108 /* Do the conversion requested by an argument else do the default
109 * conversion depending on our name. */
115 if ((o = convert(argv[optind++], ConvType)) < 0)
119 o = convert(NULL, ConvType);