aboutsummaryrefslogtreecommitdiff
path: root/coreutils/dos2unix.c
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 /coreutils/dos2unix.c
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
Diffstat (limited to 'coreutils/dos2unix.c')
-rw-r--r--coreutils/dos2unix.c76
1 files changed, 57 insertions, 19 deletions
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) {