Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / coreutils / dos2unix.c
blob07398bdfa71f1926376cb7c0a65522523acc6f5e
1 /* vi: set sw=4 ts=4: */
2 /*
3 * dos2unix for BusyBox
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.
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.
15 //usage:#define dos2unix_trivial_usage
16 //usage: "[-ud] [FILE]"
17 //usage:#define dos2unix_full_usage "\n\n"
18 //usage: "Convert FILE in-place from DOS to Unix format.\n"
19 //usage: "When no file is given, use stdin/stdout.\n"
20 //usage: "\n -u dos2unix"
21 //usage: "\n -d unix2dos"
22 //usage:
23 //usage:#define unix2dos_trivial_usage
24 //usage: "[-ud] [FILE]"
25 //usage:#define unix2dos_full_usage "\n\n"
26 //usage: "Convert FILE in-place from Unix to DOS format.\n"
27 //usage: "When no file is given, use stdin/stdout.\n"
28 //usage: "\n -u dos2unix"
29 //usage: "\n -d unix2dos"
31 #include "libbb.h"
33 /* This is a NOEXEC applet. Be very careful! */
35 enum {
36 CT_UNIX2DOS = 1,
37 CT_DOS2UNIX
40 /* if fn is NULL then input is stdin and output is stdout */
41 static void convert(char *fn, int conv_type)
43 FILE *in, *out;
44 int i;
45 char *temp_fn = temp_fn; /* for compiler */
46 char *resolved_fn = resolved_fn;
48 in = stdin;
49 out = stdout;
50 if (fn != NULL) {
51 struct stat st;
53 resolved_fn = xmalloc_follow_symlinks(fn);
54 if (resolved_fn == NULL)
55 bb_simple_perror_msg_and_die(fn);
56 in = xfopen_for_read(resolved_fn);
57 fstat(fileno(in), &st);
59 temp_fn = xasprintf("%sXXXXXX", resolved_fn);
60 i = xmkstemp(temp_fn);
61 if (fchmod(i, st.st_mode) == -1)
62 bb_simple_perror_msg_and_die(temp_fn);
64 out = xfdopen_for_write(i);
67 while ((i = fgetc(in)) != EOF) {
68 if (i == '\r')
69 continue;
70 if (i == '\n')
71 if (conv_type == CT_UNIX2DOS)
72 fputc('\r', out);
73 fputc(i, out);
76 if (fn != NULL) {
77 if (fclose(in) < 0 || fclose(out) < 0) {
78 unlink(temp_fn);
79 bb_perror_nomsg_and_die();
81 xrename(temp_fn, resolved_fn);
82 free(temp_fn);
83 free(resolved_fn);
87 int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
88 int dos2unix_main(int argc UNUSED_PARAM, char **argv)
90 int o, conv_type;
92 /* See if we are supposed to be doing dos2unix or unix2dos */
93 conv_type = CT_UNIX2DOS;
94 if (applet_name[0] == 'd') {
95 conv_type = CT_DOS2UNIX;
98 /* -u convert to unix, -d convert to dos */
99 opt_complementary = "u--d:d--u"; /* mutually exclusive */
100 o = getopt32(argv, "du");
102 /* Do the conversion requested by an argument else do the default
103 * conversion depending on our name. */
104 if (o)
105 conv_type = o;
107 argv += optind;
108 do {
109 /* might be convert(NULL) if there is no filename given */
110 convert(*argv, conv_type);
111 } while (*argv && *++argv);
113 return 0;