include the former in source files. Also add an #include
guard to proto.h, and make the config.h #include in nano.h
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:
load_open_file()
- Remove an unneeded clearok(FALSE). (DLR)
get_next_filename()
- Use an unsigned long instead of an int for the number
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()
- Don't treat NANO_CANCEL_KEY as NANO_EXIT_KEY anymore, for
consistency. (DLR)
/* The length of the current line of the file. */
size_t i = 0;
/* 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. */
char input = '\0';
/* The current input character. */
* nulls in it, so we can't just use strlen() here. */
len++;
- /* Now we allocate a bigger buffer 128 characters at a time.
- * If we allocate a lot of space for one line, we may indeed
- * have to use a buffer this big later on, so we don't
- * decrease it at all. We do free it at the end, though. */
+ /* Now we allocate a bigger buffer MAX_BUF_SIZE characters
+ * at a time. If we allocate a lot of space for one line,
+ * we may indeed have to use a buffer this big later on, so
+ * we don't decrease it at all. We do free it at the end,
+ * though. */
if (i >= bufx - 1) {
- bufx += 128;
+ bufx += MAX_BUF_SIZE;
buf = charealloc(buf, bufx);
}
}
}
- /* This conditional duplicates previous read_byte() behavior.
- * Perhaps this could use some better handling. */
+ /* Perhaps this could use some better handling. */
if (ferror(f))
nperror(filename);
fclose(f);
* write error. */
int copy_file(FILE *inn, FILE *out)
{
- char buf[BUFSIZ];
+ char buf[MAX_BUF_SIZE];
size_t charsread;
int retval = 0;
/* Open the original file to copy to the backup. */
f = fopen(realname, "rb");
+
if (f == NULL) {
statusbar(_("Error reading %s: %s"), realname,
strerror(errno));
* set its permissions, so no unauthorized person can read it as
* we write. */
backup_file = fopen(backupname, "wb");
+
if (backup_file == NULL ||
chmod(backupname, originalfilestat.st_mode) == -1) {
statusbar(_("Error writing %s: %s"), backupname,
copy_status = copy_file(f, backup_file);
/* And set metadata. */
- if (copy_status != 0 || chown(backupname,
- originalfilestat.st_uid,
- originalfilestat.st_gid) == -1 || utime(backupname,
- &filetime) == -1) {
+ if (copy_status != 0 ||
+ chown(backupname, originalfilestat.st_uid,
+ originalfilestat.st_gid) == -1 ||
+ utime(backupname, &filetime) == -1) {
free(backupname);
if (copy_status == -1)
statusbar(_("Error reading %s: %s"), realname,
strcat(tempname, ".XXXXXX");
fd = mkstemp(tempname);
f = NULL;
+
if (fd != -1) {
f = fdopen(fd, "wb");
if (f == NULL)
close(fd);
}
+
if (f == NULL) {
statusbar(_("Error writing %s: %s"), tempname,
strerror(errno));
}
fd_source = open(realname, O_RDONLY | O_CREAT);
+
if (fd_source != -1) {
f_source = fdopen(fd_source, "rb");
if (f_source == NULL)
close(fd_source);
}
+
if (f_source == NULL) {
statusbar(_("Error reading %s: %s"), realname,
strerror(errno));
/* First, just give up if we couldn't even open the file. */
if (fd == -1) {
statusbar(_("Error writing %s: %s"), realname, strerror(errno));
+
/* tempname has been set only if we're prepending. */
if (tempname != NULL)
unlink(tempname);
/* Update originalfilestat to reference the file as it is now. */
stat(filename, &originalfilestat);
#endif
- statusbar(P_("Wrote %u line", "Wrote %u lines", lineswritten),
- lineswritten);
+ statusbar(P_("Wrote %lu line", "Wrote %lu lines", lineswritten),
+ (unsigned long)lineswritten);
UNSET(MODIFIED);
titlebar(NULL);
}
/* Allocate the line the first time. */
if (*lineptr == NULL) {
- *lineptr = charalloc(128);
- *n = 128;
+ *lineptr = charalloc(MAX_BUF_SIZE);
+ *n = MAX_BUF_SIZE;
}
while ((c = getc(stream)) != EOF) {
/* Check if more memory is needed. */
if (indx >= *n) {
- *lineptr = charealloc(*lineptr, *n + 128);
- *n += 128;
+ *lineptr = charealloc(*lineptr, *n + MAX_BUF_SIZE);
+ *n += MAX_BUF_SIZE;
}
/* Push the result in the line. */
/* Make room for the null character. */
if (indx >= *n) {
- *lineptr = charealloc(*lineptr, *n + 128);
- *n += 128;
+ *lineptr = charealloc(*lineptr, *n + MAX_BUF_SIZE);
+ *n += MAX_BUF_SIZE;
}
/* Null terminate the buffer. */