1/* vi: set sw=4 ts=4: */
2/*
3 * dos2unix for BusyBox
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>.
8 * All rights reserved.
9 *
10 * dos2unix filters reading input from stdin and writing output to stdout.
11 *
12 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
13*/
14
15#include "libbb.h"
16
17enum {
18	CT_UNIX2DOS = 1,
19	CT_DOS2UNIX
20};
21
22/* if fn is NULL then input is stdin and output is stdout */
23static int convert(char *fn, int conv_type)
24{
25	FILE *in, *out;
26	int i;
27#define name_buf bb_common_bufsiz1
28
29	in = stdin;
30	out = stdout;
31	if (fn != NULL) {
32		in = xfopen(fn, "rw");
33		/*
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.
37		 */
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();
42		}
43		out = fdopen(i, "w+");
44		if (!out) {
45			close(i);
46			remove(name_buf);
47			return -2;
48		}
49	}
50
51	while ((i = fgetc(in)) != EOF) {
52		if (i == '\r')
53			continue;
54		if (i == '\n') {
55			if (conv_type == CT_UNIX2DOS)
56				fputc('\r', out);
57			fputc('\n', out);
58			continue;
59		}
60		fputc(i, out);
61	}
62
63	if (fn != NULL) {
64		if (fclose(in) < 0 || fclose(out) < 0) {
65			bb_perror_nomsg();
66			remove(name_buf);
67			return -2;
68		}
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);
74			return -1;
75		}
76	}
77
78	return 0;
79}
80
81int dos2unix_main(int argc, char **argv);
82int dos2unix_main(int argc, char **argv)
83{
84	int o, conv_type;
85
86	/* See if we are supposed to be doing dos2unix or unix2dos */
87	if (applet_name[0] == 'd') {
88		conv_type = CT_DOS2UNIX;	/* 2 */
89	} else {
90		conv_type = CT_UNIX2DOS;	/* 1 */
91	}
92
93	/* -u convert to unix, -d convert to dos */
94	opt_complementary = "u--d:d--u"; /* mutually exclusive */
95	o = getopt32(argv, "du");
96
97	/* Do the conversion requested by an argument else do the default
98	 * conversion depending on our name.  */
99	if (o)
100		conv_type = o;
101
102	do {
103		/* might be convert(NULL) if there is no filename given */
104		o = convert(argv[optind], conv_type);
105		if (o < 0)
106			break;
107		optind++;
108	} while (optind < argc);
109
110	return o;
111}
112