aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-03-14 21:49:18 +0000
committerRob Landley <rob@landley.net>2006-03-14 21:49:18 +0000
commit330ac853e370020ad274d085d2c32859cb2578de (patch)
tree7e9f7baaa882d8cf106c2a12010c7a86d321bc66
parent31642d75e1725be5f27ce9cfbe477689ec8bfb3e (diff)
downloadbusybox-w32-330ac853e370020ad274d085d2c32859cb2578de.tar.gz
busybox-w32-330ac853e370020ad274d085d2c32859cb2578de.tar.bz2
busybox-w32-330ac853e370020ad274d085d2c32859cb2578de.zip
Tito shrank dos2unix.
-rw-r--r--coreutils/dos2unix.c138
1 files changed, 37 insertions, 101 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index 0464df76f..8d6cd954d 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -2,7 +2,7 @@
2 * dos2unix for BusyBox 2 * dos2unix for BusyBox
3 * 3 *
4 * dos2unix '\n' convertor 0.5.0 4 * dos2unix '\n' convertor 0.5.0
5 * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997) 5 * based on Unix2Dos 0.9.0 by Peter Hanecak (made 19.2.1997)
6 * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>. 6 * Copyright 1997,.. by Peter Hanecak <hanecak@megaloman.sk>.
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
@@ -10,22 +10,8 @@
10 * Without arguments it reverts the format (e.i. if source is in UNIX format, 10 * Without arguments it reverts the format (e.i. if source is in UNIX format,
11 * output is in DOS format and vice versa). 11 * output is in DOS format and vice versa).
12 * 12 *
13 * This program is free software; you can redistribute it and/or 13 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
14 * modify it under the terms of the GNU General Public License 14*/
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 * See the COPYING file for license information.
28 */
29 15
30#include <string.h> 16#include <string.h>
31#include <unistd.h> 17#include <unistd.h>
@@ -36,55 +22,34 @@
36 22
37#define CT_UNIX2DOS 1 23#define CT_UNIX2DOS 1
38#define CT_DOS2UNIX 2 24#define CT_DOS2UNIX 2
39
40/* We are making a lame pseudo-random string generator here. in
41 * convert(), each pass through the while loop will add more and more
42 * stuff into value, which is _supposed_ to wrap. We don't care about
43 * it being accurate. We care about it being messy, since we use it
44 * to pick a random letter to add to out temporary file. */
45typedef unsigned long int bb_uint64_t;
46
47#define tempFn bb_common_bufsiz1 25#define tempFn bb_common_bufsiz1
48 26
49/* if fn is NULL then input is stdin and output is stdout */ 27/* if fn is NULL then input is stdin and output is stdout */
50static int convert(char *fn, int ConvType) 28static int convert(char *fn, int ConvType)
51{ 29{
52 int c, fd; 30 int c, fd;
53 struct timeval tv;
54 static bb_uint64_t value=0;
55 FILE *in, *out; 31 FILE *in, *out;
56 32
57 if (fn != NULL) { 33 if (fn != NULL) {
58 in = bb_xfopen(fn, "rw"); 34 in = bb_xfopen(fn, "rw");
59 safe_strncpy(tempFn, fn, sizeof(tempFn)); 35 /*
60 c = strlen(tempFn); 36 The file is then created with mode read/write and
61 tempFn[c] = '.'; 37 permissions 0666 for glibc 2.0.6 and earlier or
62 while(1) { 38 0600 for glibc 2.0.7 and later.
63 /* tempFn is BUFSIZ so the last addressable spot it BUFSIZ-1. 39 */
64 * The loop increments by 2. So this must check for BUFSIZ-3. */ 40 snprintf(tempFn, sizeof(tempFn), "%sXXXXXX", fn);
65 if (c >=BUFSIZ-3) 41 /*
66 bb_error_msg_and_die("unique name not found"); 42 sizeof tempFn is 4096, so it should be big enough to hold the full
67 /* Get some semi random stuff to try and make a 43 path. however if the output is truncated the subsequent call to
68 * random filename based (and in the same dir as) 44 mkstemp would fail.
69 * the input file... */ 45 */
70 gettimeofday (&tv, NULL); 46 if ((fd = mkstemp(&tempFn[0])) == -1 || chmod(tempFn, 0600) == -1) {
71 value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); 47 bb_perror_nomsg_and_die();
72 tempFn[++c] = ((value%62) < 26)?(value%62)+97: 48 }
73 ((value%62) < 52)?(value%62)+39: 49 out = fdopen(fd, "w+");
74 (value%62)-4; 50 if (!out) {
75 tempFn[c+1] = '\0';
76 value /= 62;
77
78 if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
79 continue;
80 }
81 out = fdopen(fd, "w+");
82 if (!out) {
83 close(fd); 51 close(fd);
84 remove(tempFn); 52 remove(tempFn);
85 continue;
86 }
87 break;
88 } 53 }
89 } else { 54 } else {
90 in = stdin; 55 in = stdin;
@@ -92,37 +57,6 @@ static int convert(char *fn, int ConvType)
92 } 57 }
93 58
94 while ((c = fgetc(in)) != EOF) { 59 while ((c = fgetc(in)) != EOF) {
95 if (c == '\r') {
96 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
97 /* file is already in DOS format so it is
98 * not necessary to touch it. */
99 remove(tempFn);
100 if (fclose(in) < 0 || fclose(out) < 0) {
101 bb_perror_nomsg();
102 return -2;
103 }
104 return 0;
105 }
106 break;
107 }
108 if (c == '\n') {
109 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
110 /* file is already in DOS format so it is
111 * not necessary to touch it. */
112 remove(tempFn);
113 if ((fclose(in) < 0) || (fclose(out) < 0)) {
114 bb_perror_nomsg();
115 return -2;
116 }
117 return 0;
118 }
119 if (ConvType == CT_UNIX2DOS) {
120 fputc('\r', out);
121 }
122 }
123 fputc(c, out);
124 }
125 while (c != EOF && (c = fgetc(in)) != EOF) {
126 if (c == '\r') 60 if (c == '\r')
127 continue; 61 continue;
128 if (c == '\n') { 62 if (c == '\n') {
@@ -135,18 +69,18 @@ static int convert(char *fn, int ConvType)
135 } 69 }
136 70
137 if (fn != NULL) { 71 if (fn != NULL) {
138 if (fclose(in) < 0 || fclose(out) < 0) { 72 if (fclose(in) < 0 || fclose(out) < 0) {
139 bb_perror_nomsg(); 73 bb_perror_nomsg();
140 remove(tempFn); 74 remove(tempFn);
141 return -2; 75 return -2;
142 } 76 }
143 77
144 /* Assume they are both on the same filesystem (which 78 /* Assume they are both on the same filesystem (which
145 * should be true since we put them into the same directory 79 * should be true since we put them into the same directory
146 * so we _should_ be ok, but you never know... */ 80 * so we _should_ be ok, but you never know... */
147 if (rename(tempFn, fn) < 0) { 81 if (rename(tempFn, fn) < 0) {
148 bb_perror_msg("unable to rename '%s' as '%s'", tempFn, fn); 82 bb_perror_msg("cannot rename '%s' as '%s'", tempFn, fn);
149 return -1; 83 return -1;
150 } 84 }
151 } 85 }
152 86
@@ -160,13 +94,16 @@ int dos2unix_main(int argc, char *argv[])
160 94
161 /* See if we are supposed to be doing dos2unix or unix2dos */ 95 /* See if we are supposed to be doing dos2unix or unix2dos */
162 if (argv[0][0]=='d') { 96 if (argv[0][0]=='d') {
163 ConvType = CT_DOS2UNIX; 97 ConvType = CT_DOS2UNIX; /*1*/
164 } else { 98 } else {
165 ConvType = CT_UNIX2DOS; 99 ConvType = CT_UNIX2DOS; /*2*/
166 } 100 }
167 101 /* -u and -d are mutally exclusive */
102 bb_opt_complementally = "?:u--d:d--u";
168 /* process parameters */ 103 /* process parameters */
169 o = bb_getopt_ulflags(argc, argv, "ud"); 104 /* -u convert to unix */
105 /* -d convert to dos */
106 o = bb_getopt_ulflags(argc, argv, "du");
170 107
171 /* Do the conversion requested by an argument else do the default 108 /* Do the conversion requested by an argument else do the default
172 * conversion depending on our name. */ 109 * conversion depending on our name. */
@@ -183,4 +120,3 @@ int dos2unix_main(int argc, char *argv[])
183 120
184 return o; 121 return o;
185} 122}
186