use tmpfile() and revert my previous changes... convert() belongs here
authorGlenn L McGrath <bug1@ihug.co.nz>
Thu, 12 Apr 2001 02:26:04 +0000 (02:26 -0000)
committerGlenn L McGrath <bug1@ihug.co.nz>
Thu, 12 Apr 2001 02:26:04 +0000 (02:26 -0000)
Makefile
coreutils/dos2unix.c
dos2unix.c

index ed59deb0d116db4c1b10816e390d01c0f34e8152..314119137ad4ef47a80bcc45b0a7b3ad1ae31888 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -236,17 +236,18 @@ endif
 LIBBB    = libbb
 LIBBB_LIB = libbb.a
 LIBBB_CSRC= ask_confirmation.c check_wildcard_match.c chomp.c \
-concat_path_file.c copy_file.c copy_file_chunk.c create_path.c daemon.c \
-deb_extract.c device_open.c error_msg.c error_msg_and_die.c find_mount_point.c \
-find_pid_by_name.c find_root_device.c full_read.c full_write.c \
-get_ar_headers.c get_console.c get_last_path_component.c get_line_from_file.c \
-gz_open.c human_readable.c inode_hash.c isdirectory.c kernel_version.c loop.c \
-mode_string.c module_syscalls.c mtab.c mtab_file.c my_getgrnam.c my_getgrgid.c \
-my_getpwnam.c my_getpwnamegid.c my_getpwuid.c parse_mode.c parse_number.c \
-perror_msg.c perror_msg_and_die.c print_file.c process_escape_sequence.c \
-recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \
-syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \
-verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c
+concat_path_file.c copy_file.c copy_file_chunk.c create_path.c \
+daemon.c deb_extract.c device_open.c error_msg.c error_msg_and_die.c \
+find_mount_point.c find_pid_by_name.c find_root_device.c full_read.c \
+full_write.c get_ar_headers.c get_console.c get_last_path_component.c \
+get_line_from_file.c gz_open.c human_readable.c inode_hash.c isdirectory.c \
+kernel_version.c loop.c mode_string.c module_syscalls.c mtab.c mtab_file.c \
+my_getgrnam.c my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c \
+parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \
+process_escape_sequence.c recursive_action.c safe_read.c safe_strncpy.c \
+seek_ared_file.c syscalls.c syslog_msg_with_name.c time_string.c trim.c \
+untar.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \
+xgetcwd.c xregcomp.c
 
 LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
 LIBBB_CFLAGS = -I$(LIBBB)
index b2dcfd9c21d71419904930dffd738c5940fb0fbb..8308c417996ae000a25d9af10d264f58330eaaa5 100644 (file)
  * See the COPYING file for license information.
  */
 
+#include <string.h>
 #include <getopt.h>
 #include "busybox.h"
 
+// if fn is NULL then input is stdin and output is stdout
+extern int convert(char *fn, int ConvType) {
+       char c;
+       char *tempFn = NULL;
+       FILE *in = stdin, *out = stdout;
+
+       if (fn != NULL) {
+               if ((in = wfopen(fn, "r")) == NULL) {
+                       return -1;
+               }
+               if ((out = tmpfile()) == NULL) {
+                       perror_msg(NULL);
+                       return -2;
+               }
+       }
+
+       while ((c = fgetc(in)) != EOF) {
+               if (c == '\r') {
+                       if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
+                               // file is alredy in DOS format so it is not necessery to touch it
+                               if (fclose(in) < 0 || fclose(out) < 0) {
+                                       perror_msg(NULL);
+                                       return -2;
+                               }
+                               return 0;
+                       }
+                       if (!ConvType)
+                               ConvType = CT_DOS2UNIX;
+                       break;
+               }
+               if (c == '\n') {
+                       if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
+                               // file is alredy in UNIX format so it is not necessery to touch it
+                               if ((fclose(in) < 0) || (fclose(out) < 0)) {
+                                       perror_msg(NULL);
+                                       return -2;
+                               }
+                               return 0;
+                       }
+                       if (!ConvType) {
+                               ConvType = CT_UNIX2DOS;
+                       }
+                       if (ConvType == CT_UNIX2DOS) {
+                               fputc('\r', out);
+                       }
+                       fputc('\n', out);
+                       break;
+               }
+               fputc(c, out);
+       }
+       if (c != EOF)
+               while ((c = fgetc(in)) != EOF) {
+                       if (c == '\r')
+                               continue;
+                       if (c == '\n') {
+                               if (ConvType == CT_UNIX2DOS)
+                                       fputc('\r', out);
+                               fputc('\n', out);
+                               continue;
+                       }
+               fputc(c, out);
+       }
+
+       if (fn != NULL) {
+           if (fclose(in) < 0 || fclose(out) < 0 || 
+                   (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) {
+                       perror_msg(NULL);
+                       return -2;
+           }
+
+           while ((c = fgetc(in)) != EOF) {
+                       fputc(c, out);
+               }
+
+           if ((fclose(in) < 0) || (fclose(out) < 0)) {
+                       perror_msg(NULL);
+                       return -2;
+           }
+       }
+
+       return 0;
+}
+
 int dos2unix_main(int argc, char *argv[]) {
        int ConvType = CT_AUTO;
        int o;
index b2dcfd9c21d71419904930dffd738c5940fb0fbb..8308c417996ae000a25d9af10d264f58330eaaa5 100644 (file)
  * See the COPYING file for license information.
  */
 
+#include <string.h>
 #include <getopt.h>
 #include "busybox.h"
 
+// if fn is NULL then input is stdin and output is stdout
+extern int convert(char *fn, int ConvType) {
+       char c;
+       char *tempFn = NULL;
+       FILE *in = stdin, *out = stdout;
+
+       if (fn != NULL) {
+               if ((in = wfopen(fn, "r")) == NULL) {
+                       return -1;
+               }
+               if ((out = tmpfile()) == NULL) {
+                       perror_msg(NULL);
+                       return -2;
+               }
+       }
+
+       while ((c = fgetc(in)) != EOF) {
+               if (c == '\r') {
+                       if ((ConvType == CT_UNIX2DOS) && (fn != NULL)) {
+                               // file is alredy in DOS format so it is not necessery to touch it
+                               if (fclose(in) < 0 || fclose(out) < 0) {
+                                       perror_msg(NULL);
+                                       return -2;
+                               }
+                               return 0;
+                       }
+                       if (!ConvType)
+                               ConvType = CT_DOS2UNIX;
+                       break;
+               }
+               if (c == '\n') {
+                       if ((ConvType == CT_DOS2UNIX) && (fn != NULL)) {
+                               // file is alredy in UNIX format so it is not necessery to touch it
+                               if ((fclose(in) < 0) || (fclose(out) < 0)) {
+                                       perror_msg(NULL);
+                                       return -2;
+                               }
+                               return 0;
+                       }
+                       if (!ConvType) {
+                               ConvType = CT_UNIX2DOS;
+                       }
+                       if (ConvType == CT_UNIX2DOS) {
+                               fputc('\r', out);
+                       }
+                       fputc('\n', out);
+                       break;
+               }
+               fputc(c, out);
+       }
+       if (c != EOF)
+               while ((c = fgetc(in)) != EOF) {
+                       if (c == '\r')
+                               continue;
+                       if (c == '\n') {
+                               if (ConvType == CT_UNIX2DOS)
+                                       fputc('\r', out);
+                               fputc('\n', out);
+                               continue;
+                       }
+               fputc(c, out);
+       }
+
+       if (fn != NULL) {
+           if (fclose(in) < 0 || fclose(out) < 0 || 
+                   (in = fopen(tempFn, "r")) == NULL || (out = fopen(fn, "w")) == NULL) {
+                       perror_msg(NULL);
+                       return -2;
+           }
+
+           while ((c = fgetc(in)) != EOF) {
+                       fputc(c, out);
+               }
+
+           if ((fclose(in) < 0) || (fclose(out) < 0)) {
+                       perror_msg(NULL);
+                       return -2;
+           }
+       }
+
+       return 0;
+}
+
 int dos2unix_main(int argc, char *argv[]) {
        int ConvType = CT_AUTO;
        int o;