diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-06-01 18:30:42 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-06-01 18:30:42 +0000 |
commit | b5353a20f3226ab7ff939db8691b0d826d6e6428 (patch) | |
tree | 555500dbeaafcaca8359ee04b2c078f1de7c2b51 /coreutils/dos2unix.c | |
parent | d04e9b813092162b59016ce10976fb91851b5f85 (diff) | |
download | busybox-w32-b5353a20f3226ab7ff939db8691b0d826d6e6428.tar.gz busybox-w32-b5353a20f3226ab7ff939db8691b0d826d6e6428.tar.bz2 busybox-w32-b5353a20f3226ab7ff939db8691b0d826d6e6428.zip |
- look at bb_applet_name. Should close bugs #892 and #893
also restructure code a bit so the fix only adds 1 byte..
Diffstat (limited to 'coreutils/dos2unix.c')
-rw-r--r-- | coreutils/dos2unix.c | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c index cf98c4fed..5bf16e5af 100644 --- a/coreutils/dos2unix.c +++ b/coreutils/dos2unix.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * dos2unix for BusyBox | 3 | * dos2unix for BusyBox |
3 | * | 4 | * |
@@ -8,7 +9,7 @@ | |||
8 | * | 9 | * |
9 | * dos2unix filters reading input from stdin and writing output to stdout. | 10 | * dos2unix filters reading input from stdin and writing output to stdout. |
10 | * | 11 | * |
11 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | 12 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
12 | */ | 13 | */ |
13 | 14 | ||
14 | #include <string.h> | 15 | #include <string.h> |
@@ -17,68 +18,69 @@ | |||
17 | #include <fcntl.h> | 18 | #include <fcntl.h> |
18 | #include "busybox.h" | 19 | #include "busybox.h" |
19 | 20 | ||
20 | #define CT_UNIX2DOS 1 | 21 | enum ConvType { |
21 | #define CT_DOS2UNIX 2 | 22 | CT_UNIX2DOS = 1, |
22 | #define tempFn bb_common_bufsiz1 | 23 | CT_DOS2UNIX |
24 | } ConvType; | ||
23 | 25 | ||
24 | /* if fn is NULL then input is stdin and output is stdout */ | 26 | /* if fn is NULL then input is stdin and output is stdout */ |
25 | static int convert(char *fn, int ConvType) | 27 | static int convert(char *fn) |
26 | { | 28 | { |
27 | int c, fd; | ||
28 | FILE *in, *out; | 29 | FILE *in, *out; |
30 | int i; | ||
29 | 31 | ||
30 | if (fn != NULL) { | 32 | if (fn != NULL) { |
31 | in = bb_xfopen(fn, "rw"); | 33 | in = bb_xfopen(fn, "rw"); |
32 | /* | 34 | /* |
33 | The file is then created with mode read/write and | 35 | The file is then created with mode read/write and |
34 | permissions 0666 for glibc 2.0.6 and earlier or | 36 | permissions 0666 for glibc 2.0.6 and earlier or |
35 | 0600 for glibc 2.0.7 and later. | 37 | 0600 for glibc 2.0.7 and later. |
36 | */ | 38 | */ |
37 | snprintf(tempFn, sizeof(tempFn), "%sXXXXXX", fn); | 39 | snprintf(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), "%sXXXXXX", fn); |
38 | /* | 40 | /* |
39 | sizeof tempFn is 4096, so it should be big enough to hold the full | 41 | sizeof bb_common_bufsiz1 is 4096, so it should be big enough to |
40 | path. however if the output is truncated the subsequent call to | 42 | hold the full path. However if the output is truncated the |
41 | mkstemp would fail. | 43 | subsequent call to mkstemp would fail. |
42 | */ | 44 | */ |
43 | if ((fd = mkstemp(&tempFn[0])) == -1 || chmod(tempFn, 0600) == -1) { | 45 | if ((i = mkstemp(&bb_common_bufsiz1[0])) == -1 |
46 | || chmod(bb_common_bufsiz1, 0600) == -1) { | ||
44 | bb_perror_nomsg_and_die(); | 47 | bb_perror_nomsg_and_die(); |
45 | } | 48 | } |
46 | out = fdopen(fd, "w+"); | 49 | out = fdopen(i, "w+"); |
47 | if (!out) { | 50 | if (!out) { |
48 | close(fd); | 51 | close(i); |
49 | remove(tempFn); | 52 | remove(bb_common_bufsiz1); |
50 | } | 53 | } |
51 | } else { | 54 | } else { |
52 | in = stdin; | 55 | in = stdin; |
53 | out = stdout; | 56 | out = stdout; |
54 | } | 57 | } |
55 | 58 | ||
56 | while ((c = fgetc(in)) != EOF) { | 59 | while ((i = fgetc(in)) != EOF) { |
57 | if (c == '\r') | 60 | if (i == '\r') |
58 | continue; | 61 | continue; |
59 | if (c == '\n') { | 62 | if (i == '\n') { |
60 | if (ConvType == CT_UNIX2DOS) | 63 | if (ConvType == CT_UNIX2DOS) |
61 | fputc('\r', out); | 64 | fputc('\r', out); |
62 | fputc('\n', out); | 65 | fputc('\n', out); |
63 | continue; | 66 | continue; |
64 | } | 67 | } |
65 | fputc(c, out); | 68 | fputc(i, out); |
66 | } | 69 | } |
67 | 70 | ||
68 | if (fn != NULL) { | 71 | if (fn != NULL) { |
69 | if (fclose(in) < 0 || fclose(out) < 0) { | 72 | if (fclose(in) < 0 || fclose(out) < 0) { |
70 | bb_perror_nomsg(); | 73 | bb_perror_nomsg(); |
71 | remove(tempFn); | 74 | remove(bb_common_bufsiz1); |
72 | return -2; | 75 | return -2; |
73 | } | 76 | } |
74 | |||
75 | /* Assume they are both on the same filesystem (which | 77 | /* Assume they are both on the same filesystem (which |
76 | * should be true since we put them into the same directory | 78 | * should be true since we put them into the same directory |
77 | * so we _should_ be ok, but you never know... */ | 79 | * so we _should_ be ok, but you never know... */ |
78 | if (rename(tempFn, fn) < 0) { | 80 | if (rename(bb_common_bufsiz1, fn) < 0) { |
79 | bb_perror_msg("cannot rename '%s' as '%s'", tempFn, fn); | 81 | bb_perror_msg("cannot rename '%s' as '%s'", bb_common_bufsiz1, fn); |
80 | return -1; | 82 | return -1; |
81 | } | 83 | } |
82 | } | 84 | } |
83 | 85 | ||
84 | return 0; | 86 | return 0; |
@@ -86,14 +88,13 @@ static int convert(char *fn, int ConvType) | |||
86 | 88 | ||
87 | int dos2unix_main(int argc, char *argv[]) | 89 | int dos2unix_main(int argc, char *argv[]) |
88 | { | 90 | { |
89 | int ConvType; | ||
90 | int o; | 91 | int o; |
91 | 92 | ||
92 | /* See if we are supposed to be doing dos2unix or unix2dos */ | 93 | /* See if we are supposed to be doing dos2unix or unix2dos */ |
93 | if (argv[0][0]=='d') { | 94 | if (bb_applet_name[0] == 'd') { |
94 | ConvType = CT_DOS2UNIX; /*2*/ | 95 | ConvType = CT_DOS2UNIX; /*2 */ |
95 | } else { | 96 | } else { |
96 | ConvType = CT_UNIX2DOS; /*1*/ | 97 | ConvType = CT_UNIX2DOS; /*1 */ |
97 | } | 98 | } |
98 | /* -u and -d are mutally exclusive */ | 99 | /* -u and -d are mutally exclusive */ |
99 | bb_opt_complementally = "?:u--d:d--u"; | 100 | bb_opt_complementally = "?:u--d:d--u"; |
@@ -108,12 +109,11 @@ int dos2unix_main(int argc, char *argv[]) | |||
108 | ConvType = o; | 109 | ConvType = o; |
109 | 110 | ||
110 | if (optind < argc) { | 111 | if (optind < argc) { |
111 | while(optind < argc) | 112 | while (optind < argc) |
112 | if ((o = convert(argv[optind++], ConvType)) < 0) | 113 | if ((o = convert(argv[optind++])) < 0) |
113 | break; | 114 | break; |
114 | } | 115 | } else |
115 | else | 116 | o = convert(NULL); |
116 | o = convert(NULL, ConvType); | ||
117 | 117 | ||
118 | return o; | 118 | return o; |
119 | } | 119 | } |