diff options
Diffstat (limited to 'coreutils/dos2unix.c')
-rw-r--r-- | coreutils/dos2unix.c | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index b053a0cbf..86adcd91f 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c | |||
@@ -14,17 +14,20 @@ | |||
14 | 14 | ||
15 | #include "libbb.h" | 15 | #include "libbb.h" |
16 | 16 | ||
17 | enum ConvType { | 17 | enum { |
18 | CT_UNIX2DOS = 1, | 18 | CT_UNIX2DOS = 1, |
19 | CT_DOS2UNIX | 19 | CT_DOS2UNIX |
20 | } ConvType; | 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) | 23 | static int convert(char *fn, int ConvType) |
24 | { | 24 | { |
25 | FILE *in, *out; | 25 | FILE *in, *out; |
26 | int i; | 26 | int i; |
27 | #define name_buf bb_common_bufsiz1 | ||
27 | 28 | ||
29 | in = stdin; | ||
30 | out = stdout; | ||
28 | if (fn != NULL) { | 31 | if (fn != NULL) { |
29 | in = xfopen(fn, "rw"); | 32 | in = xfopen(fn, "rw"); |
30 | /* | 33 | /* |
@@ -32,24 +35,17 @@ static int convert(char *fn) | |||
32 | permissions 0666 for glibc 2.0.6 and earlier or | 35 | permissions 0666 for glibc 2.0.6 and earlier or |
33 | 0600 for glibc 2.0.7 and later. | 36 | 0600 for glibc 2.0.7 and later. |
34 | */ | 37 | */ |
35 | snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn); | 38 | snprintf(name_buf, sizeof(name_buf), "%sXXXXXX", fn); |
36 | /* | 39 | i = mkstemp(&name_buf[0]); |
37 | sizeof bb_common_bufsiz1 is 4096, so it should be big enough to | 40 | if (i == -1 || chmod(name_buf, 0600) == -1) { |
38 | hold the full path. However if the output is truncated the | ||
39 | subsequent call to mkstemp would fail. | ||
40 | */ | ||
41 | i = mkstemp(&bb_common_bufsiz1[0]); | ||
42 | if (i == -1 || chmod(bb_common_bufsiz1, 0600) == -1) { | ||
43 | bb_perror_nomsg_and_die(); | 41 | bb_perror_nomsg_and_die(); |
44 | } | 42 | } |
45 | out = fdopen(i, "w+"); | 43 | out = fdopen(i, "w+"); |
46 | if (!out) { | 44 | if (!out) { |
47 | close(i); | 45 | close(i); |
48 | remove(bb_common_bufsiz1); | 46 | remove(name_buf); |
47 | return -2; | ||
49 | } | 48 | } |
50 | } else { | ||
51 | in = stdin; | ||
52 | out = stdout; | ||
53 | } | 49 | } |
54 | 50 | ||
55 | while ((i = fgetc(in)) != EOF) { | 51 | while ((i = fgetc(in)) != EOF) { |
@@ -67,14 +63,14 @@ static int convert(char *fn) | |||
67 | if (fn != NULL) { | 63 | if (fn != NULL) { |
68 | if (fclose(in) < 0 || fclose(out) < 0) { | 64 | if (fclose(in) < 0 || fclose(out) < 0) { |
69 | bb_perror_nomsg(); | 65 | bb_perror_nomsg(); |
70 | remove(bb_common_bufsiz1); | 66 | remove(name_buf); |
71 | return -2; | 67 | return -2; |
72 | } | 68 | } |
73 | /* Assume they are both on the same filesystem (which | 69 | /* Assume they are both on the same filesystem (which |
74 | * should be true since we put them into the same directory | 70 | * should be true since we put them into the same directory |
75 | * so we _should_ be ok, but you never know... */ | 71 | * so we _should_ be ok, but you never know... */ |
76 | if (rename(bb_common_bufsiz1, fn) < 0) { | 72 | if (rename(name_buf, fn) < 0) { |
77 | bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn); | 73 | bb_perror_msg("cannot rename '%s' as '%s'", name_buf, fn); |
78 | return -1; | 74 | return -1; |
79 | } | 75 | } |
80 | } | 76 | } |
@@ -85,13 +81,13 @@ static int convert(char *fn) | |||
85 | int dos2unix_main(int argc, char **argv); | 81 | int dos2unix_main(int argc, char **argv); |
86 | int dos2unix_main(int argc, char **argv) | 82 | int dos2unix_main(int argc, char **argv) |
87 | { | 83 | { |
88 | int o; | 84 | int o, ConvType; |
89 | 85 | ||
90 | /* See if we are supposed to be doing dos2unix or unix2dos */ | 86 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
91 | if (applet_name[0] == 'd') { | 87 | if (applet_name[0] == 'd') { |
92 | ConvType = CT_DOS2UNIX; /*2 */ | 88 | ConvType = CT_DOS2UNIX; /* 2 */ |
93 | } else { | 89 | } else { |
94 | ConvType = CT_UNIX2DOS; /*1 */ | 90 | ConvType = CT_UNIX2DOS; /* 1 */ |
95 | } | 91 | } |
96 | /* -u and -d are mutally exclusive */ | 92 | /* -u and -d are mutally exclusive */ |
97 | opt_complementary = "?:u--d:d--u"; | 93 | opt_complementary = "?:u--d:d--u"; |
@@ -105,12 +101,13 @@ int dos2unix_main(int argc, char **argv) | |||
105 | if (o) | 101 | if (o) |
106 | ConvType = o; | 102 | ConvType = o; |
107 | 103 | ||
108 | if (optind < argc) { | 104 | do { |
109 | while (optind < argc) | 105 | /* might be convert(NULL) if there is no filename given */ |
110 | if ((o = convert(argv[optind++])) < 0) | 106 | o = convert(argv[optind], ConvType); |
111 | break; | 107 | if (o < 0) |
112 | } else | 108 | break; |
113 | o = convert(NULL); | 109 | optind++; |
110 | } while (optind < argc); | ||
114 | 111 | ||
115 | return o; | 112 | return o; |
116 | } | 113 | } |