diff options
-rw-r--r-- | coreutils/dos2unix.c | 39 |
1 files changed, 12 insertions, 27 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index 295be2909..7776133f4 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c | |||
@@ -20,7 +20,7 @@ enum { | |||
20 | }; | 20 | }; |
21 | 21 | ||
22 | /* if fn is NULL then input is stdin and output is stdout */ | 22 | /* if fn is NULL then input is stdin and output is stdout */ |
23 | static int convert(char *fn, int conv_type) | 23 | static void convert(char *fn, int conv_type) |
24 | { | 24 | { |
25 | FILE *in, *out; | 25 | FILE *in, *out; |
26 | int i; | 26 | int i; |
@@ -37,15 +37,12 @@ static int convert(char *fn, int conv_type) | |||
37 | */ | 37 | */ |
38 | snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn); | 38 | snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn); |
39 | i = mkstemp(&name_buf[0]); | 39 | i = mkstemp(&name_buf[0]); |
40 | if (i == -1 || chmod(name_buf, 0600) == -1) { | 40 | if (i == -1 |
41 | || fchmod(i, 0600) == -1 | ||
42 | || !(out = fdopen(i, "w+")) | ||
43 | ) { | ||
41 | bb_perror_nomsg_and_die(); | 44 | bb_perror_nomsg_and_die(); |
42 | } | 45 | } |
43 | out = fdopen(i, "w+"); | ||
44 | if (!out) { | ||
45 | close(i); | ||
46 | remove(name_buf); | ||
47 | return -2; | ||
48 | } | ||
49 | } | 46 | } |
50 | 47 | ||
51 | while ((i = fgetc(in)) != EOF) { | 48 | while ((i = fgetc(in)) != EOF) { |
@@ -62,20 +59,11 @@ static int convert(char *fn, int conv_type) | |||
62 | 59 | ||
63 | if (fn != NULL) { | 60 | if (fn != NULL) { |
64 | if (fclose(in) < 0 || fclose(out) < 0) { | 61 | if (fclose(in) < 0 || fclose(out) < 0) { |
65 | bb_perror_nomsg(); | 62 | unlink(name_buf); |
66 | remove(name_buf); | 63 | bb_perror_nomsg_and_die(); |
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... */ | ||
72 | if (rename(name_buf, fn) < 0) { | ||
73 | bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn); | ||
74 | return -1; | ||
75 | } | 64 | } |
65 | xrename(name_buf, fn); | ||
76 | } | 66 | } |
77 | |||
78 | return 0; | ||
79 | } | 67 | } |
80 | 68 | ||
81 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 69 | int dos2unix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
@@ -84,10 +72,9 @@ int dos2unix_main(int argc, char **argv) | |||
84 | int o, conv_type; | 72 | int o, conv_type; |
85 | 73 | ||
86 | /* See if we are supposed to be doing dos2unix or unix2dos */ | 74 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
75 | conv_type = CT_UNIX2DOS; | ||
87 | if (applet_name[0] == 'd') { | 76 | if (applet_name[0] == 'd') { |
88 | conv_type = CT_DOS2UNIX; /* 2 */ | 77 | conv_type = CT_DOS2UNIX; |
89 | } else { | ||
90 | conv_type = CT_UNIX2DOS; /* 1 */ | ||
91 | } | 78 | } |
92 | 79 | ||
93 | /* -u convert to unix, -d convert to dos */ | 80 | /* -u convert to unix, -d convert to dos */ |
@@ -101,11 +88,9 @@ int dos2unix_main(int argc, char **argv) | |||
101 | 88 | ||
102 | do { | 89 | do { |
103 | /* might be convert(NULL) if there is no filename given */ | 90 | /* might be convert(NULL) if there is no filename given */ |
104 | o = convert(argv[optind], conv_type); | 91 | convert(argv[optind], conv_type); |
105 | if (o < 0) | ||
106 | break; | ||
107 | optind++; | 92 | optind++; |
108 | } while (optind < argc); | 93 | } while (optind < argc); |
109 | 94 | ||
110 | return o; | 95 | return 0; |
111 | } | 96 | } |