aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-04-12 00:52:29 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-04-12 00:52:29 +0000
commit50b787cac5e83deaeea1f0c3b73b3289321f8e80 (patch)
tree17475f03c79520d76d46d837aefbc8fc183bd71b
parenta283157c3832f151fa9f5abf5f18c56d05e5815d (diff)
downloadbusybox-w32-50b787cac5e83deaeea1f0c3b73b3289321f8e80.tar.gz
busybox-w32-50b787cac5e83deaeea1f0c3b73b3289321f8e80.tar.bz2
busybox-w32-50b787cac5e83deaeea1f0c3b73b3289321f8e80.zip
Move convert to libbb
-rw-r--r--coreutils/dos2unix.c91
-rw-r--r--dos2unix.c91
2 files changed, 0 insertions, 182 deletions
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index 7f1c5617d..b2dcfd9c2 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -27,19 +27,9 @@
27 * See the COPYING file for license information. 27 * See the COPYING file for license information.
28 */ 28 */
29 29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <errno.h>
34#include <getopt.h> 30#include <getopt.h>
35#include "busybox.h" 31#include "busybox.h"
36 32
37#define CT_AUTO 0
38#define CT_UNIX2DOS 1
39#define CT_DOS2UNIX 2
40
41int convert(char *fn, int ConvType);
42
43int dos2unix_main(int argc, char *argv[]) { 33int dos2unix_main(int argc, char *argv[]) {
44 int ConvType = CT_AUTO; 34 int ConvType = CT_AUTO;
45 int o; 35 int o;
@@ -69,84 +59,3 @@ int dos2unix_main(int argc, char *argv[]) {
69 return o; 59 return o;
70} 60}
71 61
72// if fn is NULL then input is stdin and output is stdout
73int convert(char *fn, int ConvType) {
74 char c;
75 char *tempFn = NULL;
76 FILE *in = stdin, *out = stdout;
77
78 if (fn != NULL) {
79 if ((in = fopen(fn, "r")) == NULL) {
80 perror_msg(fn);
81 return -1;
82 }
83 tempFn = tmpnam(NULL);
84 if (tempFn == NULL || (out = fopen(tempFn, "w")) == NULL) {
85 perror_msg(NULL);
86 return -2;
87 }
88 }
89
90 while ((c = fgetc(in)) != EOF) {
91 if (c == '\r') {
92 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
93 // file is alredy in DOS format so it is not necessery to touch it
94 if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) {
95 perror_msg(NULL);
96 return -2;
97 }
98 return 0;
99 }
100 if (!ConvType)
101 ConvType = CT_DOS2UNIX;
102 break;
103 }
104 if (c == '\n') {
105 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
106 // file is alredy in UNIX format so it is not necessery to touch it
107 if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) {
108 perror_msg(NULL);
109 return -2;
110 }
111 return 0;
112 }
113 if (!ConvType)
114 ConvType = CT_UNIX2DOS;
115 if (ConvType == CT_UNIX2DOS)
116 fputc('\r', out);
117 fputc('\n', out);
118 break;
119 }
120 fputc(c, out);
121 }
122 if (c != EOF)
123 while ((c = fgetc(in)) != EOF) {
124 if (c == '\r')
125 continue;
126 if (c == '\n') {
127 if (ConvType == CT_UNIX2DOS)
128 fputc('\r', out);
129 fputc('\n', out);
130 continue;
131 }
132 fputc(c, out);
133 }
134
135 if (fn != NULL) {
136 if (fclose(in) < 0 || fclose(out) < 0 ||
137 (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) {
138 perror_msg(NULL);
139 return -2;
140 }
141
142 while ((c = fgetc(in)) != EOF)
143 fputc(c, out);
144
145 if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) {
146 perror_msg(NULL);
147 return -2;
148 }
149 }
150
151 return 0;
152}
diff --git a/dos2unix.c b/dos2unix.c
index 7f1c5617d..b2dcfd9c2 100644
--- a/dos2unix.c
+++ b/dos2unix.c
@@ -27,19 +27,9 @@
27 * See the COPYING file for license information. 27 * See the COPYING file for license information.
28 */ 28 */
29 29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <errno.h>
34#include <getopt.h> 30#include <getopt.h>
35#include "busybox.h" 31#include "busybox.h"
36 32
37#define CT_AUTO 0
38#define CT_UNIX2DOS 1
39#define CT_DOS2UNIX 2
40
41int convert(char *fn, int ConvType);
42
43int dos2unix_main(int argc, char *argv[]) { 33int dos2unix_main(int argc, char *argv[]) {
44 int ConvType = CT_AUTO; 34 int ConvType = CT_AUTO;
45 int o; 35 int o;
@@ -69,84 +59,3 @@ int dos2unix_main(int argc, char *argv[]) {
69 return o; 59 return o;
70} 60}
71 61
72// if fn is NULL then input is stdin and output is stdout
73int convert(char *fn, int ConvType) {
74 char c;
75 char *tempFn = NULL;
76 FILE *in = stdin, *out = stdout;
77
78 if (fn != NULL) {
79 if ((in = fopen(fn, "r")) == NULL) {
80 perror_msg(fn);
81 return -1;
82 }
83 tempFn = tmpnam(NULL);
84 if (tempFn == NULL || (out = fopen(tempFn, "w")) == NULL) {
85 perror_msg(NULL);
86 return -2;
87 }
88 }
89
90 while ((c = fgetc(in)) != EOF) {
91 if (c == '\r') {
92 if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
93 // file is alredy in DOS format so it is not necessery to touch it
94 if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) {
95 perror_msg(NULL);
96 return -2;
97 }
98 return 0;
99 }
100 if (!ConvType)
101 ConvType = CT_DOS2UNIX;
102 break;
103 }
104 if (c == '\n') {
105 if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
106 // file is alredy in UNIX format so it is not necessery to touch it
107 if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) {
108 perror_msg(NULL);
109 return -2;
110 }
111 return 0;
112 }
113 if (!ConvType)
114 ConvType = CT_UNIX2DOS;
115 if (ConvType == CT_UNIX2DOS)
116 fputc('\r', out);
117 fputc('\n', out);
118 break;
119 }
120 fputc(c, out);
121 }
122 if (c != EOF)
123 while ((c = fgetc(in)) != EOF) {
124 if (c == '\r')
125 continue;
126 if (c == '\n') {
127 if (ConvType == CT_UNIX2DOS)
128 fputc('\r', out);
129 fputc('\n', out);
130 continue;
131 }
132 fputc(c, out);
133 }
134
135 if (fn != NULL) {
136 if (fclose(in) < 0 || fclose(out) < 0 ||
137 (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) {
138 perror_msg(NULL);
139 return -2;
140 }
141
142 while ((c = fgetc(in)) != EOF)
143 fputc(c, out);
144
145 if (fclose(in) < 0 || fclose(out) < 0 || remove(tempFn) < 0) {
146 perror_msg(NULL);
147 return -2;
148 }
149 }
150
151 return 0;
152}