commit f6cba643763ffe17d75d6ebeba0ff684fdbdd9a1
parent bb88ece93cd8221cba034e3dcd76a2b2823da945
Author: Chris Allegretta <chrisa@asty.org>
Date: Tue, 26 Mar 2002 13:05:54 +0000
David's fixes for read_file and the assert stuff for renumber
git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1147 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
Diffstat:
3 files changed, 23 insertions(+), 17 deletions(-)
diff --git a/ChangeLog b/ChangeLog
@@ -13,6 +13,9 @@ CVS code -
- Stat full_path, not path (Steven Kneizys).
read_file()
- Abort if we read a file of 0 lines (num_lines == 0), fixes BUG #70.
+ - Reverse tests to stop segfault on editing a new file of 0
+ lines (David Benbennick)
+ - Change input var to one char instead of array (David Benbennick).
- nano.c:
do_justify()
- More fixes for indented justify (David Benbennick).
diff --git a/files.c b/files.c
@@ -115,6 +115,8 @@ int read_byte(int fd, char *filename, char *input)
resetty();
endwin();
perror(filename);
+ total_refresh();
+ return -1;
}
if (!size)
return 0;
@@ -175,7 +177,7 @@ int read_file(int fd, char *filename, int quiet)
{
long size;
int num_lines = 0;
- char input[2]; /* buffer */
+ char input; /* current input character */
char *buf;
long i = 0, bufx = 128;
filestruct *fileptr = current, *tmp = NULL;
@@ -192,19 +194,18 @@ int read_file(int fd, char *filename, int quiet)
current = fileage;
line1ins = 1;
}
- input[1] = 0;
/* Read the entire file into file struct */
- while ((size = read_byte(fd, filename, input)) > 0) {
+ while ((size = read_byte(fd, filename, &input)) > 0) {
- if (input[0] == '\n') {
+ if (input == '\n') {
fileptr = read_line(buf, fileptr, &line1ins);
num_lines++;
buf[0] = 0;
i = 0;
#ifndef NANO_SMALL
- } else if (!ISSET(NO_CONVERT) && input[0] >= 0 && input[0] <= 31
- && input[0] != '\t' && input[0] != '\r'
- && input[0] != '\n')
+ } else if (!ISSET(NO_CONVERT) && input >= 0 && input <= 31
+ && input != '\t' && input != '\r'
+ && input != '\n')
/* If the file has binary chars in it, don't stupidly
assume it's a DOS or Mac formatted file! */
SET(NO_CONVERT);
@@ -215,7 +216,7 @@ int read_file(int fd, char *filename, int quiet)
fileformat = 2;
fileptr = read_line(buf, fileptr, &line1ins);
num_lines++;
- buf[0] = input[0];
+ buf[0] = input;
buf[1] = 0;
i = 1;
#endif
@@ -229,7 +230,7 @@ int read_file(int fd, char *filename, int quiet)
buf = nrealloc(buf, bufx + 128);
bufx += 128;
}
- buf[i] = input[0];
+ buf[i] = input;
buf[i + 1] = 0;
i++;
}
@@ -243,13 +244,6 @@ int read_file(int fd, char *filename, int quiet)
buf[0] = 0;
}
- /* Did we try to insert a file of 0 bytes? */
- if (num_lines == 0)
- {
- statusbar(_("Read %d lines"), 0);
- return 1;
- }
-
/* Did we even GET a file if we don't already have one? */
if (totsize == 0 || fileptr == NULL) {
new_file();
@@ -257,6 +251,13 @@ int read_file(int fd, char *filename, int quiet)
return 1;
}
+ /* Did we try to insert a file of 0 bytes? */
+ if (num_lines == 0)
+ {
+ statusbar(_("Read %d lines"), 0);
+ return 1;
+ }
+
if (current != NULL) {
fileptr->next = current;
current->prev = fileptr;
@@ -310,7 +311,7 @@ int open_pipe(char *command)
dup2(fd[1], fileno(stderr));
/* If execl() returns at all, there was an error. */
- execl("/bin/sh","/bin/sh","-c",command,0);
+ execl("/bin/sh","sh","-c",command,0);
exit(0);
}
diff --git a/nano.c b/nano.c
@@ -369,6 +369,7 @@ int renumber_all(void)
filestruct *temp;
int i = 1;
+ assert(fileage==NULL || fileage!=fileage->next);
for (temp = fileage; temp != NULL; temp = temp->next) {
temp->lineno = i++;
}
@@ -384,6 +385,7 @@ int renumber(filestruct * fileptr)
renumber_all();
return 0;
}
+ assert(fileptr==NULL || fileptr!=fileptr->next);
for (temp = fileptr; temp != NULL; temp = temp->next) {
if (temp->prev != NULL)
temp->lineno = temp->prev->lineno + 1;