aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2013-03-19 12:53:48 +0000
committerRon Yorston <rmy@pobox.com>2013-03-19 12:53:48 +0000
commite6bb3e5823c27432d025cebc950a404bc373ce41 (patch)
treeec477eec5021502a44cf2954e1cd0968b9171582
parent63d2c5fead323df5f4250ed544d0bc03527c8936 (diff)
downloadbusybox-w32-e6bb3e5823c27432d025cebc950a404bc373ce41.tar.gz
busybox-w32-e6bb3e5823c27432d025cebc950a404bc373ce41.tar.bz2
busybox-w32-e6bb3e5823c27432d025cebc950a404bc373ce41.zip
vi: handle CRLF line ending
-rw-r--r--editors/vi.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/editors/vi.c b/editors/vi.c
index 63b984ea6..d6c8c0dd8 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2532,6 +2532,9 @@ static int file_insert(const char *fn, char *p, int update_ro_status)
2532 status_line_bold_errno(fn); 2532 status_line_bold_errno(fn);
2533 goto fi0; 2533 goto fi0;
2534 } 2534 }
2535#if ENABLE_PLATFORM_MINGW32
2536 _setmode(fd, _O_TEXT);
2537#endif
2535 size = (statbuf.st_size < INT_MAX ? (int)statbuf.st_size : INT_MAX); 2538 size = (statbuf.st_size < INT_MAX ? (int)statbuf.st_size : INT_MAX);
2536 p += text_hole_make(p, size); 2539 p += text_hole_make(p, size);
2537 cnt = safe_read(fd, p, size); 2540 cnt = safe_read(fd, p, size);
@@ -2540,8 +2543,25 @@ static int file_insert(const char *fn, char *p, int update_ro_status)
2540 p = text_hole_delete(p, p + size - 1); // un-do buffer insert 2543 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
2541 } else if (cnt < size) { 2544 } else if (cnt < size) {
2542 // There was a partial read, shrink unused space text[] 2545 // There was a partial read, shrink unused space text[]
2546#if ENABLE_PLATFORM_MINGW32
2547 int i, newline;
2548
2549 newline = 0;
2550 for ( i=0; i<cnt; ++i ) {
2551 if ( p[i] == '\n' ) {
2552 ++newline;
2553 }
2554 }
2555#endif
2543 p = text_hole_delete(p + cnt, p + size - 1); // un-do buffer insert 2556 p = text_hole_delete(p + cnt, p + size - 1); // un-do buffer insert
2557#if ENABLE_PLATFORM_MINGW32
2558 // on WIN32 a partial read might just mean CRs have been removed
2559 if ( cnt+newline != size ) {
2560 status_line_bold("can't read '%s'", fn);
2561 }
2562#else
2544 status_line_bold("can't read '%s'", fn); 2563 status_line_bold("can't read '%s'", fn);
2564#endif
2545 } 2565 }
2546 if (cnt >= size) 2566 if (cnt >= size)
2547 file_modified++; 2567 file_modified++;
@@ -2564,6 +2584,9 @@ static int file_insert(const char *fn, char *p, int update_ro_status)
2564static int file_write(char *fn, char *first, char *last) 2584static int file_write(char *fn, char *first, char *last)
2565{ 2585{
2566 int fd, cnt, charcnt; 2586 int fd, cnt, charcnt;
2587#if ENABLE_PLATFORM_MINGW32
2588 int i, newline;
2589#endif
2567 2590
2568 if (fn == 0) { 2591 if (fn == 0) {
2569 status_line_bold("No current filename"); 2592 status_line_bold("No current filename");
@@ -2577,8 +2600,23 @@ static int file_write(char *fn, char *first, char *last)
2577 if (fd < 0) 2600 if (fd < 0)
2578 return -1; 2601 return -1;
2579 cnt = last - first + 1; 2602 cnt = last - first + 1;
2603#if ENABLE_PLATFORM_MINGW32
2604 /* write file in text mode; this makes it bigger so adjust
2605 * the truncation to match
2606 */
2607 _setmode(fd, _O_TEXT);
2608 newline = 0;
2609 for ( i=0; i<cnt; ++i ) {
2610 if ( first[i] == '\n' ) {
2611 ++newline;
2612 }
2613 }
2614 charcnt = full_write(fd, first, cnt);
2615 ftruncate(fd, charcnt+newline);
2616#else
2580 charcnt = full_write(fd, first, cnt); 2617 charcnt = full_write(fd, first, cnt);
2581 ftruncate(fd, charcnt); 2618 ftruncate(fd, charcnt);
2619#endif
2582 if (charcnt == cnt) { 2620 if (charcnt == cnt) {
2583 // good write 2621 // good write
2584 //file_modified = FALSE; 2622 //file_modified = FALSE;