aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2019-02-14 16:21:16 +0000
committerRon Yorston <rmy@pobox.com>2019-02-14 16:21:16 +0000
commit91e49fbc7b55e8be78ac3ff943e9b7d4494dfe59 (patch)
tree4e9b667ce35da00fee2650ca1f8996ccfc0a3440
parentee7e00dc580c4e8075ab0776aeb3526c2c22f2b6 (diff)
downloadbusybox-w32-91e49fbc7b55e8be78ac3ff943e9b7d4494dfe59.tar.gz
busybox-w32-91e49fbc7b55e8be78ac3ff943e9b7d4494dfe59.tar.bz2
busybox-w32-91e49fbc7b55e8be78ac3ff943e9b7d4494dfe59.zip
vi: add a function to count CRs in the text buffer
-rw-r--r--editors/vi.c56
1 files changed, 27 insertions, 29 deletions
diff --git a/editors/vi.c b/editors/vi.c
index a46339813..da5f0a8c3 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -2938,6 +2938,18 @@ static char *get_input_line(const char *prompt)
2938#undef buf 2938#undef buf
2939} 2939}
2940 2940
2941#if ENABLE_PLATFORM_MINGW32
2942static int count_cr(char *p, int len)
2943{
2944 int i, cnt;
2945
2946 for (i = cnt = 0; i < len; ++i)
2947 if (p[i] == '\n')
2948 ++cnt;
2949 return cnt;
2950}
2951#endif
2952
2941// might reallocate text[]! 2953// might reallocate text[]!
2942static int file_insert(const char *fn, char *p, int initial) 2954static int file_insert(const char *fn, char *p, int initial)
2943{ 2955{
@@ -2950,7 +2962,11 @@ static int file_insert(const char *fn, char *p, int initial)
2950 if (p > end) 2962 if (p > end)
2951 p = end; 2963 p = end;
2952 2964
2965#if !ENABLE_PLATFORM_MINGW32
2953 fd = open(fn, O_RDONLY); 2966 fd = open(fn, O_RDONLY);
2967#else
2968 fd = open(fn, O_RDONLY | _O_TEXT);
2969#endif
2954 if (fd < 0) { 2970 if (fd < 0) {
2955 if (!initial) 2971 if (!initial)
2956 status_line_bold_errno(fn); 2972 status_line_bold_errno(fn);
@@ -2966,9 +2982,6 @@ static int file_insert(const char *fn, char *p, int initial)
2966 status_line_bold("'%s' is not a regular file", fn); 2982 status_line_bold("'%s' is not a regular file", fn);
2967 goto fi; 2983 goto fi;
2968 } 2984 }
2969#if ENABLE_PLATFORM_MINGW32
2970 _setmode(fd, _O_TEXT);
2971#endif
2972 size = (statbuf.st_size < INT_MAX ? (int)statbuf.st_size : INT_MAX); 2985 size = (statbuf.st_size < INT_MAX ? (int)statbuf.st_size : INT_MAX);
2973 p += text_hole_make(p, size); 2986 p += text_hole_make(p, size);
2974 cnt = full_read(fd, p, size); 2987 cnt = full_read(fd, p, size);
@@ -2976,18 +2989,11 @@ static int file_insert(const char *fn, char *p, int initial)
2976 status_line_bold_errno(fn); 2989 status_line_bold_errno(fn);
2977 p = text_hole_delete(p, p + size - 1, NO_UNDO); // un-do buffer insert 2990 p = text_hole_delete(p, p + size - 1, NO_UNDO); // un-do buffer insert
2978 } else if (cnt < size) { 2991 } else if (cnt < size) {
2979#if ENABLE_PLATFORM_MINGW32
2980 int i, cnt_cr;
2981
2982 // on WIN32 a partial read might just mean CRs have been removed
2983 for (i = 0, cnt_cr = cnt; i < cnt; ++i)
2984 if (p[i] == '\n')
2985 ++cnt_cr;
2986#endif
2987 // There was a partial read, shrink unused space 2992 // There was a partial read, shrink unused space
2988 p = text_hole_delete(p + cnt, p + size - 1, NO_UNDO); 2993 p = text_hole_delete(p + cnt, p + size - 1, NO_UNDO);
2989#if ENABLE_PLATFORM_MINGW32 2994#if ENABLE_PLATFORM_MINGW32
2990 if (cnt_cr < size) 2995 // On WIN32 a partial read might just mean CRs have been removed
2996 if (cnt + count_cr(p, cnt) < size)
2991#endif 2997#endif
2992 status_line_bold("can't read '%s'", fn); 2998 status_line_bold("can't read '%s'", fn);
2993 } 2999 }
@@ -3011,9 +3017,6 @@ static int file_insert(const char *fn, char *p, int initial)
3011static int file_write(char *fn, char *first, char *last) 3017static int file_write(char *fn, char *first, char *last)
3012{ 3018{
3013 int fd, cnt, charcnt; 3019 int fd, cnt, charcnt;
3014#if ENABLE_PLATFORM_MINGW32
3015 int i, newline;
3016#endif
3017 3020
3018 if (fn == 0) { 3021 if (fn == 0) {
3019 status_line_bold("No current filename"); 3022 status_line_bold("No current filename");
@@ -3023,26 +3026,21 @@ static int file_write(char *fn, char *first, char *last)
3023 * but instead ftruncate() it _after_ successful write. 3026 * but instead ftruncate() it _after_ successful write.
3024 * Might reduce amount of data lost on power fail etc. 3027 * Might reduce amount of data lost on power fail etc.
3025 */ 3028 */
3029#if !ENABLE_PLATFORM_MINGW32
3026 fd = open(fn, (O_WRONLY | O_CREAT), 0666); 3030 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
3031#else
3032 fd = open(fn, (O_WRONLY | O_CREAT | _O_TEXT), 0666);
3033#endif
3027 if (fd < 0) 3034 if (fd < 0)
3028 return -1; 3035 return -1;
3029 cnt = last - first + 1; 3036 cnt = last - first + 1;
3030#if ENABLE_PLATFORM_MINGW32
3031 /* write file in text mode; this makes it bigger so adjust
3032 * the truncation to match
3033 */
3034 _setmode(fd, _O_TEXT);
3035 newline = 0;
3036 for ( i=0; i<cnt; ++i ) {
3037 if ( first[i] == '\n' ) {
3038 ++newline;
3039 }
3040 }
3041 charcnt = full_write(fd, first, cnt);
3042 ftruncate(fd, charcnt+newline);
3043#else
3044 charcnt = full_write(fd, first, cnt); 3037 charcnt = full_write(fd, first, cnt);
3038#if !ENABLE_PLATFORM_MINGW32
3045 ftruncate(fd, charcnt); 3039 ftruncate(fd, charcnt);
3040#else
3041 /* File was written in text mode; this makes it bigger so adjust
3042 * the truncation to match. */
3043 ftruncate(fd, charcnt + count_cr(first, cnt));
3046#endif 3044#endif
3047 if (charcnt == cnt) { 3045 if (charcnt == cnt) {
3048 // good write 3046 // good write