aboutsummaryrefslogtreecommitdiff
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
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
-rw-r--r--Makefile23
-rw-r--r--coreutils/dos2unix.c84
-rw-r--r--dos2unix.c84
3 files changed, 180 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index ed59deb0d..314119137 100644
--- a/Makefile
+++ b/Makefile
@@ -236,17 +236,18 @@ endif
236LIBBB = libbb 236LIBBB = libbb
237LIBBB_LIB = libbb.a 237LIBBB_LIB = libbb.a
238LIBBB_CSRC= ask_confirmation.c check_wildcard_match.c chomp.c \ 238LIBBB_CSRC= ask_confirmation.c check_wildcard_match.c chomp.c \
239concat_path_file.c copy_file.c copy_file_chunk.c create_path.c daemon.c \ 239concat_path_file.c copy_file.c copy_file_chunk.c create_path.c \
240deb_extract.c device_open.c error_msg.c error_msg_and_die.c find_mount_point.c \ 240daemon.c deb_extract.c device_open.c error_msg.c error_msg_and_die.c \
241find_pid_by_name.c find_root_device.c full_read.c full_write.c \ 241find_mount_point.c find_pid_by_name.c find_root_device.c full_read.c \
242get_ar_headers.c get_console.c get_last_path_component.c get_line_from_file.c \ 242full_write.c get_ar_headers.c get_console.c get_last_path_component.c \
243gz_open.c human_readable.c inode_hash.c isdirectory.c kernel_version.c loop.c \ 243get_line_from_file.c gz_open.c human_readable.c inode_hash.c isdirectory.c \
244mode_string.c module_syscalls.c mtab.c mtab_file.c my_getgrnam.c my_getgrgid.c \ 244kernel_version.c loop.c mode_string.c module_syscalls.c mtab.c mtab_file.c \
245my_getpwnam.c my_getpwnamegid.c my_getpwuid.c parse_mode.c parse_number.c \ 245my_getgrnam.c my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c \
246perror_msg.c perror_msg_and_die.c print_file.c process_escape_sequence.c \ 246parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \
247recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ 247process_escape_sequence.c recursive_action.c safe_read.c safe_strncpy.c \
248syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ 248seek_ared_file.c syscalls.c syslog_msg_with_name.c time_string.c trim.c \
249verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c 249untar.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \
250xgetcwd.c xregcomp.c
250 251
251LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) 252LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
252LIBBB_CFLAGS = -I$(LIBBB) 253LIBBB_CFLAGS = -I$(LIBBB)
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;
diff --git a/dos2unix.c b/dos2unix.c
index b2dcfd9c2..8308c4179 100644
--- a/dos2unix.c
+++ b/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;