Ignore:
Timestamp:
Nov 4, 2007, 3:16:40 AM (16 years ago)
Author:
Bruno Cornec
Message:

Update to busybox 1.7.2

File:
1 edited

Legend:

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

    r821 r1765  
    1313*/
    1414
    15 #include <string.h>
    16 #include <unistd.h>
    17 #include <stdint.h>
    18 #include <fcntl.h>
    19 #include "busybox.h"
     15#include "libbb.h"
    2016
    21 enum ConvType {
     17enum {
    2218    CT_UNIX2DOS = 1,
    2319    CT_DOS2UNIX
    24 } ConvType;
     20};
    2521
    2622/* if fn is NULL then input is stdin and output is stdout */
    27 static int convert(char *fn)
     23static int convert(char *fn, int conv_type)
    2824{
    2925    FILE *in, *out;
    3026    int i;
     27#define name_buf bb_common_bufsiz1
    3128
     29    in = stdin;
     30    out = stdout;
    3231    if (fn != NULL) {
    33         in = bb_xfopen(fn, "rw");
     32        in = xfopen(fn, "rw");
    3433        /*
    3534           The file is then created with mode read/write and
    3635           permissions 0666 for glibc 2.0.6 and earlier or
    37            0600  for glibc  2.0.7 and later.
     36           0600 for glibc 2.0.7 and later.
    3837         */
    39         snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn);
    40         /*
    41            sizeof bb_common_bufsiz1 is 4096, so it should be big enough to
    42            hold the full path.  However if the output is truncated the
    43            subsequent call to mkstemp would fail.
    44          */
    45         if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1
    46             || chmod(bb_common_bufsiz1, 0600) == -1) {
     38        snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn);
     39        i = mkstemp(&name_buf[0]);
     40        if (i == -1 || chmod(name_buf, 0600) == -1) {
    4741            bb_perror_nomsg_and_die();
    4842        }
     
    5044        if (!out) {
    5145            close(i);
    52             remove(bb_common_bufsiz1);
     46            remove(name_buf);
     47            return -2;
    5348        }
    54     } else {
    55         in = stdin;
    56         out = stdout;
    5749    }
    5850
     
    6153            continue;
    6254        if (i == '\n') {
    63             if (ConvType == CT_UNIX2DOS)
     55            if (conv_type == CT_UNIX2DOS)
    6456                fputc('\r', out);
    6557            fputc('\n', out);
     
    7264        if (fclose(in) < 0 || fclose(out) < 0) {
    7365            bb_perror_nomsg();
    74             remove(bb_common_bufsiz1);
     66            remove(name_buf);
    7567            return -2;
    7668        }
     
    7870         * should be true since we put them into the same directory
    7971         * so we _should_ be ok, but you never know... */
    80         if (rename(bb_common_bufsiz1, fn) < 0) {
    81             bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn);
     72        if (rename(name_buf, fn) < 0) {
     73            bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn);
    8274            return -1;
    8375        }
     
    8779}
    8880
    89 int dos2unix_main(int argc, char *argv[])
     81int dos2unix_main(int argc, char **argv);
     82int dos2unix_main(int argc, char **argv)
    9083{
    91     int o;
     84    int o, conv_type;
    9285
    9386    /* See if we are supposed to be doing dos2unix or unix2dos */
    94     if (bb_applet_name[0] == 'd') {
    95         ConvType = CT_DOS2UNIX; /*2 */
     87    if (applet_name[0] == 'd') {
     88        conv_type = CT_DOS2UNIX;    /* 2 */
    9689    } else {
    97         ConvType = CT_UNIX2DOS; /*1 */
     90        conv_type = CT_UNIX2DOS;    /* 1 */
    9891    }
    99     /* -u and -d are mutally exclusive */
    100     bb_opt_complementally = "?:u--d:d--u";
    101     /* process parameters */
    102     /* -u convert to unix */
    103     /* -d convert to dos  */
    104     o = bb_getopt_ulflags(argc, argv, "du");
     92
     93    /* -u convert to unix, -d convert to dos */
     94    opt_complementary = "u--d:d--u"; /* mutually exclusive */
     95    o = getopt32(argv, "du");
    10596
    10697    /* Do the conversion requested by an argument else do the default
    10798     * conversion depending on our name.  */
    10899    if (o)
    109         ConvType = o;
     100        conv_type = o;
    110101
    111     if (optind < argc) {
    112         while (optind < argc)
    113             if ((o = convert(argv[optind++])) < 0)
    114                 break;
    115     } else
    116         o = convert(NULL);
     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);
    117109
    118110    return o;
Note: See TracChangeset for help on using the changeset viewer.