2000-08-07 01:13:45 +04:00
|
|
|
/* $Id$ */
|
2000-06-19 08:22:15 +04:00
|
|
|
/**************************************************************************
|
|
|
|
* files.c *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 1999 Chris Allegretta *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 1, or (at your option) *
|
|
|
|
* any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2001-04-28 22:03:52 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2000-11-05 20:54:41 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2001-01-23 01:17:08 +03:00
|
|
|
#include <pwd.h>
|
2000-06-19 08:22:15 +04:00
|
|
|
|
|
|
|
#include "proto.h"
|
|
|
|
#include "nano.h"
|
2000-06-21 07:00:43 +04:00
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
#include <libintl.h>
|
|
|
|
#define _(string) gettext(string)
|
|
|
|
#else
|
|
|
|
#define _(string) (string)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Load file into edit buffer - takes data from file struct */
|
|
|
|
void load_file(void)
|
|
|
|
{
|
|
|
|
current = fileage;
|
|
|
|
wmove(edit, current_y, current_x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* What happens when there is no file to open? aiee! */
|
|
|
|
void new_file(void)
|
|
|
|
{
|
|
|
|
fileage = nmalloc(sizeof(filestruct));
|
2001-05-17 15:35:43 +04:00
|
|
|
fileage->data = charalloc(1);
|
2000-06-19 08:22:15 +04:00
|
|
|
strcpy(fileage->data, "");
|
|
|
|
fileage->prev = NULL;
|
|
|
|
fileage->next = NULL;
|
|
|
|
fileage->lineno = 1;
|
|
|
|
filebot = fileage;
|
|
|
|
edittop = fileage;
|
|
|
|
editbot = fileage;
|
|
|
|
current = fileage;
|
|
|
|
totlines = 1;
|
|
|
|
UNSET(VIEW_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int read_byte(int fd, char *filename, char *input)
|
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
static int index = 0;
|
|
|
|
static int size = 0;
|
|
|
|
|
|
|
|
if (index == size) {
|
|
|
|
index = 0;
|
|
|
|
size = read(fd, buf, BUFSIZ);
|
|
|
|
if (size == -1) {
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
resetty();
|
|
|
|
endwin();
|
|
|
|
perror(filename);
|
|
|
|
}
|
|
|
|
if (!size)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*input = buf[index++];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
filestruct *read_line(char *buf, filestruct * prev, int *line1ins)
|
|
|
|
{
|
|
|
|
filestruct *fileptr;
|
|
|
|
|
|
|
|
fileptr = nmalloc(sizeof(filestruct));
|
2001-05-17 15:35:43 +04:00
|
|
|
fileptr->data = charalloc(strlen(buf) + 2);
|
2000-06-19 08:22:15 +04:00
|
|
|
strcpy(fileptr->data, buf);
|
|
|
|
|
|
|
|
if (*line1ins) {
|
|
|
|
/* Special case, insert with cursor on 1st line. */
|
|
|
|
fileptr->prev = NULL;
|
|
|
|
fileptr->next = fileage;
|
|
|
|
fileptr->lineno = 1;
|
|
|
|
*line1ins = 0;
|
|
|
|
/* If we're inserting into the first line of the file, then
|
|
|
|
we want to make sure that our edit buffer stays on the
|
|
|
|
first line (and that fileage stays up to date!) */
|
|
|
|
fileage = fileptr;
|
|
|
|
edittop = fileptr;
|
|
|
|
} else if (fileage == NULL) {
|
|
|
|
fileage = fileptr;
|
|
|
|
fileage->lineno = 1;
|
|
|
|
fileage->next = fileage->prev = NULL;
|
|
|
|
fileptr = filebot = fileage;
|
|
|
|
} else if (prev) {
|
|
|
|
fileptr->prev = prev;
|
|
|
|
fileptr->next = NULL;
|
|
|
|
fileptr->lineno = prev->lineno + 1;
|
|
|
|
prev->next = fileptr;
|
|
|
|
} else {
|
|
|
|
die(_("read_line: not on first line and prev is NULL"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int read_file(int fd, char *filename)
|
|
|
|
{
|
2000-07-08 07:57:16 +04:00
|
|
|
long size, num_lines = 0, linetemp = 0;
|
2000-06-19 08:22:15 +04:00
|
|
|
char input[2]; /* buffer */
|
|
|
|
char *buf;
|
|
|
|
long i = 0, bufx = 128;
|
|
|
|
filestruct *fileptr = current, *tmp = NULL;
|
|
|
|
int line1ins = 0;
|
|
|
|
|
2001-05-17 15:35:43 +04:00
|
|
|
buf = charalloc(bufx);
|
2000-07-19 05:16:18 +04:00
|
|
|
buf[0] = '\0';
|
2000-06-19 08:22:15 +04:00
|
|
|
|
|
|
|
if (fileptr != NULL && fileptr->prev != NULL) {
|
|
|
|
fileptr = fileptr->prev;
|
|
|
|
tmp = fileptr;
|
|
|
|
} else if (fileptr != NULL && fileptr->prev == NULL) {
|
|
|
|
tmp = fileage;
|
|
|
|
current = fileage;
|
|
|
|
line1ins = 1;
|
|
|
|
}
|
|
|
|
input[1] = 0;
|
|
|
|
/* Read the entire file into file struct */
|
|
|
|
while ((size = read_byte(fd, filename, input)) > 0) {
|
|
|
|
linetemp = 0;
|
|
|
|
if (input[0] == '\n') {
|
|
|
|
fileptr = read_line(buf, fileptr, &line1ins);
|
2000-07-08 07:57:16 +04:00
|
|
|
num_lines++;
|
2000-06-19 08:22:15 +04:00
|
|
|
buf[0] = 0;
|
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
/* Now we allocate a bigger buffer 128 characters at a time.
|
|
|
|
If we allocate a lot of space for one line, we may indeed
|
|
|
|
have to use a buffer this big later on, so we don't
|
2001-05-05 21:45:54 +04:00
|
|
|
decrease it at all. We do free it at the end, though. */
|
2000-06-19 08:22:15 +04:00
|
|
|
|
|
|
|
if (i >= bufx - 1) {
|
|
|
|
buf = nrealloc(buf, bufx + 128);
|
|
|
|
bufx += 128;
|
|
|
|
}
|
|
|
|
buf[i] = input[0];
|
|
|
|
buf[i + 1] = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
totsize += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Did we not get a newline but still have stuff to do? */
|
|
|
|
if (buf[0]) {
|
|
|
|
fileptr = read_line(buf, fileptr, &line1ins);
|
2000-07-08 07:57:16 +04:00
|
|
|
num_lines++;
|
2000-06-19 08:22:15 +04:00
|
|
|
buf[0] = 0;
|
|
|
|
}
|
|
|
|
/* Did we even GET a file? */
|
2000-11-26 05:09:53 +03:00
|
|
|
if (totsize == 0 || fileptr == NULL) {
|
2000-06-19 08:22:15 +04:00
|
|
|
new_file();
|
2000-07-08 07:57:16 +04:00
|
|
|
statusbar(_("Read %d lines"), num_lines);
|
2000-06-19 08:22:15 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2000-07-05 02:15:39 +04:00
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
if (current != NULL) {
|
|
|
|
fileptr->next = current;
|
|
|
|
current->prev = fileptr;
|
|
|
|
renumber(current);
|
|
|
|
current_x = 0;
|
|
|
|
placewewant = 0;
|
|
|
|
} else if (fileptr->next == NULL) {
|
|
|
|
filebot = fileptr;
|
2000-07-05 02:15:39 +04:00
|
|
|
new_magicline();
|
2000-12-10 09:03:40 +03:00
|
|
|
totsize--;
|
2000-07-05 02:15:39 +04:00
|
|
|
|
|
|
|
/* Update the edit buffer */
|
2000-06-19 08:22:15 +04:00
|
|
|
load_file();
|
|
|
|
}
|
2000-07-08 07:57:16 +04:00
|
|
|
statusbar(_("Read %d lines"), num_lines);
|
|
|
|
totlines += num_lines;
|
2000-06-19 08:22:15 +04:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file (and decide if it exists) */
|
|
|
|
int open_file(char *filename, int insert, int quiet)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat fileinfo;
|
|
|
|
|
|
|
|
if (!strcmp(filename, "") || stat(filename, &fileinfo) == -1) {
|
|
|
|
if (insert) {
|
|
|
|
if (!quiet)
|
|
|
|
statusbar(_("\"%s\" not found"), filename);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
/* We have a new file */
|
|
|
|
statusbar(_("New File"));
|
|
|
|
new_file();
|
|
|
|
}
|
|
|
|
} else if ((fd = open(filename, O_RDONLY)) == -1) {
|
|
|
|
if (!quiet)
|
|
|
|
statusbar("%s: %s", strerror(errno), filename);
|
2000-12-18 04:09:07 +03:00
|
|
|
if (!insert)
|
|
|
|
new_file();
|
2000-06-19 08:22:15 +04:00
|
|
|
return -1;
|
|
|
|
} else { /* File is A-OK */
|
2000-12-13 18:01:29 +03:00
|
|
|
if (S_ISDIR(fileinfo.st_mode) || S_ISCHR(fileinfo.st_mode) ||
|
|
|
|
S_ISBLK(fileinfo.st_mode)) {
|
|
|
|
if (S_ISDIR(fileinfo.st_mode))
|
|
|
|
statusbar(_("File \"%s\" is a directory"), filename);
|
|
|
|
else
|
|
|
|
/* Don't open character or block files. Sorry, /dev/sndstat! */
|
|
|
|
statusbar(_("File \"%s\" is a device file"), filename);
|
|
|
|
|
2000-09-16 09:25:06 +04:00
|
|
|
if (!insert)
|
|
|
|
new_file();
|
2000-06-19 08:22:15 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!quiet)
|
|
|
|
statusbar(_("Reading File"));
|
|
|
|
read_file(fd, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_insertfile(void)
|
|
|
|
{
|
|
|
|
int i;
|
2000-11-25 08:03:20 +03:00
|
|
|
char *realname = NULL;
|
2000-06-19 08:22:15 +04:00
|
|
|
|
|
|
|
wrap_reset();
|
2001-05-21 16:56:25 +04:00
|
|
|
|
|
|
|
#ifndef DISABLE_MOUSE
|
2001-05-29 08:21:44 +04:00
|
|
|
currshortcut = insertfile_list;
|
|
|
|
currslen = INSERTFILE_LIST_LEN;
|
2001-05-21 16:56:25 +04:00
|
|
|
#endif
|
|
|
|
|
2001-05-29 08:21:44 +04:00
|
|
|
i = statusq(1, insertfile_list, INSERTFILE_LIST_LEN, "",
|
2000-06-19 08:22:15 +04:00
|
|
|
_("File to insert [from ./] "));
|
|
|
|
if (i != -1) {
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "filename is %s", answer);
|
|
|
|
#endif
|
|
|
|
|
2001-01-11 08:30:31 +03:00
|
|
|
#ifndef DISABLE_TABCOMP
|
2000-12-06 08:56:08 +03:00
|
|
|
realname = real_dir_from_tilde(answer);
|
2000-11-30 05:31:13 +03:00
|
|
|
#else
|
2000-12-06 08:56:08 +03:00
|
|
|
realname = mallocstrcpy(realname, answer);
|
2000-11-30 05:31:13 +03:00
|
|
|
#endif
|
2000-11-25 08:03:20 +03:00
|
|
|
|
2001-01-12 10:51:05 +03:00
|
|
|
#ifndef DISABLE_BROWSER
|
2001-01-03 10:11:47 +03:00
|
|
|
if (i == NANO_TOFILES_KEY) {
|
2001-01-06 00:13:14 +03:00
|
|
|
|
|
|
|
char *tmp = do_browse_from(realname);
|
2001-05-21 16:56:25 +04:00
|
|
|
#ifndef DISABLE_MOUSE
|
2001-05-29 08:21:44 +04:00
|
|
|
currshortcut = insertfile_list;
|
|
|
|
currslen = INSERTFILE_LIST_LEN;
|
2001-05-21 16:56:25 +04:00
|
|
|
#endif
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-01-11 08:30:31 +03:00
|
|
|
#ifdef DISABLE_TABCOMP
|
2001-01-05 17:31:52 +03:00
|
|
|
realname = NULL;
|
2001-01-05 01:05:47 +03:00
|
|
|
#endif
|
|
|
|
if (tmp != NULL)
|
2001-01-05 17:31:52 +03:00
|
|
|
realname = mallocstrcpy(realname, tmp);
|
2001-01-05 01:05:47 +03:00
|
|
|
else
|
2001-01-04 08:08:37 +03:00
|
|
|
return do_insertfile();
|
2001-01-03 10:11:47 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-11-25 08:03:20 +03:00
|
|
|
i = open_file(realname, 1, 0);
|
|
|
|
free(realname);
|
2000-06-19 08:22:15 +04:00
|
|
|
|
|
|
|
dump_buffer(fileage);
|
|
|
|
set_modified();
|
|
|
|
|
|
|
|
/* Here we want to rebuild the edit window */
|
2000-07-04 06:35:19 +04:00
|
|
|
fix_editbot();
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* If we've gone off the bottom, recenter; otherwise, just redraw */
|
2000-06-21 07:00:43 +04:00
|
|
|
if (current->lineno > editbot->lineno)
|
2000-07-29 08:33:38 +04:00
|
|
|
edit_update(current, CENTER);
|
2000-06-19 08:22:15 +04:00
|
|
|
else
|
|
|
|
edit_refresh();
|
|
|
|
|
|
|
|
UNSET(KEEP_CUTBUFFER);
|
|
|
|
display_main_list();
|
|
|
|
return i;
|
|
|
|
} else {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
UNSET(KEEP_CUTBUFFER);
|
|
|
|
display_main_list();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a file out. If tmp is nonzero, we set the umask to 0600,
|
2001-05-05 21:45:54 +04:00
|
|
|
* we don't set the global variable filename to its name, and don't
|
2000-06-19 08:22:15 +04:00
|
|
|
* print out how many lines we wrote on the statusbar.
|
|
|
|
*
|
2001-03-28 06:14:28 +04:00
|
|
|
* tmp means we are writing a tmp file in a secure fashion. We use
|
2000-12-02 07:36:50 +03:00
|
|
|
* it when spell checking or dumping the file on an error.
|
2001-05-29 08:21:44 +04:00
|
|
|
*
|
|
|
|
* append means, not surprisingly, whether we are appending instead
|
|
|
|
* of overwriting.
|
2001-06-06 03:24:55 +04:00
|
|
|
*
|
|
|
|
* nonamechange means don't change the current filename, it is ignored
|
|
|
|
* if tmp == 1.
|
2000-06-19 08:22:15 +04:00
|
|
|
*/
|
2001-06-06 03:24:55 +04:00
|
|
|
int write_file(char *name, int tmp, int append, int nonamechange)
|
2000-06-19 08:22:15 +04:00
|
|
|
{
|
|
|
|
long size, lineswritten = 0;
|
2000-12-14 16:56:28 +03:00
|
|
|
static char *buf = NULL;
|
2000-06-19 08:22:15 +04:00
|
|
|
filestruct *fileptr;
|
2000-12-04 08:15:39 +03:00
|
|
|
int fd, mask = 0, realexists, anyexists;
|
2000-12-13 18:01:29 +03:00
|
|
|
struct stat st, lst;
|
2000-11-24 17:02:57 +03:00
|
|
|
static char *realname = NULL;
|
2000-06-19 08:22:15 +04:00
|
|
|
|
|
|
|
if (!strcmp(name, "")) {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
return -1;
|
|
|
|
}
|
2001-01-03 10:11:47 +03:00
|
|
|
titlebar(NULL);
|
2000-06-19 08:22:15 +04:00
|
|
|
fileptr = fileage;
|
2000-11-24 17:02:57 +03:00
|
|
|
|
|
|
|
if (realname != NULL)
|
|
|
|
free(realname);
|
|
|
|
|
2000-12-14 16:56:28 +03:00
|
|
|
if (buf != NULL)
|
|
|
|
free(buf);
|
|
|
|
|
2001-01-11 08:30:31 +03:00
|
|
|
#ifndef DISABLE_TABCOMP
|
2000-11-24 17:00:16 +03:00
|
|
|
realname = real_dir_from_tilde(name);
|
|
|
|
#else
|
|
|
|
realname = mallocstrcpy(realname, name);
|
|
|
|
#endif
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2000-12-06 08:56:08 +03:00
|
|
|
/* Save the state of file at the end of the symlink (if there is one) */
|
2000-12-03 00:13:50 +03:00
|
|
|
realexists = stat(realname, &st);
|
|
|
|
|
2000-12-06 08:56:08 +03:00
|
|
|
/* Stat the link itself for the check... */
|
|
|
|
anyexists = lstat(realname, &lst);
|
2000-07-22 02:42:46 +04:00
|
|
|
|
2000-12-04 08:15:39 +03:00
|
|
|
/* New case: if the file exists, just give up */
|
|
|
|
if (tmp && anyexists != -1)
|
2000-12-06 08:56:08 +03:00
|
|
|
return -1;
|
2000-12-06 03:57:54 +03:00
|
|
|
/* NOTE: If you change this statement, you MUST CHANGE the if
|
2000-12-11 05:47:13 +03:00
|
|
|
statement below (that says:
|
|
|
|
if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) &&
|
|
|
|
S_ISLNK(lst.st_mode))) {
|
2000-12-06 08:56:08 +03:00
|
|
|
to reflect whether or not to link/unlink/rename the file */
|
|
|
|
else if (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode) || tmp) {
|
2000-12-10 01:50:38 +03:00
|
|
|
/* Use O_EXCL if tmp == 1. This is now copied from joe, because
|
2000-12-10 02:58:41 +03:00
|
|
|
wiggy says so *shrug*. */
|
2001-05-29 08:21:44 +04:00
|
|
|
if (append)
|
|
|
|
fd = open(realname, O_WRONLY | O_APPEND, (S_IRUSR|S_IWUSR));
|
|
|
|
else if (tmp)
|
2000-12-10 02:58:41 +03:00
|
|
|
fd = open(realname, O_WRONLY | O_CREAT | O_EXCL, (S_IRUSR|S_IWUSR));
|
|
|
|
else
|
|
|
|
fd = open(realname, O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR|S_IWUSR));
|
2000-12-04 08:15:39 +03:00
|
|
|
|
|
|
|
/* First, just give up if we couldn't even open the file */
|
2000-12-04 06:31:39 +03:00
|
|
|
if (fd == -1) {
|
2000-12-04 07:20:09 +03:00
|
|
|
if (!tmp && ISSET(TEMP_OPT)) {
|
2000-07-14 06:44:02 +04:00
|
|
|
UNSET(TEMP_OPT);
|
2001-05-29 08:21:44 +04:00
|
|
|
return do_writeout(filename, 1, 0);
|
2000-07-14 06:44:02 +04:00
|
|
|
}
|
2000-06-19 08:22:15 +04:00
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2000-12-04 08:15:39 +03:00
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
}
|
|
|
|
/* Don't follow symlink. Create new file. */
|
|
|
|
else {
|
2001-05-17 15:35:43 +04:00
|
|
|
buf = charalloc(strlen(realname) + 8);
|
2000-12-14 16:56:28 +03:00
|
|
|
strncpy(buf, realname, strlen(realname)+1);
|
2000-06-19 08:22:15 +04:00
|
|
|
strcat(buf, ".XXXXXX");
|
|
|
|
if ((fd = mkstemp(buf)) == -1) {
|
2000-07-14 06:44:02 +04:00
|
|
|
if (ISSET(TEMP_OPT)) {
|
|
|
|
UNSET(TEMP_OPT);
|
2001-05-29 08:21:44 +04:00
|
|
|
return do_writeout(filename, 1, 0);
|
2000-10-26 05:44:42 +04:00
|
|
|
}
|
2000-06-19 08:22:15 +04:00
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_buffer(fileage);
|
|
|
|
while (fileptr != NULL && fileptr->next != NULL) {
|
2000-07-05 02:15:39 +04:00
|
|
|
/* Next line is so we discount the "magic line" */
|
2000-10-26 05:44:42 +04:00
|
|
|
if (filebot == fileptr && fileptr->data[0] == '\0')
|
|
|
|
break;
|
2000-07-05 02:15:39 +04:00
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
size = write(fd, fileptr->data, strlen(fileptr->data));
|
|
|
|
if (size == -1) {
|
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, _("Wrote >%s\n"), fileptr->data);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
write(fd, "\n", 1);
|
|
|
|
|
|
|
|
fileptr = fileptr->next;
|
|
|
|
lineswritten++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileptr != NULL) {
|
|
|
|
size = write(fd, fileptr->data, strlen(fileptr->data));
|
|
|
|
if (size == -1) {
|
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else if (size > 0) {
|
|
|
|
size = write(fd, "\n", 1);
|
|
|
|
if (size == -1) {
|
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (close(fd) == -1) {
|
2000-11-24 17:00:16 +03:00
|
|
|
statusbar(_("Could not close %s: %s"), realname, strerror(errno));
|
2000-06-19 08:22:15 +04:00
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-12-06 08:56:08 +03:00
|
|
|
if (realexists == -1 || tmp ||
|
|
|
|
(!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) {
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* Use default umask as file permissions if file is a new file. */
|
2000-12-06 08:56:08 +03:00
|
|
|
mask = umask(0);
|
|
|
|
umask(mask);
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2000-12-06 08:56:08 +03:00
|
|
|
if (tmp) /* We don't want anyone reading our temporary file! */
|
|
|
|
mask = 0600;
|
|
|
|
else
|
|
|
|
mask = 0666 & ~mask;
|
|
|
|
} else
|
2000-12-06 08:59:31 +03:00
|
|
|
/* Use permissions from file we are overwriting. */
|
2000-12-06 08:56:08 +03:00
|
|
|
mask = st.st_mode;
|
|
|
|
|
|
|
|
if (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) {
|
|
|
|
if (unlink(realname) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2000-12-03 00:13:50 +03:00
|
|
|
statusbar(_("Could not open %s for writing: %s"),
|
2000-12-06 08:56:08 +03:00
|
|
|
realname, strerror(errno));
|
2000-12-03 00:13:50 +03:00
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-06-19 08:22:15 +04:00
|
|
|
}
|
2000-12-06 08:56:08 +03:00
|
|
|
if (link(buf, realname) != -1)
|
|
|
|
unlink(buf);
|
|
|
|
else if (errno != EPERM) {
|
|
|
|
statusbar(_("Could not open %s for writing: %s"),
|
|
|
|
name, strerror(errno));
|
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
} else if (rename(buf, realname) == -1) { /* Try a rename?? */
|
|
|
|
statusbar(_("Could not open %s for writing: %s"),
|
|
|
|
realname, strerror(errno));
|
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-06-19 08:22:15 +04:00
|
|
|
}
|
2000-12-06 08:56:08 +03:00
|
|
|
if (chmod(realname, mask) == -1)
|
|
|
|
statusbar(_("Could not set permissions %o on %s: %s"),
|
|
|
|
mask, realname, strerror(errno));
|
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
if (!tmp) {
|
2001-06-06 03:45:54 +04:00
|
|
|
if (!nonamechange)
|
2001-06-06 03:24:55 +04:00
|
|
|
filename = mallocstrcpy(filename, realname);
|
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
statusbar(_("Wrote %d lines"), lineswritten);
|
2000-12-06 08:56:08 +03:00
|
|
|
UNSET(MODIFIED);
|
2001-01-03 10:11:47 +03:00
|
|
|
titlebar(NULL);
|
2000-06-19 08:22:15 +04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-05-29 08:21:44 +04:00
|
|
|
int do_writeout(char *path, int exiting, int append)
|
2000-06-19 08:22:15 +04:00
|
|
|
{
|
|
|
|
int i = 0;
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2000-11-24 23:45:14 +03:00
|
|
|
#ifdef NANO_EXTRA
|
|
|
|
static int did_cred = 0;
|
|
|
|
#endif
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2001-05-21 16:56:25 +04:00
|
|
|
#ifndef DISABLE_MOUSE
|
2001-04-12 07:01:53 +04:00
|
|
|
currshortcut = writefile_list;
|
|
|
|
currslen = WRITEFILE_LIST_LEN;
|
2001-05-21 16:56:25 +04:00
|
|
|
#endif
|
|
|
|
|
2001-01-07 08:50:36 +03:00
|
|
|
answer = mallocstrcpy(answer, path);
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2000-07-25 01:52:17 +04:00
|
|
|
if ((exiting) && (ISSET(TEMP_OPT))) {
|
2000-10-26 05:44:42 +04:00
|
|
|
if (filename[0]) {
|
2001-06-06 03:24:55 +04:00
|
|
|
i = write_file(answer, 0, 0, 0);
|
2000-07-25 01:52:17 +04:00
|
|
|
display_main_list();
|
|
|
|
return i;
|
2000-10-26 05:44:42 +04:00
|
|
|
} else {
|
2000-07-25 02:05:18 +04:00
|
|
|
UNSET(TEMP_OPT);
|
|
|
|
do_exit();
|
|
|
|
|
|
|
|
/* They cancelled, abort quit */
|
|
|
|
return -1;
|
2000-07-25 01:52:17 +04:00
|
|
|
}
|
2000-06-19 08:22:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2001-06-22 03:58:47 +04:00
|
|
|
#ifndef NANO_SMALL
|
2001-05-29 08:21:44 +04:00
|
|
|
if (ISSET(MARK_ISSET) && !exiting)
|
2001-06-06 02:57:43 +04:00
|
|
|
i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, "",
|
2001-05-29 08:21:44 +04:00
|
|
|
_("%s Selection to File"), append ? _("Append") : _("Write"));
|
|
|
|
else
|
2001-06-22 03:58:47 +04:00
|
|
|
#endif
|
2001-05-29 08:21:44 +04:00
|
|
|
i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, answer,
|
|
|
|
_("File Name to %s"), append ? _("Append") : _("Write"));
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2000-06-21 07:00:43 +04:00
|
|
|
if (i != -1) {
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2001-01-12 10:51:05 +03:00
|
|
|
#ifndef DISABLE_BROWSER
|
2001-01-03 10:11:47 +03:00
|
|
|
if (i == NANO_TOFILES_KEY) {
|
2001-01-06 00:13:14 +03:00
|
|
|
|
|
|
|
char *tmp = do_browse_from(answer);
|
2001-05-21 16:56:25 +04:00
|
|
|
|
|
|
|
#ifndef DISABLE_MOUSE
|
2001-04-12 07:01:53 +04:00
|
|
|
currshortcut = writefile_list;
|
|
|
|
currslen = WRITEFILE_LIST_LEN;
|
2001-05-21 16:56:25 +04:00
|
|
|
#endif
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-05-29 08:21:44 +04:00
|
|
|
if (tmp != NULL) {
|
2001-01-05 17:31:52 +03:00
|
|
|
answer = mallocstrcpy(answer, tmp);
|
2001-05-29 08:21:44 +04:00
|
|
|
} else
|
|
|
|
return do_writeout(answer, exiting, append);
|
|
|
|
} else
|
2001-01-03 10:11:47 +03:00
|
|
|
#endif
|
2001-05-29 08:21:44 +04:00
|
|
|
if (i == NANO_APPEND_KEY)
|
|
|
|
return(do_writeout(answer, exiting, 1 - append));
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, _("filename is %s"), answer);
|
|
|
|
#endif
|
2000-11-24 23:45:14 +03:00
|
|
|
|
|
|
|
#ifdef NANO_EXTRA
|
2000-12-06 08:56:08 +03:00
|
|
|
if (exiting && !ISSET(TEMP_OPT) && !strcasecmp(answer, "zzy")
|
|
|
|
&& !did_cred) {
|
2000-11-24 23:45:14 +03:00
|
|
|
do_credits();
|
|
|
|
did_cred = 1;
|
2000-12-06 08:56:08 +03:00
|
|
|
return -1;
|
2000-11-24 23:45:14 +03:00
|
|
|
}
|
|
|
|
#endif
|
2001-05-29 08:21:44 +04:00
|
|
|
if (!append && strcmp(answer, filename)) {
|
2000-06-19 08:22:15 +04:00
|
|
|
struct stat st;
|
|
|
|
if (!stat(answer, &st)) {
|
|
|
|
i = do_yesno(0, 0, _("File exists, OVERWRITE ?"));
|
|
|
|
|
|
|
|
if (!i || (i == -1))
|
|
|
|
continue;
|
2000-06-21 07:00:43 +04:00
|
|
|
}
|
|
|
|
}
|
2001-05-29 08:21:44 +04:00
|
|
|
#ifndef NANO_SMALL
|
2000-06-19 08:22:15 +04:00
|
|
|
|
2001-05-29 08:21:44 +04:00
|
|
|
/* Here's where we allow the selected text to be written to
|
|
|
|
a separate file. */
|
|
|
|
if (ISSET(MARK_ISSET) && !exiting) {
|
|
|
|
filestruct *fileagebak = fileage;
|
|
|
|
filestruct *filebotbak = filebot;
|
|
|
|
filestruct *cutback = cutbuffer;
|
|
|
|
int oldmod = 0;
|
2001-06-22 03:58:47 +04:00
|
|
|
cutbuffer = NULL;
|
2001-05-29 08:21:44 +04:00
|
|
|
|
|
|
|
/* Okay, since write_file changes the filename, back it up */
|
|
|
|
if (ISSET(MODIFIED))
|
|
|
|
oldmod = 1;
|
|
|
|
|
|
|
|
/* Now, non-destructively add the marked text to the
|
|
|
|
cutbuffer, and write the file out using the cutbuffer ;) */
|
2001-06-22 03:58:47 +04:00
|
|
|
if (current->lineno <= mark_beginbuf->lineno)
|
2001-05-29 08:21:44 +04:00
|
|
|
cut_marked_segment(current, current_x, mark_beginbuf,
|
|
|
|
mark_beginx, 0);
|
|
|
|
else
|
|
|
|
cut_marked_segment(mark_beginbuf, mark_beginx, current,
|
|
|
|
current_x, 0);
|
|
|
|
|
|
|
|
fileage = cutbuffer;
|
|
|
|
for (filebot = cutbuffer; filebot->next != NULL;
|
|
|
|
filebot = filebot->next)
|
|
|
|
;
|
2001-06-06 03:24:55 +04:00
|
|
|
i = write_file(answer, 0, append, 1);
|
2001-05-29 08:21:44 +04:00
|
|
|
|
|
|
|
/* Now restore everything */
|
|
|
|
fileage = fileagebak;
|
|
|
|
filebot = filebotbak;
|
|
|
|
cutbuffer = cutback;
|
|
|
|
if (oldmod)
|
|
|
|
set_modified();
|
|
|
|
} else
|
|
|
|
#endif
|
2001-06-06 03:24:55 +04:00
|
|
|
i = write_file(answer, 0, append, 0);
|
2001-05-29 08:21:44 +04:00
|
|
|
|
2000-06-19 08:22:15 +04:00
|
|
|
display_main_list();
|
|
|
|
return i;
|
2000-06-21 07:00:43 +04:00
|
|
|
} else {
|
2000-06-19 08:22:15 +04:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
display_main_list();
|
|
|
|
return 0;
|
2000-06-21 07:00:43 +04:00
|
|
|
}
|
2000-06-19 08:22:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_writeout_void(void)
|
|
|
|
{
|
2001-05-29 08:21:44 +04:00
|
|
|
return do_writeout(filename, 0, 0);
|
2000-06-19 08:22:15 +04:00
|
|
|
}
|
2000-11-05 20:54:41 +03:00
|
|
|
|
2001-01-11 08:30:31 +03:00
|
|
|
#ifndef DISABLE_TABCOMP
|
2000-11-24 17:00:16 +03:00
|
|
|
|
|
|
|
/* Return a malloc()ed string containing the actual directory, used
|
|
|
|
* to convert ~user and ~/ notation...
|
|
|
|
*/
|
2000-12-06 08:56:08 +03:00
|
|
|
char *real_dir_from_tilde(char *buf)
|
2000-11-24 17:00:16 +03:00
|
|
|
{
|
2001-01-23 06:09:15 +03:00
|
|
|
char *dirtmp = NULL, *find_user = NULL;
|
|
|
|
int i = 1;
|
2001-01-23 01:17:08 +03:00
|
|
|
struct passwd *userdata;
|
2000-11-24 17:00:16 +03:00
|
|
|
|
2001-01-23 06:09:15 +03:00
|
|
|
/* set a default value for dirtmp, in the case user home dir not found */
|
|
|
|
dirtmp = mallocstrcpy(dirtmp, buf);
|
|
|
|
|
2000-11-24 17:00:16 +03:00
|
|
|
if (buf[0] == '~') {
|
2001-01-23 06:09:15 +03:00
|
|
|
if (buf[1] == 0 || buf[1] == '/') {
|
2000-11-24 17:00:16 +03:00
|
|
|
if (getenv("HOME") != NULL) {
|
2001-01-23 06:09:15 +03:00
|
|
|
|
|
|
|
free(dirtmp);
|
2001-05-17 15:35:43 +04:00
|
|
|
dirtmp = charalloc(strlen(buf) + 2 + strlen(getenv("HOME")));
|
2000-11-24 17:00:16 +03:00
|
|
|
|
2001-01-23 06:09:15 +03:00
|
|
|
sprintf(dirtmp, "%s%s", getenv("HOME"), &buf[1]);
|
2001-01-08 19:59:19 +03:00
|
|
|
|
2000-11-24 17:00:16 +03:00
|
|
|
}
|
2001-01-23 06:09:15 +03:00
|
|
|
}
|
|
|
|
else {
|
2000-11-24 17:00:16 +03:00
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* Figure how how much of the str we need to compare */
|
2001-01-23 06:09:15 +03:00
|
|
|
for (i = 1; buf[i] != '/' && buf[i] != 0; i++)
|
|
|
|
;
|
|
|
|
|
|
|
|
find_user = mallocstrcpy(find_user, &buf[1]);
|
|
|
|
find_user[i - 1] = 0;
|
2000-11-25 07:43:43 +03:00
|
|
|
|
2001-01-23 01:17:08 +03:00
|
|
|
for (userdata = getpwent(); userdata != NULL &&
|
2001-01-23 06:09:15 +03:00
|
|
|
strcmp(userdata->pw_name, find_user);
|
2001-01-23 01:17:08 +03:00
|
|
|
userdata = getpwent());
|
2000-11-25 07:43:43 +03:00
|
|
|
|
2001-01-23 06:09:15 +03:00
|
|
|
free(find_user);
|
|
|
|
|
|
|
|
if (userdata != NULL) { /* User found */
|
2000-12-06 08:56:08 +03:00
|
|
|
|
2001-01-23 06:09:15 +03:00
|
|
|
free(dirtmp);
|
2001-05-17 15:35:43 +04:00
|
|
|
dirtmp = charalloc(strlen(buf) + 2 + strlen(userdata->pw_dir));
|
2001-01-23 06:09:15 +03:00
|
|
|
sprintf(dirtmp, "%s%s", userdata->pw_dir, &buf[i]);
|
|
|
|
|
|
|
|
}
|
2000-11-25 07:43:43 +03:00
|
|
|
|
2001-01-23 01:17:08 +03:00
|
|
|
endpwent();
|
2000-11-24 17:00:16 +03:00
|
|
|
}
|
2001-01-23 06:09:15 +03:00
|
|
|
}
|
2000-11-25 07:43:43 +03:00
|
|
|
|
|
|
|
return dirtmp;
|
2000-11-24 17:00:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tack a slash onto the string we're completing if it's a directory */
|
2000-11-25 07:43:43 +03:00
|
|
|
int append_slash_if_dir(char *buf, int *lastWasTab, int *place)
|
2000-11-24 17:00:16 +03:00
|
|
|
{
|
|
|
|
char *dirptr;
|
|
|
|
struct stat fileinfo;
|
2000-11-25 07:43:43 +03:00
|
|
|
int ret = 0;
|
2000-11-24 17:00:16 +03:00
|
|
|
|
|
|
|
dirptr = real_dir_from_tilde(buf);
|
|
|
|
|
|
|
|
if (stat(dirptr, &fileinfo) == -1)
|
2000-12-06 08:56:08 +03:00
|
|
|
ret = 0;
|
2000-11-24 17:00:16 +03:00
|
|
|
else if (S_ISDIR(fileinfo.st_mode)) {
|
|
|
|
strncat(buf, "/", 1);
|
|
|
|
*place += 1;
|
|
|
|
/* now we start over again with # of tabs so far */
|
|
|
|
*lastWasTab = 0;
|
2000-11-25 07:43:43 +03:00
|
|
|
ret = 1;
|
2000-11-24 17:00:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dirptr != buf)
|
2000-12-06 08:56:08 +03:00
|
|
|
free(dirptr);
|
2000-11-25 07:43:43 +03:00
|
|
|
|
|
|
|
return ret;
|
2000-11-24 17:00:16 +03:00
|
|
|
}
|
2000-11-05 20:54:41 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These functions (username_tab_completion, cwd_tab_completion, and
|
|
|
|
* input_tab were taken from busybox 0.46 (cmdedit.c). Here is the notice
|
|
|
|
* from that file:
|
|
|
|
*
|
|
|
|
* Termios command line History and Editting, originally
|
|
|
|
* intended for NetBSD sh (ash)
|
|
|
|
* Copyright (c) 1999
|
|
|
|
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
|
|
|
* Etc: Dave Cinege <dcinege@psychosis.com>
|
|
|
|
* Majorly adjusted/re-written for busybox:
|
|
|
|
* Erik Andersen <andersee@debian.org>
|
|
|
|
*
|
|
|
|
* You may use this code as you wish, so long as the original author(s)
|
|
|
|
* are attributed in any redistributions of the source code.
|
|
|
|
* This code is 'as is' with no warranty.
|
|
|
|
* This code may safely be consumed by a BSD or GPL license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char **username_tab_completion(char *buf, int *num_matches)
|
|
|
|
{
|
2001-01-23 06:27:31 +03:00
|
|
|
char **matches = (char **) NULL;
|
2001-01-19 07:17:24 +03:00
|
|
|
char *matchline = NULL;
|
2001-01-23 06:27:31 +03:00
|
|
|
struct passwd *userdata;
|
2000-11-24 17:00:16 +03:00
|
|
|
|
2001-01-19 07:17:24 +03:00
|
|
|
*num_matches = 0;
|
2001-01-02 09:21:07 +03:00
|
|
|
matches = nmalloc(BUFSIZ * sizeof(char *));
|
2001-01-19 07:17:24 +03:00
|
|
|
|
2000-11-24 17:00:16 +03:00
|
|
|
strcat(buf, "*");
|
2001-01-19 07:17:24 +03:00
|
|
|
|
2001-01-23 06:27:31 +03:00
|
|
|
while ((userdata = getpwent()) != NULL) {
|
2000-11-24 17:00:16 +03:00
|
|
|
|
2001-01-23 06:27:31 +03:00
|
|
|
if (check_wildcard_match(userdata->pw_name, &buf[1]) == TRUE) {
|
2000-11-24 17:00:16 +03:00
|
|
|
|
|
|
|
/* Cool, found a match. Add it to the list
|
|
|
|
* This makes a lot more sense to me (Chris) this way...
|
|
|
|
*/
|
2001-01-19 07:17:24 +03:00
|
|
|
|
2001-05-17 15:35:43 +04:00
|
|
|
matchline = charalloc(strlen(userdata->pw_name) + 2);
|
2001-01-23 06:27:31 +03:00
|
|
|
sprintf(matchline, "~%s", userdata->pw_name);
|
|
|
|
matches[*num_matches] = matchline;
|
|
|
|
++*num_matches;
|
2000-11-24 17:00:16 +03:00
|
|
|
|
2001-01-23 06:27:31 +03:00
|
|
|
/* If there's no more room, bail out */
|
|
|
|
if (*num_matches == BUFSIZ)
|
|
|
|
break;
|
2000-11-24 17:00:16 +03:00
|
|
|
}
|
2001-01-23 06:27:31 +03:00
|
|
|
}
|
|
|
|
endpwent();
|
2001-01-19 07:17:24 +03:00
|
|
|
|
2000-11-05 20:54:41 +03:00
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This was originally called exe_n_cwd_tab_completion, but we're not
|
|
|
|
worried about executables, only filenames :> */
|
|
|
|
|
|
|
|
char **cwd_tab_completion(char *buf, int *num_matches)
|
|
|
|
{
|
2000-11-24 17:00:16 +03:00
|
|
|
char *dirName, *dirtmp = NULL, *tmp = NULL, *tmp2 = NULL;
|
2000-11-05 20:54:41 +03:00
|
|
|
char **matches = (char **) NULL;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *next;
|
|
|
|
|
2001-01-02 09:21:07 +03:00
|
|
|
matches = nmalloc(BUFSIZ * sizeof(char *));
|
2000-11-05 20:54:41 +03:00
|
|
|
|
|
|
|
/* Stick a wildcard onto the buf, for later use */
|
|
|
|
strcat(buf, "*");
|
|
|
|
|
2000-11-15 04:25:42 +03:00
|
|
|
/* Okie, if there's a / in the buffer, strip out the directory part */
|
2000-11-05 20:54:41 +03:00
|
|
|
if (strcmp(buf, "") && strstr(buf, "/")) {
|
2001-05-17 15:35:43 +04:00
|
|
|
dirName = charalloc(strlen(buf) + 1);
|
2000-11-05 20:54:41 +03:00
|
|
|
tmp = buf + strlen(buf);
|
|
|
|
while (*tmp != '/' && tmp != buf)
|
2000-11-14 21:25:26 +03:00
|
|
|
tmp--;
|
2000-11-14 20:46:06 +03:00
|
|
|
|
2000-11-05 20:54:41 +03:00
|
|
|
tmp++;
|
|
|
|
|
2000-11-14 20:46:06 +03:00
|
|
|
strncpy(dirName, buf, tmp - buf + 1);
|
|
|
|
dirName[tmp - buf] = 0;
|
|
|
|
|
2000-11-05 20:54:41 +03:00
|
|
|
} else {
|
2001-04-03 05:02:38 +04:00
|
|
|
|
|
|
|
#ifdef PATH_MAX
|
|
|
|
if ((dirName = getcwd(NULL, PATH_MAX+1)) == NULL)
|
|
|
|
#else
|
2001-05-05 21:45:54 +04:00
|
|
|
/* The better, but apparently segfault-causing way */
|
2000-11-05 20:54:41 +03:00
|
|
|
if ((dirName = getcwd(NULL, 0)) == NULL)
|
2001-04-03 05:02:38 +04:00
|
|
|
#endif /* PATH_MAX */
|
2000-11-05 20:54:41 +03:00
|
|
|
return matches;
|
|
|
|
else
|
|
|
|
tmp = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "\nDir = %s\n", dirName);
|
|
|
|
fprintf(stderr, "\nbuf = %s\n", buf);
|
|
|
|
fprintf(stderr, "\ntmp = %s\n", tmp);
|
|
|
|
#endif
|
2000-11-14 21:25:26 +03:00
|
|
|
|
2000-11-24 17:00:16 +03:00
|
|
|
dirtmp = real_dir_from_tilde(dirName);
|
|
|
|
free(dirName);
|
|
|
|
dirName = dirtmp;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "\nDir = %s\n", dirName);
|
|
|
|
fprintf(stderr, "\nbuf = %s\n", buf);
|
|
|
|
fprintf(stderr, "\ntmp = %s\n", tmp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-11-05 20:54:41 +03:00
|
|
|
dir = opendir(dirName);
|
|
|
|
if (!dir) {
|
|
|
|
/* Don't print an error, just shut up and return */
|
|
|
|
*num_matches = 0;
|
|
|
|
beep();
|
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
while ((next = readdir(dir)) != NULL) {
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2000-11-15 04:25:42 +03:00
|
|
|
fprintf(stderr, "Comparing \'%s\'\n", next->d_name);
|
2000-11-05 20:54:41 +03:00
|
|
|
#endif
|
|
|
|
/* See if this matches */
|
|
|
|
if (check_wildcard_match(next->d_name, tmp) == TRUE) {
|
2000-11-06 07:04:15 +03:00
|
|
|
|
|
|
|
/* Cool, found a match. Add it to the list
|
|
|
|
* This makes a lot more sense to me (Chris) this way...
|
|
|
|
*/
|
|
|
|
tmp2 = NULL;
|
2001-05-17 15:35:43 +04:00
|
|
|
tmp2 = charalloc(strlen(next->d_name) + 1);
|
2000-11-06 07:04:15 +03:00
|
|
|
strcpy(tmp2, next->d_name);
|
|
|
|
matches[*num_matches] = tmp2;
|
2000-11-05 20:54:41 +03:00
|
|
|
++*num_matches;
|
2000-11-15 04:25:42 +03:00
|
|
|
|
|
|
|
/* If there's no more room, bail out */
|
|
|
|
if (*num_matches == BUFSIZ)
|
|
|
|
break;
|
2000-11-05 20:54:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
|
2000-11-14 20:46:06 +03:00
|
|
|
/* This function now has an arg which refers to how much the
|
2000-11-05 20:54:41 +03:00
|
|
|
* statusbar (place) should be advanced, i.e. the new cursor pos.
|
|
|
|
*/
|
2000-11-14 21:25:26 +03:00
|
|
|
char *input_tab(char *buf, int place, int *lastWasTab, int *newplace)
|
2000-11-05 20:54:41 +03:00
|
|
|
{
|
|
|
|
/* Do TAB completion */
|
2000-11-06 00:54:23 +03:00
|
|
|
static int num_matches = 0, match_matches = 0;
|
2000-11-05 20:54:41 +03:00
|
|
|
static char **matches = (char **) NULL;
|
2000-11-14 20:46:06 +03:00
|
|
|
int pos = place, i = 0, col = 0, editline = 0;
|
2000-11-25 07:43:43 +03:00
|
|
|
int longestname = 0, is_dir = 0;
|
2000-11-06 00:56:54 +03:00
|
|
|
char *foo;
|
2000-11-05 20:54:41 +03:00
|
|
|
|
2000-11-14 21:25:26 +03:00
|
|
|
if (*lastWasTab == FALSE) {
|
2000-11-14 20:46:06 +03:00
|
|
|
char *tmp, *copyto, *matchBuf;
|
2000-11-05 20:54:41 +03:00
|
|
|
|
2000-11-14 21:25:26 +03:00
|
|
|
*lastWasTab = 1;
|
|
|
|
|
2000-11-05 20:54:41 +03:00
|
|
|
/* Make a local copy of the string -- up to the position of the
|
|
|
|
cursor */
|
2001-01-15 05:26:12 +03:00
|
|
|
matchBuf = (char *) nmalloc((strlen(buf) + 2) * sizeof(char));
|
2001-01-16 07:45:38 +03:00
|
|
|
memset(matchBuf, '\0', (strlen(buf) + 2));
|
2000-11-06 08:40:03 +03:00
|
|
|
|
2000-11-05 20:54:41 +03:00
|
|
|
strncpy(matchBuf, buf, place);
|
|
|
|
tmp = matchBuf;
|
|
|
|
|
|
|
|
/* skip any leading white space */
|
2000-11-18 06:05:50 +03:00
|
|
|
while (*tmp && isspace((int) *tmp))
|
2000-11-05 20:54:41 +03:00
|
|
|
++tmp;
|
|
|
|
|
|
|
|
/* Free up any memory already allocated */
|
|
|
|
if (matches != NULL) {
|
2000-11-14 20:46:06 +03:00
|
|
|
for (i = i; i < num_matches; i++)
|
|
|
|
free(matches[i]);
|
2000-11-05 20:54:41 +03:00
|
|
|
free(matches);
|
|
|
|
matches = (char **) NULL;
|
2000-11-06 07:04:15 +03:00
|
|
|
num_matches = 0;
|
2000-11-05 20:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the word starts with `~' and there is no slash in the word,
|
|
|
|
* then try completing this word as a username. */
|
|
|
|
|
2000-11-24 17:00:16 +03:00
|
|
|
/* FIXME -- this check is broken! */
|
2000-12-06 08:56:08 +03:00
|
|
|
if (*tmp == '~' && !strchr(tmp, '/'))
|
|
|
|
matches = username_tab_completion(tmp, &num_matches);
|
2000-11-05 20:54:41 +03:00
|
|
|
|
|
|
|
/* Try to match everything in the current working directory that
|
|
|
|
* matches. */
|
|
|
|
if (!matches)
|
|
|
|
matches = cwd_tab_completion(tmp, &num_matches);
|
|
|
|
|
|
|
|
/* Don't leak memory */
|
|
|
|
free(matchBuf);
|
|
|
|
|
2000-11-14 20:46:06 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "%d matches found...\n", num_matches);
|
|
|
|
#endif
|
2000-11-05 20:54:41 +03:00
|
|
|
/* Did we find exactly one match? */
|
2000-11-14 21:25:26 +03:00
|
|
|
switch (num_matches) {
|
2000-11-06 08:40:03 +03:00
|
|
|
case 0:
|
2000-11-06 08:41:20 +03:00
|
|
|
blank_edit();
|
2000-11-15 04:25:42 +03:00
|
|
|
wrefresh(edit);
|
2000-11-06 08:40:03 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
2000-11-14 20:46:06 +03:00
|
|
|
|
|
|
|
buf = nrealloc(buf, strlen(buf) + strlen(matches[0]) + 1);
|
|
|
|
|
|
|
|
if (strcmp(buf, "") && strstr(buf, "/")) {
|
2000-11-14 21:25:26 +03:00
|
|
|
for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf;
|
|
|
|
tmp--);
|
2000-11-14 20:46:06 +03:00
|
|
|
tmp++;
|
2000-11-14 21:25:26 +03:00
|
|
|
} else
|
2000-11-14 20:46:06 +03:00
|
|
|
tmp = buf;
|
|
|
|
|
2000-11-25 07:43:43 +03:00
|
|
|
if (!strcmp(tmp, matches[0]))
|
|
|
|
is_dir = append_slash_if_dir(buf, lastWasTab, newplace);
|
|
|
|
|
|
|
|
if (is_dir)
|
|
|
|
break;
|
2000-11-14 20:46:06 +03:00
|
|
|
|
|
|
|
copyto = tmp;
|
2000-11-14 21:25:26 +03:00
|
|
|
for (pos = 0; *tmp == matches[0][pos] &&
|
|
|
|
pos <= strlen(matches[0]); pos++)
|
2000-11-14 20:46:06 +03:00
|
|
|
tmp++;
|
|
|
|
|
2000-11-15 04:25:42 +03:00
|
|
|
/* write out the matched name */
|
2000-11-14 21:25:26 +03:00
|
|
|
strncpy(copyto, matches[0], strlen(matches[0]) + 1);
|
2000-11-14 20:46:06 +03:00
|
|
|
*newplace += strlen(matches[0]) - pos;
|
|
|
|
|
2000-11-24 17:00:16 +03:00
|
|
|
/* Is it a directory? */
|
|
|
|
append_slash_if_dir(buf, lastWasTab, newplace);
|
|
|
|
|
2000-11-06 08:40:03 +03:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-05 21:45:54 +04:00
|
|
|
/* Check to see if all matches share a beginning, and, if so,
|
2000-11-14 21:25:26 +03:00
|
|
|
tack it onto buf and then beep */
|
2000-11-06 00:54:23 +03:00
|
|
|
|
2000-11-14 20:46:06 +03:00
|
|
|
if (strcmp(buf, "") && strstr(buf, "/")) {
|
2000-11-14 21:25:26 +03:00
|
|
|
for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf;
|
|
|
|
tmp--);
|
2000-11-14 20:46:06 +03:00
|
|
|
tmp++;
|
2000-11-14 21:25:26 +03:00
|
|
|
} else
|
2000-11-14 20:46:06 +03:00
|
|
|
tmp = buf;
|
|
|
|
|
|
|
|
for (pos = 0; *tmp == matches[0][pos] && *tmp != 0 &&
|
2000-11-14 21:25:26 +03:00
|
|
|
pos <= strlen(matches[0]); pos++)
|
2000-11-14 20:46:06 +03:00
|
|
|
tmp++;
|
|
|
|
|
2000-11-06 00:54:23 +03:00
|
|
|
while (1) {
|
|
|
|
match_matches = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < num_matches; i++) {
|
|
|
|
if (matches[i][pos] == 0)
|
|
|
|
break;
|
|
|
|
else if (matches[i][pos] == matches[0][pos])
|
|
|
|
match_matches++;
|
|
|
|
}
|
2000-11-14 21:25:26 +03:00
|
|
|
if (match_matches == num_matches &&
|
|
|
|
(i == num_matches || matches[i] != 0)) {
|
2000-11-06 00:54:23 +03:00
|
|
|
/* All the matches have the same character at pos+1,
|
2000-11-14 21:25:26 +03:00
|
|
|
so paste it into buf... */
|
2000-11-06 01:48:35 +03:00
|
|
|
buf = nrealloc(buf, strlen(buf) + 2);
|
2000-11-14 20:46:06 +03:00
|
|
|
strncat(buf, matches[0] + pos, 1);
|
2000-11-14 21:25:26 +03:00
|
|
|
*newplace += 1;
|
2000-11-06 00:54:23 +03:00
|
|
|
pos++;
|
2000-11-06 01:48:35 +03:00
|
|
|
} else {
|
2000-11-06 00:54:23 +03:00
|
|
|
beep();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-11-06 08:40:03 +03:00
|
|
|
break;
|
2000-11-06 00:54:23 +03:00
|
|
|
}
|
2000-11-05 20:54:41 +03:00
|
|
|
} else {
|
|
|
|
/* Ok -- the last char was a TAB. Since they
|
|
|
|
* just hit TAB again, print a list of all the
|
|
|
|
* available choices... */
|
|
|
|
if (matches && num_matches > 0) {
|
|
|
|
|
|
|
|
/* Blank the edit window, and print the matches out there */
|
|
|
|
blank_edit();
|
|
|
|
wmove(edit, 0, 0);
|
|
|
|
|
2000-11-06 00:54:23 +03:00
|
|
|
editline = 0;
|
2000-11-15 04:25:42 +03:00
|
|
|
|
2000-11-06 00:54:23 +03:00
|
|
|
/* Figure out the length of the longest filename */
|
|
|
|
for (i = 0; i < num_matches; i++)
|
|
|
|
if (strlen(matches[i]) > longestname)
|
|
|
|
longestname = strlen(matches[i]);
|
|
|
|
|
|
|
|
if (longestname > COLS - 1)
|
|
|
|
longestname = COLS - 1;
|
|
|
|
|
2001-05-17 15:35:43 +04:00
|
|
|
foo = charalloc(longestname + 5);
|
2000-11-06 00:56:54 +03:00
|
|
|
|
2000-11-05 20:54:41 +03:00
|
|
|
/* Print the list of matches */
|
|
|
|
for (i = 0, col = 0; i < num_matches; i++) {
|
2000-11-14 21:25:26 +03:00
|
|
|
|
2000-11-06 00:54:23 +03:00
|
|
|
/* make each filename shown be the same length as the longest
|
2000-11-14 21:25:26 +03:00
|
|
|
filename, with two spaces at the end */
|
2000-11-06 00:54:23 +03:00
|
|
|
snprintf(foo, longestname + 1, matches[i]);
|
|
|
|
while (strlen(foo) < longestname)
|
|
|
|
strcat(foo, " ");
|
|
|
|
|
|
|
|
strcat(foo, " ");
|
|
|
|
|
2000-11-14 20:46:06 +03:00
|
|
|
/* Disable el cursor */
|
|
|
|
curs_set(0);
|
2000-11-06 01:48:35 +03:00
|
|
|
/* now, put the match on the screen */
|
|
|
|
waddnstr(edit, foo, strlen(foo));
|
|
|
|
col += strlen(foo);
|
|
|
|
|
|
|
|
/* And if the next match isn't going to fit on the
|
|
|
|
line, move to the next one */
|
2000-11-14 21:25:26 +03:00
|
|
|
if (col > (COLS - longestname) && matches[i + 1] != NULL) {
|
2000-11-06 00:54:23 +03:00
|
|
|
editline++;
|
|
|
|
wmove(edit, editline, 0);
|
2000-11-14 21:25:26 +03:00
|
|
|
if (editline == editwinrows - 1) {
|
|
|
|
waddstr(edit, _("(more)"));
|
|
|
|
break;
|
|
|
|
}
|
2000-11-05 20:54:41 +03:00
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
2000-11-06 00:56:54 +03:00
|
|
|
free(foo);
|
2000-11-05 20:54:41 +03:00
|
|
|
wrefresh(edit);
|
2000-11-06 08:45:48 +03:00
|
|
|
} else
|
|
|
|
beep();
|
2000-11-05 20:54:41 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
edit_refresh();
|
2000-11-14 20:46:06 +03:00
|
|
|
curs_set(1);
|
|
|
|
return buf;
|
2000-11-05 20:54:41 +03:00
|
|
|
}
|
2000-11-24 17:00:16 +03:00
|
|
|
#endif
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-01-12 10:51:05 +03:00
|
|
|
#ifndef DISABLE_BROWSER
|
2001-01-03 10:11:47 +03:00
|
|
|
|
|
|
|
/* Return the stat of the file pointed to by path */
|
|
|
|
struct stat filestat(const char *path) {
|
|
|
|
struct stat st;
|
|
|
|
|
2001-03-28 17:04:08 +04:00
|
|
|
stat(path, &st);
|
2001-01-03 10:11:47 +03:00
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Our sort routine for file listings - sort directories before
|
|
|
|
* files, and then alphabetically
|
|
|
|
*/
|
|
|
|
int diralphasort(const void *va, const void *vb) {
|
|
|
|
struct stat file1info, file2info;
|
|
|
|
char *a = *(char **)va, *b = *(char **)vb;
|
2001-04-03 05:02:38 +04:00
|
|
|
int aisdir, bisdir;
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-04-03 05:02:38 +04:00
|
|
|
aisdir = (stat(a, &file1info) != -1) && S_ISDIR(file1info.st_mode);
|
|
|
|
bisdir = (stat(b, &file2info) != -1) && S_ISDIR(file2info.st_mode);
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-04-03 05:02:38 +04:00
|
|
|
if (aisdir && !bisdir) return -1;
|
|
|
|
if (!aisdir && bisdir) return 1;
|
|
|
|
|
|
|
|
#ifdef HAVE_STRCASECMP
|
|
|
|
return(strcasecmp(a,b));
|
|
|
|
#else
|
|
|
|
return(strcmp(a,b));
|
|
|
|
#endif
|
2001-01-03 10:11:47 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
|
|
|
|
/* Initialize the browser code, including the list of files in *path */
|
2001-01-03 10:11:47 +03:00
|
|
|
char **browser_init(char *path, int *longest, int *numents)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *next;
|
|
|
|
char **filelist = (char **) NULL;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
dir = opendir(path);
|
|
|
|
if (!dir)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*numents = 0;
|
|
|
|
while ((next = readdir(dir)) != NULL) {
|
|
|
|
if (!strcmp(next->d_name, "."))
|
|
|
|
continue;
|
|
|
|
(*numents)++;
|
|
|
|
if (strlen(next->d_name) > *longest)
|
|
|
|
*longest = strlen(next->d_name);
|
|
|
|
}
|
|
|
|
rewinddir(dir);
|
|
|
|
*longest += 10;
|
|
|
|
|
|
|
|
filelist = nmalloc(*numents * sizeof (char *));
|
|
|
|
|
|
|
|
while ((next = readdir(dir)) != NULL) {
|
|
|
|
if (!strcmp(next->d_name, "."))
|
|
|
|
continue;
|
2001-05-17 15:35:43 +04:00
|
|
|
filelist[i] = charalloc(strlen(next->d_name) + strlen(path) + 2);
|
2001-01-03 10:11:47 +03:00
|
|
|
|
|
|
|
if (!strcmp(path, "/"))
|
|
|
|
snprintf(filelist[i], strlen(next->d_name) + strlen(path) + 1,
|
|
|
|
"%s%s", path, next->d_name);
|
|
|
|
else
|
|
|
|
snprintf(filelist[i], strlen(next->d_name) + strlen(path) + 2,
|
|
|
|
"%s/%s", path, next->d_name);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*longest > COLS - 1)
|
|
|
|
*longest = COLS - 1;
|
|
|
|
|
|
|
|
return filelist;
|
|
|
|
}
|
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* Free our malloc()ed memory */
|
2001-01-03 10:11:47 +03:00
|
|
|
void free_charptrarray(char **array, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len - 1; i++)
|
|
|
|
free(array[i]);
|
|
|
|
free(array);
|
|
|
|
}
|
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* only print the last part of a path; isn't there a shell
|
2001-01-04 07:59:17 +03:00
|
|
|
command for this? */
|
2001-01-03 10:11:47 +03:00
|
|
|
char *tail(char *foo)
|
|
|
|
{
|
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
tmp = foo + strlen(foo);
|
|
|
|
while (*tmp != '/' && tmp != foo)
|
|
|
|
tmp--;
|
|
|
|
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
/* Strip one dir from the end of a string */
|
2001-01-03 10:11:47 +03:00
|
|
|
void striponedir(char *foo)
|
|
|
|
{
|
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
/* Don't strip the root dir */
|
|
|
|
if (!strcmp(foo, "/"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
tmp = foo + strlen(foo);
|
|
|
|
if (*tmp == '/')
|
|
|
|
tmp--;
|
|
|
|
|
|
|
|
while (*tmp != '/' && tmp != foo)
|
|
|
|
tmp--;
|
|
|
|
|
|
|
|
if (tmp != foo)
|
|
|
|
*tmp = 0;
|
|
|
|
else
|
|
|
|
*(tmp+1) = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
/* Our browser function. inpath is the path to start browsing from */
|
2001-01-03 10:11:47 +03:00
|
|
|
char *do_browser(char *inpath)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *foo, *retval = NULL;
|
|
|
|
static char *path = NULL;
|
|
|
|
int numents = 0, i = 0, j = 0, kbinput = 0, longest = 0, abort = 0;
|
|
|
|
int col = 0, selected = 0, editline = 0, width = 0, filecols = 0;
|
2001-01-06 21:12:43 +03:00
|
|
|
int lineno = 0, kb;
|
2001-01-03 10:11:47 +03:00
|
|
|
char **filelist = (char **) NULL;
|
2001-05-06 03:17:36 +04:00
|
|
|
#ifndef DISABLE_MOUSE
|
|
|
|
#ifdef NCURSES_MOUSE_VERSION
|
|
|
|
MEVENT mevent;
|
|
|
|
#endif
|
|
|
|
#endif
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
/* If path isn't the same as inpath, we are being passed a new
|
|
|
|
dir as an arg. We free it here so it will be copied from
|
|
|
|
inpath below */
|
2001-01-03 10:11:47 +03:00
|
|
|
if (path != NULL && strcmp(path, inpath)) {
|
|
|
|
free(path);
|
|
|
|
path = NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
/* if path doesn't exist, make it so */
|
2001-01-03 10:11:47 +03:00
|
|
|
if (path == NULL)
|
|
|
|
path = mallocstrcpy(path, inpath);
|
|
|
|
|
|
|
|
filelist = browser_init(path, &longest, &numents);
|
2001-05-17 15:35:43 +04:00
|
|
|
foo = charalloc(longest + 8);
|
2001-01-04 07:59:17 +03:00
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* Sort the list by directory first, then alphabetically */
|
2001-01-03 10:11:47 +03:00
|
|
|
qsort(filelist, numents, sizeof(char *), diralphasort);
|
|
|
|
|
2001-01-06 21:12:43 +03:00
|
|
|
kb = keypad_on(edit, 1);
|
2001-01-03 10:11:47 +03:00
|
|
|
titlebar(path);
|
|
|
|
bottombars(browser_list, BROWSER_LIST_LEN);
|
|
|
|
curs_set(0);
|
|
|
|
wmove(edit, 0, 0);
|
|
|
|
i = 0;
|
|
|
|
width = 0;
|
|
|
|
filecols = 0;
|
2001-01-04 07:59:17 +03:00
|
|
|
|
|
|
|
/* Loop invariant: Microsoft sucks. */
|
2001-01-03 10:11:47 +03:00
|
|
|
do {
|
2001-04-14 10:50:24 +04:00
|
|
|
DIR *test_dir;
|
|
|
|
|
|
|
|
blank_statusbar_refresh();
|
|
|
|
|
2001-05-21 16:56:25 +04:00
|
|
|
#ifndef DISABLE_MOUSE
|
2001-05-07 16:08:37 +04:00
|
|
|
currshortcut = browser_list;
|
|
|
|
currslen = BROWSER_LIST_LEN;
|
2001-05-21 16:56:25 +04:00
|
|
|
#endif
|
|
|
|
|
2001-01-03 10:11:47 +03:00
|
|
|
editline = 0;
|
|
|
|
col = 0;
|
2001-01-04 08:20:23 +03:00
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* Compute line number we're on now, so we don't divide by zero later */
|
2001-01-04 08:20:23 +03:00
|
|
|
if (width == 0)
|
|
|
|
lineno = selected;
|
|
|
|
else
|
|
|
|
lineno = selected / width;
|
2001-01-03 10:11:47 +03:00
|
|
|
|
|
|
|
switch (kbinput) {
|
2001-04-12 07:01:53 +04:00
|
|
|
|
2001-05-06 03:17:36 +04:00
|
|
|
#ifndef DISABLE_MOUSE
|
2001-04-12 07:01:53 +04:00
|
|
|
#ifdef NCURSES_MOUSE_VERSION
|
|
|
|
case KEY_MOUSE:
|
2001-05-06 03:17:36 +04:00
|
|
|
if (getmouse(&mevent) == ERR)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* If they clicked in the edit window, they probably clicked
|
|
|
|
on a file */
|
|
|
|
if (wenclose(edit, mevent.y, mevent.x)) {
|
|
|
|
int selectedbackup = selected;
|
|
|
|
|
|
|
|
mevent.y -= 2;
|
|
|
|
|
|
|
|
/* If we're on line 0, don't toy with finding out what
|
|
|
|
page we're on */
|
|
|
|
if (lineno / editwinrows == 0)
|
|
|
|
selected = mevent.y * width + mevent.x / longest;
|
|
|
|
else
|
|
|
|
selected = (lineno / editwinrows) * editwinrows * width
|
|
|
|
+ mevent.y * width + mevent.x / longest;
|
|
|
|
|
|
|
|
/* If we're off the screen, reset to the last item.
|
|
|
|
If we clicked where we did last time, select this name! */
|
2001-05-06 07:02:21 +04:00
|
|
|
if (selected > numents - 1)
|
2001-05-06 03:17:36 +04:00
|
|
|
selected = numents - 1;
|
|
|
|
else if (selectedbackup == selected) {
|
|
|
|
ungetch('s'); /* Unget the 'select' key */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else /* Must be clicking a shortcut */
|
|
|
|
do_mouse();
|
|
|
|
|
2001-04-12 07:01:53 +04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#endif
|
2001-01-03 10:11:47 +03:00
|
|
|
case KEY_UP:
|
|
|
|
case 'u':
|
|
|
|
if (selected - width >= 0)
|
|
|
|
selected -= width;
|
|
|
|
break;
|
|
|
|
case KEY_LEFT:
|
|
|
|
case 'l':
|
|
|
|
if (selected > 0)
|
|
|
|
selected--;
|
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
|
|
case 'd':
|
|
|
|
if (selected + width <= numents - 1)
|
|
|
|
selected += width;
|
|
|
|
break;
|
|
|
|
case KEY_RIGHT:
|
|
|
|
case 'r':
|
|
|
|
if (selected < numents - 1)
|
|
|
|
selected++;
|
|
|
|
break;
|
|
|
|
case NANO_PREVPAGE_KEY:
|
2001-01-04 07:41:08 +03:00
|
|
|
case NANO_PREVPAGE_FKEY:
|
2001-01-03 10:11:47 +03:00
|
|
|
case KEY_PPAGE:
|
2001-01-09 00:14:04 +03:00
|
|
|
case '-':
|
2001-01-04 08:20:23 +03:00
|
|
|
|
|
|
|
if (lineno % editwinrows == 0) {
|
2001-01-03 18:09:27 +03:00
|
|
|
if (selected - (editwinrows * width) >= 0)
|
|
|
|
selected -= editwinrows * width;
|
2001-01-03 10:11:47 +03:00
|
|
|
else
|
|
|
|
selected = 0;
|
|
|
|
}
|
|
|
|
else if (selected - (editwinrows +
|
2001-01-04 08:20:23 +03:00
|
|
|
lineno % editwinrows) * width >= 0)
|
|
|
|
|
|
|
|
selected -= (editwinrows + lineno % editwinrows) * width;
|
2001-01-03 10:11:47 +03:00
|
|
|
else
|
|
|
|
selected = 0;
|
|
|
|
break;
|
|
|
|
case NANO_NEXTPAGE_KEY:
|
2001-01-04 07:41:08 +03:00
|
|
|
case NANO_NEXTPAGE_FKEY:
|
2001-01-03 10:11:47 +03:00
|
|
|
case KEY_NPAGE:
|
2001-01-09 00:14:04 +03:00
|
|
|
case ' ':
|
2001-01-04 08:20:23 +03:00
|
|
|
if (lineno % editwinrows == 0) {
|
2001-01-03 18:09:27 +03:00
|
|
|
if (selected + (editwinrows * width) <= numents - 1)
|
|
|
|
selected += editwinrows * width;
|
2001-01-03 10:11:47 +03:00
|
|
|
else
|
|
|
|
selected = numents - 1;
|
|
|
|
}
|
|
|
|
else if (selected + (editwinrows -
|
2001-01-04 08:20:23 +03:00
|
|
|
lineno % editwinrows) * width <= numents - 1)
|
|
|
|
selected += (editwinrows - lineno % editwinrows) * width;
|
2001-01-03 10:11:47 +03:00
|
|
|
else
|
|
|
|
selected = numents - 1;
|
|
|
|
break;
|
|
|
|
case KEY_ENTER:
|
|
|
|
case NANO_CONTROL_M:
|
2001-01-04 08:22:46 +03:00
|
|
|
case 's': /* More Pico compatibility */
|
|
|
|
case 'S':
|
2001-01-04 07:59:17 +03:00
|
|
|
|
|
|
|
/* You can't cd up from / */
|
2001-04-14 10:50:24 +04:00
|
|
|
if (!strcmp(filelist[selected], "/..") && !strcmp(path, "/")) {
|
2001-01-03 10:11:47 +03:00
|
|
|
statusbar(_("Can't move up a directory"));
|
2001-04-14 10:50:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = mallocstrcpy(path, filelist[selected]);
|
2001-01-03 10:11:47 +03:00
|
|
|
|
|
|
|
st = filestat(path);
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2001-04-14 10:50:24 +04:00
|
|
|
if ((test_dir = opendir(path)) == NULL) {
|
2001-01-04 07:59:17 +03:00
|
|
|
/* We can't open this dir for some reason. Complain */
|
2001-01-03 10:11:47 +03:00
|
|
|
statusbar(_("Can't open \"%s\": %s"), path, strerror(errno));
|
2001-04-14 10:50:24 +04:00
|
|
|
striponedir(path);
|
2001-01-03 10:11:47 +03:00
|
|
|
align(&path);
|
|
|
|
break;
|
2001-01-04 07:59:17 +03:00
|
|
|
}
|
2001-04-14 10:50:24 +04:00
|
|
|
closedir(test_dir);
|
2001-01-03 10:11:47 +03:00
|
|
|
|
|
|
|
if (!strcmp("..", tail(path))) {
|
2001-01-04 07:59:17 +03:00
|
|
|
/* They want to go up a level, so strip off .. and the
|
|
|
|
current dir */
|
2001-01-03 10:11:47 +03:00
|
|
|
striponedir(path);
|
|
|
|
striponedir(path);
|
|
|
|
align(&path);
|
|
|
|
}
|
2001-01-04 07:59:17 +03:00
|
|
|
|
|
|
|
/* Start over again with the new path value */
|
2001-01-03 10:11:47 +03:00
|
|
|
return do_browser(path);
|
|
|
|
} else {
|
|
|
|
retval = path;
|
|
|
|
abort = 1;
|
|
|
|
}
|
|
|
|
break;
|
2001-04-14 10:50:24 +04:00
|
|
|
/* Goto a specific directory */
|
|
|
|
case 'g': /* Pico compatibility */
|
|
|
|
case 'G':
|
|
|
|
case NANO_GOTO_KEY:
|
|
|
|
|
|
|
|
curs_set(1);
|
|
|
|
j = statusq(0, gotodir_list, GOTODIR_LIST_LEN, "", _("Goto Directory"));
|
|
|
|
bottombars(browser_list, BROWSER_LIST_LEN);
|
|
|
|
curs_set(0);
|
|
|
|
|
|
|
|
if (j < 0) {
|
|
|
|
statusbar(_("Goto Cancelled"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (answer[0] != '/') {
|
|
|
|
char *saveanswer = NULL;
|
|
|
|
|
|
|
|
saveanswer = mallocstrcpy(saveanswer, answer);
|
|
|
|
answer = realloc(answer, strlen(path) + strlen(saveanswer) + 2);
|
|
|
|
sprintf(answer, "%s/%s", path, saveanswer);
|
|
|
|
free(saveanswer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((test_dir = opendir(answer)) == NULL) {
|
|
|
|
/* We can't open this dir for some reason. Complain */
|
|
|
|
statusbar(_("Can't open \"%s\": %s"), answer, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
closedir(test_dir);
|
|
|
|
|
|
|
|
/* Start over again with the new path value */
|
|
|
|
path = mallocstrcpy(path, answer);
|
|
|
|
return do_browser(path);
|
|
|
|
|
2001-01-03 10:11:47 +03:00
|
|
|
/* Stuff we want to abort the browser */
|
2001-04-30 15:28:46 +04:00
|
|
|
case NANO_CONTROL_C:
|
2001-01-03 10:11:47 +03:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
case 'e': /* Pico compatibility, yeech */
|
|
|
|
case 'E':
|
2001-01-04 07:41:08 +03:00
|
|
|
case NANO_EXIT_FKEY:
|
2001-01-03 10:11:47 +03:00
|
|
|
abort = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (abort)
|
|
|
|
break;
|
|
|
|
|
2001-04-14 10:50:24 +04:00
|
|
|
blank_edit();
|
|
|
|
|
2001-01-03 10:11:47 +03:00
|
|
|
if (width)
|
|
|
|
i = width * editwinrows * ((selected / width) / editwinrows);
|
|
|
|
else
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
wmove(edit, 0, 0);
|
|
|
|
for (j = i; j < numents && editline <= editwinrows - 1; j++) {
|
|
|
|
filecols++;
|
|
|
|
|
|
|
|
strncpy(foo, tail(filelist[j]), strlen(tail(filelist[j])) + 1);
|
|
|
|
while (strlen(foo) < longest)
|
|
|
|
strcat(foo, " ");
|
|
|
|
col += strlen(foo);
|
|
|
|
|
|
|
|
/* Put file info in the string also */
|
2001-05-05 21:45:54 +04:00
|
|
|
/* We use lstat here to detect links; then, if we find a
|
|
|
|
symlink, we examine it via stat() to see if it is a
|
2001-03-28 17:04:08 +04:00
|
|
|
directory or just a file symlink */
|
|
|
|
lstat(filelist[j], &st);
|
2001-01-03 10:11:47 +03:00
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
strcpy(foo + longest - 5, "(dir)");
|
|
|
|
else {
|
2001-03-28 17:04:08 +04:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
/* Aha! It's a symlink! Now, is it a dir? If so,
|
|
|
|
mark it as such */
|
|
|
|
st = filestat(filelist[j]);
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
strcpy(foo + longest - 5, "(dir)");
|
|
|
|
else
|
|
|
|
strcpy(foo + longest - 2, "--");
|
2001-04-03 05:02:38 +04:00
|
|
|
} else if (st.st_size < (1 << 10)) /* less than 1 K */
|
|
|
|
sprintf(foo + longest - 7, "%4d B",
|
2001-05-24 01:54:47 +04:00
|
|
|
(int) st.st_size);
|
2001-04-03 05:02:38 +04:00
|
|
|
else if (st.st_size >= (1 << 30)) /* at least 1 gig */
|
|
|
|
sprintf(foo + longest - 7, "%4d GB",
|
|
|
|
(int) st.st_size >> 30);
|
|
|
|
else if (st.st_size >= (1 << 20)) /* at least 1 meg */
|
|
|
|
sprintf(foo + longest - 7, "%4d MB",
|
|
|
|
(int) st.st_size >> 20);
|
2001-01-03 10:11:47 +03:00
|
|
|
else /* Its more than 1 k and less than a meg */
|
2001-04-03 05:02:38 +04:00
|
|
|
sprintf(foo + longest - 7, "%4d KB",
|
|
|
|
(int) st.st_size >> 10);
|
2001-01-03 10:11:47 +03:00
|
|
|
}
|
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
/* Hilight the currently selected file/dir */
|
2001-04-30 15:28:46 +04:00
|
|
|
if (j == selected) {
|
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
color_on(edit, COLOR_STATUSBAR);
|
|
|
|
#else
|
2001-01-03 10:11:47 +03:00
|
|
|
wattron(edit, A_REVERSE);
|
2001-04-30 15:28:46 +04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
2001-01-03 10:11:47 +03:00
|
|
|
waddnstr(edit, foo, strlen(foo));
|
2001-04-30 15:28:46 +04:00
|
|
|
if (j == selected) {
|
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
color_off(edit, COLOR_STATUSBAR);
|
|
|
|
#else
|
2001-01-03 10:11:47 +03:00
|
|
|
wattroff(edit, A_REVERSE);
|
2001-04-30 15:28:46 +04:00
|
|
|
#endif
|
|
|
|
}
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
/* And add some space between the cols */
|
2001-01-03 10:11:47 +03:00
|
|
|
waddstr(edit, " ");
|
|
|
|
col += 2;
|
|
|
|
|
|
|
|
/* And if the next entry isn't going to fit on the
|
|
|
|
line, move to the next one */
|
|
|
|
if (col > (COLS - longest)) {
|
|
|
|
editline++;
|
|
|
|
wmove(edit, editline, 0);
|
|
|
|
col = 0;
|
|
|
|
if (width == 0)
|
|
|
|
width = filecols;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wrefresh(edit);
|
|
|
|
} while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY);
|
|
|
|
curs_set(1);
|
|
|
|
blank_edit();
|
|
|
|
titlebar(NULL);
|
|
|
|
edit_refresh();
|
2001-01-06 21:12:43 +03:00
|
|
|
kb = keypad_on(edit, kb);
|
2001-01-03 10:11:47 +03:00
|
|
|
|
2001-01-04 07:59:17 +03:00
|
|
|
/* cleanup */
|
2001-01-03 10:11:47 +03:00
|
|
|
free_charptrarray(filelist, numents);
|
|
|
|
free(foo);
|
|
|
|
return retval;
|
|
|
|
}
|
2001-01-06 00:13:14 +03:00
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* Browser front end, checks to see if inpath has a dir in it and, if so,
|
2001-01-06 00:13:14 +03:00
|
|
|
starts do_browser from there, else from the current dir */
|
|
|
|
char *do_browse_from(char *inpath)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
tmp = mallocstrcpy(tmp, inpath);
|
|
|
|
|
2001-04-03 05:02:38 +04:00
|
|
|
|
2001-05-05 21:45:54 +04:00
|
|
|
/* If there's no / in the string, we may as well start from . */
|
2001-04-03 05:02:38 +04:00
|
|
|
if (tmp == NULL || *tmp == '\0' || !strstr(tmp, "/")) {
|
|
|
|
#ifdef PATH_MAX
|
|
|
|
char *from = getcwd(NULL, PATH_MAX+1);
|
|
|
|
#else
|
2001-04-03 18:26:35 +04:00
|
|
|
char *from = getcwd(NULL, 0);
|
2001-04-03 05:02:38 +04:00
|
|
|
#endif /* PATH_MAX */
|
|
|
|
return do_browser(from ? from : "./");
|
|
|
|
}
|
2001-01-06 00:13:14 +03:00
|
|
|
|
|
|
|
/* If the string is a directory, pass do_browser that */
|
|
|
|
st = filestat(tmp);
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
return do_browser(tmp);
|
|
|
|
|
|
|
|
/* Okay, there's a dir in there, but not at the end of the string...
|
|
|
|
try stripping it off */
|
|
|
|
striponedir(tmp);
|
|
|
|
align(&tmp);
|
|
|
|
return do_browser(tmp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-03 10:11:47 +03:00
|
|
|
#endif
|
|
|
|
|