change all hardcoded instances of 128 bytes to MAX_BUF_SIZE, and #define

MAX_BUF_SIZE as 128 in nano.h


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2481 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
David Lawrence Ramsey 2005-04-19 21:47:01 +00:00
parent 3705924fc8
commit 5b2f17e591
4 changed files with 39 additions and 21 deletions

View File

@ -5,12 +5,20 @@ CVS code -
include the former in source files. Also add an #include include the former in source files. Also add an #include
guard to proto.h, and make the config.h #include in nano.h guard to proto.h, and make the config.h #include in nano.h
match the config.h #includes everywhere else. (DLR) match the config.h #includes everywhere else. (DLR)
- Change all hardcoded instances of 128 bytes to MAX_BUF_SIZE,
and #define MAX_BUF_SIZE as 128 in nano.h. (DLR)
- files.c: - files.c:
load_open_file() load_open_file()
- Remove an unneeded clearok(FALSE). (DLR) - Remove an unneeded clearok(FALSE). (DLR)
get_next_filename() get_next_filename()
- Use an unsigned long instead of an int for the number - Use an unsigned long instead of an int for the number
prepended to the filename. (DLR) prepended to the filename. (DLR)
copy_file()
- Copy files in chunks of MAX_BUF_SIZE bytes instead of BUFSIZ
bytes. (DLR)
write_file()
- Since lineswritten is a size_t, print its value as an unsigned
long instead of an unsigned int. (DLR)
do_browser() do_browser()
- Don't treat NANO_CANCEL_KEY as NANO_EXIT_KEY anymore, for - Don't treat NANO_CANCEL_KEY as NANO_EXIT_KEY anymore, for
consistency. (DLR) consistency. (DLR)

View File

@ -134,7 +134,7 @@ void read_file(FILE *f, const char *filename)
/* The length of the current line of the file. */ /* The length of the current line of the file. */
size_t i = 0; size_t i = 0;
/* The position in the current line of the file. */ /* The position in the current line of the file. */
size_t bufx = 128; size_t bufx = MAX_BUF_SIZE;
/* The size of each chunk of the file that we read. */ /* The size of each chunk of the file that we read. */
char input = '\0'; char input = '\0';
/* The current input character. */ /* The current input character. */
@ -227,12 +227,13 @@ void read_file(FILE *f, const char *filename)
* nulls in it, so we can't just use strlen() here. */ * nulls in it, so we can't just use strlen() here. */
len++; len++;
/* Now we allocate a bigger buffer 128 characters at a time. /* Now we allocate a bigger buffer MAX_BUF_SIZE characters
* If we allocate a lot of space for one line, we may indeed * at a time. If we allocate a lot of space for one line,
* have to use a buffer this big later on, so we don't * we may indeed have to use a buffer this big later on, so
* decrease it at all. We do free it at the end, though. */ * we don't decrease it at all. We do free it at the end,
* though. */
if (i >= bufx - 1) { if (i >= bufx - 1) {
bufx += 128; bufx += MAX_BUF_SIZE;
buf = charealloc(buf, bufx); buf = charealloc(buf, bufx);
} }
@ -242,8 +243,7 @@ void read_file(FILE *f, const char *filename)
} }
} }
/* This conditional duplicates previous read_byte() behavior. /* Perhaps this could use some better handling. */
* Perhaps this could use some better handling. */
if (ferror(f)) if (ferror(f))
nperror(filename); nperror(filename);
fclose(f); fclose(f);
@ -1272,7 +1272,7 @@ void init_backup_dir(void)
* write error. */ * write error. */
int copy_file(FILE *inn, FILE *out) int copy_file(FILE *inn, FILE *out)
{ {
char buf[BUFSIZ]; char buf[MAX_BUF_SIZE];
size_t charsread; size_t charsread;
int retval = 0; int retval = 0;
@ -1401,6 +1401,7 @@ int write_file(const char *name, bool tmp, int append, bool
/* Open the original file to copy to the backup. */ /* Open the original file to copy to the backup. */
f = fopen(realname, "rb"); f = fopen(realname, "rb");
if (f == NULL) { if (f == NULL) {
statusbar(_("Error reading %s: %s"), realname, statusbar(_("Error reading %s: %s"), realname,
strerror(errno)); strerror(errno));
@ -1444,6 +1445,7 @@ int write_file(const char *name, bool tmp, int append, bool
* set its permissions, so no unauthorized person can read it as * set its permissions, so no unauthorized person can read it as
* we write. */ * we write. */
backup_file = fopen(backupname, "wb"); backup_file = fopen(backupname, "wb");
if (backup_file == NULL || if (backup_file == NULL ||
chmod(backupname, originalfilestat.st_mode) == -1) { chmod(backupname, originalfilestat.st_mode) == -1) {
statusbar(_("Error writing %s: %s"), backupname, statusbar(_("Error writing %s: %s"), backupname,
@ -1463,10 +1465,10 @@ int write_file(const char *name, bool tmp, int append, bool
copy_status = copy_file(f, backup_file); copy_status = copy_file(f, backup_file);
/* And set metadata. */ /* And set metadata. */
if (copy_status != 0 || chown(backupname, if (copy_status != 0 ||
originalfilestat.st_uid, chown(backupname, originalfilestat.st_uid,
originalfilestat.st_gid) == -1 || utime(backupname, originalfilestat.st_gid) == -1 ||
&filetime) == -1) { utime(backupname, &filetime) == -1) {
free(backupname); free(backupname);
if (copy_status == -1) if (copy_status == -1)
statusbar(_("Error reading %s: %s"), realname, statusbar(_("Error reading %s: %s"), realname,
@ -1507,11 +1509,13 @@ int write_file(const char *name, bool tmp, int append, bool
strcat(tempname, ".XXXXXX"); strcat(tempname, ".XXXXXX");
fd = mkstemp(tempname); fd = mkstemp(tempname);
f = NULL; f = NULL;
if (fd != -1) { if (fd != -1) {
f = fdopen(fd, "wb"); f = fdopen(fd, "wb");
if (f == NULL) if (f == NULL)
close(fd); close(fd);
} }
if (f == NULL) { if (f == NULL) {
statusbar(_("Error writing %s: %s"), tempname, statusbar(_("Error writing %s: %s"), tempname,
strerror(errno)); strerror(errno));
@ -1520,11 +1524,13 @@ int write_file(const char *name, bool tmp, int append, bool
} }
fd_source = open(realname, O_RDONLY | O_CREAT); fd_source = open(realname, O_RDONLY | O_CREAT);
if (fd_source != -1) { if (fd_source != -1) {
f_source = fdopen(fd_source, "rb"); f_source = fdopen(fd_source, "rb");
if (f_source == NULL) if (f_source == NULL)
close(fd_source); close(fd_source);
} }
if (f_source == NULL) { if (f_source == NULL) {
statusbar(_("Error reading %s: %s"), realname, statusbar(_("Error reading %s: %s"), realname,
strerror(errno)); strerror(errno));
@ -1553,6 +1559,7 @@ int write_file(const char *name, bool tmp, int append, bool
/* First, just give up if we couldn't even open the file. */ /* First, just give up if we couldn't even open the file. */
if (fd == -1) { if (fd == -1) {
statusbar(_("Error writing %s: %s"), realname, strerror(errno)); statusbar(_("Error writing %s: %s"), realname, strerror(errno));
/* tempname has been set only if we're prepending. */ /* tempname has been set only if we're prepending. */
if (tempname != NULL) if (tempname != NULL)
unlink(tempname); unlink(tempname);
@ -1658,8 +1665,8 @@ int write_file(const char *name, bool tmp, int append, bool
/* Update originalfilestat to reference the file as it is now. */ /* Update originalfilestat to reference the file as it is now. */
stat(filename, &originalfilestat); stat(filename, &originalfilestat);
#endif #endif
statusbar(P_("Wrote %u line", "Wrote %u lines", lineswritten), statusbar(P_("Wrote %lu line", "Wrote %lu lines", lineswritten),
lineswritten); (unsigned long)lineswritten);
UNSET(MODIFIED); UNSET(MODIFIED);
titlebar(NULL); titlebar(NULL);
} }

View File

@ -523,4 +523,7 @@ typedef struct historyheadtype {
/* Maximum number of search/replace history strings saved. */ /* Maximum number of search/replace history strings saved. */
#define MAX_SEARCH_HISTORY 100 #define MAX_SEARCH_HISTORY 100
/* Maximum number of bytes we read from a file at one time. */
#define MAX_BUF_SIZE 128
#endif /* !NANO_H */ #endif /* !NANO_H */

View File

@ -170,15 +170,15 @@ ssize_t ngetdelim(char **lineptr, size_t *n, int delim, FILE *stream)
/* Allocate the line the first time. */ /* Allocate the line the first time. */
if (*lineptr == NULL) { if (*lineptr == NULL) {
*lineptr = charalloc(128); *lineptr = charalloc(MAX_BUF_SIZE);
*n = 128; *n = MAX_BUF_SIZE;
} }
while ((c = getc(stream)) != EOF) { while ((c = getc(stream)) != EOF) {
/* Check if more memory is needed. */ /* Check if more memory is needed. */
if (indx >= *n) { if (indx >= *n) {
*lineptr = charealloc(*lineptr, *n + 128); *lineptr = charealloc(*lineptr, *n + MAX_BUF_SIZE);
*n += 128; *n += MAX_BUF_SIZE;
} }
/* Push the result in the line. */ /* Push the result in the line. */
@ -191,8 +191,8 @@ ssize_t ngetdelim(char **lineptr, size_t *n, int delim, FILE *stream)
/* Make room for the null character. */ /* Make room for the null character. */
if (indx >= *n) { if (indx >= *n) {
*lineptr = charealloc(*lineptr, *n + 128); *lineptr = charealloc(*lineptr, *n + MAX_BUF_SIZE);
*n += 128; *n += MAX_BUF_SIZE;
} }
/* Null terminate the buffer. */ /* Null terminate the buffer. */