blob: 07398bdfa71f1926376cb7c0a65522523acc6f5e [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;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000044 int i;
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;
52
53 resolved_fn = xmalloc_follow_symlinks(fn);
54 if (resolved_fn == NULL)
55 bb_simple_perror_msg_and_die(fn);
Denis Vlasenko5415c852008-07-21 23:05:26 +000056 in = xfopen_for_read(resolved_fn);
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000057 fstat(fileno(in), &st);
58
59 temp_fn = xasprintf("%sXXXXXX", resolved_fn);
Alexander Shishkin67227372010-10-22 13:27:16 +020060 i = xmkstemp(temp_fn);
61 if (fchmod(i, st.st_mode) == -1)
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000062 bb_simple_perror_msg_and_die(temp_fn);
Alexander Shishkin67227372010-10-22 13:27:16 +020063
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +010064 out = xfdopen_for_write(i);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000065 }
66
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000067 while ((i = fgetc(in)) != EOF) {
68 if (i == '\r')
Rob Landleyf8154692005-09-01 03:11:19 +000069 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +000070 if (i == '\n')
Denis Vlasenko09196572007-07-21 13:27:44 +000071 if (conv_type == CT_UNIX2DOS)
Rob Landleyf8154692005-09-01 03:11:19 +000072 fputc('\r', out);
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000073 fputc(i, out);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000074 }
75
76 if (fn != NULL) {
Rob Landley330ac852006-03-14 21:49:18 +000077 if (fclose(in) < 0 || fclose(out) < 0) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000078 unlink(temp_fn);
Denis Vlasenko54296362008-02-17 14:31:50 +000079 bb_perror_nomsg_and_die();
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000080 }
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000081 xrename(temp_fn, resolved_fn);
82 free(temp_fn);
83 free(resolved_fn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000084 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +000085}
86
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000087int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010088int dos2unix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen655584b2001-07-25 07:22:55 +000089{
Denis Vlasenko09196572007-07-21 13:27:44 +000090 int o, conv_type;
Eric Andersen544891d2001-02-22 23:37:30 +000091
Rob Landleyf8154692005-09-01 03:11:19 +000092 /* See if we are supposed to be doing dos2unix or unix2dos */
Denis Vlasenko54296362008-02-17 14:31:50 +000093 conv_type = CT_UNIX2DOS;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000094 if (applet_name[0] == 'd') {
Denis Vlasenko54296362008-02-17 14:31:50 +000095 conv_type = CT_DOS2UNIX;
Eric Andersen655584b2001-07-25 07:22:55 +000096 }
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +000097
Denis Vlasenko09196572007-07-21 13:27:44 +000098 /* -u convert to unix, -d convert to dos */
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +000099 opt_complementary = "u--d:d--u"; /* mutually exclusive */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000100 o = getopt32(argv, "du");
Rob Landleyf8154692005-09-01 03:11:19 +0000101
102 /* Do the conversion requested by an argument else do the default
103 * conversion depending on our name. */
104 if (o)
Denis Vlasenko09196572007-07-21 13:27:44 +0000105 conv_type = o;
Eric Andersen544891d2001-02-22 23:37:30 +0000106
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100107 argv += optind;
Denis Vlasenko74324c82007-06-04 10:16:52 +0000108 do {
109 /* might be convert(NULL) if there is no filename given */
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100110 convert(*argv, conv_type);
Denys Vlasenkod1516c42010-07-17 22:43:42 +0200111 } while (*argv && *++argv);
Eric Andersen544891d2001-02-22 23:37:30 +0000112
Denis Vlasenko54296362008-02-17 14:31:50 +0000113 return 0;
Eric Andersen544891d2001-02-22 23:37:30 +0000114}