aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-07-25 07:22:55 +0000
committerEric Andersen <andersen@codepoet.org>2001-07-25 07:22:55 +0000
commit655584b07a4b7a1d792c37f7edf1f3467ab803e7 (patch)
treecd4e81726596669f4160754371e954024757713c
parent6c7ac21f3a44ccf4a75ce413404726b327691724 (diff)
downloadbusybox-w32-655584b07a4b7a1d792c37f7edf1f3467ab803e7.tar.gz
busybox-w32-655584b07a4b7a1d792c37f7edf1f3467ab803e7.tar.bz2
busybox-w32-655584b07a4b7a1d792c37f7edf1f3467ab803e7.zip
This fixes dos2unix and unix2dos so they behave as expected. dos2unix
was broken in the 0.52 release, and unix2dos was pretty lame... -Erik
-rw-r--r--applets.h2
-rw-r--r--coreutils/dos2unix.c76
-rw-r--r--dos2unix.c76
-rw-r--r--include/applets.h2
-rw-r--r--unix2dos.c49
5 files changed, 116 insertions, 89 deletions
diff --git a/applets.h b/applets.h
index 1fb6dcd92..9fc59cda5 100644
--- a/applets.h
+++ b/applets.h
@@ -408,7 +408,7 @@
408 APPLET(uniq, uniq_main, _BB_DIR_USR_BIN) 408 APPLET(uniq, uniq_main, _BB_DIR_USR_BIN)
409#endif 409#endif
410#ifdef BB_UNIX2DOS 410#ifdef BB_UNIX2DOS
411 APPLET(unix2dos, unix2dos_main, _BB_DIR_USR_BIN) 411 APPLET(unix2dos, dos2unix_main, _BB_DIR_USR_BIN)
412#endif 412#endif
413#ifdef BB_UPDATE 413#ifdef BB_UPDATE
414 APPLET(update, update_main, _BB_DIR_SBIN) 414 APPLET(update, update_main, _BB_DIR_SBIN)
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index e97c3ba9a..68aee13c5 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -29,21 +29,51 @@
29 29
30#include <string.h> 30#include <string.h>
31#include <getopt.h> 31#include <getopt.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <sys/time.h>
32#include "busybox.h" 35#include "busybox.h"
33 36
37static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
38
34// if fn is NULL then input is stdin and output is stdout 39// if fn is NULL then input is stdin and output is stdout
35static int convert(char *fn, int ConvType) { 40static int convert(char *fn, int ConvType)
36 int c; 41{
37 char *tempFn = NULL; 42 int c, fd;
43 struct timeval tv;
44 char tempFn[BUFSIZ];
45 static uint64_t value=0;
38 FILE *in = stdin, *out = stdout; 46 FILE *in = stdin, *out = stdout;
39 47
40 if (fn != NULL) { 48 if (fn != NULL) {
41 if ((in = wfopen(fn, "r")) == NULL) { 49 if ((in = wfopen(fn, "rw")) == NULL) {
42 return -1; 50 return -1;
43 } 51 }
44 if ((out = tmpfile()) == NULL) { 52 strcpy(tempFn, fn);
45 perror_msg(NULL); 53 c = strlen(tempFn);
46 return -2; 54 tempFn[c] = '.';
55 while(1) {
56 if (c >=BUFSIZ)
57 error_msg_and_die("unique name not found");
58 /* Get some semi random stuff to try and make a
59 * random filename based (and in the same dir as)
60 * the input file... */
61 gettimeofday (&tv, NULL);
62 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
63 tempFn[++c] = letters[value % 62];
64 tempFn[c+1] = '\0';
65 value /= 62;
66
67 if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
68 continue;
69 }
70 out = fdopen(fd, "w+");
71 if (!out) {
72 close(fd);
73 remove(tempFn);
74 continue;
75 }
76 break;
47 } 77 }
48 } 78 }
49 79
@@ -51,6 +81,7 @@ static int convert(char *fn, int ConvType) {
51 if (c == '\r') { 81 if (c == '\r') {
52 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { 82 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
53 // file is alredy in DOS format so it is not necessery to touch it 83 // file is alredy in DOS format so it is not necessery to touch it
84 remove(tempFn);
54 if (fclose(in) < 0 || fclose(out) < 0) { 85 if (fclose(in) < 0 || fclose(out) < 0) {
55 perror_msg(NULL); 86 perror_msg(NULL);
56 return -2; 87 return -2;
@@ -64,6 +95,7 @@ static int convert(char *fn, int ConvType) {
64 if (c == '\n') { 95 if (c == '\n') {
65 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { 96 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
66 // file is alredy in UNIX format so it is not necessery to touch it 97 // file is alredy in UNIX format so it is not necessery to touch it
98 remove(tempFn);
67 if ((fclose(in) < 0) || (fclose(out) < 0)) { 99 if ((fclose(in) < 0) || (fclose(out) < 0)) {
68 perror_msg(NULL); 100 perror_msg(NULL);
69 return -2; 101 return -2;
@@ -95,29 +127,35 @@ static int convert(char *fn, int ConvType) {
95 } 127 }
96 128
97 if (fn != NULL) { 129 if (fn != NULL) {
98 if (fclose(in) < 0 || fclose(out) < 0 || 130 if (fclose(in) < 0 || fclose(out) < 0) {
99 (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { 131 perror_msg(NULL);
100 perror_msg(NULL); 132 remove(tempFn);
101 return -2; 133 return -2;
102 } 134 }
103 135
104 while ((c = fgetc(in)) != EOF) { 136 /* Assume they are both on the same filesystem */
105 fputc(c, out); 137 if (rename(tempFn, fn) < 0) {
106 } 138 perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
107 139 return -1;
108 if ((fclose(in) < 0) || (fclose(out) < 0)) {
109 perror_msg(NULL);
110 return -2;
111 } 140 }
112 } 141 }
113 142
114 return 0; 143 return 0;
115} 144}
116 145
117int dos2unix_main(int argc, char *argv[]) { 146int dos2unix_main(int argc, char *argv[])
147{
118 int ConvType = CT_AUTO; 148 int ConvType = CT_AUTO;
119 int o; 149 int o;
120 150
151 //See if we are supposed to be doing dos2unix or unix2dos
152 if (argv[0][0]=='d') {
153 ConvType = CT_DOS2UNIX;
154 }
155 if (argv[0][0]=='u') {
156 ConvType = CT_UNIX2DOS;
157 }
158
121 // process parameters 159 // process parameters
122 while ((o = getopt(argc, argv, "du")) != EOF) { 160 while ((o = getopt(argc, argv, "du")) != EOF) {
123 switch (o) { 161 switch (o) {
diff --git a/dos2unix.c b/dos2unix.c
index e97c3ba9a..68aee13c5 100644
--- a/dos2unix.c
+++ b/dos2unix.c
@@ -29,21 +29,51 @@
29 29
30#include <string.h> 30#include <string.h>
31#include <getopt.h> 31#include <getopt.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <sys/time.h>
32#include "busybox.h" 35#include "busybox.h"
33 36
37static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
38
34// if fn is NULL then input is stdin and output is stdout 39// if fn is NULL then input is stdin and output is stdout
35static int convert(char *fn, int ConvType) { 40static int convert(char *fn, int ConvType)
36 int c; 41{
37 char *tempFn = NULL; 42 int c, fd;
43 struct timeval tv;
44 char tempFn[BUFSIZ];
45 static uint64_t value=0;
38 FILE *in = stdin, *out = stdout; 46 FILE *in = stdin, *out = stdout;
39 47
40 if (fn != NULL) { 48 if (fn != NULL) {
41 if ((in = wfopen(fn, "r")) == NULL) { 49 if ((in = wfopen(fn, "rw")) == NULL) {
42 return -1; 50 return -1;
43 } 51 }
44 if ((out = tmpfile()) == NULL) { 52 strcpy(tempFn, fn);
45 perror_msg(NULL); 53 c = strlen(tempFn);
46 return -2; 54 tempFn[c] = '.';
55 while(1) {
56 if (c >=BUFSIZ)
57 error_msg_and_die("unique name not found");
58 /* Get some semi random stuff to try and make a
59 * random filename based (and in the same dir as)
60 * the input file... */
61 gettimeofday (&tv, NULL);
62 value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
63 tempFn[++c] = letters[value % 62];
64 tempFn[c+1] = '\0';
65 value /= 62;
66
67 if ((fd = open(tempFn, O_RDWR | O_CREAT | O_EXCL, 0600)) < 0 ) {
68 continue;
69 }
70 out = fdopen(fd, "w+");
71 if (!out) {
72 close(fd);
73 remove(tempFn);
74 continue;
75 }
76 break;
47 } 77 }
48 } 78 }
49 79
@@ -51,6 +81,7 @@ static int convert(char *fn, int ConvType) {
51 if (c == '\r') { 81 if (c == '\r') {
52 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) { 82 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
53 // file is alredy in DOS format so it is not necessery to touch it 83 // file is alredy in DOS format so it is not necessery to touch it
84 remove(tempFn);
54 if (fclose(in) < 0 || fclose(out) < 0) { 85 if (fclose(in) < 0 || fclose(out) < 0) {
55 perror_msg(NULL); 86 perror_msg(NULL);
56 return -2; 87 return -2;
@@ -64,6 +95,7 @@ static int convert(char *fn, int ConvType) {
64 if (c == '\n') { 95 if (c == '\n') {
65 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) { 96 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
66 // file is alredy in UNIX format so it is not necessery to touch it 97 // file is alredy in UNIX format so it is not necessery to touch it
98 remove(tempFn);
67 if ((fclose(in) < 0) || (fclose(out) < 0)) { 99 if ((fclose(in) < 0) || (fclose(out) < 0)) {
68 perror_msg(NULL); 100 perror_msg(NULL);
69 return -2; 101 return -2;
@@ -95,29 +127,35 @@ static int convert(char *fn, int ConvType) {
95 } 127 }
96 128
97 if (fn != NULL) { 129 if (fn != NULL) {
98 if (fclose(in) < 0 || fclose(out) < 0 || 130 if (fclose(in) < 0 || fclose(out) < 0) {
99 (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) { 131 perror_msg(NULL);
100 perror_msg(NULL); 132 remove(tempFn);
101 return -2; 133 return -2;
102 } 134 }
103 135
104 while ((c = fgetc(in)) != EOF) { 136 /* Assume they are both on the same filesystem */
105 fputc(c, out); 137 if (rename(tempFn, fn) < 0) {
106 } 138 perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
107 139 return -1;
108 if ((fclose(in) < 0) || (fclose(out) < 0)) {
109 perror_msg(NULL);
110 return -2;
111 } 140 }
112 } 141 }
113 142
114 return 0; 143 return 0;
115} 144}
116 145
117int dos2unix_main(int argc, char *argv[]) { 146int dos2unix_main(int argc, char *argv[])
147{
118 int ConvType = CT_AUTO; 148 int ConvType = CT_AUTO;
119 int o; 149 int o;
120 150
151 //See if we are supposed to be doing dos2unix or unix2dos
152 if (argv[0][0]=='d') {
153 ConvType = CT_DOS2UNIX;
154 }
155 if (argv[0][0]=='u') {
156 ConvType = CT_UNIX2DOS;
157 }
158
121 // process parameters 159 // process parameters
122 while ((o = getopt(argc, argv, "du")) != EOF) { 160 while ((o = getopt(argc, argv, "du")) != EOF) {
123 switch (o) { 161 switch (o) {
diff --git a/include/applets.h b/include/applets.h
index 1fb6dcd92..9fc59cda5 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -408,7 +408,7 @@
408 APPLET(uniq, uniq_main, _BB_DIR_USR_BIN) 408 APPLET(uniq, uniq_main, _BB_DIR_USR_BIN)
409#endif 409#endif
410#ifdef BB_UNIX2DOS 410#ifdef BB_UNIX2DOS
411 APPLET(unix2dos, unix2dos_main, _BB_DIR_USR_BIN) 411 APPLET(unix2dos, dos2unix_main, _BB_DIR_USR_BIN)
412#endif 412#endif
413#ifdef BB_UPDATE 413#ifdef BB_UPDATE
414 APPLET(update, update_main, _BB_DIR_SBIN) 414 APPLET(update, update_main, _BB_DIR_SBIN)
diff --git a/unix2dos.c b/unix2dos.c
deleted file mode 100644
index 37da9a9cc..000000000
--- a/unix2dos.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/*
2 Mini unix2dos implementation for busybox
3
4 Copyright 1994,1995 Patrick Volkerding, Moorhead, Minnesota USA
5 All rights reserved.
6
7 Redistribution and use of this source code, with or without modification, is
8 permitted provided that the following condition is met:
9
10 1. Redistributions of this source code must retain the above copyright
11 notice, this condition, and the following disclaimer.
12
13 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24
25#include "busybox.h"
26#include <stdio.h>
27
28int unix2dos_main( int argc, char **argv ) {
29 int c;
30 if (argc > 1) {
31 c = *argv[1];
32 if (c == '-') {
33 show_usage();
34 }
35 }
36 c = getchar();
37 while (c != EOF) {
38 /* Eat any \r's... they shouldn't be here */
39 while (c == '\r') c = getchar();
40 if (c == EOF) break;
41 if (c != '\n') {
42 putchar(c);
43 } else {
44 printf("\r\n");
45 }
46 c = getchar();
47 }
48 return 0;
49}