blob: 2db7e11a17f7fb9eb375b0a4268d50e01d610f48 [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 *
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000012 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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
Denis Vlasenko74324c82007-06-04 10:16:52 +000017enum {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000018 CT_UNIX2DOS = 1,
19 CT_DOS2UNIX
Denis Vlasenko74324c82007-06-04 10:16:52 +000020};
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +000021
Rob Landleyf8154692005-09-01 03:11:19 +000022/* if fn is NULL then input is stdin and output is stdout */
Denis Vlasenko54296362008-02-17 14:31:50 +000023static void convert(char *fn, int conv_type)
Eric Andersen655584b2001-07-25 07:22:55 +000024{
Rob Landleyf8154692005-09-01 03:11:19 +000025 FILE *in, *out;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000026 int i;
Denis Vlasenkocdf62772008-03-17 08:42:43 +000027 char *name_buf = name_buf; /* for compiler */
Glenn L McGratha6ce6702001-04-12 02:26:04 +000028
Denis Vlasenko74324c82007-06-04 10:16:52 +000029 in = stdin;
30 out = stdout;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000031 if (fn != NULL) {
Denis Vlasenkocdf62772008-03-17 08:42:43 +000032 in = xfopen(fn, "r");
Rob Landley330ac852006-03-14 21:49:18 +000033 /*
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000034 The file is then created with mode read/write and
35 permissions 0666 for glibc 2.0.6 and earlier or
Denis Vlasenko50f7f442007-04-11 23:20:53 +000036 0600 for glibc 2.0.7 and later.
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000037 */
Denis Vlasenkocdf62772008-03-17 08:42:43 +000038 name_buf = xasprintf("%sXXXXXX", fn);
39 i = mkstemp(name_buf);
Denis Vlasenko54296362008-02-17 14:31:50 +000040 if (i == -1
41 || fchmod(i, 0600) == -1
42 || !(out = fdopen(i, "w+"))
43 ) {
Rob Landley330ac852006-03-14 21:49:18 +000044 bb_perror_nomsg_and_die();
45 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +000046 }
47
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000048 while ((i = fgetc(in)) != EOF) {
49 if (i == '\r')
Rob Landleyf8154692005-09-01 03:11:19 +000050 continue;
Denis Vlasenkocdf62772008-03-17 08:42:43 +000051 if (i == '\n')
Denis Vlasenko09196572007-07-21 13:27:44 +000052 if (conv_type == CT_UNIX2DOS)
Rob Landleyf8154692005-09-01 03:11:19 +000053 fputc('\r', out);
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000054 fputc(i, out);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000055 }
56
57 if (fn != NULL) {
Rob Landley330ac852006-03-14 21:49:18 +000058 if (fclose(in) < 0 || fclose(out) < 0) {
Denis Vlasenko54296362008-02-17 14:31:50 +000059 unlink(name_buf);
60 bb_perror_nomsg_and_die();
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000061 }
Denis Vlasenkocdf62772008-03-17 08:42:43 +000062// TODO: destroys symlinks. See how passwd handles this
Denis Vlasenko54296362008-02-17 14:31:50 +000063 xrename(name_buf, fn);
Denis Vlasenkocdf62772008-03-17 08:42:43 +000064 free(name_buf);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000065 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +000066}
67
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000068int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000069int dos2unix_main(int argc, char **argv)
Eric Andersen655584b2001-07-25 07:22:55 +000070{
Denis Vlasenko09196572007-07-21 13:27:44 +000071 int o, conv_type;
Eric Andersen544891d2001-02-22 23:37:30 +000072
Rob Landleyf8154692005-09-01 03:11:19 +000073 /* See if we are supposed to be doing dos2unix or unix2dos */
Denis Vlasenko54296362008-02-17 14:31:50 +000074 conv_type = CT_UNIX2DOS;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000075 if (applet_name[0] == 'd') {
Denis Vlasenko54296362008-02-17 14:31:50 +000076 conv_type = CT_DOS2UNIX;
Eric Andersen655584b2001-07-25 07:22:55 +000077 }
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +000078
Denis Vlasenko09196572007-07-21 13:27:44 +000079 /* -u convert to unix, -d convert to dos */
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +000080 opt_complementary = "u--d:d--u"; /* mutually exclusive */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000081 o = getopt32(argv, "du");
Rob Landleyf8154692005-09-01 03:11:19 +000082
83 /* Do the conversion requested by an argument else do the default
84 * conversion depending on our name. */
85 if (o)
Denis Vlasenko09196572007-07-21 13:27:44 +000086 conv_type = o;
Eric Andersen544891d2001-02-22 23:37:30 +000087
Denis Vlasenko74324c82007-06-04 10:16:52 +000088 do {
89 /* might be convert(NULL) if there is no filename given */
Denis Vlasenko54296362008-02-17 14:31:50 +000090 convert(argv[optind], conv_type);
Denis Vlasenko74324c82007-06-04 10:16:52 +000091 optind++;
92 } while (optind < argc);
Eric Andersen544891d2001-02-22 23:37:30 +000093
Denis Vlasenko54296362008-02-17 14:31:50 +000094 return 0;
Eric Andersen544891d2001-02-22 23:37:30 +000095}