aboutsummaryrefslogtreecommitdiff
path: root/coreutils/dos2unix.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-04-12 02:26:04 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-04-12 02:26:04 +0000
commita6ce670a87ee77ccb9337ed6d87442134e1a48ed (patch)
tree1edffdd583fc4386c86b34b9539245235376aca3 /coreutils/dos2unix.c
parent2709297f3ba7603a4432dc760a803856961b48df (diff)
downloadbusybox-w32-a6ce670a87ee77ccb9337ed6d87442134e1a48ed.tar.gz
busybox-w32-a6ce670a87ee77ccb9337ed6d87442134e1a48ed.tar.bz2
busybox-w32-a6ce670a87ee77ccb9337ed6d87442134e1a48ed.zip
use tmpfile() and revert my previous changes... convert() belongs here
Diffstat (limited to 'coreutils/dos2unix.c')
-rw-r--r--coreutils/dos2unix.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index b2dcfd9c2..8308c4179 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -27,9 +27,93 @@
27 * See the COPYING file for license information. 27 * See the COPYING file for license information.
28 */ 28 */
29 29
30#include <string.h>
30#include <getopt.h> 31#include <getopt.h>
31#include "busybox.h" 32#include "busybox.h"
32 33
34// if fn is NULL then input is stdin and output is stdout
35extern int convert(char *fn, int ConvType) {
36 char c;
37 char *tempFn = NULL;
38 FILE *in = stdin, *out = stdout;
39
40 if (fn != NULL) {
41 if ((in = wfopen(fn, "r")) == NULL) {
42 return -1;
43 }
44 if ((out = tmpfile()) == NULL) {
45 perror_msg(NULL);
46 return -2;
47 }
48 }
49
50 while ((c = fgetc(in)) != EOF) {
51 if (c == '\r') {
52 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
53 // file is alredy in DOS format so it is not necessery to touch it
54 if (fclose(in) < 0 || fclose(out) < 0) {
55 perror_msg(NULL);
56 return -2;
57 }
58 return 0;
59 }
60 if (!ConvType)
61 ConvType = CT_DOS2UNIX;
62 break;
63 }
64 if (c == '\n') {
65 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
66 // file is alredy in UNIX format so it is not necessery to touch it
67 if ((fclose(in) < 0) || (fclose(out) < 0)) {
68 perror_msg(NULL);
69 return -2;
70 }
71 return 0;
72 }
73 if (!ConvType) {
74 ConvType = CT_UNIX2DOS;
75 }
76 if (ConvType == CT_UNIX2DOS) {
77 fputc('\r', out);
78 }
79 fputc('\n', out);
80 break;
81 }
82 fputc(c, out);
83 }
84 if (c != EOF)
85 while ((c = fgetc(in)) != EOF) {
86 if (c == '\r')
87 continue;
88 if (c == '\n') {
89 if (ConvType == CT_UNIX2DOS)
90 fputc('\r', out);
91 fputc('\n', out);
92 continue;
93 }
94 fputc(c, out);
95 }
96
97 if (fn != NULL) {
98 if (fclose(in) < 0 || fclose(out) < 0 ||
99 (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) {
100 perror_msg(NULL);
101 return -2;
102 }
103
104 while ((c = fgetc(in)) != EOF) {
105 fputc(c, out);
106 }
107
108 if ((fclose(in) < 0) || (fclose(out) < 0)) {
109 perror_msg(NULL);
110 return -2;
111 }
112 }
113
114 return 0;
115}
116
33int dos2unix_main(int argc, char *argv[]) { 117int dos2unix_main(int argc, char *argv[]) {
34 int ConvType = CT_AUTO; 118 int ConvType = CT_AUTO;
35 int o; 119 int o;