blob: a8038a6dd3aa92ecefe544a6cddf17c69dd776ef [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
Eric Andersencbe31da2001-02-20 06:14:08 +000015#include "busybox.h"
Eric Andersencff3fe32000-09-20 19:22:26 +000016
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000017enum ConvType {
18 CT_UNIX2DOS = 1,
19 CT_DOS2UNIX
20} ConvType;
"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 */
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000023static int convert(char *fn)
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;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000027
28 if (fn != NULL) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000029 in = xfopen(fn, "rw");
Rob Landley330ac852006-03-14 21:49:18 +000030 /*
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000031 The file is then created with mode read/write and
32 permissions 0666 for glibc 2.0.6 and earlier or
33 0600 for glibc 2.0.7 and later.
34 */
35 snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
Rob Landley330ac852006-03-14 21:49:18 +000036 /*
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000037 sizeof bb_common_bufsiz1 is 4096, so it should be big enough to
38 hold the full path. However if the output is truncated the
39 subsequent call to mkstemp would fail.
40 */
41 if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1
42 || chmod(bb_common_bufsiz1, 0600) == -1) {
Rob Landley330ac852006-03-14 21:49:18 +000043 bb_perror_nomsg_and_die();
44 }
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000045 out = fdopen(i, "w+");
Rob Landley330ac852006-03-14 21:49:18 +000046 if (!out) {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000047 close(i);
48 remove(bb_common_bufsiz1);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000049 }
Rob Landleyf8154692005-09-01 03:11:19 +000050 } else {
51 in = stdin;
52 out = stdout;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000053 }
54
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000055 while ((i = fgetc(in)) != EOF) {
56 if (i == '\r')
Rob Landleyf8154692005-09-01 03:11:19 +000057 continue;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000058 if (i == '\n') {
Rob Landleyf8154692005-09-01 03:11:19 +000059 if (ConvType == CT_UNIX2DOS)
60 fputc('\r', out);
61 fputc('\n', out);
62 continue;
63 }
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000064 fputc(i, out);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000065 }
66
67 if (fn != NULL) {
Rob Landley330ac852006-03-14 21:49:18 +000068 if (fclose(in) < 0 || fclose(out) < 0) {
69 bb_perror_nomsg();
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000070 remove(bb_common_bufsiz1);
Rob Landley330ac852006-03-14 21:49:18 +000071 return -2;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000072 }
Rob Landley330ac852006-03-14 21:49:18 +000073 /* Assume they are both on the same filesystem (which
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000074 * should be true since we put them into the same directory
75 * so we _should_ be ok, but you never know... */
76 if (rename(bb_common_bufsiz1, fn) < 0) {
77 bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn);
Rob Landley330ac852006-03-14 21:49:18 +000078 return -1;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000079 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +000080 }
81
82 return 0;
83}
84
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085int dos2unix_main(int argc, char *argv[])
Eric Andersen655584b2001-07-25 07:22:55 +000086{
Eric Andersen544891d2001-02-22 23:37:30 +000087 int o;
88
Rob Landleyf8154692005-09-01 03:11:19 +000089 /* See if we are supposed to be doing dos2unix or unix2dos */
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000090 if (bb_applet_name[0] == 'd') {
91 ConvType = CT_DOS2UNIX; /*2 */
Rob Landleyf8154692005-09-01 03:11:19 +000092 } else {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000093 ConvType = CT_UNIX2DOS; /*1 */
Eric Andersen655584b2001-07-25 07:22:55 +000094 }
Rob Landley330ac852006-03-14 21:49:18 +000095 /* -u and -d are mutally exclusive */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000096 opt_complementary = "?:u--d:d--u";
Rob Landleyf8154692005-09-01 03:11:19 +000097 /* process parameters */
Rob Landley330ac852006-03-14 21:49:18 +000098 /* -u convert to unix */
99 /* -d convert to dos */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000100 o = getopt32(argc, 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)
105 ConvType = o;
Eric Andersen544891d2001-02-22 23:37:30 +0000106
107 if (optind < argc) {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +0000108 while (optind < argc)
109 if ((o = convert(argv[optind++])) < 0)
Eric Andersen544891d2001-02-22 23:37:30 +0000110 break;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +0000111 } else
112 o = convert(NULL);
Eric Andersen544891d2001-02-22 23:37:30 +0000113
114 return o;
115}