blob: ccb74a1132b99975fbd6622bb7b2a5bf52e9630d [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 *
5 * dos2unix '\n' convertor 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.
Rob Landley330ac852006-03-14 21:49:18 +000013*/
Eric Andersencff3fe32000-09-20 19:22:26 +000014
Pere Orga34425382011-03-31 14:43:25 +020015//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"
Pere Orga34425382011-03-31 14:43:25 +020020//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"
Pere Orga34425382011-03-31 14:43:25 +020028//usage: "\n -u dos2unix"
29//usage: "\n -d unix2dos"
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000032
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070033/* This is a NOEXEC applet. Be very careful! */
34
Denis Vlasenko74324c82007-06-04 10:16:52 +000035enum {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000036 CT_UNIX2DOS = 1,
37 CT_DOS2UNIX
Denis Vlasenko74324c82007-06-04 10:16:52 +000038};
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000039
Rob Landleyf8154692005-09-01 03:11:19 +000040/* if fn is NULL then input is stdin and output is stdout */
Denis Vlasenko54296362008-02-17 14:31:50 +000041static void convert(char *fn, int conv_type)
Eric Andersen655584b2001-07-25 07:22:55 +000042{
Rob Landleyf8154692005-09-01 03:11:19 +000043 FILE *in, *out;
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010044 int ch;
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000045 char *temp_fn = temp_fn; /* for compiler */
46 char *resolved_fn = resolved_fn;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000047
Denis Vlasenko74324c82007-06-04 10:16:52 +000048 in = stdin;
49 out = stdout;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000050 if (fn != NULL) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000051 struct stat st;
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010052 int fd;
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000053
54 resolved_fn = xmalloc_follow_symlinks(fn);
55 if (resolved_fn == NULL)
56 bb_simple_perror_msg_and_die(fn);
Denis Vlasenko5415c852008-07-21 23:05:26 +000057 in = xfopen_for_read(resolved_fn);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010058 xfstat(fileno(in), &st, resolved_fn);
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000059
60 temp_fn = xasprintf("%sXXXXXX", resolved_fn);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010061 fd = xmkstemp(temp_fn);
62 if (fchmod(fd, st.st_mode) == -1)
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000063 bb_simple_perror_msg_and_die(temp_fn);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010064 fchown(fd, st.st_uid, st.st_gid);
Alexander Shishkin67227372010-10-22 13:27:16 +020065
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010066 out = xfdopen_for_write(fd);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000067 }
68
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010069 while ((ch = fgetc(in)) != EOF) {
70 if (ch == '\r')
Rob Landleyf8154692005-09-01 03:11:19 +000071 continue;
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010072 if (ch == '\n')
Denis Vlasenko09196572007-07-21 13:27:44 +000073 if (conv_type == CT_UNIX2DOS)
Rob Landleyf8154692005-09-01 03:11:19 +000074 fputc('\r', out);
Denys Vlasenkoc7e47cf2016-01-17 03:50:36 +010075 fputc(ch, out);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000076 }
77
78 if (fn != NULL) {
Rob Landley330ac852006-03-14 21:49:18 +000079 if (fclose(in) < 0 || fclose(out) < 0) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000080 unlink(temp_fn);
Denis Vlasenko54296362008-02-17 14:31:50 +000081 bb_perror_nomsg_and_die();
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000082 }
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000083 xrename(temp_fn, resolved_fn);
84 free(temp_fn);
85 free(resolved_fn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000086 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +000087}
88
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000089int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010090int dos2unix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen655584b2001-07-25 07:22:55 +000091{
Denis Vlasenko09196572007-07-21 13:27:44 +000092 int o, conv_type;
Eric Andersen544891d2001-02-22 23:37:30 +000093
Rob Landleyf8154692005-09-01 03:11:19 +000094 /* See if we are supposed to be doing dos2unix or unix2dos */
Denis Vlasenko54296362008-02-17 14:31:50 +000095 conv_type = CT_UNIX2DOS;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000096 if (applet_name[0] == 'd') {
Denis Vlasenko54296362008-02-17 14:31:50 +000097 conv_type = CT_DOS2UNIX;
Eric Andersen655584b2001-07-25 07:22:55 +000098 }
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +000099
Denis Vlasenko09196572007-07-21 13:27:44 +0000100 /* -u convert to unix, -d convert to dos */
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +0000101 opt_complementary = "u--d:d--u"; /* mutually exclusive */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000102 o = getopt32(argv, "du");
Rob Landleyf8154692005-09-01 03:11:19 +0000103
104 /* Do the conversion requested by an argument else do the default
105 * conversion depending on our name. */
106 if (o)
Denis Vlasenko09196572007-07-21 13:27:44 +0000107 conv_type = o;
Eric Andersen544891d2001-02-22 23:37:30 +0000108
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100109 argv += optind;
Denis Vlasenko74324c82007-06-04 10:16:52 +0000110 do {
111 /* might be convert(NULL) if there is no filename given */
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100112 convert(*argv, conv_type);
Denys Vlasenkod1516c42010-07-17 22:43:42 +0200113 } while (*argv && *++argv);
Eric Andersen544891d2001-02-22 23:37:30 +0000114
Denis Vlasenko54296362008-02-17 14:31:50 +0000115 return 0;
Eric Andersen544891d2001-02-22 23:37:30 +0000116}