blob: 4039ed38cd1d69502c93dab2d20122e49a95ac23 [file] [log] [blame]
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +00001/* vi: set sw=4 ts=4: */
Eric Andersencff3fe32000-09-20 19:22:26 +00002/*
Eric Andersen544891d2001-02-22 23:37:30 +00003 * dos2unix for BusyBox
4 *
Denys Vlasenko10ad6222017-04-17 16:13:32 +02005 * dos2unix '\n' converter 0.5.0
Rob Landley330ac852006-03-14 21:49:18 +00006 * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997)
Eric Andersen544891d2001-02-22 23:37:30 +00007 * 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.
Eric Andersen544891d2001-02-22 23:37:30 +000011 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020012 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010013 */
14//config:config DOS2UNIX
Denys Vlasenkob097a842018-12-28 03:20:17 +010015//config: bool "dos2unix (5.2 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010016//config: default y
17//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020018//config: dos2unix is used to convert a text file from DOS format to
19//config: UNIX format, and vice versa.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010020//config:
21//config:config UNIX2DOS
Denys Vlasenkob097a842018-12-28 03:20:17 +010022//config: bool "unix2dos (5.2 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010023//config: default y
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010024//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020025//config: unix2dos is used to convert a text file from UNIX format to
26//config: DOS format, and vice versa.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010027
28//applet:IF_DOS2UNIX(APPLET_NOEXEC(dos2unix, dos2unix, BB_DIR_USR_BIN, BB_SUID_DROP, dos2unix))
29//applet:IF_UNIX2DOS(APPLET_NOEXEC(unix2dos, dos2unix, BB_DIR_USR_BIN, BB_SUID_DROP, unix2dos))
30
31//kbuild:lib-$(CONFIG_DOS2UNIX) += dos2unix.o
32//kbuild:lib-$(CONFIG_UNIX2DOS) += dos2unix.o
Eric Andersencff3fe32000-09-20 19:22:26 +000033
Pere Orga34425382011-03-31 14:43:25 +020034//usage:#define dos2unix_trivial_usage
35//usage: "[-ud] [FILE]"
36//usage:#define dos2unix_full_usage "\n\n"
37//usage: "Convert FILE in-place from DOS to Unix format.\n"
38//usage: "When no file is given, use stdin/stdout.\n"
Pere Orga34425382011-03-31 14:43:25 +020039//usage: "\n -u dos2unix"
40//usage: "\n -d unix2dos"
41//usage:
42//usage:#define unix2dos_trivial_usage
43//usage: "[-ud] [FILE]"
44//usage:#define unix2dos_full_usage "\n\n"
45//usage: "Convert FILE in-place from Unix to DOS format.\n"
46//usage: "When no file is given, use stdin/stdout.\n"
Pere Orga34425382011-03-31 14:43:25 +020047//usage: "\n -u dos2unix"
48//usage: "\n -d unix2dos"
49
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000050#include "libbb.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000051
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070052/* This is a NOEXEC applet. Be very careful! */
53
Denis Vlasenko74324c82007-06-04 10:16:52 +000054enum {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000055 CT_UNIX2DOS = 1,
56 CT_DOS2UNIX
Denis Vlasenko74324c82007-06-04 10:16:52 +000057};
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000058
Rob Landleyf8154692005-09-01 03:11:19 +000059/* if fn is NULL then input is stdin and output is stdout */
Denis Vlasenko54296362008-02-17 14:31:50 +000060static void convert(char *fn, int conv_type)
Eric Andersen655584b2001-07-25 07:22:55 +000061{
Rob Landleyf8154692005-09-01 03:11:19 +000062 FILE *in, *out;
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010063 int ch;
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000064 char *temp_fn = temp_fn; /* for compiler */
65 char *resolved_fn = resolved_fn;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000066
Denis Vlasenko74324c82007-06-04 10:16:52 +000067 in = stdin;
68 out = stdout;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000069 if (fn != NULL) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000070 struct stat st;
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010071 int fd;
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000072
73 resolved_fn = xmalloc_follow_symlinks(fn);
74 if (resolved_fn == NULL)
75 bb_simple_perror_msg_and_die(fn);
Denis Vlasenko5415c852008-07-21 23:05:26 +000076 in = xfopen_for_read(resolved_fn);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010077 xfstat(fileno(in), &st, resolved_fn);
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000078
79 temp_fn = xasprintf("%sXXXXXX", resolved_fn);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010080 fd = xmkstemp(temp_fn);
81 if (fchmod(fd, st.st_mode) == -1)
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000082 bb_simple_perror_msg_and_die(temp_fn);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010083 fchown(fd, st.st_uid, st.st_gid);
Alexander Shishkin67227372010-10-22 13:27:16 +020084
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010085 out = xfdopen_for_write(fd);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000086 }
87
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010088 while ((ch = fgetc(in)) != EOF) {
89 if (ch == '\r')
Rob Landleyf8154692005-09-01 03:11:19 +000090 continue;
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010091 if (ch == '\n')
Denis Vlasenko09196572007-07-21 13:27:44 +000092 if (conv_type == CT_UNIX2DOS)
Rob Landleyf8154692005-09-01 03:11:19 +000093 fputc('\r', out);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010094 fputc(ch, out);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000095 }
96
97 if (fn != NULL) {
Rob Landley330ac852006-03-14 21:49:18 +000098 if (fclose(in) < 0 || fclose(out) < 0) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000099 unlink(temp_fn);
Denis Vlasenko54296362008-02-17 14:31:50 +0000100 bb_perror_nomsg_and_die();
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +0000101 }
Denis Vlasenkoa1767a12008-06-14 04:28:41 +0000102 xrename(temp_fn, resolved_fn);
103 free(temp_fn);
104 free(resolved_fn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000105 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +0000106}
107
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000108int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100109int dos2unix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen655584b2001-07-25 07:22:55 +0000110{
Denis Vlasenko09196572007-07-21 13:27:44 +0000111 int o, conv_type;
Eric Andersen544891d2001-02-22 23:37:30 +0000112
Rob Landleyf8154692005-09-01 03:11:19 +0000113 /* See if we are supposed to be doing dos2unix or unix2dos */
Denys Vlasenko7caf1362016-11-23 14:52:19 +0100114 if (ENABLE_DOS2UNIX
115 && (!ENABLE_UNIX2DOS || applet_name[0] == 'd')
116 ) {
Denis Vlasenko54296362008-02-17 14:31:50 +0000117 conv_type = CT_DOS2UNIX;
Denys Vlasenko7caf1362016-11-23 14:52:19 +0100118 } else {
119 conv_type = CT_UNIX2DOS;
Eric Andersen655584b2001-07-25 07:22:55 +0000120 }
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000121
Denis Vlasenko09196572007-07-21 13:27:44 +0000122 /* -u convert to unix, -d convert to dos */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200123 o = getopt32(argv, "^" "du" "\0" "u--d:d--u"); /* mutually exclusive */
Rob Landleyf8154692005-09-01 03:11:19 +0000124
125 /* Do the conversion requested by an argument else do the default
126 * conversion depending on our name. */
127 if (o)
Denis Vlasenko09196572007-07-21 13:27:44 +0000128 conv_type = o;
Eric Andersen544891d2001-02-22 23:37:30 +0000129
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100130 argv += optind;
Denis Vlasenko74324c82007-06-04 10:16:52 +0000131 do {
132 /* might be convert(NULL) if there is no filename given */
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100133 convert(*argv, conv_type);
Denys Vlasenkod1516c42010-07-17 22:43:42 +0200134 } while (*argv && *++argv);
Eric Andersen544891d2001-02-22 23:37:30 +0000135
Denis Vlasenko54296362008-02-17 14:31:50 +0000136 return 0;
Eric Andersen544891d2001-02-22 23:37:30 +0000137}