blob: ba1ca8c79dd906f3292c2a1cb3ea31a00f282bbf [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000016
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070017/* This is a NOEXEC applet. Be very careful! */
18
Denis Vlasenko74324c82007-06-04 10:16:52 +000019enum {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000020 CT_UNIX2DOS = 1,
21 CT_DOS2UNIX
Denis Vlasenko74324c82007-06-04 10:16:52 +000022};
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000023
Rob Landleyf8154692005-09-01 03:11:19 +000024/* if fn is NULL then input is stdin and output is stdout */
Denis Vlasenko54296362008-02-17 14:31:50 +000025static void convert(char *fn, int conv_type)
Eric Andersen655584b2001-07-25 07:22:55 +000026{
Rob Landleyf8154692005-09-01 03:11:19 +000027 FILE *in, *out;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000028 int i;
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000029 char *temp_fn = temp_fn; /* for compiler */
30 char *resolved_fn = resolved_fn;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000031
Denis Vlasenko74324c82007-06-04 10:16:52 +000032 in = stdin;
33 out = stdout;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000034 if (fn != NULL) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000035 struct stat st;
36
37 resolved_fn = xmalloc_follow_symlinks(fn);
38 if (resolved_fn == NULL)
39 bb_simple_perror_msg_and_die(fn);
Denis Vlasenko5415c852008-07-21 23:05:26 +000040 in = xfopen_for_read(resolved_fn);
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000041 fstat(fileno(in), &st);
42
43 temp_fn = xasprintf("%sXXXXXX", resolved_fn);
44 i = mkstemp(temp_fn);
Denis Vlasenko54296362008-02-17 14:31:50 +000045 if (i == -1
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000046 || fchmod(i, st.st_mode) == -1
Denis Vlasenko54296362008-02-17 14:31:50 +000047 ) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000048 bb_simple_perror_msg_and_die(temp_fn);
Rob Landley330ac852006-03-14 21:49:18 +000049 }
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +010050 out = xfdopen_for_write(i);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000051 }
52
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000053 while ((i = fgetc(in)) != EOF) {
54 if (i == '\r')
Rob Landleyf8154692005-09-01 03:11:19 +000055 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +000056 if (i == '\n')
Denis Vlasenko09196572007-07-21 13:27:44 +000057 if (conv_type == CT_UNIX2DOS)
Rob Landleyf8154692005-09-01 03:11:19 +000058 fputc('\r', out);
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000059 fputc(i, out);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000060 }
61
62 if (fn != NULL) {
Rob Landley330ac852006-03-14 21:49:18 +000063 if (fclose(in) < 0 || fclose(out) < 0) {
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000064 unlink(temp_fn);
Denis Vlasenko54296362008-02-17 14:31:50 +000065 bb_perror_nomsg_and_die();
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000066 }
Denis Vlasenkoa1767a12008-06-14 04:28:41 +000067 xrename(temp_fn, resolved_fn);
68 free(temp_fn);
69 free(resolved_fn);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000070 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +000071}
72
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000073int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010074int dos2unix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen655584b2001-07-25 07:22:55 +000075{
Denis Vlasenko09196572007-07-21 13:27:44 +000076 int o, conv_type;
Eric Andersen544891d2001-02-22 23:37:30 +000077
Rob Landleyf8154692005-09-01 03:11:19 +000078 /* See if we are supposed to be doing dos2unix or unix2dos */
Denis Vlasenko54296362008-02-17 14:31:50 +000079 conv_type = CT_UNIX2DOS;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000080 if (applet_name[0] == 'd') {
Denis Vlasenko54296362008-02-17 14:31:50 +000081 conv_type = CT_DOS2UNIX;
Eric Andersen655584b2001-07-25 07:22:55 +000082 }
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +000083
Denis Vlasenko09196572007-07-21 13:27:44 +000084 /* -u convert to unix, -d convert to dos */
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +000085 opt_complementary = "u--d:d--u"; /* mutually exclusive */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000086 o = getopt32(argv, "du");
Rob Landleyf8154692005-09-01 03:11:19 +000087
88 /* Do the conversion requested by an argument else do the default
89 * conversion depending on our name. */
90 if (o)
Denis Vlasenko09196572007-07-21 13:27:44 +000091 conv_type = o;
Eric Andersen544891d2001-02-22 23:37:30 +000092
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010093 argv += optind;
Denis Vlasenko74324c82007-06-04 10:16:52 +000094 do {
95 /* might be convert(NULL) if there is no filename given */
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010096 convert(*argv, conv_type);
Denys Vlasenkod1516c42010-07-17 22:43:42 +020097 } while (*argv && *++argv);
Eric Andersen544891d2001-02-22 23:37:30 +000098
Denis Vlasenko54296362008-02-17 14:31:50 +000099 return 0;
Eric Andersen544891d2001-02-22 23:37:30 +0000100}