[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 | *
|
---|
[2725] | 12 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 13 | */
|
---|
| 14 |
|
---|
[1765] | 15 | #include "libbb.h"
|
---|
[821] | 16 |
|
---|
[2725] | 17 | /* This is a NOEXEC applet. Be very careful! */
|
---|
| 18 |
|
---|
[1765] | 19 | enum {
|
---|
[821] | 20 | CT_UNIX2DOS = 1,
|
---|
| 21 | CT_DOS2UNIX
|
---|
[1765] | 22 | };
|
---|
[821] | 23 |
|
---|
| 24 | /* if fn is NULL then input is stdin and output is stdout */
|
---|
[2725] | 25 | static void convert(char *fn, int conv_type)
|
---|
[821] | 26 | {
|
---|
| 27 | FILE *in, *out;
|
---|
| 28 | int i;
|
---|
[2725] | 29 | char *temp_fn = temp_fn; /* for compiler */
|
---|
| 30 | char *resolved_fn = resolved_fn;
|
---|
[821] | 31 |
|
---|
[1765] | 32 | in = stdin;
|
---|
| 33 | out = stdout;
|
---|
[821] | 34 | if (fn != NULL) {
|
---|
[2725] | 35 | struct stat st;
|
---|
| 36 |
|
---|
| 37 | resolved_fn = xmalloc_follow_symlinks(fn);
|
---|
| 38 | if (resolved_fn == NULL)
|
---|
| 39 | bb_simple_perror_msg_and_die(fn);
|
---|
| 40 | in = xfopen_for_read(resolved_fn);
|
---|
| 41 | fstat(fileno(in), &st);
|
---|
| 42 |
|
---|
| 43 | temp_fn = xasprintf("%sXXXXXX", resolved_fn);
|
---|
| 44 | i = xmkstemp(temp_fn);
|
---|
| 45 | if (fchmod(i, st.st_mode) == -1)
|
---|
| 46 | bb_simple_perror_msg_and_die(temp_fn);
|
---|
| 47 |
|
---|
| 48 | out = xfdopen_for_write(i);
|
---|
[821] | 49 | }
|
---|
| 50 |
|
---|
| 51 | while ((i = fgetc(in)) != EOF) {
|
---|
| 52 | if (i == '\r')
|
---|
| 53 | continue;
|
---|
[2725] | 54 | if (i == '\n')
|
---|
[1765] | 55 | if (conv_type == CT_UNIX2DOS)
|
---|
[821] | 56 | fputc('\r', out);
|
---|
| 57 | fputc(i, out);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if (fn != NULL) {
|
---|
| 61 | if (fclose(in) < 0 || fclose(out) < 0) {
|
---|
[2725] | 62 | unlink(temp_fn);
|
---|
| 63 | bb_perror_nomsg_and_die();
|
---|
[821] | 64 | }
|
---|
[2725] | 65 | xrename(temp_fn, resolved_fn);
|
---|
| 66 | free(temp_fn);
|
---|
| 67 | free(resolved_fn);
|
---|
[821] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[2725] | 71 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 72 | int dos2unix_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 73 | {
|
---|
[1765] | 74 | int o, conv_type;
|
---|
[821] | 75 |
|
---|
| 76 | /* See if we are supposed to be doing dos2unix or unix2dos */
|
---|
[2725] | 77 | conv_type = CT_UNIX2DOS;
|
---|
[1765] | 78 | if (applet_name[0] == 'd') {
|
---|
[2725] | 79 | conv_type = CT_DOS2UNIX;
|
---|
[821] | 80 | }
|
---|
| 81 |
|
---|
[1765] | 82 | /* -u convert to unix, -d convert to dos */
|
---|
| 83 | opt_complementary = "u--d:d--u"; /* mutually exclusive */
|
---|
| 84 | o = getopt32(argv, "du");
|
---|
| 85 |
|
---|
[821] | 86 | /* Do the conversion requested by an argument else do the default
|
---|
| 87 | * conversion depending on our name. */
|
---|
| 88 | if (o)
|
---|
[1765] | 89 | conv_type = o;
|
---|
[821] | 90 |
|
---|
[2725] | 91 | argv += optind;
|
---|
[1765] | 92 | do {
|
---|
| 93 | /* might be convert(NULL) if there is no filename given */
|
---|
[2725] | 94 | convert(*argv, conv_type);
|
---|
| 95 | } while (*argv && *++argv);
|
---|
[821] | 96 |
|
---|
[2725] | 97 | return 0;
|
---|
[821] | 98 | }
|
---|