Commit 0547eb36 authored by Chris Allegretta's avatar Chris Allegretta
Browse files

- General: - Change from read() and write() to file streams by Jay Carlson....

- General: - Change from read() and write() to file streams by Jay Carlson. Allows OS to implement read and write ahead rather than making us do it.  Hopefully merged properly


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1192 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
No related merge requests found
Showing with 105 additions and 55 deletions
+105 -55
...@@ -12,6 +12,9 @@ CVS code - ...@@ -12,6 +12,9 @@ CVS code -
been wanting it for awhile now and we'll see how bad it messes been wanting it for awhile now and we'll see how bad it messes
everything up. Changes to files.c:do_writeout(), write_file(). everything up. Changes to files.c:do_writeout(), write_file().
Fixes for O_CREAT & append compatibililty by David Benbennick. Fixes for O_CREAT & append compatibililty by David Benbennick.
- Change from read() and write() to file streams by Jay Carlson.
Allows OS to implement read and write ahead rather than making
us do it. Hopefully merged properly.
- configure.ac: - configure.ac:
- Define NDEBUG to silence asserts (David Benbennick). - Define NDEBUG to silence asserts (David Benbennick).
- files.c: - files.c:
......
...@@ -100,27 +100,6 @@ void new_file(void) ...@@ -100,27 +100,6 @@ void new_file(void)
} }
int read_byte(int fd, char *filename, char *input)
{
static char buf[BUFSIZ];
static int index = 0;
static int size = 0;
if (index == size) {
index = 0;
size = read(fd, buf, BUFSIZ);
if (size == -1) {
size = index; /* note index==0 */
nperror(filename);
return -1;
}
if (!size)
return 0;
}
*input = buf[index++];
return 1;
}
filestruct *read_line(char *buf, filestruct * prev, int *line1ins) filestruct *read_line(char *buf, filestruct * prev, int *line1ins)
{ {
filestruct *fileptr; filestruct *fileptr;
...@@ -169,15 +148,15 @@ filestruct *read_line(char *buf, filestruct * prev, int *line1ins) ...@@ -169,15 +148,15 @@ filestruct *read_line(char *buf, filestruct * prev, int *line1ins)
return fileptr; return fileptr;
} }
int read_file(int fd, char *filename, int quiet) int read_file(FILE *f, char *filename, int quiet)
{ {
long size;
int num_lines = 0; int num_lines = 0;
char input; /* current input character */ char input; /* current input character */
char *buf; char *buf;
long i = 0, bufx = 128; long i = 0, bufx = 128;
filestruct *fileptr = current, *tmp = NULL; filestruct *fileptr = current, *tmp = NULL;
int line1ins = 0; int line1ins = 0;
int input_int;
buf = charalloc(bufx); buf = charalloc(bufx);
buf[0] = '\0'; buf[0] = '\0';
...@@ -191,8 +170,8 @@ int read_file(int fd, char *filename, int quiet) ...@@ -191,8 +170,8 @@ int read_file(int fd, char *filename, int quiet)
line1ins = 1; line1ins = 1;
} }
/* Read the entire file into file struct */ /* Read the entire file into file struct */
while ((size = read_byte(fd, filename, &input)) > 0) { while ((input_int = getc(f)) != EOF) {
input = (char) input_int;
#ifndef NANO_SMALL #ifndef NANO_SMALL
if (!ISSET(NO_CONVERT) && input >= 0 && input <= 31 if (!ISSET(NO_CONVERT) && input >= 0 && input <= 31
&& input != 127 && input != '\t' && input != '\r' && input != 127 && input != '\t' && input != '\r'
...@@ -232,7 +211,13 @@ int read_file(int fd, char *filename, int quiet) ...@@ -232,7 +211,13 @@ int read_file(int fd, char *filename, int quiet)
buf[i + 1] = 0; buf[i + 1] = 0;
i++; i++;
} }
totsize += size; totsize++;
}
if (ferror(f)) {
/* This conditional duplicates previous read_byte(); behavior;
perhaps this could use some better handling. */
nperror(filename);
} }
/* Did we not get a newline but still have stuff to do? */ /* Did we not get a newline but still have stuff to do? */
...@@ -283,7 +268,7 @@ int read_file(int fd, char *filename, int quiet) ...@@ -283,7 +268,7 @@ int read_file(int fd, char *filename, int quiet)
totlines += num_lines; totlines += num_lines;
free(buf); free(buf);
close(fd); fclose(f);
return 1; return 1;
} }
...@@ -293,6 +278,7 @@ int read_file(int fd, char *filename, int quiet) ...@@ -293,6 +278,7 @@ int read_file(int fd, char *filename, int quiet)
int open_file(char *filename, int insert, int quiet) int open_file(char *filename, int insert, int quiet)
{ {
int fd; int fd;
FILE *f;
struct stat fileinfo; struct stat fileinfo;
if (!strcmp(filename, "") || stat(filename, &fileinfo) == -1) { if (!strcmp(filename, "") || stat(filename, &fileinfo) == -1) {
...@@ -326,7 +312,12 @@ int open_file(char *filename, int insert, int quiet) ...@@ -326,7 +312,12 @@ int open_file(char *filename, int insert, int quiet)
} }
if (!quiet) if (!quiet)
statusbar(_("Reading File")); statusbar(_("Reading File"));
read_file(fd, filename, quiet); f = fdopen(fd, "rb"); /* Binary for our own line-end munging */
if (!f) {
nperror("fdopen");
return -1;
}
read_file(f, filename, quiet);
} }
return 1; return 1;
...@@ -1187,9 +1178,11 @@ int check_operating_dir(char *currpath, int allow_tabcomp) ...@@ -1187,9 +1178,11 @@ int check_operating_dir(char *currpath, int allow_tabcomp)
int write_file(char *name, int tmp, int append, int nonamechange) int write_file(char *name, int tmp, int append, int nonamechange)
{ {
long size, lineswritten = 0; long size, lineswritten = 0;
char *buf = NULL, prechar; char *buf = NULL;
filestruct *fileptr; filestruct *fileptr;
int fd, fd2, mask = 0, realexists, anyexists; FILE *f;
int fd;
int mask = 0, realexists, anyexists;
struct stat st, lst; struct stat st, lst;
char *realname = NULL; char *realname = NULL;
...@@ -1278,15 +1271,31 @@ int write_file(char *name, int tmp, int append, int nonamechange) ...@@ -1278,15 +1271,31 @@ int write_file(char *name, int tmp, int append, int nonamechange)
} }
dump_buffer(fileage); dump_buffer(fileage);
if (append == 1) {
f = fdopen(fd, "ab");
} else {
f = fdopen(fd, "wb");
}
if (!f) {
statusbar(_("Could not open file for writing: %s"),
strerror(errno));
return -1;
}
while (fileptr != NULL && fileptr->next != NULL) { while (fileptr != NULL && fileptr->next != NULL) {
int data_len;
/* Next line is so we discount the "magic line" */ /* Next line is so we discount the "magic line" */
if (filebot == fileptr && fileptr->data[0] == '\0') if (filebot == fileptr && fileptr->data[0] == '\0')
break; break;
size = write(fd, fileptr->data, strlen(fileptr->data)); data_len = strlen(fileptr->data);
if (size == -1) { size = fwrite(fileptr->data, 1, data_len, f);
if (size < data_len) {
statusbar(_("Could not open file for writing: %s"), statusbar(_("Could not open file for writing: %s"),
strerror(errno)); strerror(errno));
fclose(f);
return -1; return -1;
} else { } else {
#ifdef DEBUG #ifdef DEBUG
...@@ -1295,50 +1304,53 @@ int write_file(char *name, int tmp, int append, int nonamechange) ...@@ -1295,50 +1304,53 @@ int write_file(char *name, int tmp, int append, int nonamechange)
} }
#ifndef NANO_SMALL #ifndef NANO_SMALL
if (ISSET(DOS_FILE) || ISSET(MAC_FILE)) if (ISSET(DOS_FILE) || ISSET(MAC_FILE))
write(fd, "\r", 1); putc('\r', f);
if (!ISSET(MAC_FILE)) if (!ISSET(MAC_FILE))
#endif #endif
write(fd, "\n", 1); putc('\n', f);
fileptr = fileptr->next; fileptr = fileptr->next;
lineswritten++; lineswritten++;
} }
if (fileptr != NULL) { if (fileptr != NULL) {
size = write(fd, fileptr->data, strlen(fileptr->data)); int data_len;
if (size == -1) {
data_len = strlen(fileptr->data);
size = fwrite(fileptr->data, 1, data_len, f);
if (size < data_len) {
statusbar(_("Could not open file for writing: %s"), statusbar(_("Could not open file for writing: %s"),
strerror(errno)); strerror(errno));
return -1; return -1;
} else if (size > 0) { } else if (data_len > 0) {
#ifndef NANO_SMALL #ifndef NANO_SMALL
if (ISSET(DOS_FILE) || ISSET(MAC_FILE)) { if (ISSET(DOS_FILE) || ISSET(MAC_FILE)) {
size = write(fd, "\r", 1); if (putc('\r', f) == EOF) {
lineswritten++;
if (size == -1) {
statusbar(_("Could not open file for writing: %s"), statusbar(_("Could not open file for writing: %s"),
strerror(errno)); strerror(errno));
fclose(f);
return -1; return -1;
} }
lineswritten++;
} }
if (!ISSET(MAC_FILE)) if (!ISSET(MAC_FILE))
#endif #endif
{ {
size = write(fd, "\n", 1); if (putc('\n', f) == EOF) {
lineswritten++;
if (size == -1) {
statusbar(_("Could not open file for writing: %s"), statusbar(_("Could not open file for writing: %s"),
strerror(errno)); strerror(errno));
fclose(f);
return -1; return -1;
} }
lineswritten++;
} }
} }
} }
if (close(fd) == -1) { if (fclose(f)) {
statusbar(_("Could not close %s: %s"), realname, strerror(errno)); statusbar(_("Could not close %s: %s"), realname, strerror(errno));
unlink(buf); unlink(buf);
return -1; return -1;
...@@ -1346,22 +1358,52 @@ int write_file(char *name, int tmp, int append, int nonamechange) ...@@ -1346,22 +1358,52 @@ int write_file(char *name, int tmp, int append, int nonamechange)
/* if we're prepending, open the real file, and append it here */ /* if we're prepending, open the real file, and append it here */
if (append == 2) { if (append == 2) {
if ((fd = open(buf, O_WRONLY | O_APPEND, (S_IRUSR|S_IWUSR))) == -1) { int fd_source, fd_dest;
FILE *f_source, *f_dest;
int prechar;
if ((fd_dest = open(buf, O_WRONLY | O_APPEND, (S_IRUSR|S_IWUSR))) == -1) {
statusbar(_("Could not reopen %s: %s"), buf, strerror(errno)); statusbar(_("Could not reopen %s: %s"), buf, strerror(errno));
return -1; return -1;
} }
if ((fd2 = open(realname, O_RDONLY | O_CREAT)) == -1) { f_dest = fdopen(fd_dest, "wb");
statusbar(_("Could not open %s for prepend: %s"), realname, if (!f_dest) {
strerror(errno)); statusbar(_("Could not reopen %s: %s"), buf, strerror(errno));
close(fd_dest);
return -1;
}
if ((fd_source = open(realname, O_RDONLY)) == -1) {
statusbar(_("Could not open %s for prepend: %s"), realname, strerror(errno));
fclose(f_dest);
return -1;
}
f_source = fdopen(fd_source, "rb");
if (!f_source) {
statusbar(_("Could not open %s for prepend: %s"), realname, strerror(errno));
fclose(f_dest);
close(fd_source);
return -1; return -1;
} }
while (read(fd2, &prechar, 1) > 0) /* Doing this in blocks is an exercise left to some other reader. */
write(fd, &prechar, 1); while ((prechar = getc(f_source)) != EOF) {
if (putc(prechar, f_dest) == EOF) {
close(fd); statusbar(_("Could not open %s for prepend: %s"), realname, strerror(errno));
close(fd2); fclose(f_source);
fclose(f_dest);
return -1;
}
}
if (ferror(f_source)) {
statusbar(_("Could not reopen %s: %s"), buf, strerror(errno));
fclose(f_source);
fclose(f_dest);
return -1;
}
fclose(f_source);
fclose(f_dest);
} }
......
...@@ -1689,6 +1689,7 @@ RETSIGTYPE cancel_fork(int signal) { ...@@ -1689,6 +1689,7 @@ RETSIGTYPE cancel_fork(int signal) {
int open_pipe(char *command) int open_pipe(char *command)
{ {
int fd[2]; int fd[2];
FILE *f;
struct sigaction oldaction, newaction; struct sigaction oldaction, newaction;
/* original and temporary handlers for SIGINT */ /* original and temporary handlers for SIGINT */
#ifdef _POSIX_VDISABLE #ifdef _POSIX_VDISABLE
...@@ -1764,7 +1765,11 @@ int open_pipe(char *command) ...@@ -1764,7 +1765,11 @@ int open_pipe(char *command)
} }
#endif /* _POSIX_VDISABLE */ #endif /* _POSIX_VDISABLE */
read_file(fd[0],"stdin",0); f = fdopen(fd[0], "rb");
if (!f)
nperror("fdopen");
read_file(f, "stdin", 0);
set_modified(); set_modified();
if (wait(NULL) == -1) if (wait(NULL) == -1)
......
...@@ -125,7 +125,7 @@ int do_insertfile(int loading_file); ...@@ -125,7 +125,7 @@ int do_insertfile(int loading_file);
int length_of_list(shortcut *s); int length_of_list(shortcut *s);
int num_of_digits(int n); int num_of_digits(int n);
int open_pipe(char *command); int open_pipe(char *command);
int read_file(int fd, char *filename, int quiet); int read_file(FILE *f, char *filename, int quiet);
#ifdef ENABLE_MULTIBUFFER #ifdef ENABLE_MULTIBUFFER
int add_open_file(int update); int add_open_file(int update);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment