aboutsummaryrefslogtreecommitdiff
path: root/coreutils/dos2unix.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2005-09-01 03:11:19 +0000
committerRob Landley <rob@landley.net>2005-09-01 03:11:19 +0000
commitf815469a767d0e5e53970136e0f7279f8d4140cf (patch)
treed076b14ff8a535d663010d4e5b883398d50cc9a9 /coreutils/dos2unix.c
parent078bacf1e92fb8e9c44e020aca6d1c2a9362a24b (diff)
downloadbusybox-w32-f815469a767d0e5e53970136e0f7279f8d4140cf.tar.gz
busybox-w32-f815469a767d0e5e53970136e0f7279f8d4140cf.tar.bz2
busybox-w32-f815469a767d0e5e53970136e0f7279f8d4140cf.zip
Bernhard Fischer trimmed down dos2unix a bit.
Diffstat (limited to 'coreutils/dos2unix.c')
-rw-r--r--coreutils/dos2unix.c78
1 files changed, 33 insertions, 45 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index df0b4f977..3488f3354 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -35,28 +35,24 @@
35#include <sys/time.h> 35#include <sys/time.h>
36#include "busybox.h" 36#include "busybox.h"
37 37
38#define CT_AUTO 0
39#define CT_UNIX2DOS 1 38#define CT_UNIX2DOS 1
40#define CT_DOS2UNIX 2 39#define CT_DOS2UNIX 2
41 40
42/* We are making a lame pseudo-random string generator here. in 41/* We are making a lame pseudo-random string generator here. in
43 * convert(), each pass through the while loop will add more and more 42 * convert(), each pass through the while loop will add more and more
44 * stuff into value, which is _supposed_ to wrap. We don't care about 43 * stuff into value, which is _supposed_ to wrap. We don't care about
45 * it being accurate. We care about it being messy, since we then mod 44 * it being accurate. We care about it being messy, since we use it
46 * it by the sizeof(letters) and then use that as an index into letters
47 * to pick a random letter to add to out temporary file. */ 45 * to pick a random letter to add to out temporary file. */
48typedef unsigned long int bb_uint64_t; 46typedef unsigned long int bb_uint64_t;
49 47
50static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 48/* if fn is NULL then input is stdin and output is stdout */
51
52// if fn is NULL then input is stdin and output is stdout
53static int convert(char *fn, int ConvType) 49static int convert(char *fn, int ConvType)
54{ 50{
55 int c, fd; 51 int c, fd;
56 struct timeval tv; 52 struct timeval tv;
57 char tempFn[BUFSIZ]; 53 RESERVE_CONFIG_BUFFER(tempFn, BUFSIZ);
58 static bb_uint64_t value=0; 54 static bb_uint64_t value=0;
59 FILE *in = stdin, *out = stdout; 55 FILE *in, *out;
60 56
61 if (fn != NULL) { 57 if (fn != NULL) {
62 in = bb_xfopen(fn, "rw"); 58 in = bb_xfopen(fn, "rw");
@@ -73,7 +69,9 @@ static int convert(char *fn, int ConvType)
73 * the input file... */ 69 * the input file... */
74 gettimeofday (&tv, NULL); 70 gettimeofday (&tv, NULL);
75 value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid (); 71 value += ((bb_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
76 tempFn[++c] = letters[value % 62]; 72 tempFn[++c] = ((value%62) < 26)?(value%62)+97:
73 ((value%62) < 52)?(value%62)+39:
74 (value%62)-4;
77 tempFn[c+1] = '\0'; 75 tempFn[c+1] = '\0';
78 value /= 62; 76 value /= 62;
79 77
@@ -88,12 +86,16 @@ static int convert(char *fn, int ConvType)
88 } 86 }
89 break; 87 break;
90 } 88 }
89 } else {
90 in = stdin;
91 out = stdout;
91 } 92 }
92 93
93 while ((c = fgetc(in)) != EOF) { 94 while ((c = fgetc(in)) != EOF) {
94 if (c == '\r') { 95 if (c == '\r') {
95 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { 96 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
96 // file is alredy in DOS format so it is not necessery to touch it 97 /* file is already in DOS format so it is
98 * not necessary to touch it. */
97 remove(tempFn); 99 remove(tempFn);
98 if (fclose(in) < 0 || fclose(out) < 0) { 100 if (fclose(in) < 0 || fclose(out) < 0) {
99 bb_perror_nomsg(); 101 bb_perror_nomsg();
@@ -101,13 +103,12 @@ static int convert(char *fn, int ConvType)
101 } 103 }
102 return 0; 104 return 0;
103 } 105 }
104 if (!ConvType)
105 ConvType = CT_DOS2UNIX;
106 break; 106 break;
107 } 107 }
108 if (c == '\n') { 108 if (c == '\n') {
109 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { 109 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
110 // file is alredy in UNIX format so it is not necessery to touch it 110 /* file is already in DOS format so it is
111 * not necessary to touch it. */
111 remove(tempFn); 112 remove(tempFn);
112 if ((fclose(in) < 0) || (fclose(out) < 0)) { 113 if ((fclose(in) < 0) || (fclose(out) < 0)) {
113 bb_perror_nomsg(); 114 bb_perror_nomsg();
@@ -115,27 +116,21 @@ static int convert(char *fn, int ConvType)
115 } 116 }
116 return 0; 117 return 0;
117 } 118 }
118 if (!ConvType) {
119 ConvType = CT_UNIX2DOS;
120 }
121 if (ConvType == CT_UNIX2DOS) { 119 if (ConvType == CT_UNIX2DOS) {
122 fputc('\r', out); 120 fputc('\r', out);
123 } 121 }
124 fputc('\n', out);
125 break;
126 } 122 }
127 fputc(c, out); 123 fputc(c, out);
128 } 124 }
129 if (c != EOF) 125 while (c != EOF && (c = fgetc(in)) != EOF) {
130 while ((c = fgetc(in)) != EOF) { 126 if (c == '\r')
131 if (c == '\r') 127 continue;
132 continue; 128 if (c == '\n') {
133 if (c == '\n') { 129 if (ConvType == CT_UNIX2DOS)
134 if (ConvType == CT_UNIX2DOS) 130 fputc('\r', out);
135 fputc('\r', out); 131 fputc('\n', out);
136 fputc('\n', out); 132 continue;
137 continue; 133 }
138 }
139 fputc(c, out); 134 fputc(c, out);
140 } 135 }
141 136
@@ -160,30 +155,23 @@ static int convert(char *fn, int ConvType)
160 155
161int dos2unix_main(int argc, char *argv[]) 156int dos2unix_main(int argc, char *argv[])
162{ 157{
163 int ConvType = CT_AUTO; 158 int ConvType;
164 int o; 159 int o;
165 160
166 //See if we are supposed to be doing dos2unix or unix2dos 161 /* See if we are supposed to be doing dos2unix or unix2dos */
167 if (argv[0][0]=='d') { 162 if (argv[0][0]=='d') {
168 ConvType = CT_DOS2UNIX; 163 ConvType = CT_DOS2UNIX;
169 } 164 } else {
170 if (argv[0][0]=='u') {
171 ConvType = CT_UNIX2DOS; 165 ConvType = CT_UNIX2DOS;
172 } 166 }
173 167
174 // process parameters 168 /* process parameters */
175 while ((o = getopt(argc, argv, "du")) != EOF) { 169 o = bb_getopt_ulflags(argc, argv, "ud");
176 switch (o) { 170
177 case 'd': 171 /* Do the conversion requested by an argument else do the default
178 ConvType = CT_UNIX2DOS; 172 * conversion depending on our name. */
179 break; 173 if (o)
180 case 'u': 174 ConvType = o;
181 ConvType = CT_DOS2UNIX;
182 break;
183 default:
184 bb_show_usage();
185 }
186 }
187 175
188 if (optind < argc) { 176 if (optind < argc) {
189 while(optind < argc) 177 while(optind < argc)