2000-06-19 04:22:15 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* files.c -- This file is part of GNU nano. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2017-04-09 10:09:23 +00:00
|
|
|
* Copyright (C) 1999-2011, 2013-2017 Free Software Foundation, Inc. *
|
2016-08-29 13:14:18 +00:00
|
|
|
* Copyright (C) 2015, 2016 Benno Schulenberg *
|
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty *
|
|
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU General Public License for more details. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2017-07-11 15:19:54 +00:00
|
|
|
#include <stdarg.h>
|
2000-06-19 04:22:15 +00:00
|
|
|
#include <stdio.h>
|
2005-11-05 20:01:11 +00:00
|
|
|
#include <string.h>
|
2000-06-19 04:22:15 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2000-11-05 17:54:41 +00:00
|
|
|
#include <ctype.h>
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2001-01-22 22:17:08 +00:00
|
|
|
#include <pwd.h>
|
2017-02-21 22:04:44 +00:00
|
|
|
#endif
|
2013-01-01 03:24:39 +00:00
|
|
|
#include <libgen.h>
|
2000-06-21 03:00:43 +00:00
|
|
|
|
2016-04-23 18:39:02 +00:00
|
|
|
#define LOCKBUFSIZE 8192
|
|
|
|
|
2016-01-20 15:56:40 +00:00
|
|
|
/* Verify that the containing directory of the given filename exists. */
|
|
|
|
bool has_valid_path(const char *filename)
|
2016-01-20 15:14:52 +00:00
|
|
|
{
|
2016-04-04 19:04:25 +00:00
|
|
|
char *namecopy = mallocstrcpy(NULL, filename);
|
|
|
|
char *parentdir = dirname(namecopy);
|
2016-01-20 15:14:52 +00:00
|
|
|
struct stat parentinfo;
|
2016-01-31 13:06:06 +00:00
|
|
|
bool validity = FALSE;
|
2016-01-20 15:14:52 +00:00
|
|
|
|
2016-01-31 13:06:06 +00:00
|
|
|
if (stat(parentdir, &parentinfo) == -1) {
|
|
|
|
if (errno == ENOENT)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Directory '%s' does not exist"), parentdir);
|
2016-01-31 13:06:06 +00:00
|
|
|
else
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Path '%s': %s"), parentdir, strerror(errno));
|
2016-04-24 19:24:17 +00:00
|
|
|
} else if (!S_ISDIR(parentinfo.st_mode))
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Path '%s' is not a directory"), parentdir);
|
2016-04-24 19:24:17 +00:00
|
|
|
else if (access(parentdir, X_OK) == -1)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Path '%s' is not accessible"), parentdir);
|
2016-05-23 14:24:12 +00:00
|
|
|
else if (ISSET(LOCKING) && access(parentdir, W_OK) == -1)
|
|
|
|
statusline(MILD, _("Directory '%s' is not writable"), parentdir);
|
2016-04-24 19:24:17 +00:00
|
|
|
else
|
|
|
|
validity = TRUE;
|
2016-01-20 15:14:52 +00:00
|
|
|
|
2016-04-04 19:04:25 +00:00
|
|
|
free(namecopy);
|
2016-01-31 13:06:06 +00:00
|
|
|
|
2016-01-20 15:56:40 +00:00
|
|
|
return validity;
|
2016-01-20 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 14:31:27 +00:00
|
|
|
/* Add an item to the circular list of openfile structs. */
|
2005-07-08 20:09:16 +00:00
|
|
|
void make_new_buffer(void)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2017-04-19 14:31:27 +00:00
|
|
|
openfilestruct *newnode = make_new_opennode();
|
2016-02-25 18:58:17 +00:00
|
|
|
|
2017-04-19 14:31:27 +00:00
|
|
|
if (openfile == NULL) {
|
2016-04-24 19:24:17 +00:00
|
|
|
/* Make the first open file the only element in the list. */
|
2017-04-19 14:31:27 +00:00
|
|
|
newnode->prev = newnode;
|
|
|
|
newnode->next = newnode;
|
2005-07-08 20:09:16 +00:00
|
|
|
} else {
|
2016-02-25 18:58:17 +00:00
|
|
|
/* Add the new open file after the current one in the list. */
|
|
|
|
newnode->prev = openfile;
|
|
|
|
newnode->next = openfile->next;
|
|
|
|
openfile->next->prev = newnode;
|
|
|
|
openfile->next = newnode;
|
|
|
|
|
2017-04-19 14:31:27 +00:00
|
|
|
/* There is more than one file open: show "Close" in help lines. */
|
2014-04-26 19:01:18 +00:00
|
|
|
exitfunc->desc = close_tag;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2002-12-22 16:30:00 +00:00
|
|
|
|
2017-04-19 14:31:27 +00:00
|
|
|
/* Make the new buffer the current one, and start initializing it. */
|
|
|
|
openfile = newnode;
|
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->filename = mallocstrcpy(NULL, "");
|
2002-03-28 01:59:34 +00:00
|
|
|
|
2005-07-23 00:41:45 +00:00
|
|
|
initialize_buffer_text();
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->placewewant = 0;
|
2005-07-12 20:09:24 +00:00
|
|
|
openfile->current_y = 0;
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->modified = FALSE;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->mark_set = FALSE;
|
2005-07-12 20:09:24 +00:00
|
|
|
openfile->mark_begin = NULL;
|
|
|
|
openfile->mark_begin_x = 0;
|
2016-04-24 09:28:28 +00:00
|
|
|
openfile->kind_of_mark = SOFTMARK;
|
2005-07-12 20:09:24 +00:00
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->fmt = NIX_FILE;
|
2002-04-22 23:52:34 +00:00
|
|
|
|
2008-07-10 20:13:04 +00:00
|
|
|
openfile->undotop = NULL;
|
|
|
|
openfile->current_undo = NULL;
|
2015-08-16 13:05:35 +00:00
|
|
|
openfile->last_action = OTHER;
|
2015-07-10 17:25:51 +00:00
|
|
|
|
|
|
|
openfile->current_stat = NULL;
|
2013-01-01 03:24:39 +00:00
|
|
|
openfile->lock_filename = NULL;
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2014-04-04 11:59:03 +00:00
|
|
|
#ifndef DISABLE_COLOR
|
2015-08-02 20:27:45 +00:00
|
|
|
openfile->syntax = NULL;
|
2005-07-13 20:18:46 +00:00
|
|
|
openfile->colorstrings = NULL;
|
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-04-19 14:31:27 +00:00
|
|
|
/* Initialize the text and pointers of the current openfile struct. */
|
2005-07-23 00:41:45 +00:00
|
|
|
void initialize_buffer_text(void)
|
2005-07-10 02:37:38 +00:00
|
|
|
{
|
2005-07-23 00:41:45 +00:00
|
|
|
openfile->fileage = make_new_node(NULL);
|
|
|
|
openfile->fileage->data = mallocstrcpy(NULL, "");
|
2005-07-15 19:37:32 +00:00
|
|
|
|
2005-07-23 00:41:45 +00:00
|
|
|
openfile->filebot = openfile->fileage;
|
|
|
|
openfile->edittop = openfile->fileage;
|
|
|
|
openfile->current = openfile->fileage;
|
|
|
|
|
2017-01-21 16:54:47 +00:00
|
|
|
openfile->firstcolumn = 0;
|
2017-02-09 18:26:27 +00:00
|
|
|
openfile->current_x = 0;
|
2005-07-23 00:41:45 +00:00
|
|
|
openfile->totsize = 0;
|
2005-07-10 02:37:38 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 14:42:07 +00:00
|
|
|
/* Mark the current file as modified if it isn't already, and then
|
|
|
|
* update the titlebar to display the file's new status. */
|
|
|
|
void set_modified(void)
|
|
|
|
{
|
|
|
|
if (openfile->modified)
|
|
|
|
return;
|
|
|
|
|
|
|
|
openfile->modified = TRUE;
|
|
|
|
titlebar(NULL);
|
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2016-01-20 15:56:40 +00:00
|
|
|
if (!ISSET(LOCKING) || openfile->filename[0] == '\0')
|
2016-01-15 14:42:07 +00:00
|
|
|
return;
|
|
|
|
|
2017-02-24 10:57:05 +00:00
|
|
|
if (openfile->lock_filename != NULL) {
|
2016-01-15 14:42:07 +00:00
|
|
|
char *fullname = get_full_path(openfile->filename);
|
2017-02-24 10:57:05 +00:00
|
|
|
|
2016-01-15 14:42:07 +00:00
|
|
|
write_lockfile(openfile->lock_filename, fullname, TRUE);
|
|
|
|
free(fullname);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-01-01 03:24:39 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-04-08 12:35:18 +00:00
|
|
|
/* Actually write the lockfile. This function will ALWAYS annihilate
|
|
|
|
* any previous version of the file. We'll borrow INSECURE_BACKUP here
|
|
|
|
* to decide about lockfile paranoia here as well...
|
|
|
|
*
|
|
|
|
* Args:
|
|
|
|
* lockfilename: file name for lock
|
|
|
|
* origfilename: name of the file the lock is for
|
|
|
|
* modified: whether to set the modified bit in the file
|
|
|
|
*
|
2016-05-21 10:45:10 +00:00
|
|
|
* Returns 1 on success, and 0 on failure (but continue anyway). */
|
2013-01-01 03:24:39 +00:00
|
|
|
int write_lockfile(const char *lockfilename, const char *origfilename, bool modified)
|
|
|
|
{
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2013-01-01 03:24:39 +00:00
|
|
|
int cflags, fd;
|
|
|
|
FILE *filestream;
|
|
|
|
pid_t mypid;
|
|
|
|
uid_t myuid;
|
|
|
|
struct passwd *mypwuid;
|
2015-01-20 06:15:34 +00:00
|
|
|
struct stat fileinfo;
|
2013-01-01 03:24:39 +00:00
|
|
|
char *lockdata = charalloc(1024);
|
|
|
|
char myhostname[32];
|
2016-12-08 19:30:59 +00:00
|
|
|
size_t lockdatalen = 1024;
|
|
|
|
size_t wroteamt;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2016-05-21 11:45:26 +00:00
|
|
|
mypid = getpid();
|
2013-01-01 03:24:39 +00:00
|
|
|
myuid = geteuid();
|
2016-05-21 11:45:26 +00:00
|
|
|
|
|
|
|
/* First run things that might fail before blowing away the old state. */
|
2013-01-01 03:24:39 +00:00
|
|
|
if ((mypwuid = getpwuid(myuid)) == NULL) {
|
2017-02-24 10:57:05 +00:00
|
|
|
/* TRANSLATORS: Keep the next eight messages at most 76 characters. */
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Couldn't determine my identity for lock file "
|
2016-05-17 12:35:49 +00:00
|
|
|
"(getpwuid() failed)"));
|
2016-05-21 10:45:10 +00:00
|
|
|
goto free_the_data;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gethostname(myhostname, 31) < 0) {
|
2015-11-06 20:14:37 +00:00
|
|
|
if (errno == ENAMETOOLONG)
|
|
|
|
myhostname[31] = '\0';
|
|
|
|
else {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Couldn't determine hostname for lock file: %s"),
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2016-05-21 10:45:10 +00:00
|
|
|
goto free_the_data;
|
2015-11-06 20:14:37 +00:00
|
|
|
}
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 11:45:26 +00:00
|
|
|
/* If the lockfile exists, try to delete it. */
|
2015-01-20 06:15:34 +00:00
|
|
|
if (stat(lockfilename, &fileinfo) != -1)
|
|
|
|
if (delete_lockfile(lockfilename) < 0)
|
2016-05-21 10:45:10 +00:00
|
|
|
goto free_the_data;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
|
|
|
if (ISSET(INSECURE_BACKUP))
|
2015-06-27 15:10:58 +00:00
|
|
|
cflags = O_WRONLY | O_CREAT | O_APPEND;
|
2013-01-01 03:24:39 +00:00
|
|
|
else
|
2015-06-27 15:10:58 +00:00
|
|
|
cflags = O_WRONLY | O_CREAT | O_EXCL | O_APPEND;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2016-05-21 11:45:26 +00:00
|
|
|
/* Try to create the lockfile. */
|
2013-01-01 03:24:39 +00:00
|
|
|
fd = open(lockfilename, cflags,
|
2015-06-27 15:10:58 +00:00
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
2015-01-20 06:15:34 +00:00
|
|
|
if (fd < 0) {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"),
|
2016-04-30 15:31:43 +00:00
|
|
|
lockfilename, strerror(errno));
|
2016-05-21 10:45:10 +00:00
|
|
|
goto free_the_data;
|
2015-01-20 06:15:34 +00:00
|
|
|
}
|
2013-01-03 03:07:27 +00:00
|
|
|
|
2016-05-21 11:45:26 +00:00
|
|
|
/* Try to associate a stream with the now open lockfile. */
|
2013-01-01 03:24:39 +00:00
|
|
|
filestream = fdopen(fd, "wb");
|
|
|
|
|
2016-05-21 11:32:28 +00:00
|
|
|
if (filestream == NULL) {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"), lockfilename,
|
2016-05-17 12:35:49 +00:00
|
|
|
strerror(errno));
|
2016-05-21 10:45:10 +00:00
|
|
|
goto free_the_data;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 11:45:26 +00:00
|
|
|
/* This is the lock data we will store:
|
2014-04-08 12:35:18 +00:00
|
|
|
*
|
|
|
|
* byte 0 - 0x62
|
|
|
|
* byte 1 - 0x30
|
|
|
|
* bytes 2-12 - program name which created the lock
|
2016-08-01 13:50:37 +00:00
|
|
|
* bytes 24-27 - PID (little endian) of creator process
|
2014-04-08 12:35:18 +00:00
|
|
|
* bytes 28-44 - username of who created the lock
|
|
|
|
* bytes 68-100 - hostname of where the lock was created
|
|
|
|
* bytes 108-876 - filename the lock is for
|
|
|
|
* byte 1007 - 0x55 if file is modified
|
|
|
|
*
|
|
|
|
* Looks like VIM also stores undo state in this file, so we're
|
|
|
|
* gonna have to figure out how to slap a 'OMG don't use recover on
|
|
|
|
* our lockfile' message in here...
|
|
|
|
*
|
|
|
|
* This is likely very wrong, so this is a WIP. */
|
2015-02-01 09:39:27 +00:00
|
|
|
memset(lockdata, 0, lockdatalen);
|
2013-01-01 03:24:39 +00:00
|
|
|
lockdata[0] = 0x62;
|
|
|
|
lockdata[1] = 0x30;
|
|
|
|
lockdata[24] = mypid % 256;
|
2016-08-01 13:50:37 +00:00
|
|
|
lockdata[25] = (mypid / 256) % 256;
|
|
|
|
lockdata[26] = (mypid / (256 * 256)) % 256;
|
|
|
|
lockdata[27] = mypid / (256 * 256 * 256);
|
2015-02-01 10:07:08 +00:00
|
|
|
snprintf(&lockdata[2], 11, "nano %s", VERSION);
|
2013-01-01 03:24:39 +00:00
|
|
|
strncpy(&lockdata[28], mypwuid->pw_name, 16);
|
|
|
|
strncpy(&lockdata[68], myhostname, 31);
|
2016-06-04 07:30:41 +00:00
|
|
|
if (origfilename != NULL)
|
|
|
|
strncpy(&lockdata[108], origfilename, 768);
|
2013-01-03 03:36:20 +00:00
|
|
|
if (modified == TRUE)
|
2015-06-27 15:10:58 +00:00
|
|
|
lockdata[1007] = 0x55;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
|
|
|
wroteamt = fwrite(lockdata, sizeof(char), lockdatalen, filestream);
|
|
|
|
if (wroteamt < lockdatalen) {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"),
|
2016-09-11 19:36:46 +00:00
|
|
|
lockfilename, ferror(filestream));
|
2016-05-21 10:45:10 +00:00
|
|
|
goto free_the_data;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-06-09 15:08:59 +00:00
|
|
|
fprintf(stderr, "In write_lockfile(), write successful (wrote %lu bytes)\n", (unsigned long)wroteamt);
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif
|
2013-01-01 03:24:39 +00:00
|
|
|
|
|
|
|
if (fclose(filestream) == EOF) {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Error writing lock file %s: %s"),
|
2016-09-11 19:36:46 +00:00
|
|
|
lockfilename, strerror(errno));
|
2016-05-21 10:45:10 +00:00
|
|
|
goto free_the_data;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 15:54:06 +00:00
|
|
|
openfile->lock_filename = (char *) lockfilename;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2015-08-03 08:32:52 +00:00
|
|
|
free(lockdata);
|
2013-01-01 03:24:39 +00:00
|
|
|
return 1;
|
2015-08-03 08:32:52 +00:00
|
|
|
|
2016-05-21 10:45:10 +00:00
|
|
|
free_the_data:
|
2015-08-03 08:32:52 +00:00
|
|
|
free(lockdata);
|
2016-05-21 10:45:10 +00:00
|
|
|
return 0;
|
2017-02-21 22:04:44 +00:00
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:52:51 +00:00
|
|
|
/* Delete the lockfile. Return -1 if unsuccessful, and 1 otherwise. */
|
2013-01-01 03:24:39 +00:00
|
|
|
int delete_lockfile(const char *lockfilename)
|
|
|
|
{
|
|
|
|
if (unlink(lockfilename) < 0 && errno != ENOENT) {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Error deleting lock file %s: %s"), lockfilename,
|
2016-05-30 07:09:36 +00:00
|
|
|
strerror(errno));
|
2015-06-27 15:10:58 +00:00
|
|
|
return -1;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-08 12:35:18 +00:00
|
|
|
/* Deal with lockfiles. Return -1 on refusing to override the lockfile,
|
|
|
|
* and 1 on successfully creating it; 0 means we were not successful in
|
2016-05-23 12:52:51 +00:00
|
|
|
* creating the lockfile but we should continue to load the file. */
|
2013-01-01 03:24:39 +00:00
|
|
|
int do_lockfile(const char *filename)
|
|
|
|
{
|
2016-04-06 08:41:42 +00:00
|
|
|
char *namecopy = (char *) mallocstrcpy(NULL, filename);
|
|
|
|
char *secondcopy = (char *) mallocstrcpy(NULL, filename);
|
2015-07-10 15:57:49 +00:00
|
|
|
size_t locknamesize = strlen(filename) + strlen(locking_prefix)
|
2014-06-04 16:30:11 +00:00
|
|
|
+ strlen(locking_suffix) + 3;
|
2015-07-10 15:57:49 +00:00
|
|
|
char *lockfilename = charalloc(locknamesize);
|
2015-02-01 09:48:50 +00:00
|
|
|
static char lockprog[11], lockuser[17];
|
2013-01-01 03:24:39 +00:00
|
|
|
struct stat fileinfo;
|
2016-01-29 16:58:02 +00:00
|
|
|
int lockfd, lockpid, retval = -1;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2016-04-06 08:41:42 +00:00
|
|
|
snprintf(lockfilename, locknamesize, "%s/%s%s%s", dirname(namecopy),
|
|
|
|
locking_prefix, basename(secondcopy), locking_suffix);
|
|
|
|
free(secondcopy);
|
2013-01-01 03:24:39 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "lock file name is %s\n", lockfilename);
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif
|
2013-01-01 03:24:39 +00:00
|
|
|
if (stat(lockfilename, &fileinfo) != -1) {
|
2016-12-08 19:30:59 +00:00
|
|
|
size_t readtot = 0;
|
|
|
|
size_t readamt = 0;
|
2016-08-01 13:50:37 +00:00
|
|
|
char *lockbuf, *question, *pidstring, *postedname, *promptstr;
|
2016-05-23 16:23:25 +00:00
|
|
|
int room, response;
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2015-06-27 15:10:58 +00:00
|
|
|
if ((lockfd = open(lockfilename, O_RDONLY)) < 0) {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Error opening lock file %s: %s"),
|
2015-06-27 15:10:58 +00:00
|
|
|
lockfilename, strerror(errno));
|
2016-01-29 16:58:02 +00:00
|
|
|
goto free_the_name;
|
2015-06-27 15:10:58 +00:00
|
|
|
}
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2016-04-23 18:39:02 +00:00
|
|
|
lockbuf = charalloc(LOCKBUFSIZE);
|
2015-06-27 15:10:58 +00:00
|
|
|
do {
|
2016-04-23 18:39:02 +00:00
|
|
|
readamt = read(lockfd, &lockbuf[readtot], LOCKBUFSIZE - readtot);
|
2015-06-27 15:10:58 +00:00
|
|
|
readtot += readamt;
|
2016-04-23 18:39:02 +00:00
|
|
|
} while (readamt > 0 && readtot < LOCKBUFSIZE);
|
2015-06-27 15:10:58 +00:00
|
|
|
|
2016-08-30 09:45:33 +00:00
|
|
|
close(lockfd);
|
|
|
|
|
2015-06-27 15:10:58 +00:00
|
|
|
if (readtot < 48) {
|
2016-05-19 18:43:08 +00:00
|
|
|
statusline(MILD, _("Error reading lock file %s: "
|
2016-04-30 15:31:43 +00:00
|
|
|
"Not enough data read"), lockfilename);
|
2016-01-29 16:58:02 +00:00
|
|
|
free(lockbuf);
|
|
|
|
goto free_the_name;
|
2015-06-27 15:10:58 +00:00
|
|
|
}
|
2016-01-29 16:58:02 +00:00
|
|
|
|
2015-06-27 15:10:58 +00:00
|
|
|
strncpy(lockprog, &lockbuf[2], 10);
|
2016-08-01 13:50:37 +00:00
|
|
|
lockpid = (((unsigned char)lockbuf[27] * 256 + (unsigned char)lockbuf[26]) * 256 +
|
|
|
|
(unsigned char)lockbuf[25]) * 256 + (unsigned char)lockbuf[24];
|
2015-06-27 15:10:58 +00:00
|
|
|
strncpy(lockuser, &lockbuf[28], 16);
|
2016-01-29 16:58:02 +00:00
|
|
|
free(lockbuf);
|
|
|
|
|
2016-08-01 13:50:37 +00:00
|
|
|
pidstring = charalloc(11);
|
|
|
|
sprintf (pidstring, "%u", (unsigned int)lockpid);
|
|
|
|
|
2013-01-01 03:24:39 +00:00
|
|
|
#ifdef DEBUG
|
2015-06-27 15:10:58 +00:00
|
|
|
fprintf(stderr, "lockpid = %d\n", lockpid);
|
|
|
|
fprintf(stderr, "program name which created this lock file should be %s\n", lockprog);
|
|
|
|
fprintf(stderr, "user which created this lock file should be %s\n", lockuser);
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif
|
2015-02-15 16:28:08 +00:00
|
|
|
/* TRANSLATORS: The second %s is the name of the user, the third that of the editor. */
|
2016-08-01 13:50:37 +00:00
|
|
|
question = _("File %s is being edited (by %s with %s, PID %s); continue?");
|
|
|
|
room = COLS - strlenpt(question) + 7 - strlenpt(lockuser) -
|
|
|
|
strlenpt(lockprog) - strlenpt(pidstring);
|
2016-04-06 08:41:42 +00:00
|
|
|
if (room < 4)
|
2016-05-18 19:04:39 +00:00
|
|
|
postedname = mallocstrcpy(NULL, "_");
|
|
|
|
else if (room < strlenpt(filename)) {
|
|
|
|
char *fragment = display_string(filename,
|
|
|
|
strlenpt(filename) - room + 3, room, FALSE);
|
|
|
|
postedname = charalloc(strlen(fragment) + 4);
|
|
|
|
strcpy(postedname, "...");
|
|
|
|
strcat(postedname, fragment);
|
|
|
|
free(fragment);
|
|
|
|
} else
|
|
|
|
postedname = mallocstrcpy(NULL, filename);
|
2016-04-06 08:41:42 +00:00
|
|
|
|
2016-08-01 13:50:37 +00:00
|
|
|
/* Allow extra space for username (14), program name (8), PID (8),
|
2016-04-06 08:41:42 +00:00
|
|
|
* and terminating \0 (1), minus the %s (2) for the file name. */
|
2016-08-01 13:50:37 +00:00
|
|
|
promptstr = charalloc(strlen(question) + 29 + strlen(postedname));
|
|
|
|
sprintf(promptstr, question, postedname, lockuser, lockprog, pidstring);
|
2016-05-18 19:04:39 +00:00
|
|
|
free(postedname);
|
2016-08-01 13:50:37 +00:00
|
|
|
free(pidstring);
|
2016-05-18 19:04:39 +00:00
|
|
|
|
2016-05-23 16:23:25 +00:00
|
|
|
response = do_yesno_prompt(FALSE, promptstr);
|
2016-01-29 16:58:02 +00:00
|
|
|
free(promptstr);
|
|
|
|
|
2016-05-23 16:23:25 +00:00
|
|
|
if (response < 1) {
|
2015-06-27 15:10:58 +00:00
|
|
|
blank_statusbar();
|
2016-01-29 16:58:02 +00:00
|
|
|
goto free_the_name;
|
2015-06-27 15:10:58 +00:00
|
|
|
}
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
2016-01-29 16:58:02 +00:00
|
|
|
|
|
|
|
retval = write_lockfile(lockfilename, filename, FALSE);
|
|
|
|
|
|
|
|
free_the_name:
|
2016-04-06 08:41:42 +00:00
|
|
|
free(namecopy);
|
2016-01-29 16:58:02 +00:00
|
|
|
if (retval < 1)
|
|
|
|
free(lockfilename);
|
|
|
|
|
|
|
|
return retval;
|
2013-01-01 03:24:39 +00:00
|
|
|
}
|
2016-02-09 20:53:11 +00:00
|
|
|
|
|
|
|
/* Perform a stat call on the given filename, allocating a stat struct
|
|
|
|
* if necessary. On success, *pstat points to the stat's result. On
|
|
|
|
* failure, *pstat is freed and made NULL. */
|
|
|
|
void stat_with_alloc(const char *filename, struct stat **pstat)
|
|
|
|
{
|
|
|
|
if (*pstat == NULL)
|
|
|
|
*pstat = (struct stat *)nmalloc(sizeof(struct stat));
|
|
|
|
|
|
|
|
if (stat(filename, *pstat) != 0) {
|
|
|
|
free(*pstat);
|
|
|
|
*pstat = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2016-05-23 12:52:51 +00:00
|
|
|
/* This does one of three things. If the filename is "", just create a new
|
|
|
|
* empty buffer. Otherwise, read the given file into the existing buffer,
|
|
|
|
* or into a new buffer when MULTIBUFFER is set or there is no buffer yet. */
|
2015-12-30 10:11:20 +00:00
|
|
|
bool open_buffer(const char *filename, bool undoable)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2016-12-03 19:25:16 +00:00
|
|
|
bool new_buffer = (openfile == NULL || ISSET(MULTIBUFFER));
|
|
|
|
/* Whether we load into the current buffer or a new one. */
|
2016-04-23 16:13:43 +00:00
|
|
|
char *realname;
|
|
|
|
/* The filename after tilde expansion. */
|
2005-07-08 20:09:16 +00:00
|
|
|
FILE *f;
|
|
|
|
int rc;
|
|
|
|
/* rc == -2 means that we have a new file. -1 means that the
|
|
|
|
* open() failed. 0 means that the open() succeeded. */
|
2002-12-22 16:30:00 +00:00
|
|
|
|
2016-12-26 11:35:50 +00:00
|
|
|
/* Display newlines in filenames as ^J. */
|
|
|
|
as_an_at = FALSE;
|
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
|
|
|
if (check_operating_dir(filename, FALSE)) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Can't insert file from outside of %s"),
|
2016-05-16 19:18:09 +00:00
|
|
|
full_operating_dir);
|
2015-12-30 10:11:20 +00:00
|
|
|
return FALSE;
|
2005-07-08 20:09:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2016-04-23 16:13:43 +00:00
|
|
|
realname = real_dir_from_tilde(filename);
|
|
|
|
|
2016-05-23 12:52:51 +00:00
|
|
|
/* When the specified filename is not empty, and the corresponding
|
|
|
|
* file exists, verify that it is a normal file. */
|
2015-07-17 20:40:44 +00:00
|
|
|
if (strcmp(filename, "") != 0) {
|
|
|
|
struct stat fileinfo;
|
|
|
|
|
2016-04-23 16:13:43 +00:00
|
|
|
if (stat(realname, &fileinfo) == 0 && !S_ISREG(fileinfo.st_mode)) {
|
2015-07-17 20:40:44 +00:00
|
|
|
if (S_ISDIR(fileinfo.st_mode))
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("\"%s\" is a directory"), realname);
|
2015-07-17 20:40:44 +00:00
|
|
|
else
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("\"%s\" is not a normal file"), realname);
|
2016-04-23 16:13:43 +00:00
|
|
|
free(realname);
|
2015-12-30 10:11:20 +00:00
|
|
|
return FALSE;
|
2015-07-17 20:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 17:25:51 +00:00
|
|
|
/* If we're going to load into a new buffer, first create the new
|
2016-01-20 15:33:41 +00:00
|
|
|
* buffer and (if possible) lock the corresponding file. */
|
2015-01-14 02:36:30 +00:00
|
|
|
if (new_buffer) {
|
2005-07-08 20:09:16 +00:00
|
|
|
make_new_buffer();
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2017-04-22 16:27:01 +00:00
|
|
|
if (!inhelp && has_valid_path(realname)) {
|
2015-01-14 02:36:30 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-01-20 15:33:41 +00:00
|
|
|
if (ISSET(LOCKING) && filename[0] != '\0') {
|
2016-05-18 11:18:07 +00:00
|
|
|
/* When not overriding an existing lock, discard the buffer. */
|
2016-05-17 11:37:53 +00:00
|
|
|
if (do_lockfile(realname) < 0) {
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2016-05-18 11:18:07 +00:00
|
|
|
close_buffer();
|
2015-01-20 06:15:34 +00:00
|
|
|
#endif
|
2016-05-18 11:18:07 +00:00
|
|
|
free(realname);
|
|
|
|
return FALSE;
|
2016-05-17 11:37:53 +00:00
|
|
|
}
|
2015-01-14 02:36:30 +00:00
|
|
|
}
|
2016-01-20 15:33:41 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2016-05-17 11:37:53 +00:00
|
|
|
}
|
2015-01-14 02:36:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 18:59:30 +00:00
|
|
|
/* If the filename isn't blank, and we are not in NOREAD_MODE,
|
|
|
|
* open the file. Otherwise, treat it as a new file. */
|
|
|
|
rc = (filename[0] != '\0' && !ISSET(NOREAD_MODE)) ?
|
2017-04-22 16:27:01 +00:00
|
|
|
open_file(realname, new_buffer, inhelp, &f) : -2;
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2006-11-27 05:25:48 +00:00
|
|
|
/* If we have a file, and we're loading into a new buffer, update
|
|
|
|
* the filename. */
|
2005-07-08 20:09:16 +00:00
|
|
|
if (rc != -1 && new_buffer)
|
2016-04-23 16:13:43 +00:00
|
|
|
openfile->filename = mallocstrcpy(openfile->filename, realname);
|
2005-01-27 20:49:07 +00:00
|
|
|
|
2005-07-15 19:37:32 +00:00
|
|
|
/* If we have a non-new file, read it in. Then, if the buffer has
|
|
|
|
* no stat, update the stat, if applicable. */
|
2009-09-03 05:45:13 +00:00
|
|
|
if (rc > 0) {
|
2016-04-23 16:13:43 +00:00
|
|
|
read_file(f, rc, realname, undoable, new_buffer);
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-02-09 20:53:11 +00:00
|
|
|
if (openfile->current_stat == NULL)
|
2016-04-23 16:13:43 +00:00
|
|
|
stat_with_alloc(realname, &openfile->current_stat);
|
2001-10-14 19:05:10 +00:00
|
|
|
#endif
|
2005-07-08 20:09:16 +00:00
|
|
|
}
|
|
|
|
|
2006-11-27 05:25:48 +00:00
|
|
|
/* If we have a file, and we're loading into a new buffer, move back
|
2005-11-05 17:20:39 +00:00
|
|
|
* to the beginning of the first line of the buffer. */
|
|
|
|
if (rc != -1 && new_buffer) {
|
2005-07-09 04:42:47 +00:00
|
|
|
openfile->current = openfile->fileage;
|
2005-11-05 17:20:39 +00:00
|
|
|
openfile->current_x = 0;
|
|
|
|
openfile->placewewant = 0;
|
|
|
|
}
|
2005-07-13 20:18:46 +00:00
|
|
|
|
2014-04-04 11:59:03 +00:00
|
|
|
#ifndef DISABLE_COLOR
|
2005-07-15 00:36:49 +00:00
|
|
|
/* If we're loading into a new buffer, update the colors to account
|
|
|
|
* for it, if applicable. */
|
2005-07-13 20:18:46 +00:00
|
|
|
if (new_buffer)
|
|
|
|
color_update();
|
|
|
|
#endif
|
2016-04-23 16:13:43 +00:00
|
|
|
free(realname);
|
2015-12-30 10:11:20 +00:00
|
|
|
return TRUE;
|
2005-07-09 04:42:47 +00:00
|
|
|
}
|
2003-09-23 04:25:05 +00:00
|
|
|
|
2005-11-08 19:15:58 +00:00
|
|
|
#ifndef DISABLE_SPELLER
|
2016-04-17 15:33:18 +00:00
|
|
|
/* Open the specified file, and if that succeeds, blow away the text of
|
|
|
|
* the current buffer and read the file contents into its place. */
|
2005-11-08 19:15:58 +00:00
|
|
|
void replace_buffer(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
2015-06-27 15:03:45 +00:00
|
|
|
int descriptor;
|
2005-11-08 19:15:58 +00:00
|
|
|
|
2015-06-27 15:03:45 +00:00
|
|
|
/* Open the file quietly. */
|
2016-04-17 15:33:18 +00:00
|
|
|
descriptor = open_file(filename, FALSE, TRUE, &f);
|
|
|
|
|
|
|
|
/* If opening failed, forget it. */
|
|
|
|
if (descriptor < 0)
|
|
|
|
return;
|
2005-11-08 19:15:58 +00:00
|
|
|
|
|
|
|
/* Reinitialize the text of the current buffer. */
|
|
|
|
free_filestruct(openfile->fileage);
|
|
|
|
initialize_buffer_text();
|
|
|
|
|
2016-04-17 15:33:18 +00:00
|
|
|
/* Insert the processed file into its place. */
|
|
|
|
read_file(f, descriptor, filename, FALSE, TRUE);
|
2015-07-13 18:04:05 +00:00
|
|
|
|
|
|
|
/* Put current at a place that is certain to exist. */
|
|
|
|
openfile->current = openfile->fileage;
|
2005-11-08 19:15:58 +00:00
|
|
|
}
|
2017-02-10 01:04:14 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* Open the specified file, and if that succeeds, blow away the text of
|
|
|
|
* the current buffer at the given coordinates and read the file
|
|
|
|
* contents into its place. */
|
|
|
|
void replace_marked_buffer(const char *filename, filestruct *top, size_t top_x,
|
|
|
|
filestruct *bot, size_t bot_x)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int descriptor;
|
|
|
|
bool old_no_newlines = ISSET(NO_NEWLINES);
|
|
|
|
filestruct *trash_top = NULL;
|
|
|
|
filestruct *trash_bot = NULL;
|
|
|
|
|
|
|
|
descriptor = open_file(filename, FALSE, TRUE, &f);
|
|
|
|
|
|
|
|
if (descriptor < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Don't add a magicline when replacing text in the buffer. */
|
|
|
|
SET(NO_NEWLINES);
|
|
|
|
|
|
|
|
/* Throw away the text under the mark, and insert the processed file
|
|
|
|
* where the marked text was. */
|
|
|
|
extract_buffer(&trash_top, &trash_bot, top, top_x, bot, bot_x);
|
|
|
|
free_filestruct(trash_top);
|
|
|
|
read_file(f, descriptor, filename, FALSE, TRUE);
|
|
|
|
|
|
|
|
/* Restore the magicline behavior now that we're done fiddling. */
|
|
|
|
if (!old_no_newlines)
|
|
|
|
UNSET(NO_NEWLINES);
|
|
|
|
}
|
2017-04-19 11:24:41 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2005-11-08 19:15:58 +00:00
|
|
|
#endif /* !DISABLE_SPELLER */
|
|
|
|
|
2017-05-01 14:48:13 +00:00
|
|
|
/* Update the titlebar and the multiline cache to match the current buffer. */
|
|
|
|
void prepare_for_display(void)
|
2005-07-09 04:42:47 +00:00
|
|
|
{
|
2005-07-14 23:06:22 +00:00
|
|
|
/* Update the titlebar, since the filename may have changed. */
|
2017-04-22 16:27:01 +00:00
|
|
|
if (!inhelp)
|
|
|
|
titlebar(NULL);
|
2005-07-14 23:06:22 +00:00
|
|
|
|
2014-04-04 11:59:03 +00:00
|
|
|
#ifndef DISABLE_COLOR
|
2015-12-20 21:10:41 +00:00
|
|
|
/* If there are multiline coloring regexes, and there is no
|
|
|
|
* multiline cache data yet, precalculate it now. */
|
|
|
|
if (openfile->syntax && openfile->syntax->nmultis > 0 &&
|
|
|
|
openfile->fileage->multidata == NULL)
|
|
|
|
precalc_multicolorinfo();
|
2005-07-14 23:06:22 +00:00
|
|
|
|
2017-05-01 14:48:13 +00:00
|
|
|
have_palette = FALSE;
|
|
|
|
#endif
|
2017-04-27 19:42:36 +00:00
|
|
|
refresh_needed = TRUE;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2015-12-18 20:44:01 +00:00
|
|
|
/* Switch to a neighbouring file buffer; to the next if to_next is TRUE;
|
|
|
|
* otherwise, to the previous one. */
|
2016-05-17 11:37:53 +00:00
|
|
|
void switch_to_prevnext_buffer(bool to_next)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2015-12-18 20:44:01 +00:00
|
|
|
/* If only one file buffer is open, say so and get out. */
|
2016-12-25 12:04:35 +00:00
|
|
|
if (openfile == openfile->next && !inhelp) {
|
2016-05-17 11:37:53 +00:00
|
|
|
statusbar(_("No more open file buffers"));
|
2005-07-08 02:47:05 +00:00
|
|
|
return;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2015-12-18 20:44:01 +00:00
|
|
|
/* Switch to the next or previous file buffer. */
|
|
|
|
openfile = to_next ? openfile->next : openfile->prev;
|
2005-07-08 02:47:05 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2005-07-08 19:57:25 +00:00
|
|
|
fprintf(stderr, "filename is %s\n", openfile->filename);
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
|
|
|
|
2017-01-21 16:54:47 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* When not in softwrap mode, make sure firstcolumn is zero. It might
|
|
|
|
* be nonzero if we had softwrap mode on while in this buffer, and then
|
|
|
|
* turned softwrap mode off while in a different buffer. */
|
|
|
|
if (!ISSET(SOFTWRAP))
|
|
|
|
openfile->firstcolumn = 0;
|
|
|
|
#endif
|
|
|
|
|
2017-05-01 14:48:13 +00:00
|
|
|
/* Update titlebar and multiline info to match the current buffer. */
|
|
|
|
prepare_for_display();
|
2005-07-09 04:42:47 +00:00
|
|
|
|
2017-07-13 08:55:02 +00:00
|
|
|
if (inhelp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Ensure that the main loop will redraw the help lines. */
|
|
|
|
currmenu = MMOST;
|
|
|
|
|
2005-07-08 20:09:16 +00:00
|
|
|
/* Indicate the switch on the statusbar. */
|
2017-07-13 08:55:02 +00:00
|
|
|
statusline(HUSH, _("Switched to %s"),
|
2017-04-22 16:27:01 +00:00
|
|
|
((openfile->filename[0] == '\0') ?
|
|
|
|
_("New Buffer") : openfile->filename));
|
2005-07-08 02:47:05 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2005-07-08 20:09:16 +00:00
|
|
|
dump_filestruct(openfile->current);
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2005-07-21 05:56:42 +00:00
|
|
|
/* Switch to the previous entry in the openfile filebuffer. */
|
2005-07-08 20:09:16 +00:00
|
|
|
void switch_to_prev_buffer_void(void)
|
2002-02-22 04:30:50 +00:00
|
|
|
{
|
2016-05-17 11:37:53 +00:00
|
|
|
switch_to_prevnext_buffer(FALSE);
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2005-07-21 05:56:42 +00:00
|
|
|
/* Switch to the next entry in the openfile filebuffer. */
|
2005-07-08 20:09:16 +00:00
|
|
|
void switch_to_next_buffer_void(void)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2016-05-17 11:37:53 +00:00
|
|
|
switch_to_prevnext_buffer(TRUE);
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2016-05-17 11:37:53 +00:00
|
|
|
/* Delete an entry from the circular list of open files, and switch to the
|
|
|
|
* one after it. Return TRUE on success, and FALSE if there are no other
|
|
|
|
* open buffers. */
|
|
|
|
bool close_buffer(void)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2005-07-21 05:56:42 +00:00
|
|
|
/* If only one file buffer is open, get out. */
|
2005-07-08 19:57:25 +00:00
|
|
|
if (openfile == openfile->next)
|
2005-07-08 02:47:05 +00:00
|
|
|
return FALSE;
|
2002-02-22 04:30:50 +00:00
|
|
|
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2016-01-29 20:43:54 +00:00
|
|
|
if (ISSET(POS_HISTORY))
|
|
|
|
update_poshistory(openfile->filename,
|
|
|
|
openfile->current->lineno, xplustabs() + 1);
|
2014-02-22 16:26:30 +00:00
|
|
|
#endif
|
2011-02-18 07:30:57 +00:00
|
|
|
|
2005-07-21 05:56:42 +00:00
|
|
|
/* Switch to the next file buffer. */
|
2016-05-17 11:37:53 +00:00
|
|
|
switch_to_prevnext_buffer(TRUE);
|
2002-02-22 04:30:50 +00:00
|
|
|
|
2005-07-21 05:56:42 +00:00
|
|
|
/* Close the file buffer we had open before. */
|
2005-07-08 19:57:25 +00:00
|
|
|
unlink_opennode(openfile->prev);
|
2002-02-22 04:30:50 +00:00
|
|
|
|
2017-07-13 08:55:02 +00:00
|
|
|
/* If now just one buffer remains open, show "Exit" in the help lines. */
|
2014-04-26 19:01:18 +00:00
|
|
|
if (openfile == openfile->next)
|
|
|
|
exitfunc->desc = exit_tag;
|
2005-04-14 03:52:28 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
return TRUE;
|
2002-02-22 04:30:50 +00:00
|
|
|
}
|
2017-05-01 18:20:34 +00:00
|
|
|
#endif /* ENABLE_MULTIBUFFER */
|
2002-02-22 04:30:50 +00:00
|
|
|
|
2016-05-23 12:52:51 +00:00
|
|
|
/* Do a quick permissions check by verifying whether the file is appendable.
|
|
|
|
* Err on the side of permissiveness (reporting TRUE when it might be wrong)
|
|
|
|
* to not fluster users editing on odd filesystems by printing incorrect
|
|
|
|
* warnings. */
|
2009-11-03 18:47:39 +00:00
|
|
|
int is_file_writable(const char *filename)
|
|
|
|
{
|
|
|
|
struct stat fileinfo, fileinfo2;
|
|
|
|
int fd;
|
|
|
|
FILE *f;
|
|
|
|
char *full_filename;
|
2016-05-23 16:23:25 +00:00
|
|
|
bool result = TRUE;
|
2009-11-03 18:47:39 +00:00
|
|
|
|
|
|
|
if (ISSET(VIEW_MODE))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* Get the specified file's full path. */
|
|
|
|
full_filename = get_full_path(filename);
|
|
|
|
|
2015-07-10 17:25:51 +00:00
|
|
|
/* Okay, if we can't stat the absolute path due to some component's
|
|
|
|
* permissions, just try the relative one. */
|
2015-06-27 15:10:58 +00:00
|
|
|
if (full_filename == NULL ||
|
|
|
|
(stat(full_filename, &fileinfo) == -1 && stat(filename, &fileinfo2) != -1))
|
|
|
|
full_filename = mallocstrcpy(NULL, filename);
|
2009-11-03 18:47:39 +00:00
|
|
|
|
|
|
|
if ((fd = open(full_filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR |
|
2015-06-27 15:10:58 +00:00
|
|
|
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1 ||
|
|
|
|
(f = fdopen(fd, "a")) == NULL)
|
2016-05-23 16:23:25 +00:00
|
|
|
result = FALSE;
|
2009-11-03 18:47:39 +00:00
|
|
|
else
|
2015-06-27 15:10:58 +00:00
|
|
|
fclose(f);
|
2009-11-03 18:47:39 +00:00
|
|
|
|
2016-05-23 16:23:25 +00:00
|
|
|
close(fd);
|
2009-11-03 18:47:39 +00:00
|
|
|
free(full_filename);
|
2016-05-23 16:23:25 +00:00
|
|
|
|
|
|
|
return result;
|
2009-11-03 18:47:39 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
/* Encode any NUL bytes in the given line of text, which is of length buf_len,
|
|
|
|
* and return a dynamically allocated copy of the resultant string. */
|
|
|
|
char *encode_data(char *buf, size_t buf_len)
|
2004-09-05 21:40:31 +00:00
|
|
|
{
|
2005-07-08 02:47:05 +00:00
|
|
|
unsunder(buf, buf_len);
|
2016-04-17 10:34:07 +00:00
|
|
|
buf[buf_len] = '\0';
|
2004-08-27 21:02:38 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
return mallocstrcpy(NULL, buf);
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2004-11-05 16:24:35 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Read an open file into the current buffer. f should be set to the
|
2008-08-03 04:48:05 +00:00
|
|
|
* open file, and filename should be set to the name of the file.
|
2014-04-08 12:35:18 +00:00
|
|
|
* undoable means do we want to create undo records to try and undo
|
|
|
|
* this. Will also attempt to check file writability if fd > 0 and
|
|
|
|
* checkwritable == TRUE. */
|
2017-02-09 18:17:54 +00:00
|
|
|
void read_file(FILE *f, int fd, const char *filename, bool undoable,
|
|
|
|
bool checkwritable)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2017-02-09 18:26:27 +00:00
|
|
|
ssize_t was_lineno = openfile->current->lineno;
|
|
|
|
/* The line number where we start the insertion. */
|
2017-02-08 04:09:16 +00:00
|
|
|
size_t was_leftedge = 0;
|
|
|
|
/* The leftedge where we start the insertion. */
|
2005-07-08 02:47:05 +00:00
|
|
|
size_t num_lines = 0;
|
|
|
|
/* The number of lines in the file. */
|
|
|
|
size_t len = 0;
|
|
|
|
/* The length of the current line of the file. */
|
|
|
|
char input = '\0';
|
|
|
|
/* The current input character. */
|
|
|
|
char *buf;
|
2017-02-17 18:59:34 +00:00
|
|
|
/* The buffer in which we assemble each line of the file. */
|
|
|
|
size_t bufx = MAX_BUF_SIZE;
|
|
|
|
/* The allocated size of the line buffer; increased as needed. */
|
2017-02-09 18:26:27 +00:00
|
|
|
filestruct *topline;
|
|
|
|
/* The top of the new buffer where we store the read file. */
|
|
|
|
filestruct *bottomline;
|
|
|
|
/* The bottom of the new buffer. */
|
2005-07-08 02:47:05 +00:00
|
|
|
int input_int;
|
|
|
|
/* The current value we read from the file, whether an input
|
|
|
|
* character or EOF. */
|
2009-09-03 05:45:13 +00:00
|
|
|
bool writable = TRUE;
|
2017-02-09 18:17:54 +00:00
|
|
|
/* Whether the file is writable (in case we care). */
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-08 02:47:05 +00:00
|
|
|
int format = 0;
|
|
|
|
/* 0 = *nix, 1 = DOS, 2 = Mac, 3 = both DOS and Mac. */
|
2004-10-01 18:34:30 +00:00
|
|
|
#endif
|
2002-09-03 22:58:40 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
buf = charalloc(bufx);
|
2004-09-05 21:40:31 +00:00
|
|
|
|
2008-08-03 04:48:05 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (undoable)
|
|
|
|
add_undo(INSERT);
|
2017-02-08 04:09:16 +00:00
|
|
|
|
|
|
|
if (ISSET(SOFTWRAP))
|
2017-07-09 19:07:38 +00:00
|
|
|
was_leftedge = leftedge_for(xplustabs(), openfile->current);
|
2008-08-03 04:48:05 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
/* Create an empty buffer. */
|
|
|
|
topline = make_new_node(NULL);
|
|
|
|
bottomline = topline;
|
|
|
|
|
|
|
|
/* Read the entire file into the new buffer. */
|
2005-07-08 02:47:05 +00:00
|
|
|
while ((input_int = getc(f)) != EOF) {
|
|
|
|
input = (char)input_int;
|
|
|
|
|
|
|
|
/* If it's a *nix file ("\n") or a DOS file ("\r\n"), and file
|
|
|
|
* conversion isn't disabled, handle it! */
|
|
|
|
if (input == '\n') {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2007-11-17 20:34:38 +00:00
|
|
|
/* If it's a DOS file or a DOS/Mac file ('\r' before '\n' on
|
|
|
|
* the first line if we think it's a *nix file, or on any
|
|
|
|
* line otherwise), and file conversion isn't disabled,
|
|
|
|
* handle it! */
|
2016-04-17 09:44:22 +00:00
|
|
|
if ((num_lines == 0 || format != 0) && !ISSET(NO_CONVERT) &&
|
2016-04-17 08:46:56 +00:00
|
|
|
len > 0 && buf[len - 1] == '\r') {
|
2007-11-17 20:34:38 +00:00
|
|
|
if (format == 0 || format == 2)
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
/* If it's a Mac file ('\r' without '\n' on the first line if we
|
|
|
|
* think it's a *nix file, or on any line otherwise), and file
|
|
|
|
* conversion isn't disabled, handle it! */
|
2016-04-17 09:44:22 +00:00
|
|
|
} else if ((num_lines == 0 || format != 0) && !ISSET(NO_CONVERT) &&
|
|
|
|
len > 0 && buf[len - 1] == '\r') {
|
2005-07-08 02:47:05 +00:00
|
|
|
/* If we currently think the file is a *nix file, set format
|
|
|
|
* to Mac. If we currently think the file is a DOS file,
|
|
|
|
* set format to both DOS and Mac. */
|
|
|
|
if (format == 0 || format == 1)
|
|
|
|
format += 2;
|
2005-03-31 17:00:43 +00:00
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
} else {
|
2016-04-17 11:02:04 +00:00
|
|
|
/* Store the character. */
|
|
|
|
buf[len] = input;
|
|
|
|
|
2016-04-17 09:44:22 +00:00
|
|
|
/* Keep track of the total length of the line. It might have
|
|
|
|
* nulls in it, so we can't just use strlen() later. */
|
2005-07-08 02:47:05 +00:00
|
|
|
len++;
|
2005-03-30 23:52:02 +00:00
|
|
|
|
2016-04-17 09:44:22 +00:00
|
|
|
/* If needed, increase the buffer size, MAX_BUF_SIZE characters at
|
|
|
|
* a time. Don't bother decreasing it; it is freed at the end. */
|
2016-04-17 11:02:04 +00:00
|
|
|
if (len == bufx) {
|
2005-07-08 02:47:05 +00:00
|
|
|
bufx += MAX_BUF_SIZE;
|
|
|
|
buf = charealloc(buf, bufx);
|
2004-11-07 18:09:41 +00:00
|
|
|
}
|
2017-02-09 18:26:27 +00:00
|
|
|
continue;
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2017-02-09 18:26:27 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* If it's a DOS or Mac line, strip the '\r' from it. */
|
|
|
|
if (len > 0 && buf[len - 1] == '\r' && !ISSET(NO_CONVERT))
|
|
|
|
buf[--len] = '\0';
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Store the data and make a new line. */
|
|
|
|
bottomline->data = encode_data(buf, len);
|
|
|
|
bottomline->next = make_new_node(bottomline);
|
|
|
|
bottomline = bottomline->next;
|
|
|
|
num_lines++;
|
|
|
|
|
|
|
|
/* Reset the length in preparation for the next line. */
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* If it happens to be a Mac line, store the character after the \r
|
|
|
|
* as the first character of the next line. */
|
|
|
|
if (input != '\n')
|
|
|
|
buf[len++] = input;
|
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perhaps this could use some better handling. */
|
|
|
|
if (ferror(f))
|
|
|
|
nperror(filename);
|
|
|
|
fclose(f);
|
2009-12-09 16:51:43 +00:00
|
|
|
if (fd > 0 && checkwritable) {
|
2009-11-10 03:44:12 +00:00
|
|
|
close(fd);
|
2009-09-03 05:45:13 +00:00
|
|
|
writable = is_file_writable(filename);
|
|
|
|
}
|
2004-11-05 15:25:53 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
/* If the file ended with newline, or it was entirely empty, make the
|
|
|
|
* last line blank. Otherwise, put the last read data in. */
|
|
|
|
if (len == 0)
|
|
|
|
bottomline->data = mallocstrcpy(NULL, "");
|
|
|
|
else {
|
|
|
|
bool mac_line_needs_newline = FALSE;
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-02-09 18:26:27 +00:00
|
|
|
/* If the final character is '\r', and file conversion isn't disabled,
|
|
|
|
* set format to Mac if we currently think the file is a *nix file, or
|
|
|
|
* to DOS-and-Mac if we currently think it is a DOS file. */
|
|
|
|
if (buf[len - 1] == '\r' && !ISSET(NO_CONVERT)) {
|
|
|
|
if (format < 2)
|
|
|
|
format += 2;
|
2004-11-15 21:49:21 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
/* Strip the carriage return. */
|
|
|
|
buf[--len] = '\0';
|
2004-11-05 15:25:53 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
/* Indicate we need to put a blank line in after this one. */
|
|
|
|
mac_line_needs_newline = TRUE;
|
2005-11-05 17:20:39 +00:00
|
|
|
}
|
2017-02-09 18:26:27 +00:00
|
|
|
#endif
|
|
|
|
/* Store the data of the final line. */
|
|
|
|
bottomline->data = encode_data(buf, len);
|
|
|
|
num_lines++;
|
2005-11-05 17:20:39 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
if (mac_line_needs_newline) {
|
|
|
|
bottomline->next = make_new_node(bottomline);
|
|
|
|
bottomline = bottomline->next;
|
|
|
|
bottomline->data = mallocstrcpy(NULL, "");
|
2005-11-05 17:20:39 +00:00
|
|
|
}
|
2005-11-09 03:37:50 +00:00
|
|
|
}
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
free(buf);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-02-09 18:26:27 +00:00
|
|
|
/* Insert the just read buffer into the current one. */
|
|
|
|
ingraft_buffer(topline);
|
2005-11-05 17:20:39 +00:00
|
|
|
|
2017-01-09 17:25:25 +00:00
|
|
|
/* Set the desired x position at the end of what was inserted. */
|
2005-11-05 17:20:39 +00:00
|
|
|
openfile->placewewant = xplustabs();
|
|
|
|
|
2017-04-22 16:27:01 +00:00
|
|
|
/* If we've read a help file, don't give any feedback. */
|
|
|
|
if (inhelp)
|
|
|
|
return;
|
|
|
|
|
2016-05-29 12:43:26 +00:00
|
|
|
if (!writable)
|
2016-11-30 16:20:30 +00:00
|
|
|
statusline(ALERT, _("File '%s' is unwritable"), filename);
|
2016-09-11 19:40:50 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-05-29 12:43:26 +00:00
|
|
|
else if (format == 3) {
|
2016-09-11 19:36:46 +00:00
|
|
|
/* TRANSLATORS: Keep the next four messages at most 78 characters. */
|
|
|
|
statusline(HUSH, P_("Read %lu line (Converted from DOS and Mac format)",
|
|
|
|
"Read %lu lines (Converted from DOS and Mac format)",
|
|
|
|
(unsigned long)num_lines), (unsigned long)num_lines);
|
2009-09-03 05:45:13 +00:00
|
|
|
} else if (format == 2) {
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->fmt = MAC_FILE;
|
2016-09-11 19:36:46 +00:00
|
|
|
statusline(HUSH, P_("Read %lu line (Converted from Mac format)",
|
|
|
|
"Read %lu lines (Converted from Mac format)",
|
|
|
|
(unsigned long)num_lines), (unsigned long)num_lines);
|
2005-07-08 02:47:05 +00:00
|
|
|
} else if (format == 1) {
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->fmt = DOS_FILE;
|
2016-09-11 19:36:46 +00:00
|
|
|
statusline(HUSH, P_("Read %lu line (Converted from DOS format)",
|
|
|
|
"Read %lu lines (Converted from DOS format)",
|
|
|
|
(unsigned long)num_lines), (unsigned long)num_lines);
|
2016-09-11 19:40:50 +00:00
|
|
|
}
|
2016-09-11 19:43:47 +00:00
|
|
|
#endif
|
2016-09-11 19:40:50 +00:00
|
|
|
else
|
2016-09-11 19:36:46 +00:00
|
|
|
statusline(HUSH, P_("Read %lu line", "Read %lu lines",
|
|
|
|
(unsigned long)num_lines), (unsigned long)num_lines);
|
2015-08-04 18:49:57 +00:00
|
|
|
|
2017-02-08 04:09:16 +00:00
|
|
|
/* If we inserted less than a screenful, don't center the cursor. */
|
2017-07-17 19:14:14 +00:00
|
|
|
if (undoable && less_than_a_screenful(was_lineno, was_leftedge))
|
2016-04-12 08:24:57 +00:00
|
|
|
focusing = FALSE;
|
|
|
|
|
2015-08-09 16:31:01 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-09-11 19:43:47 +00:00
|
|
|
if (undoable)
|
|
|
|
update_undo(INSERT);
|
|
|
|
|
2015-08-04 18:49:57 +00:00
|
|
|
if (ISSET(MAKE_IT_UNIX))
|
|
|
|
openfile->fmt = NIX_FILE;
|
2015-08-09 16:31:01 +00:00
|
|
|
#endif
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2002-05-11 03:04:44 +00:00
|
|
|
|
2016-11-26 16:41:31 +00:00
|
|
|
/* Open the file with the given name. If the file does not exist, display
|
|
|
|
* "New File" if newfie is TRUE, and say "File not found" otherwise.
|
2009-09-03 05:45:13 +00:00
|
|
|
* Return -2 if we say "New File", -1 if the file isn't opened, and the
|
2016-11-26 16:41:31 +00:00
|
|
|
* obtained fd otherwise. *f is set to the opened file. */
|
2015-01-20 06:15:34 +00:00
|
|
|
int open_file(const char *filename, bool newfie, bool quiet, FILE **f)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2009-01-26 08:48:22 +00:00
|
|
|
struct stat fileinfo, fileinfo2;
|
2015-01-20 06:15:34 +00:00
|
|
|
int fd;
|
2007-04-18 18:22:13 +00:00
|
|
|
char *full_filename;
|
2005-07-08 02:47:05 +00:00
|
|
|
|
2007-04-18 18:22:13 +00:00
|
|
|
/* Get the specified file's full path. */
|
|
|
|
full_filename = get_full_path(filename);
|
|
|
|
|
2009-01-26 08:48:22 +00:00
|
|
|
/* Okay, if we can't stat the path due to a component's
|
2015-06-27 15:10:58 +00:00
|
|
|
* permissions, just try the relative one. */
|
2015-08-29 20:14:57 +00:00
|
|
|
if (full_filename == NULL || (stat(full_filename, &fileinfo) == -1 &&
|
2016-09-11 19:36:46 +00:00
|
|
|
stat(filename, &fileinfo2) != -1))
|
2016-01-15 13:17:44 +00:00
|
|
|
full_filename = mallocstrcpy(full_filename, filename);
|
2007-04-18 18:22:13 +00:00
|
|
|
|
|
|
|
if (stat(full_filename, &fileinfo) == -1) {
|
2005-07-08 02:47:05 +00:00
|
|
|
if (newfie) {
|
2016-01-20 15:56:40 +00:00
|
|
|
if (!quiet)
|
2014-07-11 11:16:15 +00:00
|
|
|
statusbar(_("New File"));
|
2016-11-26 16:41:31 +00:00
|
|
|
free(full_filename);
|
2005-07-08 02:47:05 +00:00
|
|
|
return -2;
|
|
|
|
}
|
2016-11-26 16:41:31 +00:00
|
|
|
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("File \"%s\" not found"), filename);
|
2016-01-15 13:17:44 +00:00
|
|
|
free(full_filename);
|
2016-11-26 16:41:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-01-15 13:17:44 +00:00
|
|
|
|
2016-11-26 16:41:31 +00:00
|
|
|
/* Don't open directories, character files, or block files. */
|
|
|
|
if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) ||
|
|
|
|
S_ISBLK(fileinfo.st_mode)) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, S_ISDIR(fileinfo.st_mode) ?
|
2016-11-26 16:41:31 +00:00
|
|
|
_("\"%s\" is a directory") :
|
|
|
|
_("\"%s\" is a device file"), filename);
|
2016-01-15 13:17:44 +00:00
|
|
|
free(full_filename);
|
2014-03-17 21:36:37 +00:00
|
|
|
return -1;
|
2016-11-26 16:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try opening the file. */
|
|
|
|
fd = open(full_filename, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno));
|
|
|
|
else {
|
|
|
|
/* The file is A-OK. Associate a stream with it. */
|
2005-07-08 02:47:05 +00:00
|
|
|
*f = fdopen(fd, "rb");
|
2004-10-05 20:11:31 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
if (*f == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), filename, strerror(errno));
|
2005-07-08 02:47:05 +00:00
|
|
|
close(fd);
|
2017-04-22 16:27:01 +00:00
|
|
|
} else if (!inhelp)
|
2005-07-08 02:47:05 +00:00
|
|
|
statusbar(_("Reading File"));
|
2005-03-10 20:55:11 +00:00
|
|
|
}
|
2005-07-20 21:08:38 +00:00
|
|
|
|
2007-04-18 18:22:13 +00:00
|
|
|
free(full_filename);
|
|
|
|
|
2009-09-03 05:45:13 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
/* This function will return the name of the first available extension
|
|
|
|
* of a filename (starting with [name][suffix], then [name][suffix].1,
|
|
|
|
* etc.). Memory is allocated for the return value. If no writable
|
|
|
|
* extension exists, we return "". */
|
|
|
|
char *get_next_filename(const char *name, const char *suffix)
|
2001-07-11 02:08:33 +00:00
|
|
|
{
|
2005-07-08 02:47:05 +00:00
|
|
|
unsigned long i = 0;
|
|
|
|
char *buf;
|
2015-05-08 21:11:30 +00:00
|
|
|
size_t wholenamelen;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2015-05-08 21:11:30 +00:00
|
|
|
wholenamelen = strlen(name) + strlen(suffix);
|
2005-11-09 15:13:00 +00:00
|
|
|
|
2015-05-08 21:11:30 +00:00
|
|
|
/* Reserve space for: the name plus the suffix plus a dot plus
|
|
|
|
* possibly five digits plus a null byte. */
|
|
|
|
buf = charalloc(wholenamelen + 7);
|
2005-07-08 02:47:05 +00:00
|
|
|
sprintf(buf, "%s%s", name, suffix);
|
2005-01-27 20:49:07 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
while (TRUE) {
|
|
|
|
struct stat fs;
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
if (stat(buf, &fs) == -1)
|
|
|
|
return buf;
|
2015-05-08 21:11:30 +00:00
|
|
|
|
|
|
|
/* Limit the number of backup files to a hundred thousand. */
|
|
|
|
if (++i == 100000)
|
2005-07-08 02:47:05 +00:00
|
|
|
break;
|
2005-01-27 20:49:07 +00:00
|
|
|
|
2015-05-08 21:11:30 +00:00
|
|
|
sprintf(buf + wholenamelen, ".%lu", i);
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2017-03-20 12:01:37 +00:00
|
|
|
/* There is no possible save file: blank out the filename. */
|
|
|
|
*buf = '\0';
|
2005-01-27 20:49:07 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
return buf;
|
2002-07-19 01:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 18:06:45 +00:00
|
|
|
/* Insert a file into the current buffer, or into a new buffer when
|
|
|
|
* the MULTIBUFFER flag is set. */
|
|
|
|
void do_insertfile(void)
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char *msg;
|
2016-05-23 18:44:58 +00:00
|
|
|
char *given = mallocstrcpy(NULL, "");
|
2007-01-01 05:15:32 +00:00
|
|
|
/* The last answer the user typed at the statusbar prompt. */
|
2014-06-20 15:35:26 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-02-09 18:45:26 +00:00
|
|
|
bool execute = FALSE;
|
2007-08-16 02:34:23 +00:00
|
|
|
#endif
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2016-12-26 11:35:50 +00:00
|
|
|
/* Display newlines in filenames as ^J. */
|
2016-12-20 18:27:41 +00:00
|
|
|
as_an_at = FALSE;
|
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
while (TRUE) {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-08 02:47:05 +00:00
|
|
|
if (execute) {
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2016-08-26 10:18:04 +00:00
|
|
|
if (ISSET(MULTIBUFFER))
|
2017-03-24 11:19:30 +00:00
|
|
|
/* TRANSLATORS: The next four messages are prompts. */
|
2017-01-18 20:28:33 +00:00
|
|
|
msg = _("Command to execute in new buffer");
|
2016-08-26 10:18:04 +00:00
|
|
|
else
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2017-01-18 20:28:33 +00:00
|
|
|
msg = _("Command to execute");
|
2014-06-20 15:35:26 +00:00
|
|
|
} else
|
|
|
|
#endif /* NANO_TINY */
|
|
|
|
{
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-03-24 11:19:30 +00:00
|
|
|
if (ISSET(MULTIBUFFER))
|
2017-03-23 21:03:21 +00:00
|
|
|
msg = _("File to insert into new buffer [from %s]");
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
msg = _("File to insert [from %s]");
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2004-02-06 21:20:05 +00:00
|
|
|
|
2016-04-30 19:22:16 +00:00
|
|
|
present_path = mallocstrcpy(present_path, "./");
|
|
|
|
|
2017-01-02 20:12:44 +00:00
|
|
|
i = do_prompt(TRUE, TRUE,
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2008-03-05 07:34:01 +00:00
|
|
|
execute ? MEXTCMD :
|
2004-11-25 04:39:07 +00:00
|
|
|
#endif
|
2016-05-23 18:44:58 +00:00
|
|
|
MINSERTFILE, given,
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2005-07-08 02:47:05 +00:00
|
|
|
NULL,
|
2008-03-31 06:25:14 +00:00
|
|
|
#endif
|
2006-06-14 13:19:43 +00:00
|
|
|
edit_refresh, msg,
|
2008-03-16 14:24:28 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2017-03-23 21:03:21 +00:00
|
|
|
operating_dir != NULL ? operating_dir :
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2017-03-24 11:19:30 +00:00
|
|
|
"./");
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
/* If we're in multibuffer mode and the filename or command is
|
2016-12-20 20:41:37 +00:00
|
|
|
* blank, open a new buffer instead of canceling. */
|
|
|
|
if (i == -1 || (i == -2 && !ISSET(MULTIBUFFER))) {
|
2005-07-08 02:47:05 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
break;
|
|
|
|
} else {
|
2016-10-25 12:23:41 +00:00
|
|
|
ssize_t was_current_lineno = openfile->current->lineno;
|
|
|
|
size_t was_current_x = openfile->current_x;
|
2017-05-08 17:08:23 +00:00
|
|
|
#if !defined(NANO_TINY) || defined(ENABLE_BROWSER) || defined(ENABLE_MULTIBUFFER)
|
2014-07-27 21:07:15 +00:00
|
|
|
functionptrtype func = func_from_key(&i);
|
2015-12-05 11:38:26 +00:00
|
|
|
#endif
|
2016-05-23 18:44:58 +00:00
|
|
|
given = mallocstrcpy(given, answer);
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-05-08 12:23:47 +00:00
|
|
|
if (func == flip_newbuffer) {
|
2016-10-25 12:23:41 +00:00
|
|
|
/* Don't allow toggling when in view mode. */
|
2005-07-08 02:47:05 +00:00
|
|
|
if (!ISSET(VIEW_MODE))
|
|
|
|
TOGGLE(MULTIBUFFER);
|
2014-06-23 20:30:23 +00:00
|
|
|
else
|
|
|
|
beep();
|
2005-07-08 02:47:05 +00:00
|
|
|
continue;
|
2014-06-20 15:35:26 +00:00
|
|
|
}
|
2002-06-28 22:45:14 +00:00
|
|
|
#endif
|
2017-05-08 11:20:07 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-05-08 12:23:47 +00:00
|
|
|
if (func == flip_execute) {
|
2005-07-08 02:47:05 +00:00
|
|
|
execute = !execute;
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-08 11:20:07 +00:00
|
|
|
#endif
|
2017-05-08 17:08:23 +00:00
|
|
|
#ifdef ENABLE_BROWSER
|
2014-07-27 21:07:15 +00:00
|
|
|
if (func == to_files_void) {
|
2016-05-23 16:09:53 +00:00
|
|
|
char *chosen = do_browse_from(answer);
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2016-10-25 12:23:41 +00:00
|
|
|
/* If no file was chosen, go back to the prompt. */
|
2016-05-23 16:09:53 +00:00
|
|
|
if (chosen == NULL)
|
2005-07-08 02:47:05 +00:00
|
|
|
continue;
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
free(answer);
|
2016-05-23 16:09:53 +00:00
|
|
|
answer = chosen;
|
2005-07-08 02:47:05 +00:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-25 12:23:41 +00:00
|
|
|
/* If we don't have a file yet, go back to the prompt. */
|
|
|
|
if (i != 0 && (!ISSET(MULTIBUFFER) || i != -2))
|
2005-07-08 02:47:05 +00:00
|
|
|
continue;
|
2004-11-25 04:39:07 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2005-07-09 04:42:47 +00:00
|
|
|
if (execute) {
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2016-10-25 12:23:41 +00:00
|
|
|
/* When in multibuffer mode, first open a blank buffer. */
|
2005-07-09 04:42:47 +00:00
|
|
|
if (ISSET(MULTIBUFFER))
|
2008-08-03 04:48:05 +00:00
|
|
|
open_buffer("", FALSE);
|
2005-07-09 04:42:47 +00:00
|
|
|
#endif
|
|
|
|
/* Save the command's output in the current buffer. */
|
2005-07-08 02:47:05 +00:00
|
|
|
execute_command(answer);
|
2006-07-13 03:06:36 +00:00
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2016-10-25 12:23:41 +00:00
|
|
|
/* If this is a new buffer, put the cursor at the top. */
|
2006-07-13 03:06:36 +00:00
|
|
|
if (ISSET(MULTIBUFFER)) {
|
|
|
|
openfile->current = openfile->fileage;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
openfile->placewewant = 0;
|
2016-10-25 11:28:21 +00:00
|
|
|
|
|
|
|
set_modified();
|
2006-07-13 03:06:36 +00:00
|
|
|
}
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2015-07-10 17:25:51 +00:00
|
|
|
} else
|
2006-07-13 03:06:36 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2015-07-10 17:25:51 +00:00
|
|
|
{
|
2005-07-09 04:42:47 +00:00
|
|
|
/* Make sure the path to the file specified in answer is
|
|
|
|
* tilde-expanded. */
|
2016-07-13 13:04:40 +00:00
|
|
|
answer = free_and_assign(answer, real_dir_from_tilde(answer));
|
2005-07-09 04:42:47 +00:00
|
|
|
|
2015-07-10 17:25:51 +00:00
|
|
|
/* Save the file specified in answer in the current buffer. */
|
2008-08-03 04:48:05 +00:00
|
|
|
open_buffer(answer, TRUE);
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2001-10-22 03:15:31 +00:00
|
|
|
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2013-04-12 06:43:06 +00:00
|
|
|
if (ISSET(MULTIBUFFER)) {
|
2016-01-29 16:01:43 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
|
|
|
if (ISSET(POS_HISTORY)) {
|
|
|
|
ssize_t priorline, priorcol;
|
2014-06-20 16:33:12 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-01-29 16:01:43 +00:00
|
|
|
if (!execute)
|
2014-06-20 16:33:12 +00:00
|
|
|
#endif
|
2016-12-23 12:49:14 +00:00
|
|
|
if (has_old_position(answer, &priorline, &priorcol))
|
2016-01-29 16:01:43 +00:00
|
|
|
do_gotolinecolumn(priorline, priorcol, FALSE, FALSE);
|
|
|
|
}
|
|
|
|
#endif /* !DISABLE_HISTORIES */
|
2016-10-25 12:23:41 +00:00
|
|
|
/* Update stuff to account for the current buffer. */
|
2017-05-01 14:48:13 +00:00
|
|
|
prepare_for_display();
|
2013-04-12 06:43:06 +00:00
|
|
|
} else
|
2017-05-01 18:20:34 +00:00
|
|
|
#endif /* ENABLE_MULTIBUFFER */
|
2005-07-08 02:47:05 +00:00
|
|
|
{
|
2015-12-01 11:39:04 +00:00
|
|
|
/* Mark the file as modified if it changed. */
|
|
|
|
if (openfile->current->lineno != was_current_lineno ||
|
2015-12-01 11:43:13 +00:00
|
|
|
openfile->current_x != was_current_x)
|
2015-12-01 11:39:04 +00:00
|
|
|
set_modified();
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-04-17 09:27:29 +00:00
|
|
|
/* Update current_y to account for inserted lines. */
|
2017-05-11 20:24:39 +00:00
|
|
|
place_the_cursor(TRUE);
|
2017-01-06 03:38:00 +00:00
|
|
|
|
2016-10-25 12:23:41 +00:00
|
|
|
refresh_needed = TRUE;
|
2005-07-08 02:47:05 +00:00
|
|
|
}
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2005-07-08 02:47:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 18:44:58 +00:00
|
|
|
|
|
|
|
free(given);
|
2002-02-15 19:17:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 14:04:51 +00:00
|
|
|
/* If the current mode of operation allows it, go insert a file. */
|
2005-07-08 02:47:05 +00:00
|
|
|
void do_insertfile_void(void)
|
2001-07-11 02:08:33 +00:00
|
|
|
{
|
2017-01-01 14:04:51 +00:00
|
|
|
if (ISSET(RESTRICTED))
|
2015-07-30 18:10:16 +00:00
|
|
|
show_restricted_warning();
|
2017-05-01 18:20:34 +00:00
|
|
|
#ifdef ENABLE_MULTIBUFFER
|
2017-01-01 14:04:51 +00:00
|
|
|
else if (ISSET(VIEW_MODE) && !ISSET(MULTIBUFFER))
|
2006-07-31 01:30:31 +00:00
|
|
|
statusbar(_("Key invalid in non-multibuffer mode"));
|
2005-07-08 02:47:05 +00:00
|
|
|
#endif
|
2017-01-01 14:04:51 +00:00
|
|
|
else
|
2016-10-23 18:06:45 +00:00
|
|
|
do_insertfile();
|
2001-07-11 02:08:33 +00:00
|
|
|
}
|
|
|
|
|
2005-02-09 18:56:21 +00:00
|
|
|
/* When passed "[relative path]" or "[relative path][filename]" in
|
2001-09-19 03:19:43 +00:00
|
|
|
* origpath, return "[full path]" or "[full path][filename]" on success,
|
2005-02-09 18:56:21 +00:00
|
|
|
* or NULL on error. Do this if the file doesn't exist but the relative
|
|
|
|
* path does, since the file could exist in memory but not yet on disk).
|
|
|
|
* Don't do this if the relative path doesn't exist, since we won't be
|
|
|
|
* able to go there. */
|
2002-09-13 18:14:04 +00:00
|
|
|
char *get_full_path(const char *origpath)
|
2001-07-11 02:08:33 +00:00
|
|
|
{
|
2016-04-15 10:27:48 +00:00
|
|
|
int attempts = 0;
|
2016-04-24 19:24:17 +00:00
|
|
|
/* How often we've tried climbing back up the tree. */
|
2006-07-05 14:14:06 +00:00
|
|
|
struct stat fileinfo;
|
2016-04-15 11:26:26 +00:00
|
|
|
char *currentdir, *d_here, *d_there, *d_there_file = NULL;
|
2017-03-20 12:01:37 +00:00
|
|
|
char *last_slash;
|
2006-07-05 14:14:06 +00:00
|
|
|
bool path_only;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2005-02-09 18:56:21 +00:00
|
|
|
if (origpath == NULL)
|
2014-05-13 20:14:01 +00:00
|
|
|
return NULL;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
/* Get the current directory. If it doesn't exist, back up and try
|
2006-07-05 15:21:18 +00:00
|
|
|
* again until we get a directory that does, and use that as the
|
|
|
|
* current directory. */
|
2016-04-15 11:26:26 +00:00
|
|
|
currentdir = charalloc(PATH_MAX + 1);
|
|
|
|
d_here = getcwd(currentdir, PATH_MAX + 1);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2016-04-15 10:27:48 +00:00
|
|
|
while (d_here == NULL && attempts < 20) {
|
|
|
|
IGNORE_CALL_RESULT(chdir(".."));
|
2016-05-21 19:32:47 +00:00
|
|
|
d_here = getcwd(currentdir, PATH_MAX + 1);
|
2016-04-15 10:27:48 +00:00
|
|
|
attempts++;
|
2006-07-05 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we succeeded, canonicalize it in d_here. */
|
|
|
|
if (d_here != NULL) {
|
2005-02-09 18:56:21 +00:00
|
|
|
/* If the current directory isn't "/", tack a slash onto the end
|
|
|
|
* of it. */
|
2004-07-31 14:10:23 +00:00
|
|
|
if (strcmp(d_here, "/") != 0) {
|
2003-06-14 20:41:34 +00:00
|
|
|
d_here = charealloc(d_here, strlen(d_here) + 2);
|
2001-09-23 01:18:03 +00:00
|
|
|
strcat(d_here, "/");
|
2016-12-16 20:28:28 +00:00
|
|
|
}
|
2006-07-05 14:14:06 +00:00
|
|
|
/* Otherwise, set d_here to "". */
|
2016-04-15 11:26:26 +00:00
|
|
|
} else {
|
2006-07-05 14:14:06 +00:00
|
|
|
d_here = mallocstrcpy(NULL, "");
|
2016-04-15 11:26:26 +00:00
|
|
|
free(currentdir);
|
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
d_there = real_dir_from_tilde(origpath);
|
2005-02-09 18:56:21 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
/* If stat()ing d_there fails, assume that d_there refers to a new
|
|
|
|
* file that hasn't been saved to disk yet. Set path_only to TRUE
|
|
|
|
* if d_there refers to a directory, and FALSE otherwise. */
|
2015-08-29 20:14:57 +00:00
|
|
|
path_only = (stat(d_there, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode));
|
2005-02-09 18:56:21 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
/* If path_only is TRUE, make sure d_there ends in a slash. */
|
|
|
|
if (path_only) {
|
|
|
|
size_t d_there_len = strlen(d_there);
|
2005-02-09 18:56:21 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
if (d_there[d_there_len - 1] != '/') {
|
|
|
|
d_there = charealloc(d_there, d_there_len + 2);
|
|
|
|
strcat(d_there, "/");
|
2001-09-19 03:19:43 +00:00
|
|
|
}
|
2006-07-05 14:14:06 +00:00
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
/* Search for the last slash in d_there. */
|
|
|
|
last_slash = strrchr(d_there, '/');
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
/* If we didn't find one, then make sure the answer is in the format
|
|
|
|
* "d_here/d_there". */
|
|
|
|
if (last_slash == NULL) {
|
|
|
|
assert(!path_only);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2006-07-05 14:14:06 +00:00
|
|
|
d_there_file = d_there;
|
|
|
|
d_there = d_here;
|
|
|
|
} else {
|
|
|
|
/* If path_only is FALSE, then save the filename portion of the
|
|
|
|
* answer (everything after the last slash) in d_there_file. */
|
|
|
|
if (!path_only)
|
|
|
|
d_there_file = mallocstrcpy(NULL, last_slash + 1);
|
|
|
|
|
2006-11-08 02:48:15 +00:00
|
|
|
/* Remove the filename portion of the answer from d_there. */
|
2017-03-20 12:01:37 +00:00
|
|
|
*(last_slash + 1) = '\0';
|
2006-07-05 14:14:06 +00:00
|
|
|
|
2006-07-05 15:21:18 +00:00
|
|
|
/* Go to the path specified in d_there. */
|
2006-07-05 14:14:06 +00:00
|
|
|
if (chdir(d_there) == -1) {
|
|
|
|
free(d_there);
|
|
|
|
d_there = NULL;
|
2005-02-09 18:56:21 +00:00
|
|
|
} else {
|
2006-07-05 14:14:06 +00:00
|
|
|
free(d_there);
|
|
|
|
|
|
|
|
/* Get the full path. */
|
2016-04-15 11:26:26 +00:00
|
|
|
currentdir = charalloc(PATH_MAX + 1);
|
|
|
|
d_there = getcwd(currentdir, PATH_MAX + 1);
|
2006-07-05 14:14:06 +00:00
|
|
|
|
|
|
|
/* If we succeeded, canonicalize it in d_there. */
|
|
|
|
if (d_there != NULL) {
|
|
|
|
/* If the current directory isn't "/", tack a slash onto
|
|
|
|
* the end of it. */
|
|
|
|
if (strcmp(d_there, "/") != 0) {
|
|
|
|
d_there = charealloc(d_there, strlen(d_there) + 2);
|
|
|
|
strcat(d_there, "/");
|
2016-12-16 20:28:28 +00:00
|
|
|
}
|
2016-04-15 11:26:26 +00:00
|
|
|
/* Otherwise, make sure that we return NULL. */
|
|
|
|
} else {
|
2006-07-05 14:14:06 +00:00
|
|
|
path_only = TRUE;
|
2016-04-15 11:26:26 +00:00
|
|
|
free(currentdir);
|
|
|
|
}
|
2006-07-05 14:14:06 +00:00
|
|
|
|
|
|
|
/* Finally, go back to the path specified in d_here,
|
|
|
|
* where we were before. We don't check for a chdir()
|
2006-11-08 02:48:15 +00:00
|
|
|
* error, since we can do nothing if we get one. */
|
2009-12-02 03:24:18 +00:00
|
|
|
IGNORE_CALL_RESULT(chdir(d_here));
|
2001-09-19 03:19:43 +00:00
|
|
|
}
|
2016-02-10 12:32:43 +00:00
|
|
|
|
|
|
|
/* Free d_here, since we're done using it. */
|
|
|
|
free(d_here);
|
2006-11-08 02:48:15 +00:00
|
|
|
}
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2006-11-08 02:48:15 +00:00
|
|
|
/* At this point, if path_only is FALSE and d_there isn't NULL,
|
|
|
|
* d_there contains the path portion of the answer and d_there_file
|
|
|
|
* contains the filename portion of the answer. If this is the
|
|
|
|
* case, tack the latter onto the end of the former. d_there will
|
|
|
|
* then contain the complete answer. */
|
|
|
|
if (!path_only && d_there != NULL) {
|
|
|
|
d_there = charealloc(d_there, strlen(d_there) +
|
2005-02-09 18:56:21 +00:00
|
|
|
strlen(d_there_file) + 1);
|
2006-11-08 02:48:15 +00:00
|
|
|
strcat(d_there, d_there_file);
|
|
|
|
}
|
2005-02-09 18:56:21 +00:00
|
|
|
|
2006-11-08 02:48:15 +00:00
|
|
|
/* Free d_there_file, since we're done using it. */
|
2015-06-14 19:14:41 +00:00
|
|
|
free(d_there_file);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2005-02-09 18:56:21 +00:00
|
|
|
return d_there;
|
2001-07-11 02:08:33 +00:00
|
|
|
}
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2005-02-09 18:56:21 +00:00
|
|
|
/* Return the full version of path, as returned by get_full_path(). On
|
2015-08-29 20:14:57 +00:00
|
|
|
* error, or if path doesn't reference a directory, or if the directory
|
2005-02-09 18:56:21 +00:00
|
|
|
* isn't writable, return NULL. */
|
2002-09-13 18:14:04 +00:00
|
|
|
char *check_writable_directory(const char *path)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2002-02-15 19:17:02 +00:00
|
|
|
char *full_path = get_full_path(path);
|
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
if (full_path == NULL)
|
2002-02-15 19:17:02 +00:00
|
|
|
return NULL;
|
2002-02-16 20:03:44 +00:00
|
|
|
|
2015-08-29 20:14:57 +00:00
|
|
|
/* If we can't write to path or path isn't a directory, return NULL. */
|
2005-02-09 18:56:21 +00:00
|
|
|
if (access(full_path, W_OK) != 0 ||
|
2015-08-29 20:14:57 +00:00
|
|
|
full_path[strlen(full_path) - 1] != '/') {
|
2002-02-16 20:03:44 +00:00
|
|
|
free(full_path);
|
2002-02-15 19:17:02 +00:00
|
|
|
return NULL;
|
2002-02-16 20:03:44 +00:00
|
|
|
}
|
2002-02-15 19:17:02 +00:00
|
|
|
|
|
|
|
return full_path;
|
|
|
|
}
|
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
/* This function calls mkstemp(($TMPDIR|P_tmpdir|/tmp/)"nano.XXXXXX").
|
|
|
|
* On success, it returns the malloc()ed filename and corresponding FILE
|
2005-06-05 19:18:11 +00:00
|
|
|
* stream, opened in "r+b" mode. On error, it returns NULL for the
|
2005-05-31 04:28:15 +00:00
|
|
|
* filename and leaves the FILE stream unchanged. */
|
|
|
|
char *safe_tempfile(FILE **f)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2002-09-13 18:14:04 +00:00
|
|
|
char *full_tempdir = NULL;
|
2005-06-05 19:33:27 +00:00
|
|
|
const char *tmpdir_env;
|
|
|
|
int fd;
|
|
|
|
mode_t original_umask = 0;
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2006-04-06 21:41:48 +00:00
|
|
|
/* If $TMPDIR is set, set tempdir to it, run it through
|
|
|
|
* get_full_path(), and save the result in full_tempdir. Otherwise,
|
|
|
|
* leave full_tempdir set to NULL. */
|
2005-06-05 19:33:27 +00:00
|
|
|
tmpdir_env = getenv("TMPDIR");
|
2006-04-06 21:41:48 +00:00
|
|
|
if (tmpdir_env != NULL)
|
2005-06-05 19:33:27 +00:00
|
|
|
full_tempdir = check_writable_directory(tmpdir_env);
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2005-02-11 20:09:11 +00:00
|
|
|
/* If $TMPDIR is unset, empty, or not a writable directory, and
|
|
|
|
* full_tempdir is NULL, try P_tmpdir instead. */
|
2014-04-14 09:57:06 +00:00
|
|
|
if (full_tempdir == NULL)
|
2002-09-13 18:14:04 +00:00
|
|
|
full_tempdir = check_writable_directory(P_tmpdir);
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2014-04-14 09:57:06 +00:00
|
|
|
/* if P_tmpdir is NULL, use /tmp. */
|
|
|
|
if (full_tempdir == NULL)
|
2005-02-11 20:09:11 +00:00
|
|
|
full_tempdir = mallocstrcpy(NULL, "/tmp/");
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2003-06-14 20:41:34 +00:00
|
|
|
full_tempdir = charealloc(full_tempdir, strlen(full_tempdir) + 12);
|
2005-02-11 20:09:11 +00:00
|
|
|
strcat(full_tempdir, "nano.XXXXXX");
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2005-06-05 19:33:27 +00:00
|
|
|
original_umask = umask(0);
|
|
|
|
umask(S_IRWXG | S_IRWXO);
|
|
|
|
|
|
|
|
fd = mkstemp(full_tempdir);
|
|
|
|
|
|
|
|
if (fd != -1)
|
|
|
|
*f = fdopen(fd, "r+b");
|
2005-05-31 04:28:15 +00:00
|
|
|
else {
|
|
|
|
free(full_tempdir);
|
|
|
|
full_tempdir = NULL;
|
2002-02-15 19:17:02 +00:00
|
|
|
}
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2005-06-05 19:33:27 +00:00
|
|
|
umask(original_umask);
|
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
return full_tempdir;
|
2002-02-15 19:17:02 +00:00
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2002-09-13 18:14:04 +00:00
|
|
|
/* Initialize full_operating_dir based on operating_dir. */
|
|
|
|
void init_operating_dir(void)
|
|
|
|
{
|
2002-12-22 16:30:00 +00:00
|
|
|
if (operating_dir == NULL)
|
2002-09-13 18:14:04 +00:00
|
|
|
return;
|
2004-02-27 20:50:01 +00:00
|
|
|
|
2002-09-13 18:14:04 +00:00
|
|
|
full_operating_dir = get_full_path(operating_dir);
|
|
|
|
|
2016-06-12 09:13:45 +00:00
|
|
|
/* If the operating directory is inaccessible, fail. */
|
|
|
|
if (full_operating_dir == NULL || chdir(full_operating_dir) == -1)
|
2017-03-23 21:08:57 +00:00
|
|
|
die(_("Invalid operating directory\n"));
|
2016-12-16 20:28:28 +00:00
|
|
|
|
2016-12-18 10:12:04 +00:00
|
|
|
snuggly_fit(&full_operating_dir);
|
2002-09-13 18:14:04 +00:00
|
|
|
}
|
|
|
|
|
2005-02-11 20:09:11 +00:00
|
|
|
/* Check to see if we're inside the operating directory. Return FALSE
|
|
|
|
* if we are, or TRUE otherwise. If allow_tabcomp is TRUE, allow
|
|
|
|
* incomplete names that would be matches for the operating directory,
|
|
|
|
* so that tab completion will work. */
|
|
|
|
bool check_operating_dir(const char *currpath, bool allow_tabcomp)
|
2001-09-19 03:19:43 +00:00
|
|
|
{
|
2006-11-07 21:08:17 +00:00
|
|
|
/* full_operating_dir is global for memory cleanup. It should have
|
|
|
|
* already been initialized by init_operating_dir(). Also, a
|
|
|
|
* relative operating directory path will only be handled properly
|
|
|
|
* if this is done. */
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2002-09-13 18:14:04 +00:00
|
|
|
char *fullpath;
|
2005-02-11 20:09:11 +00:00
|
|
|
bool retval = FALSE;
|
2002-09-13 18:14:04 +00:00
|
|
|
const char *whereami1, *whereami2 = NULL;
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
/* If no operating directory is set, don't bother doing anything. */
|
2002-12-22 16:30:00 +00:00
|
|
|
if (operating_dir == NULL)
|
2005-02-11 20:09:11 +00:00
|
|
|
return FALSE;
|
2004-02-28 02:08:15 +00:00
|
|
|
|
2001-09-19 03:19:43 +00:00
|
|
|
fullpath = get_full_path(currpath);
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2006-09-21 01:48:54 +00:00
|
|
|
/* If fullpath is NULL, it means some directory in the path doesn't
|
|
|
|
* exist or is unreadable. If allow_tabcomp is FALSE, then currpath
|
|
|
|
* is what the user typed somewhere. We don't want to report a
|
2003-12-24 08:03:54 +00:00
|
|
|
* non-existent directory as being outside the operating directory,
|
2005-02-11 20:09:11 +00:00
|
|
|
* so we return FALSE. If allow_tabcomp is TRUE, then currpath
|
2003-12-24 08:03:54 +00:00
|
|
|
* exists, but is not executable. So we say it isn't in the
|
|
|
|
* operating directory. */
|
2002-12-22 16:30:00 +00:00
|
|
|
if (fullpath == NULL)
|
2003-12-24 08:03:54 +00:00
|
|
|
return allow_tabcomp;
|
2001-09-19 03:19:43 +00:00
|
|
|
|
|
|
|
whereami1 = strstr(fullpath, full_operating_dir);
|
|
|
|
if (allow_tabcomp)
|
|
|
|
whereami2 = strstr(full_operating_dir, fullpath);
|
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
/* If both searches failed, we're outside the operating directory.
|
2005-07-08 20:09:16 +00:00
|
|
|
* Otherwise, check the search results. If the full operating
|
2003-12-24 08:03:54 +00:00
|
|
|
* directory path is not at the beginning of the full current path
|
|
|
|
* (for normal usage) and vice versa (for tab completion, if we're
|
|
|
|
* allowing it), we're outside the operating directory. */
|
2001-09-19 03:19:43 +00:00
|
|
|
if (whereami1 != fullpath && whereami2 != full_operating_dir)
|
2005-02-11 20:09:11 +00:00
|
|
|
retval = TRUE;
|
|
|
|
free(fullpath);
|
2003-12-24 08:03:54 +00:00
|
|
|
|
|
|
|
/* Otherwise, we're still inside it. */
|
2002-09-13 18:14:04 +00:00
|
|
|
return retval;
|
2001-09-19 03:19:43 +00:00
|
|
|
}
|
2001-07-11 02:08:33 +00:00
|
|
|
#endif
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-04-08 12:35:18 +00:00
|
|
|
/* Although this sucks, it sucks less than having a single 'my system is
|
|
|
|
* messed up and I'm blanket allowing insecure file writing operations'. */
|
2011-02-07 02:06:20 +00:00
|
|
|
int prompt_failed_backupwrite(const char *filename)
|
|
|
|
{
|
2016-05-24 08:23:02 +00:00
|
|
|
static int response;
|
2014-04-08 12:35:18 +00:00
|
|
|
static char *prevfile = NULL; /* What was the last file we were
|
2015-06-27 15:10:58 +00:00
|
|
|
* passed so we don't keep asking
|
|
|
|
* this? Though maybe we should... */
|
2011-02-07 02:06:20 +00:00
|
|
|
if (prevfile == NULL || strcmp(filename, prevfile)) {
|
2016-05-24 08:23:02 +00:00
|
|
|
response = do_yesno_prompt(FALSE, _("Failed to write backup file; "
|
|
|
|
"continue saving? (Say N if unsure.) "));
|
2011-02-07 02:06:20 +00:00
|
|
|
prevfile = mallocstrcpy(prevfile, filename);
|
|
|
|
}
|
2016-05-24 08:23:02 +00:00
|
|
|
|
|
|
|
return response;
|
2011-02-07 02:06:20 +00:00
|
|
|
}
|
|
|
|
|
2004-02-28 16:24:31 +00:00
|
|
|
void init_backup_dir(void)
|
|
|
|
{
|
|
|
|
char *full_backup_dir;
|
|
|
|
|
|
|
|
if (backup_dir == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
full_backup_dir = get_full_path(backup_dir);
|
|
|
|
|
|
|
|
/* If get_full_path() failed or the backup directory is
|
|
|
|
* inaccessible, unset backup_dir. */
|
|
|
|
if (full_backup_dir == NULL ||
|
2016-04-17 18:58:33 +00:00
|
|
|
full_backup_dir[strlen(full_backup_dir) - 1] != '/') {
|
2004-02-28 16:24:31 +00:00
|
|
|
free(full_backup_dir);
|
|
|
|
free(backup_dir);
|
|
|
|
backup_dir = NULL;
|
|
|
|
} else {
|
|
|
|
free(backup_dir);
|
|
|
|
backup_dir = full_backup_dir;
|
2016-12-18 10:12:04 +00:00
|
|
|
snuggly_fit(&backup_dir);
|
2004-02-28 16:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2004-02-28 16:24:31 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
/* Read from inn, write to out. We assume inn is opened for reading,
|
2005-07-08 20:09:16 +00:00
|
|
|
* and out for writing. We return 0 on success, -1 on read error, or -2
|
2017-04-04 07:29:31 +00:00
|
|
|
* on write error. inn is always closed by this function, out is closed
|
|
|
|
* only if close_out is true. */
|
|
|
|
int copy_file(FILE *inn, FILE *out, bool close_out)
|
2003-12-24 08:03:54 +00:00
|
|
|
{
|
2007-07-12 03:12:31 +00:00
|
|
|
int retval = 0;
|
2005-05-28 23:51:03 +00:00
|
|
|
char buf[BUFSIZ];
|
2003-12-24 08:03:54 +00:00
|
|
|
size_t charsread;
|
2017-04-04 07:29:31 +00:00
|
|
|
int (*flush_out_fnc)(FILE *) = (close_out) ? fclose : fflush;
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2007-07-12 03:12:31 +00:00
|
|
|
assert(inn != NULL && out != NULL && inn != out);
|
2005-04-19 20:13:13 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
do {
|
|
|
|
charsread = fread(buf, sizeof(char), BUFSIZ, inn);
|
|
|
|
if (charsread == 0 && ferror(inn)) {
|
|
|
|
retval = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fwrite(buf, sizeof(char), charsread, out) < charsread) {
|
|
|
|
retval = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (charsread > 0);
|
2005-04-19 20:13:13 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
if (fclose(inn) == EOF)
|
|
|
|
retval = -1;
|
2017-04-04 07:29:31 +00:00
|
|
|
if (flush_out_fnc(out) == EOF)
|
2003-12-24 08:03:54 +00:00
|
|
|
retval = -2;
|
2005-04-19 20:13:13 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-01-11 21:47:06 +00:00
|
|
|
/* Write a file out to disk. If f_open isn't NULL, we assume that it is
|
|
|
|
* a stream associated with the file, and we don't try to open it
|
2005-05-31 04:28:15 +00:00
|
|
|
* ourselves. If tmp is TRUE, we set the umask to disallow anyone else
|
2005-07-08 20:09:16 +00:00
|
|
|
* from accessing the file, we don't set the filename to its name, and
|
|
|
|
* we don't print out how many lines we wrote on the statusbar.
|
2003-01-13 01:35:15 +00:00
|
|
|
*
|
2003-12-24 08:03:54 +00:00
|
|
|
* tmp means we are writing a temporary file in a secure fashion. We
|
2007-01-11 21:47:06 +00:00
|
|
|
* use it when spell checking or dumping the file on an error. If
|
2016-07-15 10:59:59 +00:00
|
|
|
* method is APPEND, it means we are appending instead of overwriting.
|
|
|
|
* If method is PREPEND, it means we are prepending instead of
|
2007-01-11 21:47:06 +00:00
|
|
|
* overwriting. If nonamechange is TRUE, we don't change the current
|
2016-07-15 11:03:42 +00:00
|
|
|
* filename. nonamechange is irrelevant when appending or prepending,
|
|
|
|
* or when writing a temporary file.
|
2003-12-24 08:03:54 +00:00
|
|
|
*
|
2007-01-11 21:36:29 +00:00
|
|
|
* Return TRUE on success or FALSE on error. */
|
2016-07-15 10:59:59 +00:00
|
|
|
bool write_file(const char *name, FILE *f_open, bool tmp,
|
|
|
|
kind_of_writing_type method, bool nonamechange)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2007-01-11 21:36:29 +00:00
|
|
|
bool retval = FALSE;
|
2002-09-13 18:14:04 +00:00
|
|
|
/* Instead of returning in this function, you should always
|
2007-01-11 22:54:55 +00:00
|
|
|
* set retval and then goto cleanup_and_exit. */
|
2006-11-27 05:19:24 +00:00
|
|
|
size_t lineswritten = 0;
|
2005-07-08 20:09:16 +00:00
|
|
|
const filestruct *fileptr = openfile->fileage;
|
2002-04-22 23:52:34 +00:00
|
|
|
int fd;
|
2004-07-07 23:26:08 +00:00
|
|
|
/* The file descriptor we use. */
|
2004-07-06 14:02:44 +00:00
|
|
|
mode_t original_umask = 0;
|
2003-12-24 08:03:54 +00:00
|
|
|
/* Our umask, from when nano started. */
|
2014-06-09 20:26:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2004-11-06 15:10:57 +00:00
|
|
|
bool realexists;
|
2004-07-03 03:22:23 +00:00
|
|
|
/* The result of stat(). TRUE if the file exists, FALSE
|
2003-12-24 08:03:54 +00:00
|
|
|
* otherwise. If name is a link that points nowhere, realexists
|
2004-07-03 03:22:23 +00:00
|
|
|
* is FALSE. */
|
2016-02-07 13:03:48 +00:00
|
|
|
#endif
|
2003-12-24 08:03:54 +00:00
|
|
|
struct stat st;
|
|
|
|
/* The status fields filled in by stat(). */
|
|
|
|
char *realname;
|
2016-04-24 19:24:17 +00:00
|
|
|
/* The filename after tilde expansion. */
|
2016-02-07 13:08:27 +00:00
|
|
|
FILE *f = f_open;
|
2003-12-24 08:03:54 +00:00
|
|
|
/* The actual file, realname, we are writing to. */
|
|
|
|
char *tempname = NULL;
|
2015-08-11 17:43:08 +00:00
|
|
|
/* The name of the temporary file we write to on prepend. */
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2007-07-01 21:33:17 +00:00
|
|
|
if (*name == '\0')
|
2000-06-19 04:22:15 +00:00
|
|
|
return -1;
|
2005-04-15 17:48:20 +00:00
|
|
|
|
2003-02-03 03:32:08 +00:00
|
|
|
if (!tmp)
|
|
|
|
titlebar(NULL);
|
2000-11-24 14:02:57 +00:00
|
|
|
|
2006-11-27 05:40:09 +00:00
|
|
|
realname = real_dir_from_tilde(name);
|
2006-11-27 05:03:54 +00:00
|
|
|
|
2001-09-19 03:19:43 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
2002-09-13 18:14:04 +00:00
|
|
|
/* If we're writing a temporary file, we're probably going outside
|
2003-12-24 08:03:54 +00:00
|
|
|
* the operating directory, so skip the operating directory test. */
|
2005-02-11 20:09:11 +00:00
|
|
|
if (!tmp && check_operating_dir(realname, FALSE)) {
|
2016-05-16 19:18:09 +00:00
|
|
|
statusline(ALERT, _("Can't write outside of %s"), full_operating_dir);
|
2002-09-13 18:14:04 +00:00
|
|
|
goto cleanup_and_exit;
|
2001-09-19 03:19:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
/* If the temp file exists and isn't already open, give up. */
|
2016-02-07 13:03:48 +00:00
|
|
|
if (tmp && (lstat(realname, &st) != -1) && f_open == NULL)
|
2003-12-24 08:03:54 +00:00
|
|
|
goto cleanup_and_exit;
|
2004-11-26 20:17:49 +00:00
|
|
|
|
2014-06-09 20:26:54 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-05-13 20:14:01 +00:00
|
|
|
/* Check whether the file (at the end of the symlink) exists. */
|
2004-11-06 15:10:57 +00:00
|
|
|
realexists = (stat(realname, &st) != -1);
|
2000-12-02 21:13:50 +00:00
|
|
|
|
2014-05-13 20:14:01 +00:00
|
|
|
/* If we haven't stat()d this file before (say, the user just
|
|
|
|
* specified it interactively), stat and save the value now,
|
|
|
|
* or else we will chase null pointers when we do modtime checks,
|
|
|
|
* preserve file times, and so on, during backup. */
|
2016-02-09 20:53:11 +00:00
|
|
|
if (openfile->current_stat == NULL && !tmp && realexists)
|
|
|
|
stat_with_alloc(realname, &openfile->current_stat);
|
2009-12-20 05:46:35 +00:00
|
|
|
|
2002-06-28 22:45:14 +00:00
|
|
|
/* We backup only if the backup toggle is set, the file isn't
|
2003-12-24 08:03:54 +00:00
|
|
|
* temporary, and the file already exists. Furthermore, if we
|
|
|
|
* aren't appending, prepending, or writing a selection, we backup
|
|
|
|
* only if the file has not been modified by someone else since nano
|
|
|
|
* opened it. */
|
2016-05-21 11:50:14 +00:00
|
|
|
if (ISSET(BACKUP_FILE) && !tmp && realexists && openfile->current_stat &&
|
2016-07-15 10:59:59 +00:00
|
|
|
(method != OVERWRITE || openfile->mark_set ||
|
2016-05-21 11:50:14 +00:00
|
|
|
openfile->current_stat->st_mtime == st.st_mtime)) {
|
2010-04-09 15:01:51 +00:00
|
|
|
int backup_fd;
|
2002-06-28 22:45:14 +00:00
|
|
|
FILE *backup_file;
|
2003-12-24 08:03:54 +00:00
|
|
|
char *backupname;
|
2017-04-04 07:29:31 +00:00
|
|
|
static struct timespec filetime[2];
|
2013-01-03 04:23:10 +00:00
|
|
|
int backup_cflags;
|
2002-06-28 22:45:14 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
/* Save the original file's access and modification times. */
|
2017-04-04 07:29:31 +00:00
|
|
|
filetime[0].tv_sec = openfile->current_stat->st_atime;
|
|
|
|
filetime[1].tv_sec = openfile->current_stat->st_mtime;
|
2002-06-28 22:45:14 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (f_open == NULL) {
|
|
|
|
/* Open the original file to copy to the backup. */
|
|
|
|
f = fopen(realname, "rb");
|
2005-04-19 21:47:01 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (f == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), realname,
|
2005-05-31 04:28:15 +00:00
|
|
|
strerror(errno));
|
2006-04-07 04:37:14 +00:00
|
|
|
/* If we can't read from the original file, go on, since
|
|
|
|
* only saving the original file is better than saving
|
|
|
|
* nothing. */
|
|
|
|
goto skip_backup;
|
2005-05-31 04:28:15 +00:00
|
|
|
}
|
2002-06-28 22:45:14 +00:00
|
|
|
}
|
|
|
|
|
2004-02-28 16:24:31 +00:00
|
|
|
/* If backup_dir is set, we set backupname to
|
2005-05-29 02:22:55 +00:00
|
|
|
* backup_dir/backupname~[.number], where backupname is the
|
|
|
|
* canonicalized absolute pathname of realname with every '/'
|
|
|
|
* replaced with a '!'. This means that /home/foo/file is
|
|
|
|
* backed up in backup_dir/!home!foo!file~[.number]. */
|
2004-02-28 16:24:31 +00:00
|
|
|
if (backup_dir != NULL) {
|
2005-05-29 02:22:55 +00:00
|
|
|
char *backuptemp = get_full_path(realname);
|
2004-02-28 16:24:31 +00:00
|
|
|
|
2005-05-29 02:22:55 +00:00
|
|
|
if (backuptemp == NULL)
|
2004-02-28 16:24:31 +00:00
|
|
|
/* If get_full_path() failed, we don't have a
|
|
|
|
* canonicalized absolute pathname, so just use the
|
|
|
|
* filename portion of the pathname. We use tail() so
|
|
|
|
* that e.g. ../backupname will be backed up in
|
|
|
|
* backupdir/backupname~ instead of
|
|
|
|
* backupdir/../backupname~. */
|
2005-05-29 02:22:55 +00:00
|
|
|
backuptemp = mallocstrcpy(NULL, tail(realname));
|
2004-02-28 16:24:31 +00:00
|
|
|
else {
|
2005-05-28 23:21:30 +00:00
|
|
|
size_t i = 0;
|
|
|
|
|
2005-05-29 02:22:55 +00:00
|
|
|
for (; backuptemp[i] != '\0'; i++) {
|
|
|
|
if (backuptemp[i] == '/')
|
|
|
|
backuptemp[i] = '!';
|
2004-02-28 16:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 18:58:33 +00:00
|
|
|
backupname = charalloc(strlen(backup_dir) + strlen(backuptemp) + 1);
|
2005-05-29 02:22:55 +00:00
|
|
|
sprintf(backupname, "%s%s", backup_dir, backuptemp);
|
|
|
|
free(backuptemp);
|
|
|
|
backuptemp = get_next_filename(backupname, "~");
|
2007-07-01 21:33:17 +00:00
|
|
|
if (*backuptemp == '\0') {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, _("Too many backup files?"));
|
2005-05-29 02:22:55 +00:00
|
|
|
free(backuptemp);
|
|
|
|
free(backupname);
|
2014-04-08 18:38:45 +00:00
|
|
|
/* If we can't write to the backup, DON'T go on, since
|
|
|
|
* whatever caused the backup file to fail (e.g. disk
|
|
|
|
* full may well cause the real file write to fail,
|
|
|
|
* which means we could lose both the backup and the
|
|
|
|
* original! */
|
2008-08-09 03:39:10 +00:00
|
|
|
goto cleanup_and_exit;
|
2005-05-29 02:22:55 +00:00
|
|
|
} else {
|
|
|
|
free(backupname);
|
|
|
|
backupname = backuptemp;
|
|
|
|
}
|
2004-02-28 16:24:31 +00:00
|
|
|
} else {
|
|
|
|
backupname = charalloc(strlen(realname) + 2);
|
|
|
|
sprintf(backupname, "%s~", realname);
|
|
|
|
}
|
2002-06-28 22:45:14 +00:00
|
|
|
|
2010-04-09 15:01:51 +00:00
|
|
|
/* First, unlink any existing backups. Next, open the backup
|
2014-04-08 18:38:45 +00:00
|
|
|
* file with O_CREAT and O_EXCL. If it succeeds, we have a file
|
|
|
|
* descriptor to a new backup file. */
|
2010-06-24 14:47:00 +00:00
|
|
|
if (unlink(backupname) < 0 && errno != ENOENT && !ISSET(INSECURE_BACKUP)) {
|
2011-02-07 02:06:20 +00:00
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
2010-04-07 05:48:24 +00:00
|
|
|
free(backupname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
|
2010-06-24 14:47:00 +00:00
|
|
|
if (ISSET(INSECURE_BACKUP))
|
|
|
|
backup_cflags = O_WRONLY | O_CREAT | O_APPEND;
|
2014-06-10 19:12:14 +00:00
|
|
|
else
|
2010-06-24 14:47:00 +00:00
|
|
|
backup_cflags = O_WRONLY | O_CREAT | O_EXCL | O_APPEND;
|
|
|
|
|
|
|
|
backup_fd = open(backupname, backup_cflags,
|
2010-04-09 15:01:51 +00:00
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
|
|
/* Now we've got a safe file stream. If the previous open()
|
2014-04-08 18:38:45 +00:00
|
|
|
* call failed, this will return NULL. */
|
2010-04-09 15:01:51 +00:00
|
|
|
backup_file = fdopen(backup_fd, "wb");
|
2010-04-07 05:48:24 +00:00
|
|
|
|
2010-04-09 15:01:51 +00:00
|
|
|
if (backup_fd < 0 || backup_file == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
2010-04-09 15:01:51 +00:00
|
|
|
free(backupname);
|
2017-04-04 10:39:02 +00:00
|
|
|
/* If we can't make a backup, DON'T go on, since whatever caused
|
|
|
|
* the backup to fail (e.g. disk full) may well cause the real
|
|
|
|
* file write to fail, in which case we could lose both the
|
|
|
|
* backup and the original! */
|
2010-04-09 15:01:51 +00:00
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2005-04-19 21:47:01 +00:00
|
|
|
|
2017-04-04 10:39:02 +00:00
|
|
|
/* Only try chowning the backup when we're root. */
|
|
|
|
if (geteuid() == NANO_ROOT_UID &&
|
|
|
|
fchown(backup_fd, openfile->current_stat->st_uid,
|
|
|
|
openfile->current_stat->st_gid) == -1 &&
|
|
|
|
!ISSET(INSECURE_BACKUP)) {
|
|
|
|
fclose(backup_file);
|
2011-02-07 02:06:20 +00:00
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
2010-05-23 04:30:23 +00:00
|
|
|
free(backupname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
|
2017-04-04 10:39:02 +00:00
|
|
|
/* Set the backup's mode bits. */
|
|
|
|
if (fchmod(backup_fd, openfile->current_stat->st_mode) == -1 &&
|
|
|
|
!ISSET(INSECURE_BACKUP)) {
|
|
|
|
fclose(backup_file);
|
2011-02-07 02:06:20 +00:00
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
2002-06-28 22:45:14 +00:00
|
|
|
free(backupname);
|
2008-08-09 03:39:10 +00:00
|
|
|
goto cleanup_and_exit;
|
2002-06-28 22:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2003-08-05 19:31:12 +00:00
|
|
|
fprintf(stderr, "Backing up %s to %s\n", realname, backupname);
|
2002-06-28 22:45:14 +00:00
|
|
|
#endif
|
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
/* Copy the file. */
|
2017-04-04 10:39:02 +00:00
|
|
|
if (copy_file(f, backup_file, FALSE) != 0) {
|
|
|
|
fclose(backup_file);
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), realname,
|
2004-11-26 18:10:07 +00:00
|
|
|
strerror(errno));
|
2010-06-24 14:47:00 +00:00
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
|
2017-04-04 10:39:02 +00:00
|
|
|
/* And set the backup's timestamps. */
|
2017-04-04 07:29:31 +00:00
|
|
|
if (futimens(backup_fd, filetime) == -1 && !ISSET(INSECURE_BACKUP)) {
|
|
|
|
fclose(backup_file);
|
2011-02-07 02:06:20 +00:00
|
|
|
if (prompt_failed_backupwrite(backupname))
|
|
|
|
goto skip_backup;
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, _("Error writing backup file %s: %s"),
|
|
|
|
backupname, strerror(errno));
|
2008-08-09 03:39:10 +00:00
|
|
|
goto cleanup_and_exit;
|
2003-12-24 08:03:54 +00:00
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2017-04-04 07:29:31 +00:00
|
|
|
fclose(backup_file);
|
2002-06-28 22:45:14 +00:00
|
|
|
free(backupname);
|
|
|
|
}
|
2006-04-06 05:18:23 +00:00
|
|
|
|
2008-08-09 09:31:33 +00:00
|
|
|
skip_backup:
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2000-07-21 22:42:46 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (f_open == NULL) {
|
|
|
|
original_umask = umask(0);
|
2004-07-03 03:22:23 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
/* If we create a temp file, we don't let anyone else access it.
|
2005-06-05 19:33:27 +00:00
|
|
|
* We create a temp file if tmp is TRUE. */
|
|
|
|
if (tmp)
|
2005-05-31 04:28:15 +00:00
|
|
|
umask(S_IRWXG | S_IRWXO);
|
2005-06-05 19:33:27 +00:00
|
|
|
else
|
|
|
|
umask(original_umask);
|
2005-05-31 04:28:15 +00:00
|
|
|
}
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2004-07-03 03:22:23 +00:00
|
|
|
/* If we're prepending, copy the file to a temp file. */
|
2016-07-15 10:59:59 +00:00
|
|
|
if (method == PREPEND) {
|
2003-12-24 08:03:54 +00:00
|
|
|
int fd_source;
|
|
|
|
FILE *f_source = NULL;
|
|
|
|
|
2006-04-07 04:37:14 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
f = fopen(realname, "rb");
|
|
|
|
|
|
|
|
if (f == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), realname,
|
2006-04-07 04:37:14 +00:00
|
|
|
strerror(errno));
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
tempname = safe_tempfile(&f);
|
2005-04-19 21:47:01 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (tempname == NULL) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing temp file: %s"),
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2002-09-13 18:14:04 +00:00
|
|
|
goto cleanup_and_exit;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2000-12-04 05:15:39 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (f_open == NULL) {
|
2008-08-17 16:25:40 +00:00
|
|
|
fd_source = open(realname, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
2005-04-19 21:47:01 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (fd_source != -1) {
|
|
|
|
f_source = fdopen(fd_source, "rb");
|
|
|
|
if (f_source == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), realname,
|
2016-04-17 18:58:33 +00:00
|
|
|
strerror(errno));
|
2005-05-31 04:28:15 +00:00
|
|
|
close(fd_source);
|
|
|
|
fclose(f);
|
|
|
|
unlink(tempname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
}
|
2003-12-24 08:03:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-04 07:29:31 +00:00
|
|
|
if (f_source == NULL || copy_file(f_source, f, TRUE) != 0) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing temp file: %s"),
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2003-12-24 08:03:54 +00:00
|
|
|
unlink(tempname);
|
2002-09-13 18:14:04 +00:00
|
|
|
goto cleanup_and_exit;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (f_open == NULL) {
|
|
|
|
/* Now open the file in place. Use O_EXCL if tmp is TRUE. This
|
|
|
|
* is copied from joe, because wiggy says so *shrug*. */
|
2016-07-15 10:59:59 +00:00
|
|
|
fd = open(realname, O_WRONLY | O_CREAT | ((method == APPEND) ?
|
2005-11-15 02:09:03 +00:00
|
|
|
O_APPEND : (tmp ? O_EXCL : O_TRUNC)), S_IRUSR |
|
|
|
|
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
/* Set the umask back to the user's original value. */
|
|
|
|
umask(original_umask);
|
2003-12-24 08:03:54 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
/* If we couldn't open the file, give up. */
|
|
|
|
if (fd == -1) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2005-05-31 04:28:15 +00:00
|
|
|
if (tempname != NULL)
|
|
|
|
unlink(tempname);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2002-04-22 23:52:34 +00:00
|
|
|
|
2016-07-15 10:59:59 +00:00
|
|
|
f = fdopen(fd, (method == APPEND) ? "ab" : "wb");
|
2005-05-28 23:32:45 +00:00
|
|
|
|
2005-05-31 04:28:15 +00:00
|
|
|
if (f == NULL) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2005-05-31 04:28:15 +00:00
|
|
|
close(fd);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2002-04-22 23:52:34 +00:00
|
|
|
}
|
|
|
|
|
2005-11-04 05:44:01 +00:00
|
|
|
while (fileptr != NULL) {
|
2005-11-04 05:59:41 +00:00
|
|
|
size_t data_len = strlen(fileptr->data), size;
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2006-11-27 04:35:11 +00:00
|
|
|
/* Convert newlines to nulls, just before we write to disk. */
|
2002-06-13 00:40:19 +00:00
|
|
|
sunder(fileptr->data);
|
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
size = fwrite(fileptr->data, sizeof(char), data_len, f);
|
2002-06-13 00:40:19 +00:00
|
|
|
|
2006-11-27 04:35:11 +00:00
|
|
|
/* Convert nulls to newlines. data_len is the string's real
|
|
|
|
* length. */
|
2002-06-13 00:40:19 +00:00
|
|
|
unsunder(fileptr->data, data_len);
|
|
|
|
|
2002-04-22 23:52:34 +00:00
|
|
|
if (size < data_len) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2002-04-23 10:22:33 +00:00
|
|
|
fclose(f);
|
2002-09-13 18:14:04 +00:00
|
|
|
goto cleanup_and_exit;
|
2002-04-23 10:22:33 +00:00
|
|
|
}
|
2005-04-15 17:48:20 +00:00
|
|
|
|
2005-11-04 05:59:41 +00:00
|
|
|
/* If we're on the last line of the file, don't write a newline
|
2005-11-04 06:21:39 +00:00
|
|
|
* character after it. If the last line of the file is blank,
|
|
|
|
* this means that zero bytes are written, in which case we
|
|
|
|
* don't count the last line in the total lines written. */
|
|
|
|
if (fileptr == openfile->filebot) {
|
|
|
|
if (fileptr->data[0] == '\0')
|
|
|
|
lineswritten--;
|
|
|
|
} else {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2015-07-10 17:25:51 +00:00
|
|
|
if (openfile->fmt == DOS_FILE || openfile->fmt == MAC_FILE) {
|
2005-11-04 05:44:01 +00:00
|
|
|
if (putc('\r', f) == EOF) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2016-04-17 18:58:33 +00:00
|
|
|
strerror(errno));
|
2005-11-04 05:44:01 +00:00
|
|
|
fclose(f);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
2003-12-24 08:03:54 +00:00
|
|
|
}
|
2001-09-22 04:20:25 +00:00
|
|
|
|
2015-07-10 17:25:51 +00:00
|
|
|
if (openfile->fmt != MAC_FILE)
|
2001-09-21 02:37:01 +00:00
|
|
|
#endif
|
2005-11-04 05:44:01 +00:00
|
|
|
if (putc('\n', f) == EOF) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2016-04-17 18:58:33 +00:00
|
|
|
strerror(errno));
|
2005-11-04 05:44:01 +00:00
|
|
|
fclose(f);
|
|
|
|
goto cleanup_and_exit;
|
|
|
|
}
|
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
|
|
|
fileptr = fileptr->next;
|
|
|
|
lineswritten++;
|
|
|
|
}
|
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
/* If we're prepending, open the temp file, and append it to f. */
|
2016-07-15 10:59:59 +00:00
|
|
|
if (method == PREPEND) {
|
2003-12-24 08:03:54 +00:00
|
|
|
int fd_source;
|
|
|
|
FILE *f_source = NULL;
|
|
|
|
|
2008-08-17 16:25:40 +00:00
|
|
|
fd_source = open(tempname, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR);
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2003-12-24 08:03:54 +00:00
|
|
|
if (fd_source != -1) {
|
|
|
|
f_source = fdopen(fd_source, "rb");
|
|
|
|
if (f_source == NULL)
|
|
|
|
close(fd_source);
|
2002-04-22 23:52:34 +00:00
|
|
|
}
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
if (f_source == NULL) {
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Error reading %s: %s"), tempname,
|
|
|
|
strerror(errno));
|
2003-12-24 08:03:54 +00:00
|
|
|
fclose(f);
|
2002-09-13 18:14:04 +00:00
|
|
|
goto cleanup_and_exit;
|
2002-04-16 03:15:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-04 10:21:43 +00:00
|
|
|
if (copy_file(f_source, f, TRUE) != 0) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2002-09-13 18:14:04 +00:00
|
|
|
goto cleanup_and_exit;
|
2000-12-06 05:56:08 +00:00
|
|
|
}
|
2015-08-11 17:43:08 +00:00
|
|
|
|
|
|
|
unlink(tempname);
|
2008-08-16 23:54:15 +00:00
|
|
|
} else if (fclose(f) != 0) {
|
2016-06-27 07:27:49 +00:00
|
|
|
statusline(ALERT, _("Error writing %s: %s"), realname,
|
2016-04-30 15:31:43 +00:00
|
|
|
strerror(errno));
|
2016-04-17 18:58:33 +00:00
|
|
|
goto cleanup_and_exit;
|
2008-08-16 23:54:15 +00:00
|
|
|
}
|
2000-12-06 05:56:08 +00:00
|
|
|
|
2016-07-15 11:03:42 +00:00
|
|
|
if (method == OVERWRITE && !tmp) {
|
2016-06-08 12:17:30 +00:00
|
|
|
/* If we must set the filename, and it changed, adjust things. */
|
|
|
|
if (!nonamechange && strcmp(openfile->filename, realname) != 0) {
|
2014-04-04 11:59:03 +00:00
|
|
|
#ifndef DISABLE_COLOR
|
2017-07-01 11:25:11 +00:00
|
|
|
char *newname;
|
2016-07-14 15:49:41 +00:00
|
|
|
char *oldname = openfile->syntax ? openfile->syntax->name : "";
|
2016-06-08 12:17:30 +00:00
|
|
|
filestruct *line = openfile->fileage;
|
|
|
|
#endif
|
|
|
|
openfile->filename = mallocstrcpy(openfile->filename, realname);
|
2016-06-08 10:56:46 +00:00
|
|
|
|
2016-06-08 12:17:30 +00:00
|
|
|
#ifndef DISABLE_COLOR
|
|
|
|
/* See if the applicable syntax has changed. */
|
2005-07-13 20:18:46 +00:00
|
|
|
color_update();
|
2005-07-16 22:47:12 +00:00
|
|
|
color_init();
|
2005-07-13 20:18:46 +00:00
|
|
|
|
2017-07-01 11:25:11 +00:00
|
|
|
newname = openfile->syntax ? openfile->syntax->name : "";
|
2016-07-14 15:49:41 +00:00
|
|
|
|
2016-06-08 12:17:30 +00:00
|
|
|
/* If the syntax changed, discard and recompute the multidata. */
|
2016-07-14 15:49:41 +00:00
|
|
|
if (strcmp(oldname, newname) != 0) {
|
2016-06-08 12:17:30 +00:00
|
|
|
for (; line != NULL; line = line->next) {
|
|
|
|
free(line->multidata);
|
|
|
|
line->multidata = NULL;
|
|
|
|
}
|
|
|
|
precalc_multicolorinfo();
|
2016-07-14 15:49:41 +00:00
|
|
|
refresh_needed = TRUE;
|
2016-06-08 12:17:30 +00:00
|
|
|
}
|
2003-01-13 01:35:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
2001-06-05 23:24:55 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2013-04-13 02:56:03 +00:00
|
|
|
if (!openfile->mark_set)
|
2016-02-09 20:53:11 +00:00
|
|
|
/* Get or update the stat info to reflect the current state. */
|
|
|
|
stat_with_alloc(realname, &openfile->current_stat);
|
2002-06-28 22:45:14 +00:00
|
|
|
#endif
|
2005-07-15 19:37:32 +00:00
|
|
|
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(HUSH, P_("Wrote %lu line", "Wrote %lu lines",
|
|
|
|
(unsigned long)lineswritten), (unsigned long)lineswritten);
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->modified = FALSE;
|
2001-01-03 07:11:47 +00:00
|
|
|
titlebar(NULL);
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2007-01-11 21:36:29 +00:00
|
|
|
retval = TRUE;
|
2002-09-13 18:14:04 +00:00
|
|
|
|
|
|
|
cleanup_and_exit:
|
|
|
|
free(realname);
|
2015-06-14 19:14:41 +00:00
|
|
|
free(tempname);
|
2005-05-29 02:22:55 +00:00
|
|
|
|
2002-09-13 18:14:04 +00:00
|
|
|
return retval;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2007-01-11 21:52:29 +00:00
|
|
|
/* Write a marked selection from a file out to disk. Return TRUE on
|
|
|
|
* success or FALSE on error. */
|
2007-01-11 21:36:29 +00:00
|
|
|
bool write_marked_file(const char *name, FILE *f_open, bool tmp,
|
2016-07-15 10:59:59 +00:00
|
|
|
kind_of_writing_type method)
|
2004-01-23 19:34:03 +00:00
|
|
|
{
|
2007-01-11 22:46:22 +00:00
|
|
|
bool retval;
|
2005-07-08 20:09:16 +00:00
|
|
|
bool old_modified = openfile->modified;
|
2016-04-24 15:28:21 +00:00
|
|
|
/* Save the status, because write_file() unsets the modified flag. */
|
2005-11-05 17:35:44 +00:00
|
|
|
bool added_magicline = FALSE;
|
2004-11-03 22:03:41 +00:00
|
|
|
/* Whether we added a magicline after filebot. */
|
|
|
|
filestruct *top, *bot;
|
|
|
|
size_t top_x, bot_x;
|
|
|
|
|
2017-04-19 15:38:34 +00:00
|
|
|
/* Partition the buffer so that it contains only the marked text. */
|
2004-11-03 22:03:41 +00:00
|
|
|
mark_order((const filestruct **)&top, &top_x,
|
2016-04-17 18:58:33 +00:00
|
|
|
(const filestruct **)&bot, &bot_x, NULL);
|
2004-11-03 22:03:41 +00:00
|
|
|
filepart = partition_filestruct(top, top_x, bot, bot_x);
|
2004-01-23 19:34:03 +00:00
|
|
|
|
2016-04-24 15:28:21 +00:00
|
|
|
/* If we are doing magicline, and the last line of the partition
|
|
|
|
* isn't blank, then add a newline at the end of the buffer. */
|
|
|
|
if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') {
|
2004-11-03 22:03:41 +00:00
|
|
|
new_magicline();
|
2016-04-24 15:28:21 +00:00
|
|
|
added_magicline = TRUE;
|
|
|
|
}
|
2004-01-23 19:34:03 +00:00
|
|
|
|
2016-07-15 10:59:59 +00:00
|
|
|
retval = write_file(name, f_open, tmp, method, TRUE);
|
2004-01-23 19:34:03 +00:00
|
|
|
|
2016-04-24 15:28:21 +00:00
|
|
|
/* If we added a magicline, remove it now. */
|
|
|
|
if (added_magicline)
|
2004-11-03 22:03:41 +00:00
|
|
|
remove_magicline();
|
|
|
|
|
2017-04-19 15:38:34 +00:00
|
|
|
/* Unpartition the buffer so that it contains all the text again. */
|
2004-11-22 00:16:23 +00:00
|
|
|
unpartition_filestruct(&filepart);
|
2004-11-03 22:03:41 +00:00
|
|
|
|
2004-10-08 15:35:33 +00:00
|
|
|
if (old_modified)
|
2004-01-23 19:34:03 +00:00
|
|
|
set_modified();
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2013-01-01 03:24:39 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2004-01-23 19:34:03 +00:00
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Write the current file to disk. If the mark is on, write the current
|
2015-12-23 16:34:44 +00:00
|
|
|
* marked selection to disk. If exiting is TRUE, write the entire file
|
|
|
|
* to disk regardless of whether the mark is on, and without prompting if
|
|
|
|
* the TEMP_FILE flag is set and the current file has a name. Return 0
|
|
|
|
* on error, 1 on success, and 2 when the buffer is to be discarded. */
|
|
|
|
int do_writeout(bool exiting)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2007-01-11 21:36:29 +00:00
|
|
|
int i;
|
2016-05-23 19:34:02 +00:00
|
|
|
bool result = FALSE;
|
2016-07-15 10:59:59 +00:00
|
|
|
kind_of_writing_type method = OVERWRITE;
|
2016-05-23 18:44:58 +00:00
|
|
|
char *given;
|
|
|
|
/* The filename we offer, or what the user typed so far. */
|
2016-06-07 08:15:25 +00:00
|
|
|
bool maychange = (openfile->filename[0] == '\0');
|
2016-05-23 19:34:02 +00:00
|
|
|
/* Whether it's okay to save the file under a different name. */
|
2014-04-03 20:57:44 +00:00
|
|
|
#ifndef DISABLE_EXTRA
|
2005-07-08 20:09:16 +00:00
|
|
|
static bool did_credits = FALSE;
|
2000-11-24 20:45:14 +00:00
|
|
|
#endif
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2016-12-26 11:35:50 +00:00
|
|
|
/* Display newlines in filenames as ^J. */
|
2016-12-20 18:27:41 +00:00
|
|
|
as_an_at = FALSE;
|
|
|
|
|
2016-05-23 18:57:08 +00:00
|
|
|
if (exiting && ISSET(TEMP_FILE) && openfile->filename[0] != '\0') {
|
|
|
|
if (write_file(openfile->filename, NULL, FALSE, OVERWRITE, FALSE))
|
|
|
|
return 1;
|
|
|
|
/* If writing the file failed, go on to prompt for a new name. */
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 18:44:58 +00:00
|
|
|
given = mallocstrcpy(NULL,
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-05-24 08:03:33 +00:00
|
|
|
(openfile->mark_set && !exiting) ? "" :
|
2004-03-15 20:26:30 +00:00
|
|
|
#endif
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->filename);
|
2004-03-15 20:26:30 +00:00
|
|
|
|
|
|
|
while (TRUE) {
|
|
|
|
const char *msg;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2002-09-13 18:14:04 +00:00
|
|
|
const char *formatstr, *backupstr;
|
|
|
|
|
2015-07-10 17:25:51 +00:00
|
|
|
formatstr = (openfile->fmt == DOS_FILE) ? _(" [DOS Format]") :
|
2016-05-30 07:09:36 +00:00
|
|
|
(openfile->fmt == MAC_FILE) ? _(" [Mac Format]") : "";
|
2005-07-08 20:09:16 +00:00
|
|
|
|
2006-06-14 13:19:43 +00:00
|
|
|
backupstr = ISSET(BACKUP_FILE) ? _(" [Backup]") : "";
|
2002-02-15 19:17:02 +00:00
|
|
|
|
2007-04-21 18:57:47 +00:00
|
|
|
/* If we're using restricted mode, don't display the "Write
|
2007-04-21 20:33:56 +00:00
|
|
|
* Selection to File" prompt. This function is disabled, since
|
|
|
|
* it allows reading from or writing to files not specified on
|
|
|
|
* the command line. */
|
2016-05-24 08:03:33 +00:00
|
|
|
if (openfile->mark_set && !exiting && !ISSET(RESTRICTED))
|
2016-12-07 19:56:28 +00:00
|
|
|
/* TRANSLATORS: The next six strings are prompts. */
|
2016-07-15 10:59:59 +00:00
|
|
|
msg = (method == PREPEND) ? _("Prepend Selection to File") :
|
|
|
|
(method == APPEND) ? _("Append Selection to File") :
|
2016-05-30 07:09:36 +00:00
|
|
|
_("Write Selection to File");
|
2017-05-16 18:00:42 +00:00
|
|
|
else if (method != OVERWRITE)
|
|
|
|
msg = (method == PREPEND) ? _("File Name to Prepend to") :
|
|
|
|
_("File Name to Append to");
|
2002-06-28 22:45:14 +00:00
|
|
|
else
|
2005-11-15 03:17:35 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2017-05-16 18:00:42 +00:00
|
|
|
msg = _("File Name to Write");
|
2004-03-15 20:26:30 +00:00
|
|
|
|
2016-04-30 19:22:16 +00:00
|
|
|
present_path = mallocstrcpy(present_path, "./");
|
|
|
|
|
2016-01-13 20:32:40 +00:00
|
|
|
/* If we're using restricted mode, and the filename isn't blank,
|
|
|
|
* disable tab completion. */
|
2016-12-07 18:56:27 +00:00
|
|
|
i = do_prompt(!ISSET(RESTRICTED) || openfile->filename[0] == '\0',
|
2017-01-02 20:12:44 +00:00
|
|
|
TRUE, MWRITEFILE, given,
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
2006-02-18 21:32:29 +00:00
|
|
|
NULL,
|
|
|
|
#endif
|
2006-06-14 13:19:43 +00:00
|
|
|
edit_refresh, "%s%s%s", msg,
|
2006-02-18 21:32:29 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
formatstr, backupstr
|
2004-03-15 20:26:30 +00:00
|
|
|
#else
|
2006-02-18 21:32:29 +00:00
|
|
|
"", ""
|
2004-03-15 20:26:30 +00:00
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
2016-12-20 20:41:37 +00:00
|
|
|
if (i < 0) {
|
2002-09-13 18:14:04 +00:00
|
|
|
statusbar(_("Cancelled"));
|
2004-10-05 20:11:31 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2014-07-27 21:07:15 +00:00
|
|
|
functionptrtype func = func_from_key(&i);
|
|
|
|
|
2017-05-16 18:37:19 +00:00
|
|
|
/* Upon request, abandon the buffer. */
|
2015-12-23 16:34:44 +00:00
|
|
|
if (func == discard_buffer) {
|
2017-05-16 18:37:19 +00:00
|
|
|
free(given);
|
|
|
|
return 2;
|
2015-12-23 16:34:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 18:44:58 +00:00
|
|
|
given = mallocstrcpy(given, answer);
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-05-08 17:08:23 +00:00
|
|
|
#ifdef ENABLE_BROWSER
|
2014-07-27 21:07:15 +00:00
|
|
|
if (func == to_files_void) {
|
2016-05-23 16:09:53 +00:00
|
|
|
char *chosen = do_browse_from(answer);
|
2001-05-21 12:56:25 +00:00
|
|
|
|
2016-05-23 16:09:53 +00:00
|
|
|
if (chosen == NULL)
|
2004-10-05 20:11:31 +00:00
|
|
|
continue;
|
2005-07-08 20:09:16 +00:00
|
|
|
|
2006-08-29 20:54:38 +00:00
|
|
|
/* We have a file now. Indicate this. */
|
2004-10-05 20:11:31 +00:00
|
|
|
free(answer);
|
2016-05-23 16:09:53 +00:00
|
|
|
answer = chosen;
|
2004-10-05 20:11:31 +00:00
|
|
|
} else
|
2017-05-08 17:08:23 +00:00
|
|
|
#endif
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-07-27 21:07:15 +00:00
|
|
|
if (func == dos_format_void) {
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->fmt = (openfile->fmt == DOS_FILE) ? NIX_FILE :
|
|
|
|
DOS_FILE;
|
2004-10-05 20:11:31 +00:00
|
|
|
continue;
|
2014-07-27 21:07:15 +00:00
|
|
|
} else if (func == mac_format_void) {
|
2005-07-08 20:09:16 +00:00
|
|
|
openfile->fmt = (openfile->fmt == MAC_FILE) ? NIX_FILE :
|
|
|
|
MAC_FILE;
|
2004-10-05 20:11:31 +00:00
|
|
|
continue;
|
2014-07-27 21:07:15 +00:00
|
|
|
} else if (func == backup_file_void) {
|
2004-10-05 20:11:31 +00:00
|
|
|
TOGGLE(BACKUP_FILE);
|
|
|
|
continue;
|
2017-04-19 11:24:41 +00:00
|
|
|
} else if (func == prepend_void) {
|
2016-07-15 10:59:59 +00:00
|
|
|
method = (method == PREPEND) ? OVERWRITE : PREPEND;
|
2004-10-05 20:11:31 +00:00
|
|
|
continue;
|
2014-07-27 21:07:15 +00:00
|
|
|
} else if (func == append_void) {
|
2016-07-15 10:59:59 +00:00
|
|
|
method = (method == APPEND) ? OVERWRITE : APPEND;
|
2004-10-05 20:11:31 +00:00
|
|
|
continue;
|
2017-04-19 11:24:41 +00:00
|
|
|
} else
|
|
|
|
#endif /* !NANO_TINY */
|
|
|
|
if (func == do_help_void) {
|
2008-08-30 21:00:00 +00:00
|
|
|
continue;
|
2004-10-05 20:11:31 +00:00
|
|
|
}
|
2001-01-03 07:11:47 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
#ifdef DEBUG
|
2004-10-05 20:11:31 +00:00
|
|
|
fprintf(stderr, "filename is %s\n", answer);
|
2000-06-19 04:22:15 +00:00
|
|
|
#endif
|
2000-11-24 20:45:14 +00:00
|
|
|
|
2014-04-03 20:57:44 +00:00
|
|
|
#ifndef DISABLE_EXTRA
|
2006-05-13 18:06:43 +00:00
|
|
|
/* If the current file has been modified, we've pressed
|
|
|
|
* Ctrl-X at the edit window to exit, we've pressed "y" at
|
|
|
|
* the "Save modified buffer" prompt to save, we've entered
|
|
|
|
* "zzy" as the filename to save under (hence "xyzzy"), and
|
|
|
|
* this is the first time we've done this, show an Easter
|
|
|
|
* egg. Display the credits. */
|
|
|
|
if (!did_credits && exiting && !ISSET(TEMP_FILE) &&
|
2015-08-29 20:14:57 +00:00
|
|
|
strcasecmp(answer, "zzy") == 0) {
|
2004-10-05 20:11:31 +00:00
|
|
|
do_credits();
|
2005-07-08 20:09:16 +00:00
|
|
|
did_credits = TRUE;
|
2004-10-05 20:11:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-11-24 20:45:14 +00:00
|
|
|
#endif
|
2004-10-05 20:11:31 +00:00
|
|
|
|
2016-07-15 10:59:59 +00:00
|
|
|
if (method == OVERWRITE) {
|
2007-04-18 19:09:44 +00:00
|
|
|
bool name_exists, do_warning;
|
2006-12-11 22:11:32 +00:00
|
|
|
char *full_answer, *full_filename;
|
2006-11-07 22:57:13 +00:00
|
|
|
struct stat st;
|
|
|
|
|
2006-12-11 22:11:32 +00:00
|
|
|
full_answer = get_full_path(answer);
|
|
|
|
full_filename = get_full_path(openfile->filename);
|
2016-04-17 18:58:33 +00:00
|
|
|
name_exists = (stat((full_answer == NULL) ?
|
|
|
|
answer : full_answer, &st) != -1);
|
2007-04-21 20:24:16 +00:00
|
|
|
if (openfile->filename[0] == '\0')
|
|
|
|
do_warning = name_exists;
|
|
|
|
else
|
|
|
|
do_warning = (strcmp((full_answer == NULL) ?
|
2016-04-17 18:58:33 +00:00
|
|
|
answer : full_answer, (full_filename == NULL) ?
|
|
|
|
openfile->filename : full_filename) != 0);
|
2006-12-11 22:11:32 +00:00
|
|
|
|
2015-06-14 19:14:41 +00:00
|
|
|
free(full_filename);
|
|
|
|
free(full_answer);
|
2006-11-08 13:03:37 +00:00
|
|
|
|
2007-04-18 19:09:44 +00:00
|
|
|
if (do_warning) {
|
2016-12-07 19:56:28 +00:00
|
|
|
/* When in restricted mode, we aren't allowed to overwrite
|
|
|
|
* an existing file with the current buffer, nor to change
|
|
|
|
* the name of the current file if it already has one. */
|
2016-11-27 21:01:54 +00:00
|
|
|
if (ISSET(RESTRICTED)) {
|
2016-12-07 19:56:28 +00:00
|
|
|
/* TRANSLATORS: Restricted mode forbids overwriting. */
|
2016-11-27 21:01:54 +00:00
|
|
|
warn_and_shortly_pause(_("File exists -- "
|
|
|
|
"cannot overwrite"));
|
2007-04-21 18:23:06 +00:00
|
|
|
continue;
|
2016-11-27 21:01:54 +00:00
|
|
|
}
|
2006-12-15 02:49:44 +00:00
|
|
|
|
2016-03-20 14:34:46 +00:00
|
|
|
if (!maychange) {
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-03-20 14:34:46 +00:00
|
|
|
if (exiting || !openfile->mark_set)
|
|
|
|
#endif
|
|
|
|
{
|
2016-05-24 08:15:47 +00:00
|
|
|
if (do_yesno_prompt(FALSE, _("Save file under "
|
|
|
|
"DIFFERENT NAME? ")) < 1)
|
2016-03-20 14:34:46 +00:00
|
|
|
continue;
|
|
|
|
maychange = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name_exists) {
|
|
|
|
char *question = _("File \"%s\" exists; OVERWRITE? ");
|
|
|
|
char *message = charalloc(strlen(question) +
|
|
|
|
strlen(answer) + 1);
|
|
|
|
sprintf(message, question, answer);
|
|
|
|
|
|
|
|
i = do_yesno_prompt(FALSE, message);
|
|
|
|
free(message);
|
|
|
|
|
|
|
|
if (i < 1)
|
2006-12-15 02:49:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-10-05 20:11:31 +00:00
|
|
|
}
|
2008-10-22 22:11:46 +00:00
|
|
|
#ifndef NANO_TINY
|
2014-04-08 18:38:45 +00:00
|
|
|
/* Complain if the file exists, the name hasn't changed,
|
|
|
|
* and the stat information we had before does not match
|
|
|
|
* what we have now. */
|
2014-06-20 10:48:26 +00:00
|
|
|
else if (name_exists && openfile->current_stat &&
|
|
|
|
(openfile->current_stat->st_mtime < st.st_mtime ||
|
|
|
|
openfile->current_stat->st_dev != st.st_dev ||
|
|
|
|
openfile->current_stat->st_ino != st.st_ino)) {
|
2016-05-24 08:15:47 +00:00
|
|
|
|
2017-06-27 19:55:47 +00:00
|
|
|
warn_and_shortly_pause(_("File on disk has changed"));
|
|
|
|
|
2016-05-24 08:15:47 +00:00
|
|
|
if (do_yesno_prompt(FALSE, _("File was modified since "
|
|
|
|
"you opened it; continue saving? ")) < 1)
|
2008-10-21 10:20:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-10-22 22:11:46 +00:00
|
|
|
#endif
|
2003-11-19 23:59:14 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2004-10-05 20:11:31 +00:00
|
|
|
/* Here's where we allow the selected text to be written to
|
2007-04-21 20:33:56 +00:00
|
|
|
* a separate file. If we're using restricted mode, this
|
|
|
|
* function is disabled, since it allows reading from or
|
|
|
|
* writing to files not specified on the command line. */
|
2007-01-11 23:10:03 +00:00
|
|
|
#ifndef NANO_TINY
|
2016-05-24 08:03:33 +00:00
|
|
|
if (openfile->mark_set && !exiting && !ISSET(RESTRICTED))
|
2016-07-15 10:59:59 +00:00
|
|
|
result = write_marked_file(answer, NULL, FALSE, method);
|
2016-04-24 19:24:17 +00:00
|
|
|
else
|
2006-11-07 21:08:17 +00:00
|
|
|
#endif
|
2016-07-15 10:59:59 +00:00
|
|
|
result = write_file(answer, NULL, FALSE, method, FALSE);
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2004-10-05 20:11:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-05-13 18:06:43 +00:00
|
|
|
}
|
2004-10-05 20:11:31 +00:00
|
|
|
|
2016-05-23 18:44:58 +00:00
|
|
|
free(given);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2015-12-23 16:34:44 +00:00
|
|
|
return result ? 1 : 0;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 16:34:44 +00:00
|
|
|
/* Write the current buffer to disk, or discard it. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_writeout_void(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2015-12-23 16:34:44 +00:00
|
|
|
/* If the user chose to discard the buffer, close it. */
|
|
|
|
if (do_writeout(FALSE) == 2)
|
|
|
|
close_and_go();
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2015-07-25 19:25:50 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
/* If it has a name, write the current file to disk without prompting. */
|
|
|
|
void do_savefile(void)
|
|
|
|
{
|
|
|
|
if (openfile->filename[0] != '\0')
|
|
|
|
write_file(openfile->filename, NULL, FALSE, OVERWRITE, FALSE);
|
|
|
|
else
|
|
|
|
do_writeout_void();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
/* Return a malloc()ed string containing the actual directory, used to
|
|
|
|
* convert ~user/ and ~/ notation. */
|
2002-09-13 18:14:04 +00:00
|
|
|
char *real_dir_from_tilde(const char *buf)
|
2000-11-24 14:00:16 +00:00
|
|
|
{
|
2007-04-18 17:13:36 +00:00
|
|
|
char *retval;
|
2001-01-23 03:09:15 +00:00
|
|
|
|
2007-07-01 21:33:17 +00:00
|
|
|
if (*buf == '~') {
|
2007-04-18 19:16:08 +00:00
|
|
|
size_t i = 1;
|
2007-04-18 17:13:36 +00:00
|
|
|
char *tilde_dir;
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2007-04-19 03:23:16 +00:00
|
|
|
/* Figure out how much of the string we need to compare. */
|
2007-04-18 19:16:08 +00:00
|
|
|
for (; buf[i] != '/' && buf[i] != '\0'; i++)
|
2002-09-13 18:14:04 +00:00
|
|
|
;
|
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
/* Get the home directory. */
|
|
|
|
if (i == 1) {
|
|
|
|
get_homedir();
|
2007-04-18 17:13:36 +00:00
|
|
|
tilde_dir = mallocstrcpy(NULL, homedir);
|
2005-02-08 20:37:53 +00:00
|
|
|
} else {
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2005-02-08 20:37:53 +00:00
|
|
|
const struct passwd *userdata;
|
|
|
|
|
2007-04-18 17:13:36 +00:00
|
|
|
tilde_dir = mallocstrncpy(NULL, buf, i + 1);
|
|
|
|
tilde_dir[i] = '\0';
|
|
|
|
|
2002-09-13 18:14:04 +00:00
|
|
|
do {
|
|
|
|
userdata = getpwent();
|
2016-02-23 12:37:10 +00:00
|
|
|
} while (userdata != NULL &&
|
|
|
|
strcmp(userdata->pw_name, tilde_dir + 1) != 0);
|
2005-02-08 20:37:53 +00:00
|
|
|
endpwent();
|
2007-04-17 03:43:59 +00:00
|
|
|
if (userdata != NULL)
|
2007-04-18 17:13:36 +00:00
|
|
|
tilde_dir = mallocstrcpy(tilde_dir, userdata->pw_dir);
|
2017-02-21 22:04:44 +00:00
|
|
|
#else
|
|
|
|
tilde_dir = strdup("");
|
|
|
|
#endif
|
2002-07-29 23:46:38 +00:00
|
|
|
}
|
2001-01-23 03:09:15 +00:00
|
|
|
|
2007-04-18 17:13:36 +00:00
|
|
|
retval = charalloc(strlen(tilde_dir) + strlen(buf + i) + 1);
|
|
|
|
sprintf(retval, "%s%s", tilde_dir, buf + i);
|
2000-11-25 04:43:43 +00:00
|
|
|
|
2007-04-18 17:13:36 +00:00
|
|
|
free(tilde_dir);
|
|
|
|
} else
|
|
|
|
retval = mallocstrcpy(NULL, buf);
|
2002-09-13 18:14:04 +00:00
|
|
|
|
2007-04-18 17:13:36 +00:00
|
|
|
return retval;
|
2000-11-24 14:00:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 17:15:51 +00:00
|
|
|
#if defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER)
|
2005-02-11 16:02:54 +00:00
|
|
|
/* Our sort routine for file listings. Sort alphabetically and
|
2005-06-21 04:11:04 +00:00
|
|
|
* case-insensitively, and sort directories before filenames. */
|
2005-02-08 20:37:53 +00:00
|
|
|
int diralphasort(const void *va, const void *vb)
|
|
|
|
{
|
|
|
|
struct stat fileinfo;
|
|
|
|
const char *a = *(const char *const *)va;
|
|
|
|
const char *b = *(const char *const *)vb;
|
|
|
|
bool aisdir = stat(a, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode);
|
|
|
|
bool bisdir = stat(b, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode);
|
|
|
|
|
|
|
|
if (aisdir && !bisdir)
|
|
|
|
return -1;
|
|
|
|
if (!aisdir && bisdir)
|
|
|
|
return 1;
|
|
|
|
|
2006-05-12 19:30:28 +00:00
|
|
|
/* Standard function brain damage: We should be sorting
|
|
|
|
* alphabetically and case-insensitively according to the current
|
|
|
|
* locale, but there's no standard strcasecoll() function, so we
|
2014-05-13 21:11:59 +00:00
|
|
|
* have to use multibyte strcasecmp() instead. */
|
2005-06-21 04:11:04 +00:00
|
|
|
return mbstrcasecmp(a, b);
|
2005-02-08 20:37:53 +00:00
|
|
|
}
|
2005-06-15 06:30:05 +00:00
|
|
|
|
|
|
|
/* Free the memory allocated for array, which should contain len
|
|
|
|
* elements. */
|
|
|
|
void free_chararray(char **array, size_t len)
|
|
|
|
{
|
2015-11-30 16:44:44 +00:00
|
|
|
if (array == NULL)
|
|
|
|
return;
|
2006-05-11 01:53:33 +00:00
|
|
|
|
2005-06-15 06:30:05 +00:00
|
|
|
for (; len > 0; len--)
|
|
|
|
free(array[len - 1]);
|
2016-02-23 12:37:10 +00:00
|
|
|
|
2005-06-15 06:30:05 +00:00
|
|
|
free(array);
|
|
|
|
}
|
2005-02-08 20:37:53 +00:00
|
|
|
#endif
|
|
|
|
|
2017-05-08 17:15:51 +00:00
|
|
|
#ifdef ENABLE_TABCOMP
|
2007-01-11 23:11:46 +00:00
|
|
|
/* Is the given path a directory? */
|
2007-01-11 21:36:29 +00:00
|
|
|
bool is_dir(const char *buf)
|
2000-11-24 14:00:16 +00:00
|
|
|
{
|
2007-01-11 21:36:29 +00:00
|
|
|
char *dirptr;
|
2000-11-24 14:00:16 +00:00
|
|
|
struct stat fileinfo;
|
2007-01-11 21:36:29 +00:00
|
|
|
bool retval;
|
|
|
|
|
|
|
|
dirptr = real_dir_from_tilde(buf);
|
|
|
|
|
2015-08-29 20:14:57 +00:00
|
|
|
retval = (stat(dirptr, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode));
|
2000-11-24 14:00:16 +00:00
|
|
|
|
2002-09-13 18:14:04 +00:00
|
|
|
free(dirptr);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2006-02-07 18:32:22 +00:00
|
|
|
return retval;
|
2000-11-24 14:00:16 +00:00
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2005-11-17 18:58:56 +00:00
|
|
|
/* These functions, username_tab_completion(), cwd_tab_completion()
|
|
|
|
* (originally exe_n_cwd_tab_completion()), and input_tab(), were
|
|
|
|
* adapted from busybox 0.46 (cmdedit.c). Here is the notice from that
|
2007-08-10 22:12:39 +00:00
|
|
|
* file, with the copyright years updated:
|
2000-11-05 17:54:41 +00:00
|
|
|
*
|
|
|
|
* Termios command line History and Editting, originally
|
|
|
|
* intended for NetBSD sh (ash)
|
2007-08-10 22:12:39 +00:00
|
|
|
* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
|
2000-11-05 17:54:41 +00:00
|
|
|
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
|
|
|
* Etc: Dave Cinege <dcinege@psychosis.com>
|
|
|
|
* Majorly adjusted/re-written for busybox:
|
|
|
|
* Erik Andersen <andersee@debian.org>
|
|
|
|
*
|
|
|
|
* You may use this code as you wish, so long as the original author(s)
|
|
|
|
* are attributed in any redistributions of the source code.
|
|
|
|
* This code is 'as is' with no warranty.
|
2005-02-08 20:37:53 +00:00
|
|
|
* This code may safely be consumed by a BSD or GPL license. */
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2007-10-11 15:38:32 +00:00
|
|
|
/* We consider the first buf_len characters of buf for ~username tab
|
2005-02-08 20:37:53 +00:00
|
|
|
* completion. */
|
|
|
|
char **username_tab_completion(const char *buf, size_t *num_matches,
|
2007-10-11 15:38:32 +00:00
|
|
|
size_t buf_len)
|
2000-11-05 17:54:41 +00:00
|
|
|
{
|
2005-02-08 20:37:53 +00:00
|
|
|
char **matches = NULL;
|
2017-07-01 11:25:11 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
const struct passwd *userdata;
|
|
|
|
#endif
|
2000-11-24 14:00:16 +00:00
|
|
|
|
2007-10-11 15:38:32 +00:00
|
|
|
assert(buf != NULL && num_matches != NULL && buf_len > 0);
|
2001-01-19 04:17:24 +00:00
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
*num_matches = 0;
|
2001-01-19 04:17:24 +00:00
|
|
|
|
2017-02-21 22:04:44 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2001-01-23 03:27:31 +00:00
|
|
|
while ((userdata = getpwent()) != NULL) {
|
2007-10-11 15:38:32 +00:00
|
|
|
if (strncmp(userdata->pw_name, buf + 1, buf_len - 1) == 0) {
|
2005-02-08 20:37:53 +00:00
|
|
|
/* Cool, found a match. Add it to the list. This makes a
|
|
|
|
* lot more sense to me (Chris) this way... */
|
2001-01-19 04:17:24 +00:00
|
|
|
|
2001-09-19 03:19:43 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
|
|
|
/* ...unless the match exists outside the operating
|
2005-02-08 20:37:53 +00:00
|
|
|
* directory, in which case just go to the next match. */
|
|
|
|
if (check_operating_dir(userdata->pw_dir, TRUE))
|
|
|
|
continue;
|
2001-09-19 03:19:43 +00:00
|
|
|
#endif
|
|
|
|
|
2006-02-07 18:35:26 +00:00
|
|
|
matches = (char **)nrealloc(matches, (*num_matches + 1) *
|
2016-02-23 12:37:10 +00:00
|
|
|
sizeof(char *));
|
|
|
|
matches[*num_matches] = charalloc(strlen(userdata->pw_name) + 2);
|
2005-02-08 20:37:53 +00:00
|
|
|
sprintf(matches[*num_matches], "~%s", userdata->pw_name);
|
2004-07-17 19:49:12 +00:00
|
|
|
++(*num_matches);
|
2000-11-24 14:00:16 +00:00
|
|
|
}
|
2001-01-23 03:27:31 +00:00
|
|
|
}
|
|
|
|
endpwent();
|
2017-02-21 22:04:44 +00:00
|
|
|
#endif
|
2001-01-19 04:17:24 +00:00
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
return matches;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 15:38:32 +00:00
|
|
|
/* We consider the first buf_len characters of buf for filename tab
|
2005-11-17 18:58:56 +00:00
|
|
|
* completion. */
|
2006-02-07 21:11:05 +00:00
|
|
|
char **cwd_tab_completion(const char *buf, bool allow_files, size_t
|
2007-10-11 15:38:32 +00:00
|
|
|
*num_matches, size_t buf_len)
|
2000-11-05 17:54:41 +00:00
|
|
|
{
|
2016-04-23 12:48:34 +00:00
|
|
|
char *dirname = mallocstrcpy(NULL, buf);
|
|
|
|
char *slash, *filename;
|
2005-02-08 20:37:53 +00:00
|
|
|
size_t filenamelen;
|
|
|
|
char **matches = NULL;
|
2000-11-05 17:54:41 +00:00
|
|
|
DIR *dir;
|
2005-05-14 23:14:47 +00:00
|
|
|
const struct dirent *nextdir;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
*num_matches = 0;
|
2017-03-20 12:01:37 +00:00
|
|
|
dirname[buf_len] = '\0';
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2016-04-26 12:28:01 +00:00
|
|
|
/* If there's a / in the name, split out filename and directory parts. */
|
2016-04-23 12:48:34 +00:00
|
|
|
slash = strrchr(dirname, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
char *wasdirname = dirname;
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2016-04-26 12:28:01 +00:00
|
|
|
filename = mallocstrcpy(NULL, ++slash);
|
|
|
|
/* Cut off the filename part after the slash. */
|
2016-04-23 12:48:34 +00:00
|
|
|
*slash = '\0';
|
2005-02-08 20:37:53 +00:00
|
|
|
dirname = real_dir_from_tilde(dirname);
|
2016-04-30 19:22:16 +00:00
|
|
|
/* A non-absolute path is relative to the current browser directory. */
|
|
|
|
if (dirname[0] != '/') {
|
|
|
|
dirname = charealloc(dirname, strlen(present_path) +
|
|
|
|
strlen(wasdirname) + 1);
|
|
|
|
sprintf(dirname, "%s%s", present_path, wasdirname);
|
|
|
|
}
|
2016-04-23 12:48:34 +00:00
|
|
|
free(wasdirname);
|
2000-11-05 17:54:41 +00:00
|
|
|
} else {
|
2005-02-08 20:37:53 +00:00
|
|
|
filename = dirname;
|
2016-04-30 19:22:16 +00:00
|
|
|
dirname = mallocstrcpy(NULL, present_path);
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
assert(dirname[strlen(dirname) - 1] == '/');
|
2000-11-24 14:00:16 +00:00
|
|
|
|
2002-07-20 13:57:41 +00:00
|
|
|
dir = opendir(dirname);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
if (dir == NULL) {
|
2005-02-08 20:37:53 +00:00
|
|
|
/* Don't print an error, just shut up and return. */
|
2000-11-05 17:54:41 +00:00
|
|
|
beep();
|
2005-02-08 20:37:53 +00:00
|
|
|
free(filename);
|
|
|
|
free(dirname);
|
|
|
|
return NULL;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
2005-02-08 20:37:53 +00:00
|
|
|
|
|
|
|
filenamelen = strlen(filename);
|
|
|
|
|
2005-05-14 23:14:47 +00:00
|
|
|
while ((nextdir = readdir(dir)) != NULL) {
|
2006-02-07 21:11:05 +00:00
|
|
|
bool skip_match = FALSE;
|
|
|
|
|
2000-11-05 17:54:41 +00:00
|
|
|
#ifdef DEBUG
|
2005-05-14 23:14:47 +00:00
|
|
|
fprintf(stderr, "Comparing \'%s\'\n", nextdir->d_name);
|
2000-11-05 17:54:41 +00:00
|
|
|
#endif
|
2005-02-08 20:37:53 +00:00
|
|
|
/* See if this matches. */
|
2005-05-14 23:14:47 +00:00
|
|
|
if (strncmp(nextdir->d_name, filename, filenamelen) == 0 &&
|
2015-08-29 20:14:57 +00:00
|
|
|
(*filename == '.' || (strcmp(nextdir->d_name, ".") != 0 &&
|
|
|
|
strcmp(nextdir->d_name, "..") != 0))) {
|
2005-02-08 20:37:53 +00:00
|
|
|
/* Cool, found a match. Add it to the list. This makes a
|
|
|
|
* lot more sense to me (Chris) this way... */
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2016-02-23 12:37:10 +00:00
|
|
|
char *tmp = charalloc(strlen(dirname) + strlen(nextdir->d_name) + 1);
|
2006-02-07 21:11:05 +00:00
|
|
|
sprintf(tmp, "%s%s", dirname, nextdir->d_name);
|
|
|
|
|
2001-09-19 03:19:43 +00:00
|
|
|
#ifndef DISABLE_OPERATINGDIR
|
|
|
|
/* ...unless the match exists outside the operating
|
2006-02-07 21:11:05 +00:00
|
|
|
* directory, in which case just go to the next match. */
|
|
|
|
if (check_operating_dir(tmp, TRUE))
|
|
|
|
skip_match = TRUE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ...or unless the match isn't a directory and allow_files
|
|
|
|
* isn't set, in which case just go to the next match. */
|
2006-02-07 21:16:46 +00:00
|
|
|
if (!allow_files && !is_dir(tmp))
|
2006-02-07 21:11:05 +00:00
|
|
|
skip_match = TRUE;
|
|
|
|
|
|
|
|
free(tmp);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2006-02-07 21:11:05 +00:00
|
|
|
if (skip_match)
|
2005-02-08 20:37:53 +00:00
|
|
|
continue;
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2006-02-07 18:32:22 +00:00
|
|
|
matches = (char **)nrealloc(matches, (*num_matches + 1) *
|
2016-02-23 12:37:10 +00:00
|
|
|
sizeof(char *));
|
2005-05-14 23:14:47 +00:00
|
|
|
matches[*num_matches] = mallocstrcpy(NULL, nextdir->d_name);
|
2005-02-08 20:37:53 +00:00
|
|
|
++(*num_matches);
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-08 20:09:16 +00:00
|
|
|
|
2003-02-08 02:02:02 +00:00
|
|
|
closedir(dir);
|
|
|
|
free(dirname);
|
2005-02-08 20:37:53 +00:00
|
|
|
free(filename);
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2002-12-22 16:30:00 +00:00
|
|
|
return matches;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
|
2005-05-26 18:11:32 +00:00
|
|
|
/* Do tab completion. place refers to how much the statusbar cursor
|
2006-02-18 21:32:29 +00:00
|
|
|
* position should be advanced. refresh_func is the function we will
|
|
|
|
* call to refresh the edit window. */
|
2016-04-17 16:09:24 +00:00
|
|
|
char *input_tab(char *buf, bool allow_files, size_t *place,
|
2016-04-26 15:50:25 +00:00
|
|
|
bool *lastwastab, void (*refresh_func)(void), bool *listed)
|
2000-11-05 17:54:41 +00:00
|
|
|
{
|
2007-10-11 15:49:08 +00:00
|
|
|
size_t num_matches = 0, buf_len;
|
2005-02-08 20:37:53 +00:00
|
|
|
char **matches = NULL;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2015-08-29 20:14:57 +00:00
|
|
|
assert(buf != NULL && place != NULL && *place <= strlen(buf) &&
|
2016-04-26 15:50:25 +00:00
|
|
|
lastwastab != NULL && refresh_func != NULL && listed != NULL);
|
|
|
|
|
|
|
|
*listed = FALSE;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
/* If the word starts with `~' and there is no slash in the word,
|
|
|
|
* then try completing this word as a username. */
|
2007-07-01 21:33:17 +00:00
|
|
|
if (*place > 0 && *buf == '~') {
|
2016-04-24 19:24:17 +00:00
|
|
|
const char *slash = strchr(buf, '/');
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2016-04-24 19:24:17 +00:00
|
|
|
if (slash == NULL || slash >= buf + *place)
|
2016-02-23 12:37:10 +00:00
|
|
|
matches = username_tab_completion(buf, &num_matches, *place);
|
2005-02-08 20:37:53 +00:00
|
|
|
}
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
/* Match against files relative to the current working directory. */
|
|
|
|
if (matches == NULL)
|
2015-12-04 21:11:10 +00:00
|
|
|
matches = cwd_tab_completion(buf, allow_files, &num_matches, *place);
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2007-10-11 15:49:08 +00:00
|
|
|
buf_len = strlen(buf);
|
|
|
|
|
|
|
|
if (num_matches == 0 || *place != buf_len)
|
2005-02-08 20:37:53 +00:00
|
|
|
beep();
|
|
|
|
else {
|
|
|
|
size_t match, common_len = 0;
|
2016-04-30 19:22:16 +00:00
|
|
|
char *mzero, *glued;
|
2005-02-22 23:22:37 +00:00
|
|
|
const char *lastslash = revstrstr(buf, "/", buf + *place);
|
2016-02-23 12:37:10 +00:00
|
|
|
size_t lastslash_len = (lastslash == NULL) ? 0 : lastslash - buf + 1;
|
2017-03-30 19:56:31 +00:00
|
|
|
char char1[MAXCHARLEN], char2[MAXCHARLEN];
|
2016-12-26 10:01:40 +00:00
|
|
|
int len1, len2;
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2016-04-16 09:15:33 +00:00
|
|
|
/* Get the number of characters that all matches have in common. */
|
2005-02-08 20:37:53 +00:00
|
|
|
while (TRUE) {
|
2016-12-26 10:01:40 +00:00
|
|
|
len1 = parse_mbchar(matches[0] + common_len, char1, NULL);
|
2016-04-16 09:15:33 +00:00
|
|
|
|
|
|
|
for (match = 1; match < num_matches; match++) {
|
2016-12-26 10:01:40 +00:00
|
|
|
len2 = parse_mbchar(matches[match] + common_len, char2, NULL);
|
|
|
|
|
|
|
|
if (len1 != len2 || strncmp(char1, char2, len2) != 0)
|
2005-02-08 20:37:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-11-25 04:43:43 +00:00
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
if (match < num_matches || matches[0][common_len] == '\0')
|
2000-11-25 04:43:43 +00:00
|
|
|
break;
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2016-12-26 10:01:40 +00:00
|
|
|
common_len += len1;
|
2005-02-08 20:37:53 +00:00
|
|
|
}
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2005-02-22 23:22:37 +00:00
|
|
|
mzero = charalloc(lastslash_len + common_len + 1);
|
2006-10-03 18:46:00 +00:00
|
|
|
|
|
|
|
strncpy(mzero, buf, lastslash_len);
|
|
|
|
strncpy(mzero + lastslash_len, matches[0], common_len);
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2005-02-22 23:22:37 +00:00
|
|
|
common_len += lastslash_len;
|
2006-10-13 16:18:40 +00:00
|
|
|
mzero[common_len] = '\0';
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2016-04-30 19:22:16 +00:00
|
|
|
/* Cover also the case of the user specifying a relative path. */
|
|
|
|
glued = charalloc(strlen(present_path) + strlen(mzero) + 1);
|
|
|
|
sprintf(glued, "%s%s", present_path, mzero);
|
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
assert(common_len >= *place);
|
2000-11-24 14:00:16 +00:00
|
|
|
|
2016-04-30 19:22:16 +00:00
|
|
|
if (num_matches == 1 && (is_dir(mzero) || is_dir(glued))) {
|
2006-10-13 16:35:57 +00:00
|
|
|
mzero[common_len++] = '/';
|
2005-05-26 03:32:41 +00:00
|
|
|
|
2005-02-08 20:37:53 +00:00
|
|
|
assert(common_len > *place);
|
|
|
|
}
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2006-07-19 13:34:55 +00:00
|
|
|
if (num_matches > 1 && (common_len != *place || !*lastwastab))
|
2005-02-08 20:37:53 +00:00
|
|
|
beep();
|
2000-11-14 17:46:06 +00:00
|
|
|
|
2016-02-21 13:33:52 +00:00
|
|
|
/* If the matches have something in common, show that part. */
|
2005-02-08 20:37:53 +00:00
|
|
|
if (common_len != *place) {
|
2005-06-21 22:32:50 +00:00
|
|
|
buf = charealloc(buf, common_len + buf_len - *place + 1);
|
2016-02-23 12:37:10 +00:00
|
|
|
charmove(buf + common_len, buf + *place, buf_len - *place + 1);
|
2005-06-22 00:24:11 +00:00
|
|
|
strncpy(buf, mzero, common_len);
|
2005-05-26 19:48:41 +00:00
|
|
|
*place = common_len;
|
2016-02-21 13:33:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*lastwastab)
|
2005-02-08 20:37:53 +00:00
|
|
|
*lastwastab = TRUE;
|
2016-02-21 13:33:52 +00:00
|
|
|
else if (num_matches > 1) {
|
2008-05-31 23:09:40 +00:00
|
|
|
int longest_name = 0, ncols, editline = 0;
|
2000-11-05 21:54:23 +00:00
|
|
|
|
2016-04-24 19:24:17 +00:00
|
|
|
/* Sort the list of available choices. */
|
2005-02-08 20:37:53 +00:00
|
|
|
qsort(matches, num_matches, sizeof(char *), diralphasort);
|
|
|
|
|
2016-04-24 19:24:17 +00:00
|
|
|
/* Find the length of the longest among the choices. */
|
2005-02-08 20:37:53 +00:00
|
|
|
for (match = 0; match < num_matches; match++) {
|
2016-04-24 19:44:11 +00:00
|
|
|
size_t namelen = strlenpt(matches[match]);
|
2005-05-26 18:20:05 +00:00
|
|
|
|
2016-04-24 19:24:17 +00:00
|
|
|
if (namelen > longest_name)
|
|
|
|
longest_name = namelen;
|
2000-11-05 21:54:23 +00:00
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2016-04-24 19:24:17 +00:00
|
|
|
if (longest_name > COLS - 1)
|
|
|
|
longest_name = COLS - 1;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
2006-07-28 17:06:27 +00:00
|
|
|
/* Each column will be (longest_name + 2) columns wide, i.e.
|
2005-07-18 18:43:39 +00:00
|
|
|
* two spaces between columns, except that there will be
|
|
|
|
* only one space after the last column. */
|
2008-05-31 23:09:40 +00:00
|
|
|
ncols = (COLS + 1) / (longest_name + 2);
|
2000-11-15 01:25:42 +00:00
|
|
|
|
2016-04-24 19:24:17 +00:00
|
|
|
/* Blank the edit window and hide the cursor. */
|
2005-02-08 20:37:53 +00:00
|
|
|
blank_edit();
|
|
|
|
curs_set(0);
|
2016-04-24 19:24:17 +00:00
|
|
|
wmove(edit, 0, 0);
|
2000-11-05 21:56:54 +00:00
|
|
|
|
2016-04-24 19:24:17 +00:00
|
|
|
/* Now print the list of matches out there. */
|
2005-02-08 20:37:53 +00:00
|
|
|
for (match = 0; match < num_matches; match++) {
|
|
|
|
char *disp;
|
2000-11-14 18:25:26 +00:00
|
|
|
|
2016-04-17 18:58:33 +00:00
|
|
|
wmove(edit, editline, (longest_name + 2) * (match % ncols));
|
2000-11-05 21:54:23 +00:00
|
|
|
|
2016-04-17 18:58:33 +00:00
|
|
|
if (match % ncols == 0 && editline == editwinrows - 1 &&
|
2008-05-31 23:09:40 +00:00
|
|
|
num_matches - match > ncols) {
|
2005-02-08 20:37:53 +00:00
|
|
|
waddstr(edit, _("(more)"));
|
|
|
|
break;
|
|
|
|
}
|
2000-11-05 21:54:23 +00:00
|
|
|
|
2016-02-23 12:37:10 +00:00
|
|
|
disp = display_string(matches[match], 0, longest_name, FALSE);
|
2005-02-08 20:37:53 +00:00
|
|
|
waddstr(edit, disp);
|
|
|
|
free(disp);
|
2000-11-05 22:48:35 +00:00
|
|
|
|
2008-05-31 23:09:40 +00:00
|
|
|
if ((match + 1) % ncols == 0)
|
2000-11-05 21:54:23 +00:00
|
|
|
editline++;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
2005-07-01 22:58:47 +00:00
|
|
|
|
2005-07-26 14:42:57 +00:00
|
|
|
wnoutrefresh(edit);
|
2016-04-26 15:50:25 +00:00
|
|
|
*listed = TRUE;
|
2005-02-08 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 19:22:16 +00:00
|
|
|
free(glued);
|
2005-02-08 20:37:53 +00:00
|
|
|
free(mzero);
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
|
2005-06-15 06:30:05 +00:00
|
|
|
free_chararray(matches, num_matches);
|
2005-02-08 20:37:53 +00:00
|
|
|
|
2016-04-26 15:50:25 +00:00
|
|
|
/* When we didn't list any matches now, refresh the edit window, just
|
|
|
|
* in case a previous tab showed a list, so we know where we are. */
|
|
|
|
if (!*listed)
|
2016-04-23 13:38:40 +00:00
|
|
|
refresh_func();
|
|
|
|
|
2000-11-14 17:46:06 +00:00
|
|
|
return buf;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
2017-05-08 17:15:51 +00:00
|
|
|
#endif /* ENABLE_TABCOMP */
|
2001-01-03 07:11:47 +00:00
|
|
|
|
2016-04-23 11:23:49 +00:00
|
|
|
/* Return the filename part of the given path. */
|
|
|
|
const char *tail(const char *path)
|
2004-02-28 16:24:31 +00:00
|
|
|
{
|
2016-04-23 11:23:49 +00:00
|
|
|
const char *slash = strrchr(path, '/');
|
2004-02-28 16:24:31 +00:00
|
|
|
|
2016-04-23 11:23:49 +00:00
|
|
|
if (slash == NULL)
|
|
|
|
return path;
|
2007-04-19 04:12:54 +00:00
|
|
|
else
|
2016-04-23 11:23:49 +00:00
|
|
|
return ++slash;
|
2004-02-28 16:24:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 20:05:24 +00:00
|
|
|
#ifndef DISABLE_HISTORIES
|
|
|
|
/* Return the constructed dirfile path, or NULL if we can't find the home
|
2016-02-23 12:37:10 +00:00
|
|
|
* directory. The string is dynamically allocated, and should be freed. */
|
2011-02-24 02:47:25 +00:00
|
|
|
char *construct_filename(const char *str)
|
2003-01-16 23:44:46 +00:00
|
|
|
{
|
2011-02-16 06:52:30 +00:00
|
|
|
char *newstr = NULL;
|
2003-01-16 23:44:46 +00:00
|
|
|
|
2004-08-17 05:23:38 +00:00
|
|
|
if (homedir != NULL) {
|
|
|
|
size_t homelen = strlen(homedir);
|
2003-01-16 23:44:46 +00:00
|
|
|
|
2011-02-16 06:52:30 +00:00
|
|
|
newstr = charalloc(homelen + strlen(str) + 1);
|
|
|
|
strcpy(newstr, homedir);
|
|
|
|
strcpy(newstr + homelen, str);
|
2003-02-13 22:00:19 +00:00
|
|
|
}
|
2006-10-03 19:15:01 +00:00
|
|
|
|
2011-02-16 06:52:30 +00:00
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *histfilename(void)
|
|
|
|
{
|
2011-02-24 02:47:25 +00:00
|
|
|
return construct_filename("/.nano/search_history");
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 19:02:26 +00:00
|
|
|
/* Construct the legacy history filename. */
|
|
|
|
/* (To be removed in 2018.) */
|
2011-02-16 06:52:30 +00:00
|
|
|
char *legacyhistfilename(void)
|
|
|
|
{
|
|
|
|
return construct_filename("/.nano_history");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *poshistfilename(void)
|
|
|
|
{
|
2011-02-24 02:47:25 +00:00
|
|
|
return construct_filename("/.nano/filepos_history");
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void history_error(const char *msg, ...)
|
|
|
|
{
|
2014-06-20 10:48:26 +00:00
|
|
|
va_list ap;
|
2011-02-16 06:52:30 +00:00
|
|
|
|
|
|
|
va_start(ap, msg);
|
|
|
|
vfprintf(stderr, _(msg), ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
fprintf(stderr, _("\nPress Enter to continue\n"));
|
|
|
|
while (getchar() != '\n')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2014-04-08 12:35:18 +00:00
|
|
|
/* Now that we have more than one history file, let's just rely on a
|
|
|
|
* .nano dir for this stuff. Return 1 if the dir exists or was
|
|
|
|
* successfully created, and return 0 otherwise. */
|
2011-02-16 06:52:30 +00:00
|
|
|
int check_dotnano(void)
|
|
|
|
{
|
2015-07-22 18:02:36 +00:00
|
|
|
int ret = 1;
|
2011-02-16 06:52:30 +00:00
|
|
|
struct stat dirstat;
|
|
|
|
char *nanodir = construct_filename("/.nano");
|
|
|
|
|
|
|
|
if (stat(nanodir, &dirstat) == -1) {
|
|
|
|
if (mkdir(nanodir, S_IRWXU | S_IRWXG | S_IRWXO) == -1) {
|
2014-06-20 10:39:31 +00:00
|
|
|
history_error(N_("Unable to create directory %s: %s\n"
|
2016-05-30 07:09:36 +00:00
|
|
|
"It is required for saving/loading "
|
|
|
|
"search history or cursor positions.\n"),
|
2014-06-20 10:39:31 +00:00
|
|
|
nanodir, strerror(errno));
|
2015-07-22 18:02:36 +00:00
|
|
|
ret = 0;
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(dirstat.st_mode)) {
|
2014-06-20 10:39:31 +00:00
|
|
|
history_error(N_("Path %s is not a directory and needs to be.\n"
|
2016-05-30 07:09:36 +00:00
|
|
|
"Nano will be unable to load or save "
|
|
|
|
"search history or cursor positions.\n"),
|
2014-06-20 10:39:31 +00:00
|
|
|
nanodir);
|
2015-07-22 18:02:36 +00:00
|
|
|
ret = 0;
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
2015-07-22 18:02:36 +00:00
|
|
|
|
|
|
|
free(nanodir);
|
|
|
|
return ret;
|
2004-08-17 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 13:08:23 +00:00
|
|
|
/* Load the search and replace histories from ~/.nano/search_history. */
|
2004-08-17 05:23:38 +00:00
|
|
|
void load_history(void)
|
|
|
|
{
|
|
|
|
char *nanohist = histfilename();
|
2011-02-16 06:52:30 +00:00
|
|
|
char *legacyhist = legacyhistfilename();
|
|
|
|
struct stat hstat;
|
|
|
|
|
2016-09-03 19:02:26 +00:00
|
|
|
/* If there is an old history file, migrate it. */
|
|
|
|
/* (To be removed in 2018.) */
|
2011-02-24 02:47:25 +00:00
|
|
|
if (stat(legacyhist, &hstat) != -1 && stat(nanohist, &hstat) == -1) {
|
2014-04-15 15:02:43 +00:00
|
|
|
if (rename(legacyhist, nanohist) == -1)
|
2014-06-19 13:08:23 +00:00
|
|
|
history_error(N_("Detected a legacy nano history file (%s) which I tried to move\n"
|
|
|
|
"to the preferred location (%s) but encountered an error: %s"),
|
|
|
|
legacyhist, nanohist, strerror(errno));
|
2011-02-16 06:52:30 +00:00
|
|
|
else
|
2014-06-19 13:08:23 +00:00
|
|
|
history_error(N_("Detected a legacy nano history file (%s) which I moved\n"
|
|
|
|
"to the preferred location (%s)\n(see the nano FAQ about this change)"),
|
|
|
|
legacyhist, nanohist);
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
|
|
|
|
2005-04-20 14:55:46 +00:00
|
|
|
/* Assume do_rcfile() has reported a missing home directory. */
|
2004-08-17 05:23:38 +00:00
|
|
|
if (nanohist != NULL) {
|
2006-04-05 21:25:47 +00:00
|
|
|
FILE *hist = fopen(nanohist, "rb");
|
2003-02-13 22:00:19 +00:00
|
|
|
|
2003-09-06 21:44:37 +00:00
|
|
|
if (hist == NULL) {
|
2004-08-11 05:13:08 +00:00
|
|
|
if (errno != ENOENT) {
|
2003-03-11 03:50:40 +00:00
|
|
|
/* Don't save history when we quit. */
|
|
|
|
UNSET(HISTORYLOG);
|
2011-02-16 06:52:30 +00:00
|
|
|
history_error(N_("Error reading %s: %s"), nanohist,
|
2005-02-14 05:00:15 +00:00
|
|
|
strerror(errno));
|
2003-03-11 03:50:40 +00:00
|
|
|
}
|
2003-01-16 23:44:46 +00:00
|
|
|
} else {
|
2006-12-15 04:53:47 +00:00
|
|
|
/* Load a history list (first the search history, then the
|
|
|
|
* replace history) from the oldest entry to the newest.
|
|
|
|
* Assume the last history entry is a blank line. */
|
2005-05-23 16:30:06 +00:00
|
|
|
filestruct **history = &search_history;
|
2004-08-17 05:23:38 +00:00
|
|
|
char *line = NULL;
|
2007-10-11 15:38:32 +00:00
|
|
|
size_t buf_len = 0;
|
2004-08-17 05:23:38 +00:00
|
|
|
ssize_t read;
|
|
|
|
|
2016-12-18 16:56:18 +00:00
|
|
|
while ((read = getline(&line, &buf_len, hist)) > 0) {
|
|
|
|
line[--read] = '\0';
|
2016-12-23 09:43:15 +00:00
|
|
|
if (read > 0) {
|
|
|
|
/* Encode any embedded NUL as 0x0A. */
|
|
|
|
unsunder(line, read);
|
2005-06-02 18:41:31 +00:00
|
|
|
update_history(history, line);
|
2016-12-23 09:43:15 +00:00
|
|
|
} else
|
2003-01-16 23:44:46 +00:00
|
|
|
history = &replace_history;
|
|
|
|
}
|
2005-05-23 16:30:06 +00:00
|
|
|
|
2003-01-16 23:44:46 +00:00
|
|
|
fclose(hist);
|
2004-08-17 05:23:38 +00:00
|
|
|
free(line);
|
2003-01-16 23:44:46 +00:00
|
|
|
}
|
2004-08-17 05:23:38 +00:00
|
|
|
free(nanohist);
|
2011-02-16 06:52:30 +00:00
|
|
|
free(legacyhist);
|
2004-08-17 05:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 17:15:18 +00:00
|
|
|
/* Write the lines of a history list, starting with the line at head, to
|
2005-12-08 07:09:08 +00:00
|
|
|
* the open file at hist. Return TRUE if the write succeeded, and FALSE
|
|
|
|
* otherwise. */
|
2016-12-18 17:15:18 +00:00
|
|
|
bool writehist(FILE *hist, const filestruct *head)
|
2004-08-17 05:23:38 +00:00
|
|
|
{
|
2016-12-18 17:15:18 +00:00
|
|
|
const filestruct *item;
|
2004-08-17 05:23:38 +00:00
|
|
|
|
2017-01-09 17:25:25 +00:00
|
|
|
/* Write a history list, from the oldest item to the newest. */
|
2016-12-18 17:15:18 +00:00
|
|
|
for (item = head; item != NULL; item = item->next) {
|
|
|
|
size_t length = strlen(item->data);
|
2004-08-17 05:23:38 +00:00
|
|
|
|
2016-12-23 09:43:15 +00:00
|
|
|
/* Decode 0x0A bytes as embedded NULs. */
|
|
|
|
sunder(item->data);
|
|
|
|
|
2016-12-18 17:15:18 +00:00
|
|
|
if (fwrite(item->data, sizeof(char), length, hist) < length)
|
|
|
|
return FALSE;
|
|
|
|
if (putc('\n', hist) == EOF)
|
2004-08-17 05:23:38 +00:00
|
|
|
return FALSE;
|
2003-01-16 23:44:46 +00:00
|
|
|
}
|
2005-05-23 16:30:06 +00:00
|
|
|
|
2004-08-17 05:23:38 +00:00
|
|
|
return TRUE;
|
2003-01-16 23:44:46 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 13:08:23 +00:00
|
|
|
/* Save the search and replace histories to ~/.nano/search_history. */
|
2003-01-16 23:44:46 +00:00
|
|
|
void save_history(void)
|
|
|
|
{
|
2004-08-17 05:23:38 +00:00
|
|
|
char *nanohist;
|
2003-01-16 23:44:46 +00:00
|
|
|
|
2005-04-20 14:55:46 +00:00
|
|
|
/* Don't save unchanged or empty histories. */
|
2005-05-23 16:30:06 +00:00
|
|
|
if (!history_has_changed() || (searchbot->lineno == 1 &&
|
2015-08-29 20:14:57 +00:00
|
|
|
replacebot->lineno == 1))
|
2003-01-16 23:44:46 +00:00
|
|
|
return;
|
|
|
|
|
2004-08-17 05:23:38 +00:00
|
|
|
nanohist = histfilename();
|
|
|
|
|
|
|
|
if (nanohist != NULL) {
|
|
|
|
FILE *hist = fopen(nanohist, "wb");
|
2003-02-13 22:00:19 +00:00
|
|
|
|
2004-07-18 17:43:43 +00:00
|
|
|
if (hist == NULL)
|
2015-12-23 13:37:55 +00:00
|
|
|
fprintf(stderr, _("Error writing %s: %s\n"), nanohist,
|
|
|
|
strerror(errno));
|
2004-07-18 17:43:43 +00:00
|
|
|
else {
|
2005-04-20 14:55:46 +00:00
|
|
|
/* Make sure no one else can read from or write to the
|
|
|
|
* history file. */
|
2003-01-16 23:44:46 +00:00
|
|
|
chmod(nanohist, S_IRUSR | S_IWUSR);
|
2004-08-17 05:23:38 +00:00
|
|
|
|
2015-08-29 20:14:57 +00:00
|
|
|
if (!writehist(hist, searchage) || !writehist(hist, replaceage))
|
2015-12-23 13:37:55 +00:00
|
|
|
fprintf(stderr, _("Error writing %s: %s\n"), nanohist,
|
2005-02-14 05:00:15 +00:00
|
|
|
strerror(errno));
|
2005-05-23 16:30:06 +00:00
|
|
|
|
2003-01-16 23:44:46 +00:00
|
|
|
fclose(hist);
|
|
|
|
}
|
2005-05-23 16:30:06 +00:00
|
|
|
|
2003-01-16 23:44:46 +00:00
|
|
|
free(nanohist);
|
|
|
|
}
|
|
|
|
}
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2014-06-19 13:08:23 +00:00
|
|
|
/* Save the recorded last file positions to ~/.nano/filepos_history. */
|
2011-02-16 06:52:30 +00:00
|
|
|
void save_poshistory(void)
|
|
|
|
{
|
2016-01-13 20:08:36 +00:00
|
|
|
char *poshist = poshistfilename();
|
2011-02-18 07:30:57 +00:00
|
|
|
poshiststruct *posptr;
|
2016-01-13 20:08:36 +00:00
|
|
|
FILE *hist;
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2016-01-13 20:08:36 +00:00
|
|
|
if (poshist == NULL)
|
|
|
|
return;
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2016-01-13 20:08:36 +00:00
|
|
|
hist = fopen(poshist, "wb");
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2016-01-13 20:08:36 +00:00
|
|
|
if (hist == NULL)
|
|
|
|
fprintf(stderr, _("Error writing %s: %s\n"), poshist, strerror(errno));
|
|
|
|
else {
|
|
|
|
/* Don't allow others to read or write the history file. */
|
|
|
|
chmod(poshist, S_IRUSR | S_IWUSR);
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2016-01-13 20:08:36 +00:00
|
|
|
for (posptr = position_history; posptr != NULL; posptr = posptr->next) {
|
2016-12-18 17:15:18 +00:00
|
|
|
char *path_and_place;
|
2016-12-18 11:22:58 +00:00
|
|
|
size_t length;
|
|
|
|
|
2016-02-13 17:00:06 +00:00
|
|
|
/* Assume 20 decimal positions each for line and column number,
|
|
|
|
* plus two spaces, plus the line feed, plus the null byte. */
|
2016-12-18 17:15:18 +00:00
|
|
|
path_and_place = charalloc(strlen(posptr->filename) + 44);
|
|
|
|
sprintf(path_and_place, "%s %ld %ld\n", posptr->filename,
|
2016-12-18 15:34:19 +00:00
|
|
|
(long)posptr->lineno, (long)posptr->xno);
|
2016-12-18 17:15:18 +00:00
|
|
|
length = strlen(path_and_place);
|
2016-12-18 11:22:58 +00:00
|
|
|
|
|
|
|
/* Encode newlines in filenames as nulls. */
|
2016-12-18 17:15:18 +00:00
|
|
|
sunder(path_and_place);
|
2016-12-18 11:22:58 +00:00
|
|
|
/* Restore the terminating newline. */
|
2016-12-18 17:15:18 +00:00
|
|
|
path_and_place[length - 1] = '\n';
|
2016-12-18 11:22:58 +00:00
|
|
|
|
2016-12-18 17:15:18 +00:00
|
|
|
if (fwrite(path_and_place, sizeof(char), length, hist) < length)
|
2016-12-18 15:34:19 +00:00
|
|
|
fprintf(stderr, _("Error writing %s: %s\n"),
|
|
|
|
poshist, strerror(errno));
|
2016-12-18 17:15:18 +00:00
|
|
|
free(path_and_place);
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
2016-01-13 20:08:36 +00:00
|
|
|
fclose(hist);
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
2016-01-13 20:08:36 +00:00
|
|
|
free(poshist);
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 13:08:23 +00:00
|
|
|
/* Update the recorded last file positions, given a filename, a line
|
|
|
|
* and a column. If no entry is found, add a new one at the end. */
|
2011-02-18 07:30:57 +00:00
|
|
|
void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos)
|
|
|
|
{
|
2016-01-24 14:49:42 +00:00
|
|
|
poshiststruct *posptr, *theone, *posprev = NULL;
|
2014-06-20 10:48:26 +00:00
|
|
|
char *fullpath = get_full_path(filename);
|
2011-02-18 07:30:57 +00:00
|
|
|
|
2017-05-17 08:38:36 +00:00
|
|
|
if (fullpath == NULL || fullpath[strlen(fullpath) - 1] == '/' || inhelp) {
|
2016-01-29 20:39:12 +00:00
|
|
|
free(fullpath);
|
2014-06-20 10:48:26 +00:00
|
|
|
return;
|
2016-01-29 20:39:12 +00:00
|
|
|
}
|
2011-02-18 07:30:57 +00:00
|
|
|
|
2016-01-24 14:49:42 +00:00
|
|
|
/* Look for a matching filename in the list. */
|
2016-01-12 19:20:40 +00:00
|
|
|
for (posptr = position_history; posptr != NULL; posptr = posptr->next) {
|
2016-01-24 14:49:42 +00:00
|
|
|
if (!strcmp(posptr->filename, fullpath))
|
|
|
|
break;
|
2011-02-18 07:30:57 +00:00
|
|
|
posprev = posptr;
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:49:42 +00:00
|
|
|
/* Don't record files that have the default cursor position. */
|
|
|
|
if (lineno == 1 && xpos == 1) {
|
|
|
|
if (posptr != NULL) {
|
|
|
|
if (posprev == NULL)
|
|
|
|
position_history = posptr->next;
|
|
|
|
else
|
|
|
|
posprev->next = posptr->next;
|
|
|
|
free(posptr->filename);
|
|
|
|
free(posptr);
|
|
|
|
}
|
|
|
|
free(fullpath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-24 14:49:42 +00:00
|
|
|
theone = posptr;
|
2011-02-18 07:30:57 +00:00
|
|
|
|
2016-01-24 14:49:42 +00:00
|
|
|
/* If we didn't find it, make a new node; otherwise, if we're
|
|
|
|
* not at the end, move the matching one to the end. */
|
|
|
|
if (theone == NULL) {
|
|
|
|
theone = (poshiststruct *)nmalloc(sizeof(poshiststruct));
|
|
|
|
theone->filename = mallocstrcpy(NULL, fullpath);
|
|
|
|
if (position_history == NULL)
|
|
|
|
position_history = theone;
|
|
|
|
else
|
|
|
|
posprev->next = theone;
|
|
|
|
} else if (posptr->next != NULL) {
|
2016-01-25 20:26:01 +00:00
|
|
|
if (posprev == NULL)
|
|
|
|
position_history = posptr->next;
|
|
|
|
else
|
|
|
|
posprev->next = posptr->next;
|
2016-01-24 14:49:42 +00:00
|
|
|
while (posptr->next != NULL)
|
|
|
|
posptr = posptr->next;
|
|
|
|
posptr->next = theone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store the last cursor position. */
|
|
|
|
theone->lineno = lineno;
|
|
|
|
theone->xno = xpos;
|
|
|
|
theone->next = NULL;
|
2011-02-18 07:30:57 +00:00
|
|
|
|
|
|
|
free(fullpath);
|
|
|
|
}
|
|
|
|
|
2016-12-23 12:49:14 +00:00
|
|
|
/* Check whether the given file matches an existing entry in the recorded
|
|
|
|
* last file positions. If not, return FALSE. If yes, return TRUE and
|
|
|
|
* set line and column to the retrieved values. */
|
|
|
|
bool has_old_position(const char *file, ssize_t *line, ssize_t *column)
|
2011-02-16 06:52:30 +00:00
|
|
|
{
|
2016-12-23 12:49:14 +00:00
|
|
|
poshiststruct *posptr = position_history;
|
2011-02-16 06:52:30 +00:00
|
|
|
char *fullpath = get_full_path(file);
|
|
|
|
|
|
|
|
if (fullpath == NULL)
|
2016-12-23 12:49:14 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
while (posptr != NULL && strcmp(posptr->filename, fullpath) != 0)
|
|
|
|
posptr = posptr->next;
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2011-02-18 07:30:57 +00:00
|
|
|
free(fullpath);
|
2016-12-23 12:49:14 +00:00
|
|
|
|
|
|
|
if (posptr == NULL)
|
2017-05-05 20:48:54 +00:00
|
|
|
return FALSE;
|
2016-12-23 12:49:14 +00:00
|
|
|
|
|
|
|
*line = posptr->lineno;
|
|
|
|
*column = posptr->xno;
|
|
|
|
return TRUE;
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
|
|
|
|
2014-06-19 13:08:23 +00:00
|
|
|
/* Load the recorded file positions from ~/.nano/filepos_history. */
|
2011-02-16 06:52:30 +00:00
|
|
|
void load_poshistory(void)
|
|
|
|
{
|
2016-01-12 11:16:39 +00:00
|
|
|
char *poshist = poshistfilename();
|
2016-01-12 20:26:59 +00:00
|
|
|
FILE *hist;
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2016-01-12 20:26:59 +00:00
|
|
|
/* If the home directory is missing, do_rcfile() will have reported it. */
|
|
|
|
if (poshist == NULL)
|
|
|
|
return;
|
2011-02-16 06:52:30 +00:00
|
|
|
|
2016-01-12 20:26:59 +00:00
|
|
|
hist = fopen(poshist, "rb");
|
2016-01-12 20:08:31 +00:00
|
|
|
|
2016-01-12 20:26:59 +00:00
|
|
|
if (hist == NULL) {
|
|
|
|
if (errno != ENOENT) {
|
2016-01-13 20:32:40 +00:00
|
|
|
/* When reading failed, don't save history when we quit. */
|
2016-01-12 20:26:59 +00:00
|
|
|
UNSET(POS_HISTORY);
|
|
|
|
history_error(N_("Error reading %s: %s"), poshist, strerror(errno));
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
2016-01-12 20:26:59 +00:00
|
|
|
} else {
|
|
|
|
char *line = NULL, *lineptr, *xptr;
|
|
|
|
size_t buf_len = 0;
|
2016-01-17 17:04:28 +00:00
|
|
|
ssize_t read, count = 0;
|
2016-01-12 20:26:59 +00:00
|
|
|
poshiststruct *record_ptr = NULL, *newrecord;
|
|
|
|
|
|
|
|
/* Read and parse each line, and store the extracted data. */
|
2016-12-19 08:52:30 +00:00
|
|
|
while ((read = getline(&line, &buf_len, hist)) > 5) {
|
|
|
|
/* Decode nulls as embedded newlines. */
|
2016-01-12 20:26:59 +00:00
|
|
|
unsunder(line, read);
|
|
|
|
|
2016-12-19 08:52:30 +00:00
|
|
|
/* Find where the x index and line number are in the line. */
|
|
|
|
xptr = revstrstr(line, " ", line + read - 3);
|
2016-12-21 16:26:12 +00:00
|
|
|
if (xptr == NULL)
|
|
|
|
continue;
|
2016-12-19 08:52:30 +00:00
|
|
|
lineptr = revstrstr(line, " ", xptr - 2);
|
2016-12-21 16:26:12 +00:00
|
|
|
if (lineptr == NULL)
|
|
|
|
continue;
|
2016-12-18 18:57:33 +00:00
|
|
|
|
|
|
|
/* Now separate the three elements of the line. */
|
|
|
|
*(xptr++) = '\0';
|
|
|
|
*(lineptr++) = '\0';
|
2016-01-12 20:26:59 +00:00
|
|
|
|
|
|
|
/* Create a new position record. */
|
|
|
|
newrecord = (poshiststruct *)nmalloc(sizeof(poshiststruct));
|
|
|
|
newrecord->filename = mallocstrcpy(NULL, line);
|
|
|
|
newrecord->lineno = atoi(lineptr);
|
|
|
|
newrecord->xno = atoi(xptr);
|
|
|
|
newrecord->next = NULL;
|
|
|
|
|
|
|
|
/* Add the record to the list. */
|
|
|
|
if (position_history == NULL)
|
|
|
|
position_history = newrecord;
|
|
|
|
else
|
|
|
|
record_ptr->next = newrecord;
|
|
|
|
|
|
|
|
record_ptr = newrecord;
|
2016-01-17 17:04:28 +00:00
|
|
|
|
|
|
|
/* Impose a limit, so the file will not grow indefinitely. */
|
2016-02-10 20:29:23 +00:00
|
|
|
if (++count > 200) {
|
|
|
|
poshiststruct *drop_record = position_history;
|
|
|
|
|
2016-01-17 17:04:28 +00:00
|
|
|
position_history = position_history->next;
|
2016-02-10 20:29:23 +00:00
|
|
|
|
|
|
|
free(drop_record->filename);
|
|
|
|
free(drop_record);
|
|
|
|
}
|
2016-01-12 20:26:59 +00:00
|
|
|
}
|
|
|
|
fclose(hist);
|
|
|
|
free(line);
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
2016-01-12 20:26:59 +00:00
|
|
|
free(poshist);
|
2011-02-16 06:52:30 +00:00
|
|
|
}
|
2014-06-19 20:05:24 +00:00
|
|
|
#endif /* !DISABLE_HISTORIES */
|