blob: af0205f02dc8263f89ee3879588a4512314cf511 [file] [log] [blame]
Chris Allegretta11b00112000-08-06 21:13:45 +00001/* $Id$ */
Chris Allegrettabceb1b22000-06-19 04:22:15 +00002/**************************************************************************
3 * files.c *
4 * *
David Lawrence Ramseyc13b7f02005-01-01 07:28:15 +00005 * Copyright (C) 1999-2005 Chris Allegretta *
Chris Allegrettabceb1b22000-06-19 04:22:15 +00006 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
Chris Allegretta3a24f3f2001-10-24 11:33:54 +00008 * the Free Software Foundation; either version 2, or (at your option) *
Chris Allegrettabceb1b22000-06-19 04:22:15 +00009 * any later version. *
10 * *
David Lawrence Ramsey6e925cf2005-05-15 19:57:17 +000011 * This program is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * General Public License for more details. *
Chris Allegrettabceb1b22000-06-19 04:22:15 +000015 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
David Lawrence Ramsey6e925cf2005-05-15 19:57:17 +000018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
19 * 02110-1301, USA. *
Chris Allegrettabceb1b22000-06-19 04:22:15 +000020 * *
21 **************************************************************************/
22
Jordi Mallach55381aa2004-11-17 23:17:05 +000023#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
Chris Allegretta6efda542001-04-28 18:03:52 +000026
Chris Allegrettabceb1b22000-06-19 04:22:15 +000027#include <stdlib.h>
28#include <string.h>
29#include <stdio.h>
30#include <unistd.h>
Chris Allegrettabb88ece2002-03-25 13:40:39 +000031#include <sys/wait.h>
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +000032#include <utime.h>
Chris Allegrettabceb1b22000-06-19 04:22:15 +000033#include <fcntl.h>
34#include <errno.h>
Chris Allegretta04d848e2000-11-05 17:54:41 +000035#include <ctype.h>
Chris Allegrettaee733e62001-01-22 22:17:08 +000036#include <pwd.h>
Chris Allegretta7662c862003-01-13 01:35:15 +000037#include <assert.h>
Chris Allegrettabceb1b22000-06-19 04:22:15 +000038#include "proto.h"
Chris Allegretta4da1fc62000-06-21 03:00:43 +000039
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +000040static file_format fmt = NIX_FILE;
41 /* The format of the current file. */
Chris Allegretta858d9d92003-01-30 00:53:32 +000042
Chris Allegrettabceb1b22000-06-19 04:22:15 +000043/* What happens when there is no file to open? aiee! */
44void new_file(void)
45{
Chris Allegrettadffa2072002-07-24 01:02:26 +000046 fileage = make_new_node(NULL);
David Lawrence Ramseyf6e4d332005-01-14 17:17:11 +000047 fileage->data = mallocstrcpy(NULL, "");
Chris Allegrettabceb1b22000-06-19 04:22:15 +000048 filebot = fileage;
49 edittop = fileage;
Chris Allegrettabceb1b22000-06-19 04:22:15 +000050 current = fileage;
Chris Allegrettadffa2072002-07-24 01:02:26 +000051 current_x = 0;
Chris Allegrettabceb1b22000-06-19 04:22:15 +000052 totlines = 1;
Chris Allegretta1b3381b2001-09-28 21:59:01 +000053 totsize = 0;
Chris Allegrettae6421972001-07-18 01:03:36 +000054
Chris Allegretta7c27be42002-05-05 23:03:54 +000055#ifdef ENABLE_COLOR
56 update_color();
David Lawrence Ramsey202d3c22005-03-10 20:55:11 +000057 if (!ISSET(NO_COLOR_SYNTAX))
David Lawrence Ramsey760a2dc2004-01-14 06:38:00 +000058 edit_refresh();
Chris Allegretta7c27be42002-05-05 23:03:54 +000059#endif
Chris Allegrettabceb1b22000-06-19 04:22:15 +000060}
61
David Lawrence Ramsey02517e02004-09-05 21:40:31 +000062/* We make a new line of text from buf. buf is length len. If
63 * first_line_ins is TRUE, then we put the new line at the top of the
David Lawrence Ramsey50995bd2005-05-16 20:13:09 +000064 * file. Otherwise, we assume prevnode is the last line of the file,
65 * and put our line after prevnode. */
66filestruct *read_line(char *buf, filestruct *prevnode, bool
67 *first_line_ins, size_t len)
Chris Allegrettabceb1b22000-06-19 04:22:15 +000068{
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +000069 filestruct *fileptr = (filestruct *)nmalloc(sizeof(filestruct));
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +000070
David Lawrence Ramsey02517e02004-09-05 21:40:31 +000071 /* Convert nulls to newlines. len is the string's real length
72 * here. */
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +000073 unsunder(buf, len);
74
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +000075 assert(strlen(buf) == len);
76
77 fileptr->data = mallocstrcpy(NULL, buf);
Chris Allegrettabceb1b22000-06-19 04:22:15 +000078
Chris Allegretta91841892001-09-21 02:37:01 +000079#ifndef NANO_SMALL
David Lawrence Ramseyd1323e42005-06-15 03:56:36 +000080 /* If it's a DOS file ("\r\n"), and file conversion isn't disabled,
81 * strip the '\r' part from fileptr->data. */
David Lawrence Ramseyc7935e22005-06-08 20:12:57 +000082 if (!ISSET(NO_CONVERT) && len > 0 && buf[len - 1] == '\r')
83 fileptr->data[len - 1] = '\0';
Chris Allegretta91841892001-09-21 02:37:01 +000084#endif
85
David Lawrence Ramsey44289752005-06-08 20:13:38 +000086 if (*first_line_ins == TRUE || fileage == NULL) {
David Lawrence Ramseyd1323e42005-06-15 03:56:36 +000087 /* Special case: We're inserting with the cursor on the first
David Lawrence Ramsey02517e02004-09-05 21:40:31 +000088 * line. */
Chris Allegrettabceb1b22000-06-19 04:22:15 +000089 fileptr->prev = NULL;
90 fileptr->next = fileage;
91 fileptr->lineno = 1;
David Lawrence Ramsey928babf2005-03-21 06:12:07 +000092 if (*first_line_ins == TRUE) {
David Lawrence Ramsey02517e02004-09-05 21:40:31 +000093 *first_line_ins = FALSE;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +000094 /* If we're inserting into the first line of the file, then
David Lawrence Ramsey02517e02004-09-05 21:40:31 +000095 * we want to make sure that our edit buffer stays on the
96 * first line and that fileage stays up to date. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +000097 edittop = fileptr;
98 } else
99 filebot = fileptr;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000100 fileage = fileptr;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000101 } else {
David Lawrence Ramsey50995bd2005-05-16 20:13:09 +0000102 assert(prevnode != NULL);
David Lawrence Ramsey928babf2005-03-21 06:12:07 +0000103
David Lawrence Ramsey50995bd2005-05-16 20:13:09 +0000104 fileptr->prev = prevnode;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000105 fileptr->next = NULL;
David Lawrence Ramsey50995bd2005-05-16 20:13:09 +0000106 fileptr->lineno = prevnode->lineno + 1;
107 prevnode->next = fileptr;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000108 }
109
110 return fileptr;
111}
112
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000113/* Load a file into the edit buffer. This takes data from the file
114 * struct. */
115void load_file(void)
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000116{
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000117 current = fileage;
118
119#ifdef ENABLE_MULTIBUFFER
120 /* Add a new entry to the open_files structure. */
121 add_open_file(FALSE);
122
123 /* Reinitialize the shortcut list. */
124 shortcut_init(FALSE);
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000125#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000126}
127
128void read_file(FILE *f, const char *filename)
129{
130 size_t num_lines = 0;
131 /* The number of lines in the file. */
David Lawrence Ramsey96de1b02005-01-27 21:00:10 +0000132 size_t num_chars;
133 /* The number of characters in the file. */
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000134 size_t len = 0;
135 /* The length of the current line of the file. */
136 size_t i = 0;
137 /* The position in the current line of the file. */
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +0000138 size_t bufx = MAX_BUF_SIZE;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000139 /* The size of each chunk of the file that we read. */
140 char input = '\0';
141 /* The current input character. */
142 char *buf;
143 /* The buffer where we store chunks of the file. */
144 filestruct *fileptr = current;
145 /* The current line of the file. */
146 bool first_line_ins = FALSE;
147 /* Whether we're inserting with the cursor on the first line. */
Chris Allegretta0547eb32002-04-22 23:52:34 +0000148 int input_int;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000149 /* The current value we read from the file, whether an input
150 * character or EOF. */
151#ifndef NANO_SMALL
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000152 int format = 0;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000153 /* 0 = *nix, 1 = DOS, 2 = Mac, 3 = both DOS and Mac. */
154#endif
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000155
Chris Allegretta88b09152001-05-17 11:35:43 +0000156 buf = charalloc(bufx);
Chris Allegretta8f6c0692000-07-19 01:16:18 +0000157 buf[0] = '\0';
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000158
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000159 if (current != NULL) {
160 if (current == fileage)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000161 first_line_ins = TRUE;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000162 else
163 fileptr = current->prev;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000164 }
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000165
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000166 /* For the assertion in read_line(), it must be true that if current
167 * is NULL, then so is fileage. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000168 assert(current != NULL || fileage == NULL);
169
David Lawrence Ramsey7c1f17a2004-10-03 13:47:26 +0000170#ifndef NANO_SMALL
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000171 /* We don't know which file format we have yet, so assume it's a
172 * *nix file for now. */
173 fmt = NIX_FILE;
David Lawrence Ramsey7c1f17a2004-10-03 13:47:26 +0000174#endif
175
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000176 /* Read the entire file into the file struct. */
Chris Allegretta0547eb32002-04-22 23:52:34 +0000177 while ((input_int = getc(f)) != EOF) {
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000178 input = (char)input_int;
Chris Allegretta2598c662002-03-28 01:59:34 +0000179
David Lawrence Ramseyd1323e42005-06-15 03:56:36 +0000180 /* If it's a *nix file ("\n") or a DOS file ("\r\n"), and file
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000181 * conversion isn't disabled, handle it! */
Chris Allegrettaf6cba642002-03-26 13:05:54 +0000182 if (input == '\n') {
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000183#ifndef NANO_SMALL
David Lawrence Ramseyd1323e42005-06-15 03:56:36 +0000184 /* If there's a '\r' before the '\n', set format to DOS if
185 * we currently think this is a *nix file, or to both if we
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000186 * currently think it's a Mac file. */
187 if (!ISSET(NO_CONVERT) && i > 0 && buf[i - 1] == '\r' &&
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000188 (format == 0 || format == 2))
189 format++;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000190#endif
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000191
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000192 /* Read in the line properly. */
193 fileptr = read_line(buf, fileptr, &first_line_ins, len);
194
195 /* Reset the line length in preparation for the next
196 * line. */
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000197 len = 0;
198
Adam Rogoyski1e328fb2000-07-08 03:57:16 +0000199 num_lines++;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000200 buf[0] = '\0';
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000201 i = 0;
Chris Allegretta03191762001-09-22 06:38:38 +0000202#ifndef NANO_SMALL
David Lawrence Ramseyd1323e42005-06-15 03:56:36 +0000203 /* If it's a Mac file ('\r' without '\n'), and file conversion
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000204 * isn't disabled, handle it! */
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000205 } else if (!ISSET(NO_CONVERT) && i > 0 && buf[i - 1] == '\r') {
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000206
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000207 /* If we currently think the file is a *nix file, set format
208 * to Mac. If we currently think the file is a DOS file,
209 * set format to both DOS and Mac. */
210 if (format == 0 || format == 1)
211 format += 2;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000212
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000213 /* Read in the line properly. */
214 fileptr = read_line(buf, fileptr, &first_line_ins, len);
215
216 /* Reset the line length in preparation for the next line.
217 * Since we've already read in the next character, reset it
218 * to 1 instead of 0. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000219 len = 1;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000220
Chris Allegretta03191762001-09-22 06:38:38 +0000221 num_lines++;
Chris Allegrettaf6cba642002-03-26 13:05:54 +0000222 buf[0] = input;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000223 buf[1] = '\0';
Chris Allegretta03191762001-09-22 06:38:38 +0000224 i = 1;
225#endif
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000226 } else {
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000227 /* Calculate the total length of the line. It might have
228 * nulls in it, so we can't just use strlen() here. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000229 len++;
230
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +0000231 /* Now we allocate a bigger buffer MAX_BUF_SIZE characters
232 * at a time. If we allocate a lot of space for one line,
233 * we may indeed have to use a buffer this big later on, so
234 * we don't decrease it at all. We do free it at the end,
235 * though. */
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000236 if (i >= bufx - 1) {
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +0000237 bufx += MAX_BUF_SIZE;
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +0000238 buf = charealloc(buf, bufx);
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000239 }
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000240
Chris Allegrettaf6cba642002-03-26 13:05:54 +0000241 buf[i] = input;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +0000242 buf[i + 1] = '\0';
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000243 i++;
244 }
Chris Allegretta0547eb32002-04-22 23:52:34 +0000245 }
246
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +0000247 /* Perhaps this could use some better handling. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000248 if (ferror(f))
249 nperror(filename);
250 fclose(f);
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000251
Chris Allegrettac4e3d9e2002-07-21 15:44:13 +0000252#ifndef NANO_SMALL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000253 /* If file conversion isn't disabled and the last character in this
David Lawrence Ramseyd1323e42005-06-15 03:56:36 +0000254 * file is '\r', read it in properly as a Mac format line. */
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000255 if (len == 0 && !ISSET(NO_CONVERT) && input == '\r') {
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000256 len = 1;
257
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000258 buf[0] = input;
259 buf[1] = '\0';
Chris Allegrettac4e3d9e2002-07-21 15:44:13 +0000260 }
261#endif
Chris Allegretta52c5a6e2002-03-21 05:07:28 +0000262
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000263 /* Did we not get a newline and still have stuff to do? */
264 if (len > 0) {
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000265#ifndef NANO_SMALL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000266 /* If file conversion isn't disabled and the last character in
David Lawrence Ramseyd1323e42005-06-15 03:56:36 +0000267 * this file is '\r', set format to Mac if we currently think
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000268 * the file is a *nix file, or to both DOS and Mac if we
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000269 * currently think the file is a DOS file. */
270 if (!ISSET(NO_CONVERT) && buf[len - 1] == '\r' &&
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000271 (format == 0 || format == 1))
272 format += 2;
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +0000273#endif
274
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000275 /* Read in the last line properly. */
276 fileptr = read_line(buf, fileptr, &first_line_ins, len);
277 num_lines++;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000278 }
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000279
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000280 free(buf);
281
282 /* If we didn't get a file and we don't already have one, make a new
283 * file. */
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000284 if (fileptr == NULL)
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000285 new_file();
Robert Siemborski63b3d7e2000-07-04 22:15:39 +0000286
Chris Allegrettaf6cba642002-03-26 13:05:54 +0000287 /* Did we try to insert a file of 0 bytes? */
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +0000288 if (num_lines != 0) {
289 if (current != NULL) {
290 fileptr->next = current;
291 current->prev = fileptr;
292 renumber(current);
293 current_x = 0;
294 placewewant = 0;
295 } else if (fileptr->next == NULL) {
296 filebot = fileptr;
297 new_magicline();
David Lawrence Ramseydaa533a2005-02-07 05:31:51 +0000298 totsize--;
David Lawrence Ramsey2ab03f62002-10-17 02:19:31 +0000299 }
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000300 }
Chris Allegretta76e291b2001-10-14 19:05:10 +0000301
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000302 get_totals(fileage, filebot, NULL, &num_chars);
303 totsize += num_chars;
304
Chris Allegretta76e291b2001-10-14 19:05:10 +0000305#ifndef NANO_SMALL
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000306 if (format == 3)
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000307 statusbar(
308 P_("Read %lu line (Converted from DOS and Mac format)",
309 "Read %lu lines (Converted from DOS and Mac format)",
310 (unsigned long)num_lines), (unsigned long)num_lines);
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000311 else if (format == 2) {
312 fmt = MAC_FILE;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000313 statusbar(P_("Read %lu line (Converted from Mac format)",
314 "Read %lu lines (Converted from Mac format)",
315 (unsigned long)num_lines), (unsigned long)num_lines);
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +0000316 } else if (format == 1) {
317 fmt = DOS_FILE;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000318 statusbar(P_("Read %lu line (Converted from DOS format)",
319 "Read %lu lines (Converted from DOS format)",
320 (unsigned long)num_lines), (unsigned long)num_lines);
David Lawrence Ramsey7c1f17a2004-10-03 13:47:26 +0000321 } else
Chris Allegretta76e291b2001-10-14 19:05:10 +0000322#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000323 statusbar(P_("Read %lu line", "Read %lu lines",
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000324 (unsigned long)num_lines), (unsigned long)num_lines);
David Lawrence Ramsey7bf00de2003-09-23 04:25:05 +0000325
Adam Rogoyski1e328fb2000-07-08 03:57:16 +0000326 totlines += num_lines;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000327}
328
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000329/* Open the file (and decide if it exists). If newfie is TRUE, display
330 * "New File" if the file is missing. Otherwise, say "[filename] not
331 * found".
332 *
333 * Return -2 if we say "New File". Otherwise, -1 if the file isn't
334 * opened, 0 otherwise. The file might still have an error while
335 * reading with a 0 return value. *f is set to the opened file. */
336int open_file(const char *filename, bool newfie, FILE **f)
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000337{
338 int fd;
339 struct stat fileinfo;
340
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000341 assert(f != NULL);
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000342
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000343 if (filename == NULL || filename[0] == '\0' ||
344 stat(filename, &fileinfo) == -1) {
345 if (newfie) {
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000346 statusbar(_("New File"));
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000347 return -2;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000348 }
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000349 statusbar(_("\"%s\" not found"), filename);
350 return -1;
Chris Allegretta54c1f792003-01-26 04:11:09 +0000351 } else if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) ||
352 S_ISBLK(fileinfo.st_mode)) {
353 /* Don't open character or block files. Sorry, /dev/sndstat! */
David Lawrence Ramseye08765d2004-11-26 20:17:49 +0000354 statusbar(S_ISDIR(fileinfo.st_mode) ? _("\"%s\" is a directory")
355 : _("File \"%s\" is a device file"), filename);
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000356 return -1;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000357 } else if ((fd = open(filename, O_RDONLY)) == -1) {
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000358 statusbar(_("Error reading %s: %s"), filename, strerror(errno));
359 return -1;
360 } else {
David Lawrence Ramseye08765d2004-11-26 20:17:49 +0000361 /* File is A-OK. Open it in binary mode for our own end-of-line
362 * character munging. */
363 *f = fdopen(fd, "rb");
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000364
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000365 if (*f == NULL) {
David Lawrence Ramseye08765d2004-11-26 20:17:49 +0000366 statusbar(_("Error reading %s: %s"), filename,
367 strerror(errno));
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000368 close(fd);
369 } else
370 statusbar(_("Reading File"));
371 }
372 return 0;
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000373}
374
Chris Allegretta48b06702002-02-22 04:30:50 +0000375/* This function will return the name of the first available extension
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +0000376 * of a filename (starting with [name][suffix], then [name][suffix].1,
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000377 * etc.). Memory is allocated for the return value. If no writable
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000378 * extension exists, we return "". */
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +0000379char *get_next_filename(const char *name, const char *suffix)
Chris Allegretta48b06702002-02-22 04:30:50 +0000380{
David Lawrence Ramseyc596c0c2005-04-19 16:32:08 +0000381 unsigned long i = 0;
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000382 char *buf;
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +0000383 size_t namelen, suffixlen;
Chris Allegretta48b06702002-02-22 04:30:50 +0000384
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +0000385 assert(name != NULL && suffix != NULL);
386
387 namelen = strlen(name);
388 suffixlen = strlen(suffix);
389
390 buf = charalloc(namelen + suffixlen + digits(ULONG_MAX) + 2);
391 sprintf(buf, "%s%s", name, suffix);
Chris Allegretta48b06702002-02-22 04:30:50 +0000392
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +0000393 while (TRUE) {
David Lawrence Ramsey049e4a52004-08-10 23:05:59 +0000394 struct stat fs;
Chris Allegretta48b06702002-02-22 04:30:50 +0000395
396 if (stat(buf, &fs) == -1)
Chris Allegretta7162e3d2002-04-06 05:02:14 +0000397 return buf;
David Lawrence Ramseyc596c0c2005-04-19 16:32:08 +0000398 if (i == ULONG_MAX)
Chris Allegretta48b06702002-02-22 04:30:50 +0000399 break;
400
401 i++;
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +0000402 sprintf(buf + namelen + suffixlen, ".%lu", i);
Chris Allegretta48b06702002-02-22 04:30:50 +0000403 }
404
David Lawrence Ramseydac3bca2005-06-01 04:23:52 +0000405 /* We get here only if there is no possible save file. Blank out
406 * the filename to indicate this. */
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000407 null_at(&buf, 0);
David Lawrence Ramsey6a244b62005-04-14 03:52:28 +0000408
Chris Allegretta48b06702002-02-22 04:30:50 +0000409 return buf;
410}
411
David Lawrence Ramsey146bb602004-08-27 21:02:38 +0000412#ifndef NANO_SMALL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000413void execute_command(const char *command)
414{
415#ifdef ENABLE_MULTIBUFFER
416 if (ISSET(MULTIBUFFER)) {
417 /* Update the current entry in the open_files structure. */
418 add_open_file(TRUE);
419 new_file();
420 UNSET(MODIFIED);
421 UNSET(MARK_ISSET);
422 }
423#endif /* ENABLE_MULTIBUFFER */
424 open_pipe(command);
425#ifdef ENABLE_MULTIBUFFER
426 /* Add this new entry to the open_files structure. */
427 if (ISSET(MULTIBUFFER))
428 load_file();
429#endif /* ENABLE_MULTIBUFFER */
430}
431#endif /* !NANO_SMALL */
David Lawrence Ramsey146bb602004-08-27 21:02:38 +0000432
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000433/* name is a file name to open. We make a new buffer if necessary, then
434 * open and read the file. */
435void load_buffer(const char *name)
436{
David Lawrence Ramsey3d002ed2004-10-31 22:45:24 +0000437 bool new_buffer = (fileage == NULL
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000438#ifdef ENABLE_MULTIBUFFER
439 || ISSET(MULTIBUFFER)
Chris Allegretta6fe61492001-05-21 12:56:25 +0000440#endif
David Lawrence Ramsey3d002ed2004-10-31 22:45:24 +0000441 );
David Lawrence Ramsey1c293d22004-11-05 16:22:27 +0000442 /* new_buffer says whether we load into this buffer or a new
443 * one. If new_buffer is TRUE, we display "New File" if the
444 * file is not found, and if it is found we set filename and add
445 * a new open_files entry. */
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000446 FILE *f;
447 int rc;
448 /* rc == -2 means that the statusbar displayed "New File". -1
449 * means that the open failed. 0 means success. */
Chris Allegretta6fe61492001-05-21 12:56:25 +0000450
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000451#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000452 if (check_operating_dir(name, FALSE)) {
David Lawrence Ramsey8a2c0ba2004-11-24 20:36:36 +0000453 statusbar(_("Can't insert file from outside of %s"),
454 operating_dir);
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000455 return;
456 }
Chris Allegretta7662c862003-01-13 01:35:15 +0000457#endif
Chris Allegrettaf7c68112002-09-03 22:58:40 +0000458
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000459#ifdef ENABLE_MULTIBUFFER
460 /* Update the current entry in the open_files structure. */
461 add_open_file(TRUE);
462#endif
463
464 rc = open_file(name, new_buffer, &f);
465
466#ifdef ENABLE_MULTIBUFFER
467 if (rc != -1 && ISSET(MULTIBUFFER)) {
468 UNSET(MODIFIED);
469#ifndef NANO_SMALL
470 UNSET(MARK_ISSET);
471#endif
472 }
473#endif
474
475 if (rc != -1 && new_buffer) {
476 filename = mallocstrcpy(filename, name);
477 new_file();
478 }
479
480 if (rc == 0) {
David Lawrence Ramsey78d644a2004-11-05 16:24:35 +0000481 file_format fmt_save = fmt;
482
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000483 read_file(f, filename);
David Lawrence Ramsey78d644a2004-11-05 16:24:35 +0000484
485 /* If we're not loading into a new buffer, preserve the file
486 * format. */
487 if (!new_buffer)
488 fmt = fmt_save;
489
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000490#ifndef NANO_SMALL
491 stat(filename, &originalfilestat);
492#endif
493 }
494
495 /* Add this new entry to the open_files structure if we have
496 * multibuffer support, or to the main filestruct if we don't. */
497 if (rc != -1 && new_buffer)
498 load_file();
499}
500
David Lawrence Ramseybe908f62004-10-01 18:34:30 +0000501void do_insertfile(
502#ifndef NANO_SMALL
503 bool execute
504#else
505 void
506#endif
507 )
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000508{
509 int i;
510 const char *msg;
David Lawrence Ramsey670a56c2004-09-28 16:03:03 +0000511 char *ans = mallocstrcpy(NULL, "");
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000512 /* The last answer the user typed on the statusbar. */
David Lawrence Ramseyb73c0c02004-11-05 15:25:53 +0000513 filestruct *edittop_save = edittop;
David Lawrence Ramsey036a3d42004-11-15 21:49:21 +0000514 int current_y_save = current_y;
David Lawrence Ramsey4a3879f2004-11-15 20:42:31 +0000515 bool at_edittop = FALSE;
516 /* Whether we're at the top of the edit window. */
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000517
David Lawrence Ramsey487149e2004-10-05 20:24:48 +0000518#ifndef DISABLE_WRAPPING
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000519 wrap_reset();
David Lawrence Ramsey487149e2004-10-05 20:24:48 +0000520#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000521
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000522 while (TRUE) {
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000523#ifndef NANO_SMALL
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000524 if (execute) {
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000525#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000526 if (ISSET(MULTIBUFFER))
527 msg = N_("Command to execute in new buffer [from %s] ");
528 else
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000529#endif
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000530 msg = N_("Command to execute [from %s] ");
531 } else {
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000532#endif
533#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000534 if (ISSET(MULTIBUFFER)) {
535 msg = N_("File to insert into new buffer [from %s] ");
536 } else
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000537#endif
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000538 msg = N_("File to insert [from %s] ");
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000539#ifndef NANO_SMALL
540 }
541#endif
David Lawrence Ramsey04a8d1c2004-09-28 22:14:58 +0000542
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000543 i = statusq(TRUE,
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000544#ifndef NANO_SMALL
David Lawrence Ramseybe908f62004-10-01 18:34:30 +0000545 execute ? extcmd_list :
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +0000546#endif
547 insertfile_list, ans,
Chris Allegretta7662c862003-01-13 01:35:15 +0000548#ifndef NANO_SMALL
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +0000549 NULL,
Chris Allegrettaf7c68112002-09-03 22:58:40 +0000550#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000551 _(msg),
552#ifndef DISABLE_OPERATINGDIR
553 operating_dir != NULL && strcmp(operating_dir, ".") != 0 ?
554 operating_dir :
Chris Allegretta7662c862003-01-13 01:35:15 +0000555#endif
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000556 "./");
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000557
David Lawrence Ramseyfdd3bec2004-11-07 21:30:55 +0000558 /* If we're in multibuffer mode and the filename or command is
559 * blank, open a new buffer instead of canceling. */
David Lawrence Ramseyc136f752004-11-07 18:35:53 +0000560 if (i == -1 || (i == -2
David Lawrence Ramseya084ad92004-11-07 18:22:33 +0000561#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramseyc136f752004-11-07 18:35:53 +0000562 && !ISSET(MULTIBUFFER)
David Lawrence Ramseya084ad92004-11-07 18:22:33 +0000563#endif
David Lawrence Ramsey88ad64d2005-03-30 23:52:02 +0000564 )) {
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000565 statusbar(_("Cancelled"));
566 break;
567 } else {
David Lawrence Ramsey31f99362004-11-05 16:01:04 +0000568 size_t pww_save = placewewant;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000569
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000570 ans = mallocstrcpy(ans, answer);
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000571
David Lawrence Ramsey7e62d102005-06-06 19:25:29 +0000572#ifndef NANO_SMALL
573#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000574 if (i == TOGGLE_MULTIBUFFER_KEY) {
575 /* Don't allow toggling if we're in view mode. */
576 if (!ISSET(VIEW_MODE))
577 TOGGLE(MULTIBUFFER);
578 continue;
David Lawrence Ramsey457847f2005-06-06 19:35:53 +0000579 } else
David Lawrence Ramsey04a8d1c2004-09-28 22:14:58 +0000580#endif
David Lawrence Ramsey7e62d102005-06-06 19:25:29 +0000581 if (i == NANO_TOOTHERINSERT_KEY) {
582 execute = !execute;
583 continue;
584 }
David Lawrence Ramsey457847f2005-06-06 19:35:53 +0000585#ifndef DISABLE_BROWSER
586 else
587#endif
David Lawrence Ramsey7e62d102005-06-06 19:25:29 +0000588#endif /* !NANO_SMALL */
589
Chris Allegretta434d6862003-02-03 04:55:17 +0000590#ifndef DISABLE_BROWSER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000591 if (i == NANO_TOFILES_KEY) {
592 char *tmp = do_browse_from(answer);
Chris Allegretta434d6862003-02-03 04:55:17 +0000593
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000594 if (tmp == NULL)
595 continue;
David Lawrence Ramsey88ad64d2005-03-30 23:52:02 +0000596
David Lawrence Ramseyb49daec2004-10-05 02:46:24 +0000597 free(answer);
598 answer = tmp;
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000599
David Lawrence Ramsey7e62d102005-06-06 19:25:29 +0000600 /* We have a file now. Indicate this and get out of the
601 * statusbar prompt cleanly. */
602 i = 0;
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000603 statusq_abort();
David Lawrence Ramseyb49daec2004-10-05 02:46:24 +0000604 }
Chris Allegrettaf4b96012001-01-03 07:11:47 +0000605#endif
606
David Lawrence Ramsey88ad64d2005-03-30 23:52:02 +0000607 /* If we don't have a file yet, go back to the statusbar
608 * prompt. */
David Lawrence Ramseyd4a071e2005-03-31 17:00:43 +0000609 if (i != 0
610#ifdef ENABLE_MULTIBUFFER
611 && (i != -2 || !ISSET(MULTIBUFFER))
612#endif
613 )
David Lawrence Ramsey88ad64d2005-03-30 23:52:02 +0000614 continue;
615
David Lawrence Ramseyc15802f2004-11-07 18:09:41 +0000616#ifdef ENABLE_MULTIBUFFER
617 if (!ISSET(MULTIBUFFER)) {
618#endif
619 /* If we're not inserting into a new buffer, partition
620 * the filestruct so that it contains no text and hence
David Lawrence Ramsey4a3879f2004-11-15 20:42:31 +0000621 * looks like a new buffer, and keep track of whether
622 * the top of the partition is the top of the edit
623 * window. */
David Lawrence Ramseyc15802f2004-11-07 18:09:41 +0000624 filepart = partition_filestruct(current, current_x,
625 current, current_x);
David Lawrence Ramsey4a3879f2004-11-15 20:42:31 +0000626 at_edittop = (fileage == edittop);
David Lawrence Ramseyc15802f2004-11-07 18:09:41 +0000627#ifdef ENABLE_MULTIBUFFER
628 }
629#endif
630
631#ifndef NANO_SMALL
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000632 if (execute)
633 execute_command(answer);
634 else {
David Lawrence Ramseyd717d4b2004-07-10 16:53:17 +0000635#endif
David Lawrence Ramsey15aaa2c2005-05-28 23:21:30 +0000636 answer = mallocstrassn(answer,
637 real_dir_from_tilde(answer));
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000638 load_buffer(answer);
David Lawrence Ramseyd717d4b2004-07-10 16:53:17 +0000639#ifndef NANO_SMALL
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000640 }
David Lawrence Ramseyd717d4b2004-07-10 16:53:17 +0000641#endif
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000642
Chris Allegretta355fbe52001-07-14 19:32:47 +0000643#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramseyc15802f2004-11-07 18:09:41 +0000644 if (!ISSET(MULTIBUFFER))
645#endif
646 {
647 filestruct *top_save = fileage;
648
David Lawrence Ramsey036a3d42004-11-15 21:49:21 +0000649 /* If we didn't insert into a new buffer, and we were at
650 * the top of the edit window before, set the saved
651 * value of edittop to the new top of the edit window,
652 * and update the current y-coordinate to account for
653 * the number of lines inserted. */
654 if (at_edittop)
655 edittop_save = fileage;
656 current_y += current_y_save;
657
David Lawrence Ramsey56cf0342004-11-15 18:44:30 +0000658 /* If we didn't insert into a new buffer, unpartition
David Lawrence Ramseyc15802f2004-11-07 18:09:41 +0000659 * the filestruct so that it contains all the text
660 * again. Note that we've replaced the non-text
661 * originally in the partition with the text in the
662 * inserted file/executed command output. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +0000663 unpartition_filestruct(&filepart);
David Lawrence Ramseyc15802f2004-11-07 18:09:41 +0000664
665 /* Renumber starting with the beginning line of the old
666 * partition. */
667 renumber(top_save);
668
669 /* Set edittop back to what it was before. */
670 edittop = edittop_save;
671 }
672
673#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000674 if (ISSET(MULTIBUFFER)) {
675 /* Update the titlebar. */
676 titlebar(NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000677
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000678 /* Reinitialize the shortcut list. */
679 shortcut_init(FALSE);
680 } else {
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000681#endif
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000682 /* Mark the file as modified. */
683 set_modified();
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000684
David Lawrence Ramseyb73c0c02004-11-05 15:25:53 +0000685 /* Restore the old place we want. */
David Lawrence Ramsey31f99362004-11-05 16:01:04 +0000686 placewewant = pww_save;
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000687#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000688 }
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000689#endif
Chris Allegretta4dc03d52002-05-11 03:04:44 +0000690
David Lawrence Ramsey045883a2004-10-05 20:11:31 +0000691 /* Refresh the screen. */
692 edit_refresh();
693
694 break;
695 }
David Lawrence Ramsey202d3c22005-03-10 20:55:11 +0000696 }
Chris Allegretta7662c862003-01-13 01:35:15 +0000697
David Lawrence Ramsey670a56c2004-09-28 16:03:03 +0000698 free(ans);
Chris Allegrettabceb1b22000-06-19 04:22:15 +0000699}
700
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000701void do_insertfile_void(void)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000702{
Chris Allegretta355fbe52001-07-14 19:32:47 +0000703#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000704 if (ISSET(VIEW_MODE) && !ISSET(MULTIBUFFER))
705 statusbar(_("Key illegal in non-multibuffer mode"));
Chris Allegretta32da4562002-01-02 15:12:21 +0000706 else
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000707#endif
David Lawrence Ramseybe908f62004-10-01 18:34:30 +0000708 do_insertfile(
709#ifndef NANO_SMALL
710 FALSE
711#endif
712 );
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000713
714 display_main_list();
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000715}
716
Chris Allegretta355fbe52001-07-14 19:32:47 +0000717#ifdef ENABLE_MULTIBUFFER
Chris Allegretta6df90f52002-07-19 01:08:59 +0000718/* Create a new openfilestruct node. */
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000719openfilestruct *make_new_opennode(void)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000720{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000721 openfilestruct *newnode =
722 (openfilestruct *)nmalloc(sizeof(openfilestruct));
Chris Allegretta6df90f52002-07-19 01:08:59 +0000723 newnode->filename = NULL;
David Lawrence Ramsey31b159c2005-05-26 05:17:13 +0000724
Chris Allegretta6df90f52002-07-19 01:08:59 +0000725 return newnode;
726}
727
728/* Splice a node into an existing openfilestruct. */
729void splice_opennode(openfilestruct *begin, openfilestruct *newnode,
David Lawrence Ramseyf7080372004-07-08 17:15:10 +0000730 openfilestruct *end)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000731{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000732 assert(newnode != NULL && begin != NULL);
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000733
Chris Allegretta6df90f52002-07-19 01:08:59 +0000734 newnode->next = end;
735 newnode->prev = begin;
736 begin->next = newnode;
737 if (end != NULL)
738 end->prev = newnode;
739}
740
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000741/* Unlink a node from the rest of the openfilestruct, and delete it. */
742void unlink_opennode(openfilestruct *fileptr)
Chris Allegretta6df90f52002-07-19 01:08:59 +0000743{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000744 assert(fileptr != NULL && fileptr->prev != NULL && fileptr->next != NULL && fileptr != fileptr->prev && fileptr != fileptr->next);
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000745
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000746 fileptr->prev->next = fileptr->next;
747 fileptr->next->prev = fileptr->prev;
748 delete_opennode(fileptr);
Chris Allegretta6df90f52002-07-19 01:08:59 +0000749}
750
751/* Delete a node from the openfilestruct. */
752void delete_opennode(openfilestruct *fileptr)
753{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000754 assert(fileptr != NULL && fileptr->filename != NULL && fileptr->fileage != NULL);
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000755
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000756 free(fileptr->filename);
757 free_filestruct(fileptr->fileage);
758 free(fileptr);
Chris Allegretta6df90f52002-07-19 01:08:59 +0000759}
760
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000761#ifdef DEBUG
762/* Deallocate all memory associated with this and later files, including
763 * the lines of text. */
Chris Allegretta6df90f52002-07-19 01:08:59 +0000764void free_openfilestruct(openfilestruct *src)
765{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000766 assert(src != NULL);
David Lawrence Ramsey23c44502005-01-27 20:49:07 +0000767
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000768 while (src != src->next) {
769 src = src->next;
770 delete_opennode(src->prev);
Chris Allegretta6df90f52002-07-19 01:08:59 +0000771 }
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000772 delete_opennode(src);
Chris Allegretta6df90f52002-07-19 01:08:59 +0000773}
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000774#endif
Chris Allegretta6df90f52002-07-19 01:08:59 +0000775
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000776/* Add/update an entry to the open_files openfilestruct. If update is
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000777 * FALSE, a new entry is created; otherwise, the current entry is
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000778 * updated. */
David Lawrence Ramsey3e189a82004-10-03 19:18:48 +0000779void add_open_file(bool update)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000780{
David Lawrence Ramsey31b159c2005-05-26 05:17:13 +0000781 if (update && open_files == NULL)
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000782 return;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000783
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000784 /* If there are no entries in open_files, make the first one. */
785 if (open_files == NULL) {
786 open_files = make_new_opennode();
787 splice_opennode(open_files, open_files, open_files);
788 /* Otherwise, if we're not updating, make a new entry for
789 * open_files and splice it in after the current entry. */
790 } else if (!update) {
791 splice_opennode(open_files, make_new_opennode(),
792 open_files->next);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000793 open_files = open_files->next;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000794 }
795
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000796 /* Save the current filename. */
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000797 open_files->filename = mallocstrcpy(open_files->filename, filename);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000798
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000799#ifndef NANO_SMALL
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000800 /* Save the current file's stat. */
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000801 open_files->originalfilestat = originalfilestat;
802#endif
803
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000804 /* Save the current file buffer. */
805 open_files->fileage = fileage;
806 open_files->filebot = filebot;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000807
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000808 /* Save the current top of the edit window. */
809 open_files->edittop = edittop;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000810
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000811 /* Save the current line. */
812 open_files->current = current;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000813
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000814 /* Save the current cursor position. */
815 open_files->current_x = current_x;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000816
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000817 /* Save the current place we want. */
818 open_files->placewewant = placewewant;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000819
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000820 /* Save the current total number of lines. */
821 open_files->totlines = totlines;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000822
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000823 /* Save the current total size. */
824 open_files->totsize = totsize;
David Lawrence Ramseycb34a672004-02-06 21:20:05 +0000825
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000826 /* Start with no flags saved. */
827 open_files->flags = 0;
828
829 /* Save the current modification status. */
830 if (ISSET(MODIFIED))
831 open_files->flags |= MODIFIED;
832
David Lawrence Ramseya3370c42004-04-05 01:08:14 +0000833#ifndef NANO_SMALL
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000834 /* Save the current marking status and mark, if applicable. */
835 if (ISSET(MARK_ISSET)) {
836 open_files->flags |= MARK_ISSET;
837 open_files->mark_beginbuf = mark_beginbuf;
838 open_files->mark_beginx = mark_beginx;
Chris Allegretta4dc03d52002-05-11 03:04:44 +0000839 }
840
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000841 /* Save the current file format. */
842 open_files->fmt = fmt;
843#endif
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000844
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000845#ifdef DEBUG
Jordi Mallachf9390af2003-08-05 19:31:12 +0000846 fprintf(stderr, "filename is %s\n", open_files->filename);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000847#endif
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000848}
849
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000850/* Read the current entry in the open_files structure and set up the
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000851 * currently open file buffer using that entry's information. */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000852void load_open_file(void)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000853{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000854 assert(open_files != NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000855
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000856 /* Restore the current filename. */
Chris Allegrettaf2387fb2002-04-10 02:31:20 +0000857 filename = mallocstrcpy(filename, open_files->filename);
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000858
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000859#ifndef NANO_SMALL
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000860 /* Restore the current file's stat. */
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +0000861 originalfilestat = open_files->originalfilestat;
862#endif
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000863
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000864 /* Restore the current file buffer. */
865 fileage = open_files->fileage;
866 filebot = open_files->filebot;
867
868 /* Restore the current top of the edit window. */
869 edittop = open_files->edittop;
870
871 /* Restore the current line. */
872 current = open_files->current;
873
874 /* Restore the current cursor position. */
875 current_x = open_files->current_x;
876
877 /* Restore the current place we want. */
878 placewewant = open_files->placewewant;
879
880 /* Restore the current total number of lines. */
881 totlines = open_files->totlines;
882
883 /* Restore the current total size. */
884 totsize = open_files->totsize;
885
886 /* Restore the current modification status. */
887 if (open_files->flags & MODIFIED)
Chris Allegretta4dc03d52002-05-11 03:04:44 +0000888 SET(MODIFIED);
889 else
890 UNSET(MODIFIED);
891
892#ifndef NANO_SMALL
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000893 /* Restore the current marking status and mark, if applicable. */
894 if (open_files->flags & MARK_ISSET) {
895 mark_beginbuf = open_files->mark_beginbuf;
896 mark_beginx = open_files->mark_beginx;
Chris Allegretta4dc03d52002-05-11 03:04:44 +0000897 SET(MARK_ISSET);
898 } else
899 UNSET(MARK_ISSET);
David Lawrence Ramsey7c1f17a2004-10-03 13:47:26 +0000900
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000901 /* Restore the current file format. */
902 fmt = open_files->fmt;
Chris Allegretta4dc03d52002-05-11 03:04:44 +0000903#endif
Chris Allegrettab3655b42001-10-22 03:15:31 +0000904
Chris Allegretta7662c862003-01-13 01:35:15 +0000905#ifdef ENABLE_COLOR
906 update_color();
907#endif
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000908 edit_refresh();
Chris Allegretta7662c862003-01-13 01:35:15 +0000909
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000910 /* Update the titlebar. */
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000911 titlebar(NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000912}
913
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000914/* Open either the next or previous file buffer. */
David Lawrence Ramseyd26f1eb2005-06-15 03:01:41 +0000915void open_prevnext_file(bool next_file)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000916{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000917 assert(open_files != NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000918
David Lawrence Ramseyd26f1eb2005-06-15 03:01:41 +0000919 add_open_file(TRUE);
920
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000921 /* If only one file buffer is open, indicate it on the statusbar and
922 * get out. */
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000923 if (open_files == open_files->next) {
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000924 statusbar(_("No more open file buffers"));
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000925 return;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000926 }
927
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000928 /* Switch to the next or previous file, depending on the value of
929 * next. */
David Lawrence Ramseyd26f1eb2005-06-15 03:01:41 +0000930 open_files = next_file ? open_files->next : open_files->prev;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000931
932#ifdef DEBUG
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000933 fprintf(stderr, "filename is %s\n", open_files->filename);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000934#endif
935
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000936 /* Load the file we switched to. */
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000937 load_open_file();
938
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000939 /* And indicate the switch on the statusbar. */
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000940 statusbar(_("Switched to %s"),
David Lawrence Ramsey3e189a82004-10-03 19:18:48 +0000941 ((open_files->filename[0] == '\0') ? _("New Buffer") :
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000942 open_files->filename));
Chris Allegrettaf5de33a2002-02-27 04:14:16 +0000943
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000944#ifdef DEBUG
945 dump_buffer(current);
946#endif
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000947}
948
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000949/* Open the previous entry in the open_files structure. This function
950 * is used by the shortcut list. */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000951void open_prevfile_void(void)
Chris Allegrettaa8c22572002-02-15 19:17:02 +0000952{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000953 open_prevnext_file(FALSE);
Chris Allegrettaa8c22572002-02-15 19:17:02 +0000954}
955
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000956/* Open the next entry in the open_files structure. This function is
957 * used by the shortcut list. */
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +0000958void open_nextfile_void(void)
Chris Allegrettaa8c22572002-02-15 19:17:02 +0000959{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000960 open_prevnext_file(TRUE);
Chris Allegrettaa8c22572002-02-15 19:17:02 +0000961}
962
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000963/* Delete an entry from the open_files filestruct. After deletion of an
964 * entry, the next entry is opened. Return TRUE on success or FALSE if
David Lawrence Ramsey3ece0b92004-12-01 15:11:27 +0000965 * there are no more open file buffers. */
David Lawrence Ramsey3e189a82004-10-03 19:18:48 +0000966bool close_open_file(void)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000967{
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000968 assert(open_files != NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000969
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000970 /* If only one file is open, get out. */
971 if (open_files == open_files->next)
David Lawrence Ramsey3e189a82004-10-03 19:18:48 +0000972 return FALSE;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000973
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000974 /* Open the next file. */
975 open_nextfile_void();
Chris Allegretta7162e3d2002-04-06 05:02:14 +0000976
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000977 /* Close the file we had open before. */
978 unlink_opennode(open_files->prev);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000979
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000980 /* Reinitialize the shortcut list. */
David Lawrence Ramseyebd0d7c2004-07-01 18:59:52 +0000981 shortcut_init(FALSE);
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000982 display_main_list();
David Lawrence Ramsey5b3dd0f2004-11-25 04:39:07 +0000983
David Lawrence Ramsey3e189a82004-10-03 19:18:48 +0000984 return TRUE;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000985}
David Lawrence Ramsey02517e02004-09-05 21:40:31 +0000986#endif /* ENABLE_MULTIBUFFER */
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000987
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +0000988/* When passed "[relative path]" or "[relative path][filename]" in
Chris Allegrettae1f14522001-09-19 03:19:43 +0000989 * origpath, return "[full path]" or "[full path][filename]" on success,
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +0000990 * or NULL on error. Do this if the file doesn't exist but the relative
991 * path does, since the file could exist in memory but not yet on disk).
992 * Don't do this if the relative path doesn't exist, since we won't be
993 * able to go there. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +0000994char *get_full_path(const char *origpath)
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000995{
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +0000996 char *d_here, *d_there = NULL;
Chris Allegretta2d7893d2001-07-11 02:08:33 +0000997
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +0000998 if (origpath == NULL)
999 return NULL;
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001000
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001001 /* Get the current directory. */
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001002 d_here = charalloc(PATH_MAX + 1);
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001003 d_here = getcwd(d_here, PATH_MAX + 1);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001004
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001005 if (d_here != NULL) {
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001006 const char *last_slash;
1007 char *d_there_file = NULL;
1008 bool path_only;
1009 struct stat fileinfo;
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001010
David Lawrence Ramsey6e0ed3c2005-03-26 04:42:28 +00001011 align(&d_here);
1012
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001013 /* If the current directory isn't "/", tack a slash onto the end
1014 * of it. */
David Lawrence Ramseyb8c479a2004-07-31 14:10:23 +00001015 if (strcmp(d_here, "/") != 0) {
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001016 d_here = charealloc(d_here, strlen(d_here) + 2);
Chris Allegrettace78c1e2001-09-23 01:18:03 +00001017 strcat(d_here, "/");
1018 }
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001019
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001020 d_there = real_dir_from_tilde(origpath);
Chris Allegrettae1f14522001-09-19 03:19:43 +00001021
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001022 assert(d_there != NULL);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001023
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001024 /* Stat d_there. If stat() fails, assume that d_there refers to
1025 * a new file that hasn't been saved to disk yet. Set path_only
1026 * to TRUE if d_there refers to a directory, and FALSE if
1027 * d_there refers to a file. */
1028 path_only = !stat(d_there, &fileinfo) &&
1029 S_ISDIR(fileinfo.st_mode);
1030
1031 /* If path_only is TRUE, make sure d_there ends in a slash. */
Chris Allegrettae1f14522001-09-19 03:19:43 +00001032 if (path_only) {
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001033 size_t d_there_len = strlen(d_there);
1034
1035 if (d_there[d_there_len - 1] != '/') {
1036 d_there = charealloc(d_there, d_there_len + 2);
Chris Allegrettae1f14522001-09-19 03:19:43 +00001037 strcat(d_there, "/");
Chris Allegrettae1f14522001-09-19 03:19:43 +00001038 }
1039 }
1040
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001041 /* Search for the last slash in d_there. */
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001042 last_slash = strrchr(d_there, '/');
1043
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001044 /* If we didn't find one, then make sure the answer is in the
1045 * format "d_here/d_there". */
1046 if (last_slash == NULL) {
1047 assert(!path_only);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001048
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001049 d_there_file = d_there;
1050 d_there = d_here;
1051 } else {
1052 /* If path_only is FALSE, then save the filename portion of
1053 * the answer, everything after the last slash, in
1054 * d_there_file. */
1055 if (!path_only)
1056 d_there_file = mallocstrcpy(NULL, last_slash + 1);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001057
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001058 /* And remove the filename portion of the answer from
1059 * d_there. */
1060 null_at(&d_there, last_slash - d_there + 1);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001061
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001062 /* Go to the path specified in d_there. */
1063 if (chdir(d_there) == -1) {
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001064 free(d_there);
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001065 d_there = NULL;
1066 } else {
1067 /* Get the full path and save it in d_there. */
1068 free(d_there);
Chris Allegrettace78c1e2001-09-23 01:18:03 +00001069
David Lawrence Ramsey6e0ed3c2005-03-26 04:42:28 +00001070 d_there = charalloc(PATH_MAX + 1);
1071 d_there = getcwd(d_there, PATH_MAX + 1);
1072
1073 if (d_there != NULL) {
1074 align(&d_there);
1075
1076 if (strcmp(d_there, "/") != 0) {
1077 /* Make sure d_there ends in a slash. */
1078 d_there = charealloc(d_there,
1079 strlen(d_there) + 2);
1080 strcat(d_there, "/");
1081 }
1082 } else
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001083 /* If we couldn't get the full path, set path_only
1084 * to TRUE so that we clean up correctly, free all
1085 * allocated memory, and return NULL. */
1086 path_only = TRUE;
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001087
1088 /* Finally, go back to the path specified in d_here,
1089 * where we were before. */
1090 chdir(d_here);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001091 }
1092
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001093 /* Free d_here, since we're done using it. */
1094 free(d_here);
Chris Allegrettae1f14522001-09-19 03:19:43 +00001095 }
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001096
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001097 /* At this point, if path_only is FALSE and d_there exists,
1098 * d_there contains the path portion of the answer and
1099 * d_there_file contains the filename portion of the answer. If
1100 * this is the case, tack d_there_file onto the end of
1101 * d_there, so that d_there contains the complete answer. */
David Lawrence Ramsey6e0ed3c2005-03-26 04:42:28 +00001102 if (!path_only && d_there != NULL) {
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001103 d_there = charealloc(d_there, strlen(d_there) +
1104 strlen(d_there_file) + 1);
1105 strcat(d_there, d_there_file);
1106 }
1107
1108 /* Free d_there_file, since we're done using it. */
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001109 free(d_there_file);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001110 }
1111
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001112 return d_there;
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001113}
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001114
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001115/* Return the full version of path, as returned by get_full_path(). On
1116 * error, if path doesn't reference a directory, or if the directory
1117 * isn't writable, return NULL. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001118char *check_writable_directory(const char *path)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001119{
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001120 char *full_path = get_full_path(path);
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001121
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001122 /* If get_full_path() fails, return NULL. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001123 if (full_path == NULL)
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001124 return NULL;
Chris Allegrettabc72e362002-02-16 20:03:44 +00001125
David Lawrence Ramseyd7ca85c2005-02-09 18:56:21 +00001126 /* If we can't write to path or path isn't a directory, return
1127 * NULL. */
1128 if (access(full_path, W_OK) != 0 ||
David Lawrence Ramseybec2f202005-05-31 03:39:59 +00001129 full_path[strlen(full_path) - 1] != '/') {
Chris Allegrettabc72e362002-02-16 20:03:44 +00001130 free(full_path);
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001131 return NULL;
Chris Allegrettabc72e362002-02-16 20:03:44 +00001132 }
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001133
David Lawrence Ramsey8d07efa2005-06-03 01:33:04 +00001134 /* Otherwise, return the full path. */
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001135 return full_path;
1136}
1137
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001138/* This function calls mkstemp(($TMPDIR|P_tmpdir|/tmp/)"nano.XXXXXX").
1139 * On success, it returns the malloc()ed filename and corresponding FILE
David Lawrence Ramsey3db02382005-06-05 19:18:11 +00001140 * stream, opened in "r+b" mode. On error, it returns NULL for the
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001141 * filename and leaves the FILE stream unchanged. */
1142char *safe_tempfile(FILE **f)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00001143{
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001144 char *full_tempdir = NULL;
David Lawrence Ramsey6fdc81a2005-06-05 19:33:27 +00001145 const char *tmpdir_env;
1146 int fd;
1147 mode_t original_umask = 0;
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001148
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001149 assert(f != NULL);
1150
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001151 /* If $TMPDIR is set and non-empty, set tempdir to it, run it
1152 * through get_full_path(), and save the result in full_tempdir.
1153 * Otherwise, leave full_tempdir set to NULL. */
David Lawrence Ramsey6fdc81a2005-06-05 19:33:27 +00001154 tmpdir_env = getenv("TMPDIR");
1155 if (tmpdir_env != NULL && tmpdir_env[0] != '\0')
1156 full_tempdir = check_writable_directory(tmpdir_env);
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001157
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001158 /* If $TMPDIR is unset, empty, or not a writable directory, and
1159 * full_tempdir is NULL, try P_tmpdir instead. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001160 if (full_tempdir == NULL)
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001161 full_tempdir = check_writable_directory(P_tmpdir);
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001162
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001163 /* if P_tmpdir is NULL, use /tmp. */
1164 if (full_tempdir == NULL)
1165 full_tempdir = mallocstrcpy(NULL, "/tmp/");
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001166
David Lawrence Ramsey70047ee2003-06-14 20:41:34 +00001167 full_tempdir = charealloc(full_tempdir, strlen(full_tempdir) + 12);
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001168 strcat(full_tempdir, "nano.XXXXXX");
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001169
David Lawrence Ramsey6fdc81a2005-06-05 19:33:27 +00001170 original_umask = umask(0);
1171 umask(S_IRWXG | S_IRWXO);
1172
1173 fd = mkstemp(full_tempdir);
1174
1175 if (fd != -1)
1176 *f = fdopen(fd, "r+b");
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001177 else {
1178 free(full_tempdir);
1179 full_tempdir = NULL;
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001180 }
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001181
David Lawrence Ramsey6fdc81a2005-06-05 19:33:27 +00001182 umask(original_umask);
1183
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001184 return full_tempdir;
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001185}
Chris Allegrettae1f14522001-09-19 03:19:43 +00001186
1187#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001188/* Initialize full_operating_dir based on operating_dir. */
1189void init_operating_dir(void)
1190{
1191 assert(full_operating_dir == NULL);
1192
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001193 if (operating_dir == NULL)
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001194 return;
David Lawrence Ramsey807681a2004-02-27 20:50:01 +00001195
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001196 full_operating_dir = get_full_path(operating_dir);
1197
1198 /* If get_full_path() failed or the operating directory is
David Lawrence Ramsey95b7bdb2004-02-27 03:21:23 +00001199 * inaccessible, unset operating_dir. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001200 if (full_operating_dir == NULL || chdir(full_operating_dir) == -1) {
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001201 free(full_operating_dir);
1202 full_operating_dir = NULL;
1203 free(operating_dir);
1204 operating_dir = NULL;
1205 }
1206}
1207
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001208/* Check to see if we're inside the operating directory. Return FALSE
1209 * if we are, or TRUE otherwise. If allow_tabcomp is TRUE, allow
1210 * incomplete names that would be matches for the operating directory,
1211 * so that tab completion will work. */
1212bool check_operating_dir(const char *currpath, bool allow_tabcomp)
Chris Allegrettae1f14522001-09-19 03:19:43 +00001213{
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001214 /* The char *full_operating_dir is global for mem cleanup. It
1215 * should have already been initialized by init_operating_dir().
1216 * Also, a relative operating directory path will only be handled
1217 * properly if this is done. */
Chris Allegrettae1f14522001-09-19 03:19:43 +00001218
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001219 char *fullpath;
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001220 bool retval = FALSE;
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001221 const char *whereami1, *whereami2 = NULL;
Chris Allegrettae1f14522001-09-19 03:19:43 +00001222
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001223 /* If no operating directory is set, don't bother doing anything. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001224 if (operating_dir == NULL)
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001225 return FALSE;
David Lawrence Ramsey6a2877d2004-02-28 02:08:15 +00001226
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001227 assert(full_operating_dir != NULL);
Chris Allegrettae1f14522001-09-19 03:19:43 +00001228
Chris Allegrettae1f14522001-09-19 03:19:43 +00001229 fullpath = get_full_path(currpath);
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001230
1231 /* fullpath == NULL means some directory in the path doesn't exist
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001232 * or is unreadable. If allow_tabcomp is FALSE, then currpath is
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001233 * what the user typed somewhere. We don't want to report a
1234 * non-existent directory as being outside the operating directory,
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001235 * so we return FALSE. If allow_tabcomp is TRUE, then currpath
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001236 * exists, but is not executable. So we say it isn't in the
1237 * operating directory. */
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001238 if (fullpath == NULL)
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001239 return allow_tabcomp;
Chris Allegrettae1f14522001-09-19 03:19:43 +00001240
1241 whereami1 = strstr(fullpath, full_operating_dir);
1242 if (allow_tabcomp)
1243 whereami2 = strstr(full_operating_dir, fullpath);
1244
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001245 /* If both searches failed, we're outside the operating directory.
1246 * Otherwise, check the search results; if the full operating
1247 * directory path is not at the beginning of the full current path
1248 * (for normal usage) and vice versa (for tab completion, if we're
1249 * allowing it), we're outside the operating directory. */
Chris Allegrettae1f14522001-09-19 03:19:43 +00001250 if (whereami1 != fullpath && whereami2 != full_operating_dir)
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001251 retval = TRUE;
1252 free(fullpath);
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001253
1254 /* Otherwise, we're still inside it. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001255 return retval;
Chris Allegrettae1f14522001-09-19 03:19:43 +00001256}
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001257#endif
1258
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001259#ifndef NANO_SMALL
1260void init_backup_dir(void)
1261{
1262 char *full_backup_dir;
1263
1264 if (backup_dir == NULL)
1265 return;
1266
1267 full_backup_dir = get_full_path(backup_dir);
1268
1269 /* If get_full_path() failed or the backup directory is
1270 * inaccessible, unset backup_dir. */
1271 if (full_backup_dir == NULL ||
1272 full_backup_dir[strlen(full_backup_dir) - 1] != '/') {
1273 free(full_backup_dir);
1274 free(backup_dir);
1275 backup_dir = NULL;
1276 } else {
1277 free(backup_dir);
1278 backup_dir = full_backup_dir;
1279 }
1280}
1281#endif
1282
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001283/* Read from inn, write to out. We assume inn is opened for reading,
1284 * and out for writing. We return 0 on success, -1 on read error, -2 on
1285 * write error. */
1286int copy_file(FILE *inn, FILE *out)
1287{
David Lawrence Ramseydf2a8252005-05-28 23:51:03 +00001288 char buf[BUFSIZ];
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001289 size_t charsread;
1290 int retval = 0;
1291
1292 assert(inn != NULL && out != NULL);
David Lawrence Ramsey37059242005-04-19 20:13:13 +00001293
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001294 do {
1295 charsread = fread(buf, sizeof(char), BUFSIZ, inn);
1296 if (charsread == 0 && ferror(inn)) {
1297 retval = -1;
1298 break;
1299 }
1300 if (fwrite(buf, sizeof(char), charsread, out) < charsread) {
1301 retval = -2;
1302 break;
1303 }
1304 } while (charsread > 0);
David Lawrence Ramsey37059242005-04-19 20:13:13 +00001305
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001306 if (fclose(inn) == EOF)
1307 retval = -1;
1308 if (fclose(out) == EOF)
1309 retval = -2;
David Lawrence Ramsey37059242005-04-19 20:13:13 +00001310
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001311 return retval;
1312}
1313
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001314/* Write a file out. If f_open isn't NULL, we assume that it is a
1315 * stream associated with the file, and we don't try to open it
1316 * ourselves. If tmp is TRUE, we set the umask to disallow anyone else
1317 * from accessing the file, we don't set the global variable filename to
David Lawrence Ramsey81dabf22005-06-05 19:20:36 +00001318 * its name, and we don't print out how many lines we wrote on the
1319 * statusbar.
Chris Allegretta7662c862003-01-13 01:35:15 +00001320 *
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001321 * tmp means we are writing a temporary file in a secure fashion. We
1322 * use it when spell checking or dumping the file on an error.
Chris Allegrettacc197ef2001-05-29 04:21:44 +00001323 *
Chris Allegretta0e9b7aa2002-04-16 03:15:47 +00001324 * append == 1 means we are appending instead of overwriting.
1325 * append == 2 means we are prepending instead of overwriting.
Chris Allegrettaecc3d7f2001-06-05 23:24:55 +00001326 *
David Lawrence Ramsey2f0d03b2004-05-28 00:15:28 +00001327 * nonamechange means don't change the current filename. It is ignored
1328 * if tmp is FALSE or if we're appending/prepending.
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001329 *
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001330 * Return 0 on success or -1 on error. */
1331int write_file(const char *name, FILE *f_open, bool tmp, int append,
1332 bool nonamechange)
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001333{
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001334 int retval = -1;
1335 /* Instead of returning in this function, you should always
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001336 * merely set retval and then goto cleanup_and_exit. */
1337 size_t lineswritten = 0;
1338 const filestruct *fileptr = fileage;
Chris Allegretta0547eb32002-04-22 23:52:34 +00001339 int fd;
David Lawrence Ramsey0af86042004-07-07 23:26:08 +00001340 /* The file descriptor we use. */
David Lawrence Ramsey25529072004-07-06 14:02:44 +00001341 mode_t original_umask = 0;
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001342 /* Our umask, from when nano started. */
David Lawrence Ramsey8c16bac2004-11-06 15:10:57 +00001343 bool realexists;
David Lawrence Ramseyc8c69d52004-07-03 03:22:23 +00001344 /* The result of stat(). TRUE if the file exists, FALSE
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001345 * otherwise. If name is a link that points nowhere, realexists
David Lawrence Ramseyc8c69d52004-07-03 03:22:23 +00001346 * is FALSE. */
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001347 struct stat st;
1348 /* The status fields filled in by stat(). */
David Lawrence Ramsey8c16bac2004-11-06 15:10:57 +00001349 bool anyexists;
David Lawrence Ramsey0af86042004-07-07 23:26:08 +00001350 /* The result of lstat(). Same as realexists unless name is a
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001351 * link. */
1352 struct stat lst;
1353 /* The status fields filled in by lstat(). */
1354 char *realname;
David Lawrence Ramsey0af86042004-07-07 23:26:08 +00001355 /* name after tilde expansion. */
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001356 FILE *f = NULL;
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001357 /* The actual file, realname, we are writing to. */
1358 char *tempname = NULL;
1359 /* The temp file name we write to on prepend. */
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001360
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001361 assert(name != NULL);
David Lawrence Ramsey894d80f2005-03-31 05:26:06 +00001362
David Lawrence Ramsey951d7142004-10-04 15:23:47 +00001363 if (name[0] == '\0')
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001364 return -1;
David Lawrence Ramseyac71fdd2005-04-15 17:48:20 +00001365
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001366 if (f_open != NULL)
1367 f = f_open;
1368
Chris Allegrettaa0d89972003-02-03 03:32:08 +00001369 if (!tmp)
1370 titlebar(NULL);
Chris Allegretta0f5dfef2000-11-24 14:02:57 +00001371
Chris Allegrettabe77c612000-11-24 14:00:16 +00001372 realname = real_dir_from_tilde(name);
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001373
Chris Allegrettae1f14522001-09-19 03:19:43 +00001374#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001375 /* If we're writing a temporary file, we're probably going outside
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001376 * the operating directory, so skip the operating directory test. */
David Lawrence Ramsey1b9d3f92005-02-11 20:09:11 +00001377 if (!tmp && check_operating_dir(realname, FALSE)) {
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001378 statusbar(_("Can't write outside of %s"), operating_dir);
1379 goto cleanup_and_exit;
Chris Allegrettae1f14522001-09-19 03:19:43 +00001380 }
1381#endif
1382
David Lawrence Ramsey8c16bac2004-11-06 15:10:57 +00001383 anyexists = (lstat(realname, &lst) != -1);
David Lawrence Ramseye08765d2004-11-26 20:17:49 +00001384
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001385 /* If the temp file exists and isn't already open, give up. */
1386 if (tmp && anyexists && f_open == NULL)
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001387 goto cleanup_and_exit;
David Lawrence Ramseye08765d2004-11-26 20:17:49 +00001388
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001389 /* If NOFOLLOW_SYMLINKS is set, it doesn't make sense to prepend or
1390 * append to a symlink. Here we warn about the contradiction. */
1391 if (ISSET(NOFOLLOW_SYMLINKS) && anyexists && S_ISLNK(lst.st_mode)) {
David Lawrence Ramseyc8c69d52004-07-03 03:22:23 +00001392 statusbar(
1393 _("Cannot prepend or append to a symlink with --nofollow set"));
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001394 goto cleanup_and_exit;
1395 }
1396
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001397 /* Save the state of file at the end of the symlink (if there is
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001398 * one). */
David Lawrence Ramsey8c16bac2004-11-06 15:10:57 +00001399 realexists = (stat(realname, &st) != -1);
Chris Allegrettaf7ee9e62000-12-02 21:13:50 +00001400
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001401#ifndef NANO_SMALL
1402 /* We backup only if the backup toggle is set, the file isn't
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001403 * temporary, and the file already exists. Furthermore, if we
1404 * aren't appending, prepending, or writing a selection, we backup
1405 * only if the file has not been modified by someone else since nano
1406 * opened it. */
David Lawrence Ramsey8c16bac2004-11-06 15:10:57 +00001407 if (ISSET(BACKUP_FILE) && !tmp && realexists &&
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001408 (append != 0 || ISSET(MARK_ISSET) ||
1409 originalfilestat.st_mtime == st.st_mtime)) {
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001410 FILE *backup_file;
1411 char *backupname;
1412 struct utimbuf filetime;
1413 int copy_status;
1414
1415 /* Save the original file's access and modification times. */
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001416 filetime.actime = originalfilestat.st_atime;
1417 filetime.modtime = originalfilestat.st_mtime;
1418
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001419 if (f_open == NULL) {
1420 /* Open the original file to copy to the backup. */
1421 f = fopen(realname, "rb");
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +00001422
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001423 if (f == NULL) {
1424 statusbar(_("Error reading %s: %s"), realname,
1425 strerror(errno));
1426 goto cleanup_and_exit;
1427 }
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001428 }
1429
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001430 /* If backup_dir is set, we set backupname to
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001431 * backup_dir/backupname~[.number], where backupname is the
1432 * canonicalized absolute pathname of realname with every '/'
1433 * replaced with a '!'. This means that /home/foo/file is
1434 * backed up in backup_dir/!home!foo!file~[.number]. */
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001435 if (backup_dir != NULL) {
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001436 char *backuptemp = get_full_path(realname);
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001437
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001438 if (backuptemp == NULL)
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001439 /* If get_full_path() failed, we don't have a
1440 * canonicalized absolute pathname, so just use the
1441 * filename portion of the pathname. We use tail() so
1442 * that e.g. ../backupname will be backed up in
1443 * backupdir/backupname~ instead of
1444 * backupdir/../backupname~. */
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001445 backuptemp = mallocstrcpy(NULL, tail(realname));
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001446 else {
David Lawrence Ramsey15aaa2c2005-05-28 23:21:30 +00001447 size_t i = 0;
1448
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001449 for (; backuptemp[i] != '\0'; i++) {
1450 if (backuptemp[i] == '/')
1451 backuptemp[i] = '!';
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001452 }
1453 }
1454
1455 backupname = charalloc(strlen(backup_dir) +
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001456 strlen(backuptemp) + 1);
1457 sprintf(backupname, "%s%s", backup_dir, backuptemp);
1458 free(backuptemp);
1459 backuptemp = get_next_filename(backupname, "~");
1460 if (backuptemp[0] == '\0') {
1461 statusbar(_("Error writing %s: %s"), backupname,
1462 _("Too many backup files?"));
1463 free(backuptemp);
1464 free(backupname);
1465 fclose(f);
1466 goto cleanup_and_exit;
1467 } else {
1468 free(backupname);
1469 backupname = backuptemp;
1470 }
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00001471 } else {
1472 backupname = charalloc(strlen(realname) + 2);
1473 sprintf(backupname, "%s~", realname);
1474 }
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001475
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001476 /* Open the destination backup file. Before we write to it, we
1477 * set its permissions, so no unauthorized person can read it as
1478 * we write. */
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001479 backup_file = fopen(backupname, "wb");
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +00001480
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001481 if (backup_file == NULL ||
1482 chmod(backupname, originalfilestat.st_mode) == -1) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001483 statusbar(_("Error writing %s: %s"), backupname,
1484 strerror(errno));
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001485 free(backupname);
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001486 if (backup_file != NULL)
1487 fclose(backup_file);
1488 fclose(f);
1489 goto cleanup_and_exit;
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001490 }
1491
1492#ifdef DEBUG
Jordi Mallachf9390af2003-08-05 19:31:12 +00001493 fprintf(stderr, "Backing up %s to %s\n", realname, backupname);
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001494#endif
1495
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001496 /* Copy the file. */
1497 copy_status = copy_file(f, backup_file);
David Lawrence Ramseye08765d2004-11-26 20:17:49 +00001498
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001499 /* And set metadata. */
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +00001500 if (copy_status != 0 ||
1501 chown(backupname, originalfilestat.st_uid,
1502 originalfilestat.st_gid) == -1 ||
1503 utime(backupname, &filetime) == -1) {
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001504 free(backupname);
1505 if (copy_status == -1)
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001506 statusbar(_("Error reading %s: %s"), realname,
1507 strerror(errno));
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001508 else
1509 statusbar(_("Error writing %s: %s"), backupname,
1510 strerror(errno));
1511 goto cleanup_and_exit;
1512 }
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001513
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001514 free(backupname);
1515 }
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001516#endif /* !NANO_SMALL */
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001517
David Lawrence Ramseyac71fdd2005-04-15 17:48:20 +00001518 /* If NOFOLLOW_SYMLINKS is set and the file is a link, we aren't
1519 * doing prepend or append. So we delete the link first, and just
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001520 * overwrite. */
1521 if (ISSET(NOFOLLOW_SYMLINKS) && anyexists && S_ISLNK(lst.st_mode) &&
1522 unlink(realname) == -1) {
1523 statusbar(_("Error writing %s: %s"), realname, strerror(errno));
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001524 goto cleanup_and_exit;
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001525 }
Chris Allegretta07f9ee02000-12-04 05:15:39 +00001526
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001527 if (f_open == NULL) {
1528 original_umask = umask(0);
David Lawrence Ramseyc8c69d52004-07-03 03:22:23 +00001529
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001530 /* If we create a temp file, we don't let anyone else access it.
David Lawrence Ramsey6fdc81a2005-06-05 19:33:27 +00001531 * We create a temp file if tmp is TRUE. */
1532 if (tmp)
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001533 umask(S_IRWXG | S_IRWXO);
David Lawrence Ramsey6fdc81a2005-06-05 19:33:27 +00001534 else
1535 umask(original_umask);
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001536 }
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001537
David Lawrence Ramseyc8c69d52004-07-03 03:22:23 +00001538 /* If we're prepending, copy the file to a temp file. */
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001539 if (append == 2) {
1540 int fd_source;
1541 FILE *f_source = NULL;
1542
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001543 tempname = safe_tempfile(&f);
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +00001544
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001545 if (tempname == NULL) {
1546 statusbar(_("Prepending to %s failed: %s"), realname,
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001547 strerror(errno));
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001548 goto cleanup_and_exit;
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001549 }
Chris Allegretta07f9ee02000-12-04 05:15:39 +00001550
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001551 if (f_open == NULL) {
1552 fd_source = open(realname, O_RDONLY | O_CREAT);
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +00001553
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001554 if (fd_source != -1) {
1555 f_source = fdopen(fd_source, "rb");
1556 if (f_source == NULL) {
1557 statusbar(_("Error reading %s: %s"), realname,
1558 strerror(errno));
1559 close(fd_source);
1560 fclose(f);
1561 unlink(tempname);
1562 goto cleanup_and_exit;
1563 }
1564 }
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001565 }
1566
1567 if (copy_file(f_source, f) != 0) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001568 statusbar(_("Error writing %s: %s"), tempname,
1569 strerror(errno));
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001570 unlink(tempname);
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001571 goto cleanup_and_exit;
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001572 }
1573 }
1574
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001575 if (f_open == NULL) {
1576 /* Now open the file in place. Use O_EXCL if tmp is TRUE. This
1577 * is copied from joe, because wiggy says so *shrug*. */
1578 fd = open(realname, O_WRONLY | O_CREAT |
1579 ((append == 1) ? O_APPEND : (tmp ? O_EXCL : O_TRUNC)),
1580 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
1581 S_IWOTH);
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001582
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001583 /* Set the umask back to the user's original value. */
1584 umask(original_umask);
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001585
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001586 /* If we couldn't open the file, give up. */
1587 if (fd == -1) {
1588 statusbar(_("Error writing %s: %s"), realname,
1589 strerror(errno));
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +00001590
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001591 /* tempname has been set only if we're prepending. */
1592 if (tempname != NULL)
1593 unlink(tempname);
1594 goto cleanup_and_exit;
1595 }
Chris Allegretta0547eb32002-04-22 23:52:34 +00001596
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001597 f = fdopen(fd, (append == 1) ? "ab" : "wb");
David Lawrence Ramsey1a1ea3d2005-05-28 23:32:45 +00001598
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001599 if (f == NULL) {
1600 statusbar(_("Error writing %s: %s"), realname,
1601 strerror(errno));
1602 close(fd);
1603 goto cleanup_and_exit;
1604 }
Chris Allegretta0547eb32002-04-22 23:52:34 +00001605 }
1606
David Lawrence Ramsey32d19ce2004-05-24 05:05:07 +00001607 /* There might not be a magicline. There won't be when writing out
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001608 * a selection. */
1609 assert(fileage != NULL && filebot != NULL);
David Lawrence Ramseyac71fdd2005-04-15 17:48:20 +00001610
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001611 while (fileptr != filebot) {
David Lawrence Ramsey1a1ea3d2005-05-28 23:32:45 +00001612 size_t data_len = strlen(fileptr->data), size;
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001613
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001614 /* Newlines to nulls, just before we write to disk. */
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001615 sunder(fileptr->data);
1616
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001617 size = fwrite(fileptr->data, sizeof(char), data_len, f);
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001618
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001619 /* Nulls to newlines; data_len is the string's real length. */
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001620 unsunder(fileptr->data, data_len);
1621
Chris Allegretta0547eb32002-04-22 23:52:34 +00001622 if (size < data_len) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001623 statusbar(_("Error writing %s: %s"), realname,
1624 strerror(errno));
Chris Allegrettab2751752002-04-23 10:22:33 +00001625 fclose(f);
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001626 goto cleanup_and_exit;
Chris Allegrettab2751752002-04-23 10:22:33 +00001627 }
David Lawrence Ramseyac71fdd2005-04-15 17:48:20 +00001628
Chris Allegretta91841892001-09-21 02:37:01 +00001629#ifndef NANO_SMALL
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001630 if (fmt == DOS_FILE || fmt == MAC_FILE) {
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001631 if (putc('\r', f) == EOF) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001632 statusbar(_("Error writing %s: %s"), realname,
1633 strerror(errno));
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001634 fclose(f);
1635 goto cleanup_and_exit;
1636 }
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001637 }
Chris Allegretta8fa1e282001-09-22 04:20:25 +00001638
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001639 if (fmt != MAC_FILE) {
Chris Allegretta91841892001-09-21 02:37:01 +00001640#endif
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001641 if (putc('\n', f) == EOF) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001642 statusbar(_("Error writing %s: %s"), realname,
1643 strerror(errno));
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001644 fclose(f);
1645 goto cleanup_and_exit;
1646 }
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001647#ifndef NANO_SMALL
1648 }
1649#endif
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001650
1651 fileptr = fileptr->next;
1652 lineswritten++;
1653 }
1654
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001655 /* If we're prepending, open the temp file, and append it to f. */
1656 if (append == 2) {
1657 int fd_source;
1658 FILE *f_source = NULL;
David Lawrence Ramseyf21cd102002-06-13 00:40:19 +00001659
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001660 fd_source = open(tempname, O_RDONLY | O_CREAT);
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001661
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001662 if (fd_source != -1) {
1663 f_source = fdopen(fd_source, "rb");
1664 if (f_source == NULL)
1665 close(fd_source);
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001666 }
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001667
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001668 if (f_source == NULL) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001669 statusbar(_("Error reading %s: %s"), tempname,
1670 strerror(errno));
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001671 fclose(f);
1672 goto cleanup_and_exit;
1673 }
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001674
David Lawrence Ramsey2c4e34c2004-10-22 03:30:39 +00001675 if (copy_file(f_source, f) == -1 || unlink(tempname) == -1) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001676 statusbar(_("Error writing %s: %s"), realname,
1677 strerror(errno));
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001678 goto cleanup_and_exit;
1679 }
1680 } else if (fclose(f) == EOF) {
1681 statusbar(_("Error writing %s: %s"), realname, strerror(errno));
1682 unlink(tempname);
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001683 goto cleanup_and_exit;
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001684 }
1685
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001686 if (!tmp && append == 0) {
Chris Allegretta7662c862003-01-13 01:35:15 +00001687 if (!nonamechange) {
Chris Allegrettaecc3d7f2001-06-05 23:24:55 +00001688 filename = mallocstrcpy(filename, realname);
Chris Allegretta7662c862003-01-13 01:35:15 +00001689#ifdef ENABLE_COLOR
1690 update_color();
David Lawrence Ramsey202d3c22005-03-10 20:55:11 +00001691 if (!ISSET(NO_COLOR_SYNTAX))
David Lawrence Ramsey760a2dc2004-01-14 06:38:00 +00001692 edit_refresh();
Chris Allegretta7662c862003-01-13 01:35:15 +00001693#endif
1694 }
Chris Allegrettaecc3d7f2001-06-05 23:24:55 +00001695
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001696#ifndef NANO_SMALL
1697 /* Update originalfilestat to reference the file as it is now. */
1698 stat(filename, &originalfilestat);
1699#endif
David Lawrence Ramseybdfa9272005-06-14 23:36:13 +00001700 statusbar(P_("Wrote %lu line", "Wrote %lu lines",
1701 (unsigned long)lineswritten),
David Lawrence Ramsey5b2f17e2005-04-19 21:47:01 +00001702 (unsigned long)lineswritten);
Chris Allegretta1bd0ce22000-12-06 05:56:08 +00001703 UNSET(MODIFIED);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00001704 titlebar(NULL);
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001705 }
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001706
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001707 retval = 0;
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001708
1709 cleanup_and_exit:
1710 free(realname);
David Lawrence Ramsey82138502003-12-24 08:03:54 +00001711 free(tempname);
David Lawrence Ramseyb9b57222005-05-29 02:22:55 +00001712
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001713 return retval;
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001714}
1715
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001716#ifndef NANO_SMALL
1717/* Write a marked selection from a file out. First, set fileage and
1718 * filebot as the top and bottom of the mark, respectively. Then call
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001719 * write_file() with the values of name, f_open, temp, and append, and
1720 * with nonamechange set to TRUE so that we don't change the current
David Lawrence Ramsey2f0d03b2004-05-28 00:15:28 +00001721 * filename. Finally, set fileage and filebot back to their old values
1722 * and return. */
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001723int write_marked(const char *name, FILE *f_open, bool tmp, int append)
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001724{
1725 int retval = -1;
David Lawrence Ramsey7cfea8b2004-10-08 15:35:33 +00001726 bool old_modified = ISSET(MODIFIED);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001727 /* write_file() unsets the MODIFIED flag. */
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001728 bool added_magicline;
1729 /* Whether we added a magicline after filebot. */
1730 filestruct *top, *bot;
1731 size_t top_x, bot_x;
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001732
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001733 /* Partition the filestruct so that it contains only the marked
1734 * text. */
1735 mark_order((const filestruct **)&top, &top_x,
David Lawrence Ramsey90e59c12004-11-05 23:03:03 +00001736 (const filestruct **)&bot, &bot_x, NULL);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001737 filepart = partition_filestruct(top, top_x, bot, bot_x);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001738
1739 /* If the line at filebot is blank, treat it as the magicline and
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001740 * hence the end of the file. Otherwise, add a magicline and treat
1741 * it as the end of the file. */
1742 added_magicline = (filebot->data[0] != '\0');
1743 if (added_magicline)
1744 new_magicline();
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001745
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001746 retval = write_file(name, f_open, tmp, append, TRUE);
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001747
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001748 /* If we added a magicline, remove it now. */
1749 if (added_magicline)
1750 remove_magicline();
1751
1752 /* Unpartition the filestruct so that it contains all the text
1753 * again. */
David Lawrence Ramsey74d87072004-11-22 00:16:23 +00001754 unpartition_filestruct(&filepart);
David Lawrence Ramsey40bdb682004-11-03 22:03:41 +00001755
David Lawrence Ramsey7cfea8b2004-10-08 15:35:33 +00001756 if (old_modified)
David Lawrence Ramsey35961c42004-01-23 19:34:03 +00001757 set_modified();
1758
1759 return retval;
1760}
1761#endif /* !NANO_SMALL */
1762
David Lawrence Ramsey951d7142004-10-04 15:23:47 +00001763int do_writeout(bool exiting)
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001764{
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001765 int i;
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001766 int retval = 0, append = 0;
1767 char *ans;
1768 /* The last answer the user typed on the statusbar. */
Chris Allegretta8a0de3b2000-11-24 20:45:14 +00001769#ifdef NANO_EXTRA
David Lawrence Ramsey056c5ef2004-10-05 16:14:19 +00001770 static bool did_cred = FALSE;
Chris Allegretta8a0de3b2000-11-24 20:45:14 +00001771#endif
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001772
Chris Allegretta6b58acd2001-04-12 03:01:53 +00001773 currshortcut = writefile_list;
Chris Allegretta6fe61492001-05-21 12:56:25 +00001774
David Lawrence Ramseyedab0cc2004-07-03 03:09:12 +00001775 if (exiting && filename[0] != '\0' && ISSET(TEMP_FILE)) {
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001776 retval = write_file(filename, NULL, FALSE, 0, FALSE);
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001777
1778 /* Write succeeded. */
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001779 if (retval == 0)
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001780 return retval;
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001781 }
1782
Chris Allegretta500b5e32001-06-21 23:58:47 +00001783#ifndef NANO_SMALL
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001784 if (ISSET(MARK_ISSET) && !exiting)
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001785 ans = mallocstrcpy(NULL, "");
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001786 else
1787#endif
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001788 ans = mallocstrcpy(NULL, filename);
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001789
1790 while (TRUE) {
1791 const char *msg;
1792#ifndef NANO_SMALL
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001793 const char *formatstr, *backupstr;
1794
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +00001795 if (fmt == DOS_FILE)
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001796 formatstr = N_(" [DOS Format]");
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +00001797 else if (fmt == MAC_FILE)
David Lawrence Ramsey360191c2004-10-01 21:37:36 +00001798 formatstr = N_(" [Mac Format]");
Chris Allegrettaa8c22572002-02-15 19:17:02 +00001799 else
1800 formatstr = "";
1801
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001802 if (ISSET(BACKUP_FILE))
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001803 backupstr = N_(" [Backup]");
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001804 else
1805 backupstr = "";
1806
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001807 /* Be nice to the translation folks. */
Chris Allegretta9519eb02001-09-27 20:46:25 +00001808 if (ISSET(MARK_ISSET) && !exiting) {
Chris Allegretta0e9b7aa2002-04-16 03:15:47 +00001809 if (append == 2)
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001810 msg = N_("Prepend Selection to File");
Chris Allegretta7662c862003-01-13 01:35:15 +00001811 else if (append == 1)
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001812 msg = N_("Append Selection to File");
Chris Allegretta9519eb02001-09-27 20:46:25 +00001813 else
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001814 msg = N_("Write Selection to File");
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001815 } else
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001816#endif /* !NANO_SMALL */
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001817 if (append == 2)
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001818 msg = N_("File Name to Prepend to");
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001819 else if (append == 1)
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001820 msg = N_("File Name to Append to");
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001821 else
David Lawrence Ramsey576bf332004-07-12 03:10:30 +00001822 msg = N_("File Name to Write");
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001823
David Lawrence Ramseyf7b5d932004-07-05 14:27:29 +00001824 /* If we're using restricted mode, the filename isn't blank,
1825 * and we're at the "Write File" prompt, disable tab
1826 * completion. */
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00001827 i = statusq(!ISSET(RESTRICTED) || filename[0] == '\0',
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001828 writefile_list, ans,
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001829#ifndef NANO_SMALL
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001830 NULL, "%s%s%s", _(msg), formatstr, backupstr
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001831#else
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001832 "%s", _(msg)
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001833#endif
1834 );
1835
David Lawrence Ramsey951d7142004-10-04 15:23:47 +00001836 if (i < 0) {
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001837 statusbar(_("Cancelled"));
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001838 retval = -1;
1839 break;
1840 } else {
1841 ans = mallocstrcpy(ans, answer);
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001842
Rocco Corsiaf5c3022001-01-12 07:51:05 +00001843#ifndef DISABLE_BROWSER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001844 if (i == NANO_TOFILES_KEY) {
1845 char *tmp = do_browse_from(answer);
Chris Allegretta6fe61492001-05-21 12:56:25 +00001846
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001847 currshortcut = writefile_list;
1848
1849 if (tmp == NULL)
1850 continue;
1851 free(answer);
1852 answer = tmp;
1853
1854 /* We have a file now. Get out of the statusbar prompt
1855 * cleanly. */
1856 statusq_abort();
1857 } else
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001858#endif /* !DISABLE_BROWSER */
Chris Allegrettacf287c82002-07-20 13:57:41 +00001859#ifndef NANO_SMALL
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001860 if (i == TOGGLE_DOS_KEY) {
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +00001861 fmt = (fmt == DOS_FILE) ? NIX_FILE : DOS_FILE;
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001862 continue;
1863 } else if (i == TOGGLE_MAC_KEY) {
David Lawrence Ramsey3e0c8a62004-11-04 04:08:18 +00001864 fmt = (fmt == MAC_FILE) ? NIX_FILE : MAC_FILE;
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001865 continue;
1866 } else if (i == TOGGLE_BACKUP_KEY) {
1867 TOGGLE(BACKUP_FILE);
1868 continue;
1869 } else
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00001870#endif /* !NANO_SMALL */
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001871 if (i == NANO_PREPEND_KEY) {
1872 append = (append == 2) ? 0 : 2;
1873 continue;
1874 } else if (i == NANO_APPEND_KEY) {
1875 append = (append == 1) ? 0 : 1;
1876 continue;
1877 }
Chris Allegrettaf4b96012001-01-03 07:11:47 +00001878
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001879#ifdef DEBUG
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001880 fprintf(stderr, "filename is %s\n", answer);
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001881#endif
Chris Allegretta8a0de3b2000-11-24 20:45:14 +00001882
1883#ifdef NANO_EXTRA
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001884 if (exiting && !ISSET(TEMP_FILE) &&
1885 strcasecmp(answer, "zzy") == 0 && !did_cred) {
1886 do_credits();
1887 did_cred = TRUE;
1888 retval = -1;
1889 break;
David Lawrence Ramseybc503c82003-11-19 23:59:14 +00001890 }
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001891#endif
1892 if (append == 0 && strcmp(answer, filename) != 0) {
1893 struct stat st;
1894
1895 if (!stat(answer, &st)) {
1896 i = do_yesno(FALSE, _("File exists, OVERWRITE ? "));
1897 if (i == 0 || i == -1)
1898 continue;
1899 /* If we're using restricted mode, we aren't allowed to
1900 * change the name of a file once it has one because
1901 * that would allow reading from or writing to files not
1902 * specified on the command line. In this case, don't
1903 * bother showing the "Different Name" prompt. */
1904 } else if (!ISSET(RESTRICTED) && filename[0] != '\0'
1905#ifndef NANO_SMALL
1906 && (exiting || !ISSET(MARK_ISSET))
1907#endif
1908 ) {
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001909 i = do_yesno(FALSE,
1910 _("Save file under DIFFERENT NAME ? "));
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001911 if (i == 0 || i == -1)
1912 continue;
1913 }
1914 }
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001915
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001916#ifndef NANO_SMALL
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001917 /* Here's where we allow the selected text to be written to
1918 * a separate file. If we're using restricted mode, this is
1919 * disabled since it allows reading from or writing to files
1920 * not specified on the command line. */
1921 if (!ISSET(RESTRICTED) && !exiting && ISSET(MARK_ISSET))
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001922 retval = write_marked(answer, NULL, FALSE, append);
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001923 else
David Lawrence Ramsey5db0cdc2002-06-28 22:45:14 +00001924#endif /* !NANO_SMALL */
David Lawrence Ramsey5e068c62005-05-31 04:28:15 +00001925 retval = write_file(answer, NULL, FALSE, append, FALSE);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001926
Chris Allegretta355fbe52001-07-14 19:32:47 +00001927#ifdef ENABLE_MULTIBUFFER
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001928 /* If we're not about to exit, update the current entry in
1929 * the open_files structure. */
1930 if (!exiting)
1931 add_open_file(TRUE);
Chris Allegretta2d7893d2001-07-11 02:08:33 +00001932#endif
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001933
1934 break;
1935 }
David Lawrence Ramsey6aec4b82004-03-15 20:26:30 +00001936 } /* while (TRUE) */
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001937
1938 free(ans);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001939
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001940 return retval;
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001941}
1942
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001943void do_writeout_void(void)
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001944{
David Lawrence Ramsey72e51ab2004-07-02 14:31:03 +00001945 do_writeout(FALSE);
David Lawrence Ramsey045883a2004-10-05 20:11:31 +00001946 display_main_list();
Chris Allegrettabceb1b22000-06-19 04:22:15 +00001947}
Chris Allegretta04d848e2000-11-05 17:54:41 +00001948
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001949/* Return a malloc()ed string containing the actual directory, used to
1950 * convert ~user/ and ~/ notation. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001951char *real_dir_from_tilde(const char *buf)
Chris Allegrettabe77c612000-11-24 14:00:16 +00001952{
Chris Allegrettad865da12002-07-29 23:46:38 +00001953 char *dirtmp = NULL;
Chris Allegrettae5e4d492001-01-23 03:09:15 +00001954
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001955 if (buf == NULL)
1956 return NULL;
1957
Chris Allegrettabe77c612000-11-24 14:00:16 +00001958 if (buf[0] == '~') {
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001959 size_t i;
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001960 const char *tilde_dir;
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001961
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001962 /* Figure out how much of the str we need to compare. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001963 for (i = 1; buf[i] != '/' && buf[i] != '\0'; i++)
1964 ;
1965
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001966 /* Get the home directory. */
1967 if (i == 1) {
1968 get_homedir();
1969 tilde_dir = homedir;
1970 } else {
1971 const struct passwd *userdata;
1972
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001973 do {
1974 userdata = getpwent();
1975 } while (userdata != NULL &&
David Lawrence Ramsey2dbcc802004-11-26 18:10:07 +00001976 strncmp(userdata->pw_name, buf + 1, i - 1) != 0);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001977 endpwent();
1978 tilde_dir = userdata->pw_dir;
Chris Allegrettad865da12002-07-29 23:46:38 +00001979 }
Chris Allegretta04fec912000-11-25 04:43:43 +00001980
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001981 if (tilde_dir != NULL) {
1982 dirtmp = charalloc(strlen(tilde_dir) + strlen(buf + i) + 1);
1983 sprintf(dirtmp, "%s%s", tilde_dir, buf + i);
Chris Allegrettabe77c612000-11-24 14:00:16 +00001984 }
Chris Allegrettae5e4d492001-01-23 03:09:15 +00001985 }
Chris Allegrettabe77c612000-11-24 14:00:16 +00001986
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001987 /* Set a default value for dirtmp, in case the user's home directory
1988 * isn't found. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001989 if (dirtmp == NULL)
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001990 dirtmp = mallocstrcpy(NULL, buf);
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00001991
Chris Allegretta1bd0ce22000-12-06 05:56:08 +00001992 return dirtmp;
Chris Allegrettabe77c612000-11-24 14:00:16 +00001993}
1994
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001995#if !defined(DISABLE_TABCOMP) || !defined(DISABLE_BROWSER)
David Lawrence Ramsey54dca7b2005-02-11 16:02:54 +00001996/* Our sort routine for file listings. Sort alphabetically and
1997 * case-insensitively, and sort directories before filenames. */
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00001998int diralphasort(const void *va, const void *vb)
1999{
2000 struct stat fileinfo;
2001 const char *a = *(const char *const *)va;
2002 const char *b = *(const char *const *)vb;
2003 bool aisdir = stat(a, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode);
2004 bool bisdir = stat(b, &fileinfo) != -1 && S_ISDIR(fileinfo.st_mode);
2005
2006 if (aisdir && !bisdir)
2007 return -1;
2008 if (!aisdir && bisdir)
2009 return 1;
2010
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002011 return strcasecmp(a, b);
2012}
David Lawrence Ramseyab41ab92005-06-15 06:30:05 +00002013
2014/* Free the memory allocated for array, which should contain len
2015 * elements. */
2016void free_chararray(char **array, size_t len)
2017{
2018 for (; len > 0; len--)
2019 free(array[len - 1]);
2020 free(array);
2021}
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002022#endif
2023
Chris Allegretta7662c862003-01-13 01:35:15 +00002024#ifndef DISABLE_TABCOMP
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002025/* Is the given file a directory? */
2026int is_dir(const char *buf)
Chris Allegrettabe77c612000-11-24 14:00:16 +00002027{
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002028 char *dirptr = real_dir_from_tilde(buf);
Chris Allegrettabe77c612000-11-24 14:00:16 +00002029 struct stat fileinfo;
2030
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002031 int ret = (stat(dirptr, &fileinfo) != -1 &&
David Lawrence Ramsey10796ac2005-05-27 13:50:31 +00002032 S_ISDIR(fileinfo.st_mode));
Chris Allegrettabe77c612000-11-24 14:00:16 +00002033
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002034 assert(buf != NULL && dirptr != buf);
Chris Allegrettabe77c612000-11-24 14:00:16 +00002035
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002036 free(dirptr);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002037
Chris Allegretta04fec912000-11-25 04:43:43 +00002038 return ret;
Chris Allegrettabe77c612000-11-24 14:00:16 +00002039}
Chris Allegretta04d848e2000-11-05 17:54:41 +00002040
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002041/* These functions (username_tab_completion(), cwd_tab_completion(), and
Chris Allegretta7662c862003-01-13 01:35:15 +00002042 * input_tab()) were taken from busybox 0.46 (cmdedit.c). Here is the
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00002043 * notice from that file:
Chris Allegretta04d848e2000-11-05 17:54:41 +00002044 *
2045 * Termios command line History and Editting, originally
2046 * intended for NetBSD sh (ash)
2047 * Copyright (c) 1999
2048 * Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
2049 * Etc: Dave Cinege <dcinege@psychosis.com>
2050 * Majorly adjusted/re-written for busybox:
2051 * Erik Andersen <andersee@debian.org>
2052 *
2053 * You may use this code as you wish, so long as the original author(s)
2054 * are attributed in any redistributions of the source code.
2055 * This code is 'as is' with no warranty.
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002056 * This code may safely be consumed by a BSD or GPL license. */
Chris Allegretta04d848e2000-11-05 17:54:41 +00002057
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002058/* We consider the first buflen characters of buf for ~username tab
2059 * completion. */
2060char **username_tab_completion(const char *buf, size_t *num_matches,
2061 size_t buflen)
Chris Allegretta04d848e2000-11-05 17:54:41 +00002062{
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002063 char **matches = NULL;
2064 const struct passwd *userdata;
2065
2066 assert(buf != NULL && num_matches != NULL && buflen > 0);
Chris Allegrettabe77c612000-11-24 14:00:16 +00002067
Chris Allegretta8d9b11a2001-01-19 04:17:24 +00002068 *num_matches = 0;
Chris Allegretta8d9b11a2001-01-19 04:17:24 +00002069
Chris Allegretta2c2c5f22001-01-23 03:27:31 +00002070 while ((userdata = getpwent()) != NULL) {
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002071 if (strncmp(userdata->pw_name, buf + 1, buflen - 1) == 0) {
2072 /* Cool, found a match. Add it to the list. This makes a
2073 * lot more sense to me (Chris) this way... */
Chris Allegretta8d9b11a2001-01-19 04:17:24 +00002074
Chris Allegrettae1f14522001-09-19 03:19:43 +00002075#ifndef DISABLE_OPERATINGDIR
2076 /* ...unless the match exists outside the operating
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002077 * directory, in which case just go to the next match. */
2078 if (check_operating_dir(userdata->pw_dir, TRUE))
2079 continue;
Chris Allegrettae1f14522001-09-19 03:19:43 +00002080#endif
2081
David Lawrence Ramsey17260502005-06-02 05:50:58 +00002082 matches = (char **)nrealloc(matches,
2083 (*num_matches + 1) * sizeof(char *));
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002084 matches[*num_matches] =
2085 charalloc(strlen(userdata->pw_name) + 2);
2086 sprintf(matches[*num_matches], "~%s", userdata->pw_name);
David Lawrence Ramseyfd3039a2004-07-17 19:49:12 +00002087 ++(*num_matches);
Chris Allegrettabe77c612000-11-24 14:00:16 +00002088 }
Chris Allegretta2c2c5f22001-01-23 03:27:31 +00002089 }
2090 endpwent();
Chris Allegretta8d9b11a2001-01-19 04:17:24 +00002091
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00002092 return matches;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002093}
2094
David Lawrence Ramseye781ddf2005-05-14 22:44:16 +00002095/* This was originally called exe_n_cwd_tab_completion(), but we're not
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002096 * worried about executables, only filenames :> */
2097char **cwd_tab_completion(const char *buf, size_t *num_matches, size_t
2098 buflen)
Chris Allegretta04d848e2000-11-05 17:54:41 +00002099{
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002100 char *dirname = mallocstrcpy(NULL, buf);
2101 char *filename;
2102#ifndef DISABLE_OPERATINGDIR
2103 size_t dirnamelen;
2104#endif
2105 size_t filenamelen;
2106 char **matches = NULL;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002107 DIR *dir;
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002108 const struct dirent *nextdir;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002109
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002110 assert(dirname != NULL && num_matches != NULL && buflen >= 0);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002111
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002112 *num_matches = 0;
2113 null_at(&dirname, buflen);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002114
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002115 /* Okie, if there's a / in the buffer, strip out the directory
2116 * part. */
2117 filename = strrchr(dirname, '/');
2118 if (filename != NULL) {
2119 char *tmpdirname = filename + 1;
Chris Allegretta442f2c52000-11-14 17:46:06 +00002120
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002121 filename = mallocstrcpy(NULL, tmpdirname);
David Lawrence Ramsey10796ac2005-05-27 13:50:31 +00002122 *tmpdirname = '\0';
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002123 tmpdirname = dirname;
2124 dirname = real_dir_from_tilde(dirname);
2125 free(tmpdirname);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002126 } else {
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002127 filename = dirname;
2128 dirname = mallocstrcpy(NULL, "./");
Chris Allegretta04d848e2000-11-05 17:54:41 +00002129 }
2130
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002131 assert(dirname[strlen(dirname) - 1] == '/');
Chris Allegrettabe77c612000-11-24 14:00:16 +00002132
Chris Allegrettacf287c82002-07-20 13:57:41 +00002133 dir = opendir(dirname);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002134
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00002135 if (dir == NULL) {
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002136 /* Don't print an error, just shut up and return. */
Chris Allegretta04d848e2000-11-05 17:54:41 +00002137 beep();
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002138 free(filename);
2139 free(dirname);
2140 return NULL;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002141 }
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002142
2143#ifndef DISABLE_OPERATINGDIR
2144 dirnamelen = strlen(dirname);
2145#endif
2146 filenamelen = strlen(filename);
2147
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002148 while ((nextdir = readdir(dir)) != NULL) {
Chris Allegretta04d848e2000-11-05 17:54:41 +00002149
Chris Allegretta04d848e2000-11-05 17:54:41 +00002150#ifdef DEBUG
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002151 fprintf(stderr, "Comparing \'%s\'\n", nextdir->d_name);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002152#endif
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002153 /* See if this matches. */
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002154 if (strncmp(nextdir->d_name, filename, filenamelen) == 0 &&
2155 (*filename == '.' ||
2156 (strcmp(nextdir->d_name, ".") != 0 &&
2157 strcmp(nextdir->d_name, "..") != 0))) {
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002158 /* Cool, found a match. Add it to the list. This makes a
2159 * lot more sense to me (Chris) this way... */
Chris Allegrettae1f14522001-09-19 03:19:43 +00002160
2161#ifndef DISABLE_OPERATINGDIR
2162 /* ...unless the match exists outside the operating
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002163 * directory, in which case just go to the next match. To
2164 * properly do operating directory checking, we have to add
2165 * the directory name to the beginning of the proposed match
2166 * before we check it. */
2167 char *tmp2 = charalloc(strlen(dirname) +
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002168 strlen(nextdir->d_name) + 1);
Chris Allegrettae1f14522001-09-19 03:19:43 +00002169
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002170 sprintf(tmp2, "%s%s", dirname, nextdir->d_name);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002171 if (check_operating_dir(tmp2, TRUE)) {
2172 free(tmp2);
2173 continue;
Chris Allegrettae1f14522001-09-19 03:19:43 +00002174 }
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002175 free(tmp2);
Chris Allegrettae1f14522001-09-19 03:19:43 +00002176#endif
2177
David Lawrence Ramsey17260502005-06-02 05:50:58 +00002178 matches = (char **)nrealloc(matches,
2179 (*num_matches + 1) * sizeof(char *));
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002180 matches[*num_matches] = mallocstrcpy(NULL, nextdir->d_name);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002181 ++(*num_matches);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002182 }
2183 }
Chris Allegretta3cdf6ff2003-02-08 02:02:02 +00002184 closedir(dir);
2185 free(dirname);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002186 free(filename);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002187
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00002188 return matches;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002189}
2190
David Lawrence Ramsey638cbe52005-05-26 18:11:32 +00002191/* Do tab completion. place refers to how much the statusbar cursor
2192 * position should be advanced. */
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002193char *input_tab(char *buf, size_t *place, bool *lastwastab, bool *list)
Chris Allegretta04d848e2000-11-05 17:54:41 +00002194{
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002195 size_t num_matches = 0;
2196 char **matches = NULL;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002197
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002198 assert(buf != NULL && place != NULL && *place <= strlen(buf) && lastwastab != NULL && list != NULL);
Chris Allegretta2084acc2001-11-29 03:43:08 +00002199
David Lawrence Ramsey11846fa2005-05-27 03:52:21 +00002200 *list = FALSE;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002201
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002202 /* If the word starts with `~' and there is no slash in the word,
2203 * then try completing this word as a username. */
2204 if (*place > 0 && *buf == '~') {
2205 const char *bob = strchr(buf, '/');
Chris Allegrettab5b89ae2000-11-14 18:25:26 +00002206
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002207 if (bob == NULL || bob >= buf + *place)
2208 matches = username_tab_completion(buf, &num_matches,
2209 *place);
2210 }
Chris Allegrettae118acc2000-11-06 05:40:03 +00002211
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002212 /* Match against files relative to the current working directory. */
2213 if (matches == NULL)
2214 matches = cwd_tab_completion(buf, &num_matches, *place);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002215
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002216 if (num_matches <= 0)
2217 beep();
2218 else {
2219 size_t match, common_len = 0;
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002220 char *mzero;
David Lawrence Ramseyad96aff2005-02-22 23:22:37 +00002221 const char *lastslash = revstrstr(buf, "/", buf + *place);
2222 size_t lastslash_len = (lastslash == NULL) ? 0 :
2223 lastslash - buf + 1;
David Lawrence Ramseyaff5a1c2005-05-27 13:06:18 +00002224 char *match1_mb = charalloc(mb_cur_max() + 1);
2225 char *match2_mb = charalloc(mb_cur_max() + 1);
2226 int match1_mb_len, match2_mb_len;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002227
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002228 while (TRUE) {
2229 for (match = 1; match < num_matches; match++) {
David Lawrence Ramseyaff5a1c2005-05-27 13:06:18 +00002230 match1_mb_len = parse_mbchar(matches[0] + common_len,
2231 match1_mb, NULL, NULL);
2232 match2_mb_len = parse_mbchar(matches[match] +
2233 common_len, match2_mb, NULL, NULL);
2234 match1_mb[match1_mb_len] = '\0';
2235 match2_mb[match2_mb_len] = '\0';
2236 if (strcmp(match1_mb, match2_mb) != 0)
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002237 break;
2238 }
Chris Allegretta04d848e2000-11-05 17:54:41 +00002239
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002240 if (match < num_matches || matches[0][common_len] == '\0')
Chris Allegretta04fec912000-11-25 04:43:43 +00002241 break;
Chris Allegretta442f2c52000-11-14 17:46:06 +00002242
David Lawrence Ramseyaff5a1c2005-05-27 13:06:18 +00002243 common_len += parse_mbchar(buf + common_len, NULL, NULL,
2244 NULL);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002245 }
Chris Allegretta442f2c52000-11-14 17:46:06 +00002246
David Lawrence Ramseyaff5a1c2005-05-27 13:06:18 +00002247 free(match1_mb);
2248 free(match2_mb);
2249
David Lawrence Ramseyad96aff2005-02-22 23:22:37 +00002250 mzero = charalloc(lastslash_len + common_len + 1);
2251 sprintf(mzero, "%.*s%.*s", lastslash_len, buf, common_len,
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002252 matches[0]);
Chris Allegretta442f2c52000-11-14 17:46:06 +00002253
David Lawrence Ramseyad96aff2005-02-22 23:22:37 +00002254 common_len += lastslash_len;
Chris Allegrettae1f14522001-09-19 03:19:43 +00002255
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002256 assert(common_len >= *place);
Chris Allegrettabe77c612000-11-24 14:00:16 +00002257
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002258 if (num_matches == 1 && is_dir(mzero)) {
2259 mzero[common_len] = '/';
2260 common_len++;
David Lawrence Ramsey16f88132005-05-26 03:32:41 +00002261
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002262 assert(common_len > *place);
2263 }
Chris Allegrettaec58a992000-11-05 21:54:23 +00002264
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002265 if (num_matches > 1 && (common_len != *place ||
2266 *lastwastab == FALSE))
2267 beep();
Chris Allegretta442f2c52000-11-14 17:46:06 +00002268
David Lawrence Ramsey7de67872005-05-26 18:20:05 +00002269 /* If there is more of a match to display on the statusbar, show
2270 * it. We reset lastwastab to FALSE: it requires hitting Tab
2271 * twice in succession with no statusbar changes to see a match
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002272 * list. */
2273 if (common_len != *place) {
2274 size_t buflen = strlen(buf);
Chris Allegretta442f2c52000-11-14 17:46:06 +00002275
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002276 *lastwastab = FALSE;
2277 buf = charealloc(buf, common_len + buflen - *place + 1);
David Lawrence Ramseye5b23bd2005-05-26 18:57:56 +00002278 charmove(buf + common_len, buf + *place,
2279 buflen - *place + 1);
David Lawrence Ramseye3970f52005-05-26 03:47:24 +00002280 charcpy(buf, mzero, common_len);
David Lawrence Ramsey32fe95d2005-05-26 19:48:41 +00002281 *place = common_len;
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002282 } else if (*lastwastab == FALSE || num_matches < 2)
2283 *lastwastab = TRUE;
2284 else {
2285 int longest_name = 0, editline = 0;
2286 size_t columns;
Chris Allegrettaec58a992000-11-05 21:54:23 +00002287
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002288 /* Now we show a list of the available choices. */
2289 assert(num_matches > 1);
2290
2291 /* Sort the list. */
2292 qsort(matches, num_matches, sizeof(char *), diralphasort);
2293
2294 for (match = 0; match < num_matches; match++) {
2295 common_len = strnlenpt(matches[match], COLS - 1);
David Lawrence Ramsey7de67872005-05-26 18:20:05 +00002296
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002297 if (common_len > COLS - 1) {
2298 longest_name = COLS - 1;
Chris Allegrettaec58a992000-11-05 21:54:23 +00002299 break;
2300 }
David Lawrence Ramsey7de67872005-05-26 18:20:05 +00002301
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002302 if (common_len > longest_name)
2303 longest_name = common_len;
Chris Allegrettaec58a992000-11-05 21:54:23 +00002304 }
Chris Allegretta04d848e2000-11-05 17:54:41 +00002305
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002306 assert(longest_name <= COLS - 1);
2307
2308 /* Each column will be longest_name + 2 characters wide,
2309 * i.e, two spaces between columns, except that there will
2310 * be only one space after the last column. */
2311 columns = (COLS + 1) / (longest_name + 2);
2312
2313 /* Blank the edit window, and print the matches out
2314 * there. */
Chris Allegretta04d848e2000-11-05 17:54:41 +00002315 blank_edit();
2316 wmove(edit, 0, 0);
2317
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002318 /* Disable el cursor. */
2319 curs_set(0);
Chris Allegretta2c975222000-11-15 01:25:42 +00002320
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002321 for (match = 0; match < num_matches; match++) {
2322 char *disp;
Chris Allegrettaec58a992000-11-05 21:54:23 +00002323
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002324 wmove(edit, editline, (longest_name + 2) *
2325 (match % columns));
Chris Allegrettaec58a992000-11-05 21:54:23 +00002326
David Lawrence Ramsey16f88132005-05-26 03:32:41 +00002327 if (match % columns == 0 &&
2328 editline == editwinrows - 1 &&
2329 num_matches - match > columns) {
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002330 waddstr(edit, _("(more)"));
2331 break;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002332 }
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002333
2334 disp = display_string(matches[match], 0, longest_name,
2335 FALSE);
2336 waddstr(edit, disp);
2337 free(disp);
2338
2339 if ((match + 1) % columns == 0)
2340 editline++;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002341 }
2342 wrefresh(edit);
David Lawrence Ramsey4d44d2d2004-08-01 22:35:31 +00002343 *list = TRUE;
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002344 }
2345
2346 free(mzero);
Chris Allegretta04d848e2000-11-05 17:54:41 +00002347 }
2348
David Lawrence Ramseyab41ab92005-06-15 06:30:05 +00002349 free_chararray(matches, num_matches);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002350
Chris Allegretta2084acc2001-11-29 03:43:08 +00002351 /* Only refresh the edit window if we don't have a list of filename
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002352 * matches on it. */
David Lawrence Ramsey4d44d2d2004-08-01 22:35:31 +00002353 if (*list == FALSE)
Chris Allegretta2084acc2001-11-29 03:43:08 +00002354 edit_refresh();
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002355
2356 /* Enable el cursor. */
Chris Allegretta442f2c52000-11-14 17:46:06 +00002357 curs_set(1);
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002358
Chris Allegretta442f2c52000-11-14 17:46:06 +00002359 return buf;
Chris Allegretta04d848e2000-11-05 17:54:41 +00002360}
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002361#endif /* !DISABLE_TABCOMP */
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002362
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002363/* Only print the last part of a path. Isn't there a shell command for
2364 * this? */
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00002365const char *tail(const char *foo)
2366{
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002367 const char *tmp = strrchr(foo, '/');
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00002368
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002369 if (tmp == NULL)
2370 tmp = foo;
2371 else if (*tmp == '/')
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00002372 tmp++;
2373
2374 return tmp;
2375}
David Lawrence Ramsey04e42a62004-02-28 16:24:31 +00002376
Rocco Corsiaf5c3022001-01-12 07:51:05 +00002377#ifndef DISABLE_BROWSER
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002378/* Strip one directory from the end of path. */
2379void striponedir(char *path)
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002380{
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002381 char *tmp;
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002382
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002383 assert(path != NULL);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002384
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002385 tmp = strrchr(path, '/');
David Lawrence Ramsey16f88132005-05-26 03:32:41 +00002386
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002387 if (tmp != NULL)
2388 *tmp = '\0';
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002389}
2390
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002391/* Return a list of files contained in the directory path. *longest is
2392 * the maximum display length of a file, up to COLS - 1 (but at least
2393 * 7). *numents is the number of files. We assume path exists and is a
2394 * directory. If neither is true, we return NULL. */
2395char **browser_init(const char *path, int *longest, size_t *numents, DIR
2396 *dir)
Chris Allegretta858d9d92003-01-30 00:53:32 +00002397{
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002398 const struct dirent *nextdir;
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002399 char **filelist;
David Lawrence Ramsey9c06f342005-06-15 07:18:30 +00002400 size_t i = 0, path_len;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002401
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002402 assert(dir != NULL);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002403
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002404 *longest = 0;
2405
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002406 while ((nextdir = readdir(dir)) != NULL) {
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002407 size_t dlen;
2408
David Lawrence Ramsey9c06f342005-06-15 07:18:30 +00002409 /* Don't show the "." entry. */
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002410 if (strcmp(nextdir->d_name, ".") == 0)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002411 continue;
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002412 i++;
2413
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002414 dlen = strlenpt(nextdir->d_name);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002415 if (dlen > *longest)
David Lawrence Ramsey9c06f342005-06-15 07:18:30 +00002416 *longest = (dlen > COLS - 1) ? COLS - 1 : dlen;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002417 }
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002418
2419 *numents = i;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002420 rewinddir(dir);
2421 *longest += 10;
2422
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002423 filelist = (char **)nmalloc(*numents * sizeof(char *));
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002424
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002425 path_len = strlen(path);
2426
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002427 i = 0;
2428
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002429 while ((nextdir = readdir(dir)) != NULL && i < *numents) {
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002430 /* Don't show the "." entry. */
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002431 if (strcmp(nextdir->d_name, ".") == 0)
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002432 continue;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002433
David Lawrence Ramsey546f5b32005-05-14 23:14:47 +00002434 filelist[i] = charalloc(path_len + strlen(nextdir->d_name) + 1);
2435 sprintf(filelist[i], "%s%s", path, nextdir->d_name);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002436 i++;
2437 }
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002438
2439 /* Maybe the number of files in the directory changed between the
2440 * first time we scanned and the second. i is the actual length of
2441 * filelist, so record it. */
2442 *numents = i;
Chris Allegrettae9b5c6f2003-02-12 23:49:56 +00002443 closedir(dir);
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002444
2445 if (*longest > COLS - 1)
2446 *longest = COLS - 1;
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002447 if (*longest < 7)
2448 *longest = 7;
David Lawrence Ramseyad40fdb2002-09-06 20:35:28 +00002449
2450 return filelist;
2451}
2452
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002453/* Our browser function. path is the path to start browsing from.
2454 * Assume path has already been tilde-expanded. */
2455char *do_browser(char *path, DIR *dir)
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002456{
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002457 int kbinput, longest, selected, width;
David Lawrence Ramsey3435a0f2005-06-17 21:08:13 +00002458 bool meta_key, func_key, old_const_update = ISSET(CONST_UPDATE);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002459 size_t numents;
2460 char **filelist, *retval = NULL;
2461
2462 curs_set(0);
2463 blank_statusbar();
2464 bottombars(browser_list);
2465 wrefresh(bottomwin);
2466
2467#if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE)
2468 /* Set currshortcut so the user can click in the shortcut area, and
2469 * so the browser help screen will come up. */
2470 currshortcut = browser_list;
Chris Allegretta051fc6e2001-05-05 23:17:36 +00002471#endif
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002472
David Lawrence Ramsey3435a0f2005-06-17 21:08:13 +00002473 UNSET(CONST_UPDATE);
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002474
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002475 change_browser_directory:
2476 /* We go here after the user selects a new directory. */
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002477
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002478 kbinput = ERR;
2479 selected = 0;
2480 width = 0;
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002481
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002482 path = mallocstrassn(path, get_full_path(path));
2483
2484 /* Assume that path exists and ends with a slash. */
2485 assert(path != NULL && path[strlen(path) - 1] == '/');
2486
2487 /* Get the list of files. */
2488 filelist = browser_init(path, &longest, &numents, dir);
2489
2490 assert(filelist != NULL);
Chris Allegretta0e9d3c62001-01-04 04:59:17 +00002491
David Lawrence Ramsey65e6ecb2005-02-08 20:37:53 +00002492 /* Sort the list. */
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002493 qsort(filelist, numents, sizeof(char *), diralphasort);
2494
2495 titlebar(path);
Chris Allegretta0e9d3c62001-01-04 04:59:17 +00002496
2497 /* Loop invariant: Microsoft sucks. */
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002498 do {
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002499 bool abort = FALSE;
David Lawrence Ramsey928babf2005-03-21 06:12:07 +00002500 int j, col = 0, editline = 0, fileline;
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002501 int filecols = 0;
2502 /* Used only if width == 0, to calculate the number of files
2503 * per row below. */
2504 struct stat st;
Chris Allegrettaf80a59c2003-01-30 00:57:33 +00002505 char *new_path;
2506 /* Used by the Go To Directory prompt. */
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002507#ifndef DISABLE_MOUSE
2508 MEVENT mevent;
2509#endif
Rocco Corsi12f294c2001-04-14 06:50:24 +00002510
David Lawrence Ramseye5d8f322004-09-30 22:07:21 +00002511 check_statusblank();
Rocco Corsi12f294c2001-04-14 06:50:24 +00002512
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002513 /* Compute the line number we're on now, so that we don't divide
2514 * by zero later. */
David Lawrence Ramsey928babf2005-03-21 06:12:07 +00002515 fileline = selected;
Chris Allegrettadffa2072002-07-24 01:02:26 +00002516 if (width != 0)
David Lawrence Ramsey928babf2005-03-21 06:12:07 +00002517 fileline /= width;
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002518
2519 switch (kbinput) {
David Lawrence Ramseyf5b256b2003-10-03 20:26:25 +00002520#ifndef DISABLE_MOUSE
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002521 case KEY_MOUSE:
2522 if (getmouse(&mevent) == ERR)
2523 break;
Chris Allegretta051fc6e2001-05-05 23:17:36 +00002524
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002525 /* If we clicked in the edit window, we probably clicked
2526 * on a file. */
2527 if (wenclose(edit, mevent.y, mevent.x)) {
David Lawrence Ramsey9c06f342005-06-15 07:18:30 +00002528 int old_selected = selected;
Chris Allegretta051fc6e2001-05-05 23:17:36 +00002529
David Lawrence Ramseya01690c2005-06-15 07:38:25 +00002530 /* Subtract out the size of topwin. */
David Lawrence Ramsey42e271d2005-06-15 07:32:58 +00002531 mevent.y -= 2 - no_more_space();
Chris Allegrettadffa2072002-07-24 01:02:26 +00002532
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002533 /* longest is the width of each column. There are
2534 * two spaces between each column. */
David Lawrence Ramsey928babf2005-03-21 06:12:07 +00002535 selected = (fileline / editwinrows) * editwinrows *
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002536 width + mevent.y * width + mevent.x /
2537 (longest + 2);
Chris Allegretta051fc6e2001-05-05 23:17:36 +00002538
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002539 /* If they clicked beyond the end of a row, select
2540 * the end of that row. */
2541 if (mevent.x > width * (longest + 2))
2542 selected--;
Chris Allegretta051fc6e2001-05-05 23:17:36 +00002543
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002544 /* If we're off the screen, reset to the last item.
2545 * If we clicked the same place as last time, select
2546 * this name! */
2547 if (selected > numents - 1)
2548 selected = numents - 1;
David Lawrence Ramsey9c06f342005-06-15 07:18:30 +00002549 else if (old_selected == selected)
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002550 /* Put back the 'select' key. */
2551 unget_kbinput('s', FALSE, FALSE);
2552 } else {
2553 /* We must have clicked a shortcut. Put back the
2554 * equivalent shortcut key. */
2555 int mouse_x, mouse_y;
2556 get_mouseinput(&mouse_x, &mouse_y, TRUE);
Chris Allegretta0eab2362003-02-03 03:39:05 +00002557 }
Chris Allegretta0eab2362003-02-03 03:39:05 +00002558
Chris Allegretta0eab2362003-02-03 03:39:05 +00002559 break;
Chris Allegretta7662c862003-01-13 01:35:15 +00002560#endif
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002561 case NANO_PREVLINE_KEY:
2562 if (selected >= width)
2563 selected -= width;
Rocco Corsi12f294c2001-04-14 06:50:24 +00002564 break;
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002565 case NANO_BACK_KEY:
2566 if (selected > 0)
2567 selected--;
2568 break;
2569 case NANO_NEXTLINE_KEY:
2570 if (selected + width <= numents - 1)
2571 selected += width;
2572 break;
2573 case NANO_FORWARD_KEY:
2574 if (selected < numents - 1)
2575 selected++;
2576 break;
2577 case NANO_PREVPAGE_KEY:
2578 case NANO_PREVPAGE_FKEY:
2579 case '-': /* Pico compatibility. */
David Lawrence Ramsey928babf2005-03-21 06:12:07 +00002580 if (selected >= (editwinrows + fileline % editwinrows) *
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002581 width)
David Lawrence Ramsey928babf2005-03-21 06:12:07 +00002582 selected -= (editwinrows + fileline % editwinrows) *
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002583 width;
2584 else
2585 selected = 0;
2586 break;
2587 case NANO_NEXTPAGE_KEY:
2588 case NANO_NEXTPAGE_FKEY:
2589 case ' ': /* Pico compatibility. */
David Lawrence Ramsey928babf2005-03-21 06:12:07 +00002590 selected += (editwinrows - fileline % editwinrows) *
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002591 width;
2592 if (selected >= numents)
2593 selected = numents - 1;
2594 break;
2595 case NANO_HELP_KEY:
2596 case NANO_HELP_FKEY:
2597 case '?': /* Pico compatibility. */
2598#ifndef DISABLE_HELP
2599 do_help();
2600 curs_set(0);
2601#else
2602 nano_disabled_msg();
2603#endif
2604 break;
2605 case NANO_ENTER_KEY:
2606 case 'S': /* Pico compatibility. */
2607 case 's':
2608 /* You can't move up from "/". */
2609 if (strcmp(filelist[selected], "/..") == 0) {
2610 statusbar(_("Can't move up a directory"));
2611 beep();
2612 break;
2613 }
Rocco Corsi12f294c2001-04-14 06:50:24 +00002614
Chris Allegrettaf80a59c2003-01-30 00:57:33 +00002615#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002616 /* Note: the selected file can be outside the operating
2617 * directory if it's ".." or if it's a symlink to a
2618 * directory outside the operating directory. */
2619 if (check_operating_dir(filelist[selected], FALSE)) {
2620 statusbar(
2621 _("Can't go outside of %s in restricted mode"),
2622 operating_dir);
2623 beep();
2624 break;
2625 }
Chris Allegrettaf80a59c2003-01-30 00:57:33 +00002626#endif
2627
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002628 if (stat(filelist[selected], &st) == -1) {
2629 statusbar(_("Error reading %s: %s"),
2630 filelist[selected], strerror(errno));
2631 beep();
2632 break;
2633 }
2634
2635 if (!S_ISDIR(st.st_mode)) {
2636 retval = mallocstrcpy(retval, filelist[selected]);
2637 abort = TRUE;
2638 break;
2639 }
2640
2641 dir = opendir(filelist[selected]);
2642 if (dir == NULL) {
2643 /* We can't open this dir for some reason.
2644 * Complain. */
2645 statusbar(_("Error reading %s: %s"),
2646 filelist[selected], strerror(errno));
2647 break;
2648 }
2649
2650 path = mallocstrcpy(path, filelist[selected]);
2651
2652 /* Start over again with the new path value. */
David Lawrence Ramseyab41ab92005-06-15 06:30:05 +00002653 free_chararray(filelist, numents);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002654 goto change_browser_directory;
2655
David Lawrence Ramseyb349c802005-03-17 19:10:29 +00002656 /* Refresh the screen. */
2657 case NANO_REFRESH_KEY:
David Lawrence Ramseyc54c4d12005-06-18 15:49:17 +00002658 total_redraw();
David Lawrence Ramseyb349c802005-03-17 19:10:29 +00002659 break;
2660
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002661 /* Go to a specific directory. */
2662 case NANO_GOTOLINE_KEY:
2663 case NANO_GOTOLINE_FKEY:
2664 case 'G': /* Pico compatibility. */
2665 case 'g':
2666 curs_set(1);
2667
2668 j = statusq(FALSE, gotodir_list, "",
2669#ifndef NANO_SMALL
2670 NULL,
2671#endif
2672 _("Go To Directory"));
2673
2674 curs_set(0);
2675 bottombars(browser_list);
2676
2677 if (j < 0) {
2678 statusbar(_("Cancelled"));
2679 break;
2680 }
2681
2682 new_path = real_dir_from_tilde(answer);
2683
2684 if (new_path[0] != '/') {
2685 new_path = charealloc(new_path, strlen(path) +
2686 strlen(answer) + 1);
2687 sprintf(new_path, "%s%s", path, answer);
2688 }
2689
2690#ifndef DISABLE_OPERATINGDIR
2691 if (check_operating_dir(new_path, FALSE)) {
2692 statusbar(
2693 _("Can't go outside of %s in restricted mode"),
2694 operating_dir);
2695 free(new_path);
2696 break;
2697 }
2698#endif
2699
2700 dir = opendir(new_path);
2701 if (dir == NULL) {
2702 /* We can't open this dir for some reason.
2703 * Complain. */
2704 statusbar(_("Error reading %s: %s"), answer,
2705 strerror(errno));
2706 free(new_path);
2707 break;
2708 }
2709
2710 /* Start over again with the new path value. */
2711 free(path);
2712 path = new_path;
David Lawrence Ramseyab41ab92005-06-15 06:30:05 +00002713 free_chararray(filelist, numents);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002714 goto change_browser_directory;
2715
2716 /* Abort the browser. */
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002717 case NANO_EXIT_KEY:
2718 case NANO_EXIT_FKEY:
2719 case 'E': /* Pico compatibility. */
2720 case 'e':
2721 abort = TRUE;
Rocco Corsi12f294c2001-04-14 06:50:24 +00002722 break;
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002723 }
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002724
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002725 if (abort)
2726 break;
2727
Rocco Corsi12f294c2001-04-14 06:50:24 +00002728 blank_edit();
2729
David Lawrence Ramsey9b13ff32002-12-22 16:30:00 +00002730 if (width != 0)
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002731 j = width * editwinrows *
2732 ((selected / width) / editwinrows);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002733 else
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002734 j = 0;
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002735
2736 wmove(edit, 0, 0);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002737
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002738 {
2739 int foo_len = mb_cur_max() * 7;
2740 char *foo = charalloc(foo_len + 1);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002741
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002742 for (; j < numents && editline <= editwinrows - 1; j++) {
2743 char *disp = display_string(tail(filelist[j]), 0,
2744 longest, FALSE);
2745
2746 /* Highlight the currently selected file/dir. */
2747 if (j == selected)
2748 wattron(edit, A_REVERSE);
2749
2750 mvwaddnstr(edit, editline, col, hblank, longest);
2751 mvwaddstr(edit, editline, col, disp);
2752 free(disp);
2753
2754 col += longest;
2755 filecols++;
2756
2757 /* Show file info also. We don't want to report file
2758 * sizes for links, so we use lstat(). Also, stat() and
2759 * lstat() return an error if, for example, the file is
2760 * deleted while the file browser is open. In that
2761 * case, we report "--" as the file info. */
2762 if (lstat(filelist[j], &st) == -1 ||
2763 S_ISLNK(st.st_mode)) {
2764 /* Aha! It's a symlink! Now, is it a dir? If so,
2765 * mark it as such. */
2766 if (stat(filelist[j], &st) == 0 &&
2767 S_ISDIR(st.st_mode)) {
David Lawrence Ramseye3970f52005-05-26 03:47:24 +00002768 charcpy(foo, _("(dir)"), foo_len);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002769 foo[foo_len] = '\0';
2770 } else
2771 strcpy(foo, "--");
David Lawrence Ramsey16f88132005-05-26 03:32:41 +00002772 } else if (S_ISDIR(st.st_mode)) {
David Lawrence Ramseye3970f52005-05-26 03:47:24 +00002773 charcpy(foo, _("(dir)"), foo_len);
David Lawrence Ramsey16f88132005-05-26 03:32:41 +00002774 foo[foo_len] = '\0';
2775 } else if (st.st_size < (1 << 10)) /* less than 1 k. */
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002776 sprintf(foo, "%4u B", (unsigned int)st.st_size);
2777 else if (st.st_size < (1 << 20)) /* less than 1 meg. */
2778 sprintf(foo, "%4u KB",
2779 (unsigned int)(st.st_size >> 10));
2780 else if (st.st_size < (1 << 30)) /* less than 1 gig. */
2781 sprintf(foo, "%4u MB",
2782 (unsigned int)(st.st_size >> 20));
2783 else
2784 sprintf(foo, "%4u GB",
2785 (unsigned int)(st.st_size >> 30));
2786
2787 mvwaddnstr(edit, editline, col - strlen(foo), foo,
2788 foo_len);
2789
2790 if (j == selected)
2791 wattroff(edit, A_REVERSE);
2792
2793 /* Add some space between the columns. */
2794 col += 2;
2795
2796 /* If the next entry isn't going to fit on the line,
2797 * move to the next line. */
2798 if (col > COLS - longest) {
2799 editline++;
2800 col = 0;
2801 if (width == 0)
2802 width = filecols;
2803 }
David Lawrence Ramsey6a0d5b82005-06-13 14:00:22 +00002804
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002805 wmove(edit, editline, col);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002806 }
2807
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002808 free(foo);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002809 }
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002810
David Lawrence Ramseya0b5ba22004-08-25 15:39:10 +00002811 wrefresh(edit);
David Lawrence Ramseyeb16f432004-09-27 01:04:50 +00002812 } while ((kbinput = get_kbinput(edit, &meta_key, &func_key)) !=
2813 NANO_EXIT_KEY && kbinput != NANO_EXIT_FKEY);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002814
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002815 blank_edit();
Chris Allegretta4dc03d52002-05-11 03:04:44 +00002816 titlebar(NULL);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002817 edit_refresh();
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002818 curs_set(1);
David Lawrence Ramsey3435a0f2005-06-17 21:08:13 +00002819 if (old_const_update)
2820 SET(CONST_UPDATE);
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002821
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002822 /* Clean up. */
David Lawrence Ramseyab41ab92005-06-15 06:30:05 +00002823 free_chararray(filelist, numents);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002824 free(path);
2825
Chris Allegrettaf4b96012001-01-03 07:11:47 +00002826 return retval;
2827}
Chris Allegretta150469a2001-01-05 21:13:14 +00002828
David Lawrence Ramseyd8367982005-03-26 04:49:01 +00002829/* The file browser front end. We check to see if inpath has a dir in
2830 * it. If it does, we start do_browser() from there. Otherwise, we
2831 * start do_browser() from the current directory. */
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002832char *do_browse_from(const char *inpath)
Chris Allegretta150469a2001-01-05 21:13:14 +00002833{
2834 struct stat st;
Chris Allegretta858d9d92003-01-30 00:53:32 +00002835 char *path;
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002836 /* This holds the tilde-expanded version of inpath. */
David Lawrence Ramsey6e0ed3c2005-03-26 04:42:28 +00002837 DIR *dir = NULL;
Chris Allegretta90d30742001-04-03 01:02:38 +00002838
Chris Allegretta858d9d92003-01-30 00:53:32 +00002839 assert(inpath != NULL);
David Lawrence Ramseye21adfa2002-09-13 18:14:04 +00002840
Chris Allegretta858d9d92003-01-30 00:53:32 +00002841 path = real_dir_from_tilde(inpath);
2842
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002843 /* Perhaps path is a directory. If so, we'll pass it to
2844 * do_browser(). Or perhaps path is a directory / a file. If so,
2845 * we'll try stripping off the last path element and passing it to
2846 * do_browser(). Or perhaps path doesn't have a directory portion
2847 * at all. If so, we'll just pass the current directory to
2848 * do_browser(). */
Chris Allegretta858d9d92003-01-30 00:53:32 +00002849 if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
2850 striponedir(path);
Chris Allegrettae9b5c6f2003-02-12 23:49:56 +00002851 if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
2852 free(path);
David Lawrence Ramsey6e0ed3c2005-03-26 04:42:28 +00002853
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002854 path = charalloc(PATH_MAX + 1);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002855 path = getcwd(path, PATH_MAX + 1);
David Lawrence Ramsey6e0ed3c2005-03-26 04:42:28 +00002856
2857 if (path != NULL)
2858 align(&path);
Chris Allegrettae9b5c6f2003-02-12 23:49:56 +00002859 }
Chris Allegretta90d30742001-04-03 01:02:38 +00002860 }
Chris Allegretta150469a2001-01-05 21:13:14 +00002861
Chris Allegretta858d9d92003-01-30 00:53:32 +00002862#ifndef DISABLE_OPERATINGDIR
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002863 /* If the resulting path isn't in the operating directory, use
2864 * the operating directory instead. */
2865 if (check_operating_dir(path, FALSE)) {
2866 if (path != NULL)
2867 free(path);
2868 path = mallocstrcpy(NULL, operating_dir);
2869 }
Chris Allegretta858d9d92003-01-30 00:53:32 +00002870#endif
Chris Allegretta150469a2001-01-05 21:13:14 +00002871
David Lawrence Ramsey6e0ed3c2005-03-26 04:42:28 +00002872 if (path != NULL)
2873 dir = opendir(path);
2874
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002875 if (dir == NULL) {
Chris Allegretta858d9d92003-01-30 00:53:32 +00002876 beep();
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002877 free(path);
2878 return NULL;
2879 }
2880
2881 return do_browser(path, dir);
Chris Allegretta150469a2001-01-05 21:13:14 +00002882}
Chris Allegrettacf287c82002-07-20 13:57:41 +00002883#endif /* !DISABLE_BROWSER */
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002884
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00002885#if !defined(NANO_SMALL) && defined(ENABLE_NANORC)
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002886/* Return $HOME/.nano_history, or NULL if we can't find the homedir.
2887 * The string is dynamically allocated, and should be freed. */
2888char *histfilename(void)
2889{
2890 char *nanohist = NULL;
2891
2892 if (homedir != NULL) {
2893 size_t homelen = strlen(homedir);
2894
2895 nanohist = charalloc(homelen + 15);
2896 strcpy(nanohist, homedir);
2897 strcpy(nanohist + homelen, "/.nano_history");
2898 }
2899 return nanohist;
2900}
2901
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002902void load_history(void)
2903{
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002904 char *nanohist = histfilename();
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002905
David Lawrence Ramsey29be8032005-04-20 14:55:46 +00002906 /* Assume do_rcfile() has reported a missing home directory. */
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002907 if (nanohist != NULL) {
2908 FILE *hist = fopen(nanohist, "r");
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002909
David Lawrence Ramsey417b03a2003-09-06 21:44:37 +00002910 if (hist == NULL) {
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00002911 if (errno != ENOENT) {
Chris Allegrettad8451932003-03-11 03:50:40 +00002912 /* Don't save history when we quit. */
2913 UNSET(HISTORYLOG);
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002914 rcfile_error(N_("Error reading %s: %s"), nanohist,
2915 strerror(errno));
2916 fprintf(stderr,
2917 _("\nPress Return to continue starting nano\n"));
David Lawrence Ramsey7dfec432004-08-12 15:20:14 +00002918 while (getchar() != '\n')
2919 ;
Chris Allegrettad8451932003-03-11 03:50:40 +00002920 }
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002921 } else {
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002922 /* Load a history (first the search history, then the
2923 * replace history) from oldest to newest. Assume the last
2924 * history entry is a blank line. */
2925 filestruct **history = &search_history;
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002926 char *line = NULL;
2927 size_t buflen = 0;
2928 ssize_t read;
2929
2930 while ((read = getline(&line, &buflen, hist)) >= 0) {
2931 if (read > 0 && line[read - 1] == '\n') {
2932 read--;
2933 line[read] = '\0';
2934 }
2935 if (read > 0) {
2936 unsunder(line, read);
David Lawrence Ramsey34bdc352005-06-02 18:41:31 +00002937 update_history(history, line);
2938 } else
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002939 history = &replace_history;
2940 }
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002941
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002942 fclose(hist);
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002943 free(line);
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002944 }
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002945 free(nanohist);
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002946 }
2947}
2948
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002949bool writehist(FILE *hist, filestruct *h)
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002950{
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002951 filestruct *p;
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002952
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002953 /* Write history from oldest to newest. Assume the last history
2954 * entry is a blank line. */
2955 for (p = h; p != NULL; p = p->next) {
David Lawrence Ramsey066713e2005-05-14 23:21:02 +00002956 size_t p_len = strlen(p->data);
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002957
David Lawrence Ramsey066713e2005-05-14 23:21:02 +00002958 sunder(p->data);
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002959
David Lawrence Ramsey066713e2005-05-14 23:21:02 +00002960 if (fwrite(p->data, sizeof(char), p_len, hist) < p_len ||
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002961 putc('\n', hist) == EOF)
2962 return FALSE;
2963 }
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002964
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002965 return TRUE;
2966}
2967
David Lawrence Ramsey29be8032005-04-20 14:55:46 +00002968/* Save histories to ~/.nano_history. */
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002969void save_history(void)
2970{
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002971 char *nanohist;
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002972
David Lawrence Ramsey29be8032005-04-20 14:55:46 +00002973 /* Don't save unchanged or empty histories. */
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002974 if (!history_has_changed() || (searchbot->lineno == 1 &&
2975 replacebot->lineno == 1))
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002976 return;
2977
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002978 nanohist = histfilename();
Chris Allegretta1debce22003-02-13 22:00:19 +00002979
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002980 if (nanohist != NULL) {
2981 FILE *hist = fopen(nanohist, "wb");
2982
David Lawrence Ramsey36dd87b2004-07-18 17:43:43 +00002983 if (hist == NULL)
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002984 rcfile_error(N_("Error writing %s: %s"), nanohist,
2985 strerror(errno));
David Lawrence Ramsey36dd87b2004-07-18 17:43:43 +00002986 else {
David Lawrence Ramsey29be8032005-04-20 14:55:46 +00002987 /* Make sure no one else can read from or write to the
2988 * history file. */
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002989 chmod(nanohist, S_IRUSR | S_IWUSR);
David Lawrence Ramseya27bd652004-08-17 05:23:38 +00002990
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002991 if (!writehist(hist, searchage) || !writehist(hist,
2992 replaceage))
David Lawrence Ramsey7d367712005-02-14 05:00:15 +00002993 rcfile_error(N_("Error writing %s: %s"), nanohist,
2994 strerror(errno));
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002995
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002996 fclose(hist);
2997 }
David Lawrence Ramsey934f9682005-05-23 16:30:06 +00002998
Chris Allegrettaf3de8b52003-01-16 23:44:46 +00002999 free(nanohist);
3000 }
3001}
David Lawrence Ramsey6420d442004-08-11 05:13:08 +00003002#endif /* !NANO_SMALL && ENABLE_NANORC */