diff options
-rw-r--r-- | Makefile | 23 | ||||
-rw-r--r-- | coreutils/dos2unix.c | 84 | ||||
-rw-r--r-- | dos2unix.c | 84 |
3 files changed, 180 insertions, 11 deletions
@@ -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) 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 @@ * 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; diff --git a/dos2unix.c b/dos2unix.c index b2dcfd9c2..8308c4179 100644 --- a/dos2unix.c +++ b/dos2unix.c @@ -27,9 +27,93 @@ * 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; |