[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * dos2unix for BusyBox
|
---|
| 4 | *
|
---|
| 5 | * dos2unix '\n' convertor 0.5.0
|
---|
| 6 | * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997)
|
---|
| 7 | * 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.
|
---|
| 11 | *
|
---|
| 12 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[1765] | 15 | #include "libbb.h"
|
---|
[821] | 16 |
|
---|
[1765] | 17 | enum {
|
---|
[821] | 18 | CT_UNIX2DOS = 1,
|
---|
| 19 | CT_DOS2UNIX
|
---|
[1765] | 20 | };
|
---|
[821] | 21 |
|
---|
| 22 | /* if fn is NULL then input is stdin and output is stdout */
|
---|
[1765] | 23 | static int convert(char *fn, int conv_type)
|
---|
[821] | 24 | {
|
---|
| 25 | FILE *in, *out;
|
---|
| 26 | int i;
|
---|
[1765] | 27 | #define name_buf bb_common_bufsiz1
|
---|
[821] | 28 |
|
---|
[1765] | 29 | in = stdin;
|
---|
| 30 | out = stdout;
|
---|
[821] | 31 | if (fn != NULL) {
|
---|
[1765] | 32 | in = xfopen(fn, "rw");
|
---|
[821] | 33 | /*
|
---|
| 34 | The file is then created with mode read/write and
|
---|
| 35 | permissions 0666 for glibc 2.0.6 and earlier or
|
---|
[1765] | 36 | 0600 for glibc 2.0.7 and later.
|
---|
[821] | 37 | */
|
---|
[1765] | 38 | snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
|
---|
| 39 | i = mkstemp(&name_buf[0]);
|
---|
| 40 | if (i == -1 || chmod(name_buf, 0600) == -1) {
|
---|
[821] | 41 | bb_perror_nomsg_and_die();
|
---|
| 42 | }
|
---|
| 43 | out = fdopen(i, "w+");
|
---|
| 44 | if (!out) {
|
---|
| 45 | close(i);
|
---|
[1765] | 46 | remove(name_buf);
|
---|
| 47 | return -2;
|
---|
[821] | 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | while ((i = fgetc(in)) != EOF) {
|
---|
| 52 | if (i == '\r')
|
---|
| 53 | continue;
|
---|
| 54 | if (i == '\n') {
|
---|
[1765] | 55 | if (conv_type == CT_UNIX2DOS)
|
---|
[821] | 56 | fputc('\r', out);
|
---|
| 57 | fputc('\n', out);
|
---|
| 58 | continue;
|
---|
| 59 | }
|
---|
| 60 | fputc(i, out);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (fn != NULL) {
|
---|
| 64 | if (fclose(in) < 0 || fclose(out) < 0) {
|
---|
| 65 | bb_perror_nomsg();
|
---|
[1765] | 66 | remove(name_buf);
|
---|
[821] | 67 | return -2;
|
---|
| 68 | }
|
---|
| 69 | /* Assume they are both on the same filesystem (which
|
---|
| 70 | * should be true since we put them into the same directory
|
---|
| 71 | * so we _should_ be ok, but you never know... */
|
---|
[1765] | 72 | if (rename(name_buf, fn) < 0) {
|
---|
| 73 | bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
|
---|
[821] | 74 | return -1;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | return 0;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[1765] | 81 | int dos2unix_main(int argc, char **argv);
|
---|
| 82 | int dos2unix_main(int argc, char **argv)
|
---|
[821] | 83 | {
|
---|
[1765] | 84 | int o, conv_type;
|
---|
[821] | 85 |
|
---|
| 86 | /* See if we are supposed to be doing dos2unix or unix2dos */
|
---|
[1765] | 87 | if (applet_name[0] == 'd') {
|
---|
| 88 | conv_type = CT_DOS2UNIX; /* 2 */
|
---|
[821] | 89 | } else {
|
---|
[1765] | 90 | conv_type = CT_UNIX2DOS; /* 1 */
|
---|
[821] | 91 | }
|
---|
| 92 |
|
---|
[1765] | 93 | /* -u convert to unix, -d convert to dos */
|
---|
| 94 | opt_complementary = "u--d:d--u"; /* mutually exclusive */
|
---|
| 95 | o = getopt32(argv, "du");
|
---|
| 96 |
|
---|
[821] | 97 | /* Do the conversion requested by an argument else do the default
|
---|
| 98 | * conversion depending on our name. */
|
---|
| 99 | if (o)
|
---|
[1765] | 100 | conv_type = o;
|
---|
[821] | 101 |
|
---|
[1765] | 102 | do {
|
---|
| 103 | /* might be convert(NULL) if there is no filename given */
|
---|
| 104 | o = convert(argv[optind], conv_type);
|
---|
| 105 | if (o < 0)
|
---|
| 106 | break;
|
---|
| 107 | optind++;
|
---|
| 108 | } while (optind < argc);
|
---|
[821] | 109 |
|
---|
| 110 | return o;
|
---|
| 111 | }
|
---|