Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/coreutils/dos2unix.c

    r1765 r2725  
    1010 * dos2unix filters reading input from stdin and writing output to stdout.
    1111 *
    12  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
     12 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    1313*/
    1414
    1515#include "libbb.h"
     16
     17/* This is a NOEXEC applet. Be very careful! */
    1618
    1719enum {
     
    2123
    2224/* if fn is NULL then input is stdin and output is stdout */
    23 static int convert(char *fn, int conv_type)
     25static void convert(char *fn, int conv_type)
    2426{
    2527    FILE *in, *out;
    2628    int i;
    27 #define name_buf bb_common_bufsiz1
     29    char *temp_fn = temp_fn; /* for compiler */
     30    char *resolved_fn = resolved_fn;
    2831
    2932    in = stdin;
    3033    out = stdout;
    3134    if (fn != NULL) {
    32         in = xfopen(fn, "rw");
    33         /*
    34            The file is then created with mode read/write and
    35            permissions 0666 for glibc 2.0.6 and earlier or
    36            0600 for glibc 2.0.7 and later.
    37          */
    38         snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
    39         i = mkstemp(&name_buf[0]);
    40         if (i == -1 || chmod(name_buf, 0600) == -1) {
    41             bb_perror_nomsg_and_die();
    42         }
    43         out = fdopen(i, "w+");
    44         if (!out) {
    45             close(i);
    46             remove(name_buf);
    47             return -2;
    48         }
     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);
    4949    }
    5050
     
    5252        if (i == '\r')
    5353            continue;
    54         if (i == '\n') {
     54        if (i == '\n')
    5555            if (conv_type == CT_UNIX2DOS)
    5656                fputc('\r', out);
    57             fputc('\n', out);
    58             continue;
    59         }
    6057        fputc(i, out);
    6158    }
     
    6360    if (fn != NULL) {
    6461        if (fclose(in) < 0 || fclose(out) < 0) {
    65             bb_perror_nomsg();
    66             remove(name_buf);
    67             return -2;
     62            unlink(temp_fn);
     63            bb_perror_nomsg_and_die();
    6864        }
    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... */
    72         if (rename(name_buf, fn) < 0) {
    73             bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
    74             return -1;
    75         }
     65        xrename(temp_fn, resolved_fn);
     66        free(temp_fn);
     67        free(resolved_fn);
    7668    }
    77 
    78     return 0;
    7969}
    8070
    81 int dos2unix_main(int argc, char **argv);
    82 int dos2unix_main(int argc, char **argv)
     71int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     72int dos2unix_main(int argc UNUSED_PARAM, char **argv)
    8373{
    8474    int o, conv_type;
    8575
    8676    /* See if we are supposed to be doing dos2unix or unix2dos */
     77    conv_type = CT_UNIX2DOS;
    8778    if (applet_name[0] == 'd') {
    88         conv_type = CT_DOS2UNIX;    /* 2 */
    89     } else {
    90         conv_type = CT_UNIX2DOS;    /* 1 */
     79        conv_type = CT_DOS2UNIX;
    9180    }
    9281
     
    10089        conv_type = o;
    10190
     91    argv += optind;
    10292    do {
    10393        /* 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);
     94        convert(*argv, conv_type);
     95    } while (*argv && *++argv);
    10996
    110     return o;
     97    return 0;
    11198}
Note: See TracChangeset for help on using the changeset viewer.