blob: 295be290945a3655126a18e247754683d9fa11c8 [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 Vlasenko09196572007-07-21 13:27:44 +000023static int 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 Vlasenko74324c82007-06-04 10:16:52 +000027#define name_buf bb_common_bufsiz1
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) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000032 in = xfopen(fn, "rw");
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 Vlasenko74324c82007-06-04 10:16:52 +000038 snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
39 i = mkstemp(&name_buf[0]);
40 if (i == -1 || chmod(name_buf, 0600) == -1) {
Rob Landley330ac852006-03-14 21:49:18 +000041 bb_perror_nomsg_and_die();
42 }
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000043 out = fdopen(i, "w+");
Rob Landley330ac852006-03-14 21:49:18 +000044 if (!out) {
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000045 close(i);
Denis Vlasenko74324c82007-06-04 10:16:52 +000046 remove(name_buf);
47 return -2;
Glenn L McGratha6ce6702001-04-12 02:26:04 +000048 }
49 }
50
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000051 while ((i = fgetc(in)) != EOF) {
52 if (i == '\r')
Rob Landleyf8154692005-09-01 03:11:19 +000053 continue;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000054 if (i == '\n') {
Denis Vlasenko09196572007-07-21 13:27:44 +000055 if (conv_type == CT_UNIX2DOS)
Rob Landleyf8154692005-09-01 03:11:19 +000056 fputc('\r', out);
57 fputc('\n', out);
58 continue;
59 }
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000060 fputc(i, out);
Glenn L McGratha6ce6702001-04-12 02:26:04 +000061 }
62
63 if (fn != NULL) {
Rob Landley330ac852006-03-14 21:49:18 +000064 if (fclose(in) < 0 || fclose(out) < 0) {
65 bb_perror_nomsg();
Denis Vlasenko74324c82007-06-04 10:16:52 +000066 remove(name_buf);
Rob Landley330ac852006-03-14 21:49:18 +000067 return -2;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000068 }
Rob Landley330ac852006-03-14 21:49:18 +000069 /* Assume they are both on the same filesystem (which
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000070 * should be true since we put them into the same directory
71 * so we _should_ be ok, but you never know... */
Denis Vlasenko74324c82007-06-04 10:16:52 +000072 if (rename(name_buf, fn) < 0) {
73 bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
Rob Landley330ac852006-03-14 21:49:18 +000074 return -1;
Bernhard Reutner-Fischerb5353a22006-06-01 18:30:42 +000075 }
Glenn L McGratha6ce6702001-04-12 02:26:04 +000076 }
77
78 return 0;
79}
80
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000081int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000082int dos2unix_main(int argc, char **argv)
Eric Andersen655584b2001-07-25 07:22:55 +000083{
Denis Vlasenko09196572007-07-21 13:27:44 +000084 int o, conv_type;
Eric Andersen544891d2001-02-22 23:37:30 +000085
Rob Landleyf8154692005-09-01 03:11:19 +000086 /* See if we are supposed to be doing dos2unix or unix2dos */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000087 if (applet_name[0] == 'd') {
Denis Vlasenko09196572007-07-21 13:27:44 +000088 conv_type = CT_DOS2UNIX; /* 2 */
Rob Landleyf8154692005-09-01 03:11:19 +000089 } else {
Denis Vlasenko09196572007-07-21 13:27:44 +000090 conv_type = CT_UNIX2DOS; /* 1 */
Eric Andersen655584b2001-07-25 07:22:55 +000091 }
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +000092
Denis Vlasenko09196572007-07-21 13:27:44 +000093 /* -u convert to unix, -d convert to dos */
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +000094 opt_complementary = "u--d:d--u"; /* mutually exclusive */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000095 o = getopt32(argv, "du");
Rob Landleyf8154692005-09-01 03:11:19 +000096
97 /* Do the conversion requested by an argument else do the default
98 * conversion depending on our name. */
99 if (o)
Denis Vlasenko09196572007-07-21 13:27:44 +0000100 conv_type = o;
Eric Andersen544891d2001-02-22 23:37:30 +0000101
Denis Vlasenko74324c82007-06-04 10:16:52 +0000102 do {
103 /* might be convert(NULL) if there is no filename given */
Denis Vlasenko09196572007-07-21 13:27:44 +0000104 o = convert(argv[optind], conv_type);
Denis Vlasenko74324c82007-06-04 10:16:52 +0000105 if (o < 0)
106 break;
107 optind++;
108 } while (optind < argc);
Eric Andersen544891d2001-02-22 23:37:30 +0000109
110 return o;
111}