files.c 31.9 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**************************************************************************
 *   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.            *
 *                                                                        *
 **************************************************************************/

#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>
Chris Allegretta's avatar
Chris Allegretta committed
30
31
#include <ctype.h>
#include <dirent.h>
Chris Allegretta's avatar
Chris Allegretta committed
32
33
34
35

#include "config.h"
#include "proto.h"
#include "nano.h"
Chris Allegretta's avatar
Chris Allegretta committed
36

Chris Allegretta's avatar
Chris Allegretta committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#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));
    fileage->data = nmalloc(1);
    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));
    fileptr->data = nmalloc(strlen(buf) + 2);
    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)
{
131
    long size, num_lines = 0, linetemp = 0;
Chris Allegretta's avatar
Chris Allegretta committed
132
133
134
135
136
137
138
    char input[2];		/* buffer */
    char *buf;
    long i = 0, bufx = 128;
    filestruct *fileptr = current, *tmp = NULL;
    int line1ins = 0;

    buf = nmalloc(bufx);
139
    buf[0] = '\0';
Chris Allegretta's avatar
Chris Allegretta committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

    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);
155
	    num_lines++;
Chris Allegretta's avatar
Chris Allegretta committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
	    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
	       decrease it at all.  We do free it at the end though. */

	    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);
178
	num_lines++;
Chris Allegretta's avatar
Chris Allegretta committed
179
180
181
	buf[0] = 0;
    }
    /* Did we even GET a file? */
182
    if (totsize == 0 || fileptr == NULL) {
Chris Allegretta's avatar
Chris Allegretta committed
183
	new_file();
184
	statusbar(_("Read %d lines"), num_lines);
Chris Allegretta's avatar
Chris Allegretta committed
185
186
	return 1;
    }
Robert Siemborski's avatar
Robert Siemborski committed
187

Chris Allegretta's avatar
Chris Allegretta committed
188
189
190
191
192
193
194
195
    if (current != NULL) {
	fileptr->next = current;
	current->prev = fileptr;
	renumber(current);
	current_x = 0;
	placewewant = 0;
    } else if (fileptr->next == NULL) {
	filebot = fileptr;
Robert Siemborski's avatar
Robert Siemborski committed
196
	new_magicline();
Chris Allegretta's avatar
Chris Allegretta committed
197
	totsize--;
Robert Siemborski's avatar
Robert Siemborski committed
198
199

	/* Update the edit buffer */
Chris Allegretta's avatar
Chris Allegretta committed
200
201
	load_file();
    }
202
203
    statusbar(_("Read %d lines"), num_lines);
    totlines += num_lines;
Chris Allegretta's avatar
Chris Allegretta committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

    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);
230
231
	if (!insert)
	    new_file();
Chris Allegretta's avatar
Chris Allegretta committed
232
233
	return -1;
    } else {			/* File is A-OK */
234
235
236
237
238
239
240
241
	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);

Chris Allegretta's avatar
Chris Allegretta committed
242
243
	    if (!insert)
		new_file();
Chris Allegretta's avatar
Chris Allegretta committed
244
245
246
247
248
249
250
251
252
253
254
255
256
	    return -1;
	}
	if (!quiet)
	    statusbar(_("Reading File"));
	read_file(fd, filename);
    }

    return 1;
}

int do_insertfile(void)
{
    int i;
257
    char *realname = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
258
259

    wrap_reset();
260
    i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, "",
Chris Allegretta's avatar
Chris Allegretta committed
261
262
263
264
265
266
267
		_("File to insert [from ./] "));
    if (i != -1) {

#ifdef DEBUG
	fprintf(stderr, "filename is %s", answer);
#endif

268
#ifndef DISABLE_TABCOMP
269
	realname = real_dir_from_tilde(answer);
270
#else
271
	realname = mallocstrcpy(realname, answer);
272
#endif
273

Chris Allegretta's avatar
Chris Allegretta committed
274
275
276
277
278
279
280
281
282
283
284
#ifdef ENABLE_BROWSER
	if (i == NANO_TOFILES_KEY) {
	    char *tmp = do_browser(getcwd(NULL, 0));

	    if 	(tmp != NULL) {
		free(realname);
		realname = tmp;
	    }
	}
#endif

285
286
	i = open_file(realname, 1, 0);
	free(realname);
Chris Allegretta's avatar
Chris Allegretta committed
287
288
289
290
291

	dump_buffer(fileage);
	set_modified();

	/* Here we want to rebuild the edit window */
Robert Siemborski's avatar
Robert Siemborski committed
292
	fix_editbot();
Chris Allegretta's avatar
Chris Allegretta committed
293
294

	/* If we've gone off the bottom, recenter, otherwise just redraw */
Chris Allegretta's avatar
Chris Allegretta committed
295
	if (current->lineno > editbot->lineno)
296
	    edit_update(current, CENTER);
Chris Allegretta's avatar
Chris Allegretta committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
	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,
 * we don't set the global variable filename to it's name, and don't
 * print out how many lines we wrote on the statusbar.
 * 
316
317
 * tmp means we are writing a tmp file in a secute fashion.  We use
 * it when spell checking or dumping the file on an error.
Chris Allegretta's avatar
Chris Allegretta committed
318
319
320
321
 */
int write_file(char *name, int tmp)
{
    long size, lineswritten = 0;
Chris Allegretta's avatar
Chris Allegretta committed
322
    static char *buf = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
323
    filestruct *fileptr;
324
    int fd, mask = 0, realexists, anyexists;
325
    struct stat st, lst;
326
    static char *realname = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
327
328
329
330
331

    if (!strcmp(name, "")) {
	statusbar(_("Cancelled"));
	return -1;
    }
Chris Allegretta's avatar
Chris Allegretta committed
332
    titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
333
    fileptr = fileage;
334
335
336
337

    if (realname != NULL)
	free(realname);

Chris Allegretta's avatar
Chris Allegretta committed
338
339
340
    if (buf != NULL)
	free(buf);

341
342
343
344
345
#ifndef DISABLE_TABCOMP
    realname = real_dir_from_tilde(name);
#else
    realname = mallocstrcpy(realname, name);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
346

347
    /* Save the state of file at the end of the symlink (if there is one) */
348
349
    realexists = stat(realname, &st);

350
351
    /* Stat the link itself for the check... */
    anyexists = lstat(realname, &lst);
352

353
354
    /* New case: if the file exists, just give up */
    if (tmp && anyexists != -1)
355
	return -1;
356
    /* NOTE: If you change this statement, you MUST CHANGE the if 
357
358
359
       statement below (that says:
		if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) &&
		S_ISLNK(lst.st_mode))) {
360
361
       to reflect whether or not to link/unlink/rename the file */
    else if (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode) || tmp) {
Chris Allegretta's avatar
Chris Allegretta committed
362
	/* Use O_EXCL if tmp == 1.  This is now copied from joe, because
363
364
365
366
367
	   wiggy says so *shrug*. */
	if (tmp)
	    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));
368
369

	/* First, just give up if we couldn't even open the file */
370
	if (fd == -1) {
371
	    if (!tmp && ISSET(TEMP_OPT)) {
Chris Allegretta's avatar
Chris Allegretta committed
372
373
374
		UNSET(TEMP_OPT);
		return do_writeout(1);
	    }
Chris Allegretta's avatar
Chris Allegretta committed
375
376
	    statusbar(_("Could not open file for writing: %s"),
		      strerror(errno));
377
	    free(realname);
Chris Allegretta's avatar
Chris Allegretta committed
378
379
	    return -1;
	}
380

Chris Allegretta's avatar
Chris Allegretta committed
381
382
383
    }
    /* Don't follow symlink.  Create new file. */
    else {
Chris Allegretta's avatar
Chris Allegretta committed
384
385
	buf = nmalloc(strlen(realname) + 8);
	strncpy(buf, realname, strlen(realname)+1);
Chris Allegretta's avatar
Chris Allegretta committed
386
387
	strcat(buf, ".XXXXXX");
	if ((fd = mkstemp(buf)) == -1) {
Chris Allegretta's avatar
Chris Allegretta committed
388
389
390
	    if (ISSET(TEMP_OPT)) {
		UNSET(TEMP_OPT);
		return do_writeout(1);
391
	    }
Chris Allegretta's avatar
Chris Allegretta committed
392
393
394
395
396
397
398
399
	    statusbar(_("Could not open file for writing: %s"),
		      strerror(errno));
	    return -1;
	}
    }

    dump_buffer(fileage);
    while (fileptr != NULL && fileptr->next != NULL) {
Robert Siemborski's avatar
Robert Siemborski committed
400
	/* Next line is so we discount the "magic line" */
401
402
	if (filebot == fileptr && fileptr->data[0] == '\0')
	    break;
Robert Siemborski's avatar
Robert Siemborski committed
403

Chris Allegretta's avatar
Chris Allegretta committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
	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) {
438
	statusbar(_("Could not close %s: %s"), realname, strerror(errno));
Chris Allegretta's avatar
Chris Allegretta committed
439
440
441
442
	unlink(buf);
	return -1;
    }

443
444
    if (realexists == -1 || tmp ||
	(!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) {
Chris Allegretta's avatar
Chris Allegretta committed
445

446
447
448
	/* Use default umask as file permisions if file is a new file. */
	mask = umask(0);
	umask(mask);
Chris Allegretta's avatar
Chris Allegretta committed
449

450
451
452
453
454
	if (tmp)		/* We don't want anyone reading our temporary file! */
	    mask = 0600;
	else
	    mask = 0666 & ~mask;
    } else
Chris Allegretta's avatar
Chris Allegretta committed
455
	/* Use permissions from file we are overwriting. */
456
457
458
459
460
	mask = st.st_mode;

    if (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) {
	if (unlink(realname) == -1) {
	    if (errno != ENOENT) {
461
		statusbar(_("Could not open %s for writing: %s"),
462
			  realname, strerror(errno));
463
464
465
		unlink(buf);
		return -1;
	    }
Chris Allegretta's avatar
Chris Allegretta committed
466
	}
467
468
469
470
471
472
473
474
475
476
477
478
479
	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;
	}
Chris Allegretta's avatar
Chris Allegretta committed
480
    }
481
482
483
484
    if (chmod(realname, mask) == -1)
	statusbar(_("Could not set permissions %o on %s: %s"),
		  mask, realname, strerror(errno));

Chris Allegretta's avatar
Chris Allegretta committed
485
    if (!tmp) {
Chris Allegretta's avatar
Chris Allegretta committed
486
	filename = mallocstrcpy(filename, realname);
Chris Allegretta's avatar
Chris Allegretta committed
487
	statusbar(_("Wrote %d lines"), lineswritten);
488
	UNSET(MODIFIED);
Chris Allegretta's avatar
Chris Allegretta committed
489
	titlebar(NULL);
Chris Allegretta's avatar
Chris Allegretta committed
490
491
492
493
494
495
496
    }
    return 1;
}

int do_writeout(int exiting)
{
    int i = 0;
Chris Allegretta's avatar
Chris Allegretta committed
497

498
499
500
#ifdef NANO_EXTRA
    static int did_cred = 0;
#endif
Chris Allegretta's avatar
Chris Allegretta committed
501

502
    answer = mallocstrcpy(answer, filename);
Chris Allegretta's avatar
Chris Allegretta committed
503

504
    if ((exiting) && (ISSET(TEMP_OPT))) {
505
	if (filename[0]) {
506
507
508
	    i = write_file(answer, 0);
	    display_main_list();
	    return i;
509
	} else {
Chris Allegretta's avatar
Chris Allegretta committed
510
511
512
513
514
	    UNSET(TEMP_OPT);
	    do_exit();

	    /* They cancelled, abort quit */
	    return -1;
515
	}
Chris Allegretta's avatar
Chris Allegretta committed
516
517
518
    }

    while (1) {
519
	i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, answer,
Chris Allegretta's avatar
Chris Allegretta committed
520
		    _("File Name to write"));
Chris Allegretta's avatar
Chris Allegretta committed
521

Chris Allegretta's avatar
Chris Allegretta committed
522
	if (i != -1) {
Chris Allegretta's avatar
Chris Allegretta committed
523

Chris Allegretta's avatar
Chris Allegretta committed
524
525
526
527
528
529
530
531
532
533
534
#ifdef ENABLE_BROWSER
	if (i == NANO_TOFILES_KEY) {
	    char *tmp = do_browser(getcwd(NULL, 0));

	    if (tmp != NULL) {
		free(answer);
		answer = tmp;
	    }
	}
#endif

Chris Allegretta's avatar
Chris Allegretta committed
535
536
537
#ifdef DEBUG
	    fprintf(stderr, _("filename is %s"), answer);
#endif
538
539

#ifdef NANO_EXTRA
540
541
	    if (exiting && !ISSET(TEMP_OPT) && !strcasecmp(answer, "zzy")
		&& !did_cred) {
542
543
		do_credits();
		did_cred = 1;
544
		return -1;
545
546
	    }
#endif
547
	    if (strcmp(answer, filename)) {
Chris Allegretta's avatar
Chris Allegretta committed
548
549
550
551
552
553
		struct stat st;
		if (!stat(answer, &st)) {
		    i = do_yesno(0, 0, _("File exists, OVERWRITE ?"));

		    if (!i || (i == -1))
			continue;
Chris Allegretta's avatar
Chris Allegretta committed
554
555
		}
	    }
Chris Allegretta's avatar
Chris Allegretta committed
556
557
558
559
	    i = write_file(answer, 0);

	    display_main_list();
	    return i;
Chris Allegretta's avatar
Chris Allegretta committed
560
	} else {
Chris Allegretta's avatar
Chris Allegretta committed
561
562
563
	    statusbar(_("Cancelled"));
	    display_main_list();
	    return 0;
Chris Allegretta's avatar
Chris Allegretta committed
564
	}
Chris Allegretta's avatar
Chris Allegretta committed
565
566
567
568
569
570
571
    }
}

int do_writeout_void(void)
{
    return do_writeout(0);
}
Chris Allegretta's avatar
Chris Allegretta committed
572

573
574
575
576
577
578
#ifndef DISABLE_TABCOMP
static char **homedirs;

/* Return a malloc()ed string containing the actual directory, used
 * to convert ~user and ~/ notation...
 */
579
char *real_dir_from_tilde(char *buf)
580
{
581
582
    char *dirtmp = NULL, *line = NULL, byte[1], *lineptr;
    int fd, i, status, searchctr = 1;
583
584

    if (buf[0] == '~') {
585
586
587
	if (buf[1] == '~')
	    goto abort;		/* Handle ~~ without segfaulting =) */
	else if (buf[1] == '/') {
588
589
590
591
592
	    if (getenv("HOME") != NULL) {
		dirtmp = nmalloc(strlen(buf) + 2 + strlen(getenv("HOME")));

		sprintf(dirtmp, "%s/%s", getenv("HOME"), &buf[2]);
	    }
593
	} else if (buf[1] != 0) {
594

595
	    if ((fd = open("/etc/passwd", O_RDONLY)) == -1)
596
597
598
599
		goto abort;

	    /* Figure how how much of of the str we need to compare */
	    for (searchctr = 1; buf[searchctr] != '/' &&
600
		 buf[searchctr] != 0; searchctr++);
601
602
603
604

	    do {
		i = 0;
		line = nmalloc(1);
605
606
		while ((status = read(fd, byte, 1)) != 0
		       && byte[0] != '\n') {
607
608
609

		    line[i] = byte[0];
		    i++;
610
		    line = nrealloc(line, i + 1);
611
612
613
614
		}
		line[i] = 0;

		if (i == 0)
615
		    goto abort;
616
617
618
619
620
621
622
623
624
625
626
627
628

		line[i] = 0;
		lineptr = strtok(line, ":");

		if (!strncmp(lineptr, &buf[1], searchctr - 1)) {

		    /* Okay, skip to the password portion now */
		    for (i = 0; i <= 4 && lineptr != NULL; i++)
			lineptr = strtok(NULL, ":");

		    if (lineptr == NULL)
			goto abort;

629
		    /* Else copy the new string into the new buf */
630
631
632
633
634
635
		    dirtmp = nmalloc(strlen(buf) + 2 + strlen(lineptr));

		    sprintf(dirtmp, "%s%s", lineptr, &buf[searchctr]);
		    free(line);
		    break;
		}
636

637
638
639
		free(line);

	    } while (status != 0);
640
	}
641
    } else
642
643
	dirtmp = mallocstrcpy(dirtmp, buf);

644
    return dirtmp;
645

646
  abort:
647
648
    dirtmp = mallocstrcpy(dirtmp, buf);
    return dirtmp;
649
650
651
}

/* Tack a slash onto the string we're completing if it's a directory */
652
int append_slash_if_dir(char *buf, int *lastWasTab, int *place)
653
654
655
{
    char *dirptr;
    struct stat fileinfo;
656
    int ret = 0;
657
658
659
660

    dirptr = real_dir_from_tilde(buf);

    if (stat(dirptr, &fileinfo) == -1)
661
	ret = 0;
662
663
664
665
666
    else if (S_ISDIR(fileinfo.st_mode)) {
	strncat(buf, "/", 1);
	*place += 1;
	/* now we start over again with # of tabs so far */
	*lastWasTab = 0;
667
	ret = 1;
668
669
670
    }

    if (dirptr != buf)
671
	free(dirptr);
672
673

    return ret;
674
}
Chris Allegretta's avatar
Chris Allegretta committed
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696

/*
 * 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)
{
697
698
    char **matches = (char **) NULL, *line = NULL, *lineptr;
    char *matchline = NULL, *matchdir = NULL;
699

700
701
702
    int fd, i = 0, status = 1;
    char byte[1];

703
    if ((fd = open("/etc/passwd", O_RDONLY)) == -1) {
704
705
706
707
708
709
710
711
712
713
	return NULL;
    }

    if (homedirs != NULL) {
	for (i = 0; i < *num_matches; i++)
	    free(homedirs[i]);
	free(homedirs);
	homedirs = (char **) NULL;
	*num_matches = 0;
    }
714
715
    matches = nmalloc(BUFSIZ * sizeof(char *));
    homedirs = nmalloc(BUFSIZ * sizeof(char *));
716
717
718
719
720
721
722
723
    strcat(buf, "*");
    do {
	i = 0;
	line = nmalloc(1);
	while ((status = read(fd, byte, 1)) != 0 && byte[0] != '\n') {

	    line[i] = byte[0];
	    i++;
724
	    line = nrealloc(line, i + 1);
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
	}

	if (i == 0)
	    break;

	line[i] = 0;
	lineptr = strtok(line, ":");

	if (check_wildcard_match(line, &buf[1]) == TRUE) {

	    if (*num_matches == BUFSIZ)
		break;

	    /* Cool, found a match.  Add it to the list
	     * This makes a lot more sense to me (Chris) this way...
	     */
	    matchline = nmalloc(strlen(line) + 2);
	    sprintf(matchline, "~%s", line);

	    for (i = 0; i <= 4 && lineptr != NULL; i++)
		lineptr = strtok(NULL, ":");

	    if (lineptr == NULL)
		break;

	    matchdir = mallocstrcpy(matchdir, lineptr);
	    homedirs[*num_matches] = matchdir;
	    matches[*num_matches] = matchline;

	    ++*num_matches;

	    /* If there's no more room, bail out */
	    if (*num_matches == BUFSIZ)
		break;
	}

	free(line);

    } while (status != 0);

    close(fd);
    return matches;
Chris Allegretta's avatar
Chris Allegretta committed
767
768
769
770
771
772
773
774
775
776
777
#ifdef DEBUG
    fprintf(stderr, "\nin username_tab_completion\n");
#endif
    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)
{
778
    char *dirName, *dirtmp = NULL, *tmp = NULL, *tmp2 = NULL;
Chris Allegretta's avatar
Chris Allegretta committed
779
780
781
782
    char **matches = (char **) NULL;
    DIR *dir;
    struct dirent *next;

783
    matches = nmalloc(BUFSIZ * sizeof(char *));
Chris Allegretta's avatar
Chris Allegretta committed
784
785
786
787

    /* Stick a wildcard onto the buf, for later use */
    strcat(buf, "*");

788
    /* Okie, if there's a / in the buffer, strip out the directory part */
Chris Allegretta's avatar
Chris Allegretta committed
789
790
791
792
    if (strcmp(buf, "") && strstr(buf, "/")) {
	dirName = malloc(strlen(buf) + 1);
	tmp = buf + strlen(buf);
	while (*tmp != '/' && tmp != buf)
793
	    tmp--;
794

Chris Allegretta's avatar
Chris Allegretta committed
795
796
	tmp++;

797
798
799
	strncpy(dirName, buf, tmp - buf + 1);
	dirName[tmp - buf] = 0;

Chris Allegretta's avatar
Chris Allegretta committed
800
801
802
803
804
805
806
807
808
809
810
811
    } else {
	if ((dirName = getcwd(NULL, 0)) == NULL)
	    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
812

813
814
815
816
817
818
819
820
821
822
823
    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


Chris Allegretta's avatar
Chris Allegretta committed
824
825
826
827
828
829
830
831
832
833
    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
834
	fprintf(stderr, "Comparing \'%s\'\n", next->d_name);
Chris Allegretta's avatar
Chris Allegretta committed
835
836
837
#endif
	/* See if this matches */
	if (check_wildcard_match(next->d_name, tmp) == TRUE) {
838
839
840
841
842
843
844
845

	    /* Cool, found a match.  Add it to the list
	     * This makes a lot more sense to me (Chris) this way...
	     */
	    tmp2 = NULL;
	    tmp2 = nmalloc(strlen(next->d_name) + 1);
	    strcpy(tmp2, next->d_name);
	    matches[*num_matches] = tmp2;
Chris Allegretta's avatar
Chris Allegretta committed
846
	    ++*num_matches;
847
848
849
850

	    /* If there's no more room, bail out */
	    if (*num_matches == BUFSIZ)
		break;
Chris Allegretta's avatar
Chris Allegretta committed
851
852
853
854
855
856
	}
    }

    return (matches);
}

857
/* This function now has an arg which refers to how much the 
Chris Allegretta's avatar
Chris Allegretta committed
858
859
 * statusbar (place) should be advanced, i.e. the new cursor pos.
 */
860
char *input_tab(char *buf, int place, int *lastWasTab, int *newplace)
Chris Allegretta's avatar
Chris Allegretta committed
861
862
{
    /* Do TAB completion */
863
    static int num_matches = 0, match_matches = 0;
Chris Allegretta's avatar
Chris Allegretta committed
864
    static char **matches = (char **) NULL;
865
    int pos = place, i = 0, col = 0, editline = 0;
866
    int longestname = 0, is_dir = 0;
867
    char *foo;
Chris Allegretta's avatar
Chris Allegretta committed
868

869
    if (*lastWasTab == FALSE) {
870
	char *tmp, *copyto, *matchBuf;
Chris Allegretta's avatar
Chris Allegretta committed
871

872
873
	*lastWasTab = 1;

Chris Allegretta's avatar
Chris Allegretta committed
874
875
	/* Make a local copy of the string -- up to the position of the
	   cursor */
876
877
	matchBuf = (char *) calloc(strlen(buf) + 2, sizeof(char));

Chris Allegretta's avatar
Chris Allegretta committed
878
879
880
881
	strncpy(matchBuf, buf, place);
	tmp = matchBuf;

	/* skip any leading white space */
882
	while (*tmp && isspace((int) *tmp))
Chris Allegretta's avatar
Chris Allegretta committed
883
884
885
886
	    ++tmp;

	/* Free up any memory already allocated */
	if (matches != NULL) {
887
888
	    for (i = i; i < num_matches; i++)
		free(matches[i]);
Chris Allegretta's avatar
Chris Allegretta committed
889
890
	    free(matches);
	    matches = (char **) NULL;
891
	    num_matches = 0;
Chris Allegretta's avatar
Chris Allegretta committed
892
893
894
895
896
	}

	/* If the word starts with `~' and there is no slash in the word, 
	 * then try completing this word as a username. */

897
	/* FIXME -- this check is broken! */
898
899
	if (*tmp == '~' && !strchr(tmp, '/'))
	    matches = username_tab_completion(tmp, &num_matches);
Chris Allegretta's avatar
Chris Allegretta committed
900
901
902
903
904
905
906
907
908

	/* 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);

909
910
911
#ifdef DEBUG
	fprintf(stderr, "%d matches found...\n", num_matches);
#endif
Chris Allegretta's avatar
Chris Allegretta committed
912
	/* Did we find exactly one match? */
913
	switch (num_matches) {
914
	case 0:
915
	    blank_edit();
916
	    wrefresh(edit);
917
918
	    break;
	case 1:
919
920
921
922

	    buf = nrealloc(buf, strlen(buf) + strlen(matches[0]) + 1);

	    if (strcmp(buf, "") && strstr(buf, "/")) {
923
924
		for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf;
		     tmp--);
925
		tmp++;
926
	    } else
927
928
		tmp = buf;

929
930
931
932
933
	    if (!strcmp(tmp, matches[0]))
		is_dir = append_slash_if_dir(buf, lastWasTab, newplace);

	    if (is_dir)
		break;
934
935

	    copyto = tmp;
936
937
	    for (pos = 0; *tmp == matches[0][pos] &&
		 pos <= strlen(matches[0]); pos++)
938
939
		tmp++;

940
	    /* write out the matched name */
941
	    strncpy(copyto, matches[0], strlen(matches[0]) + 1);
942
943
	    *newplace += strlen(matches[0]) - pos;

944
945
946
	    /* Is it a directory? */
	    append_slash_if_dir(buf, lastWasTab, newplace);

947
948
	    break;
	default:
949
	    /* Check to see if all matches share a beginning, and if so
950
	       tack it onto buf and then beep */
951

952
	    if (strcmp(buf, "") && strstr(buf, "/")) {
953
954
		for (tmp = buf + strlen(buf); *tmp != '/' && tmp != buf;
		     tmp--);
955
		tmp++;
956
	    } else
957
958
959
		tmp = buf;

	    for (pos = 0; *tmp == matches[0][pos] && *tmp != 0 &&
960
		 pos <= strlen(matches[0]); pos++)
961
962
		tmp++;

963
964
965
966
967
968
969
970
971
	    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++;
		}
972
973
		if (match_matches == num_matches &&
		    (i == num_matches || matches[i] != 0)) {
974
		    /* All the matches have the same character at pos+1,
975
		       so paste it into buf... */
976
		    buf = nrealloc(buf, strlen(buf) + 2);
977
		    strncat(buf, matches[0] + pos, 1);
978
		    *newplace += 1;
979
		    pos++;
980
		} else {
981
982
983
984
		    beep();
		    break;
		}
	    }
985
	    break;
986
	}
Chris Allegretta's avatar
Chris Allegretta committed
987
988
989
990
991
992
993
994
995
996
    } 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);

997
	    editline = 0;
998

999
1000
1001
1002
1003
1004
1005
1006
	    /* 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;

1007
1008
	    foo = nmalloc(longestname + 5);

Chris Allegretta's avatar
Chris Allegretta committed
1009
1010
	    /* Print the list of matches */
	    for (i = 0, col = 0; i < num_matches; i++) {
1011

1012
		/* make each filename shown be the same length as the longest
1013
		   filename, with two spaces at the end */
1014
1015
1016
1017
1018
1019
		snprintf(foo, longestname + 1, matches[i]);
		while (strlen(foo) < longestname)
		    strcat(foo, " ");

		strcat(foo, "  ");

1020
1021
		/* Disable el cursor */
		curs_set(0);
1022
1023
1024
1025
1026
1027
		/* 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 */
1028
		if (col > (COLS - longestname) && matches[i + 1] != NULL) {
1029
1030
		    editline++;
		    wmove(edit, editline, 0);
1031
1032
1033
1034
		    if (editline == editwinrows - 1) {
			waddstr(edit, _("(more)"));
			break;
		    }
Chris Allegretta's avatar
Chris Allegretta committed
1035
1036
1037
		    col = 0;
		}
	    }
1038
	    free(foo);
Chris Allegretta's avatar
Chris Allegretta committed
1039
	    wrefresh(edit);
1040
1041
	} else
	    beep();
Chris Allegretta's avatar
Chris Allegretta committed
1042
1043
1044
1045

    }

    edit_refresh();
1046
1047
    curs_set(1);
    return buf;
Chris Allegretta's avatar
Chris Allegretta committed
1048
}
1049
#endif
Chris Allegretta's avatar
Chris Allegretta committed
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207

#ifdef ENABLE_BROWSER

/* Return the stat of the file pointed to by path */
struct stat filestat(const char *path) {
    struct stat st;

    stat(path, &st);
    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;
    int answer = 0;

    if (stat(a, &file1info) == -1)
	answer = 1;
    else if (stat(b, &file2info) == -1)
	answer = 1;
    else {
	/* If is a is a dir and b isn't, return -1.
	   Else if b is a dir and a isn't, return 0.
	   Else return a < b */

	if (S_ISDIR(file1info.st_mode) && !S_ISDIR(file2info.st_mode))
	    return -1;
	else if (!S_ISDIR(file1info.st_mode) && S_ISDIR(file2info.st_mode))
	    return 1;
	else
	    answer = strcmp(a, b);
    }

    return(answer);
}

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;
	filelist[i] = nmalloc(strlen(next->d_name) + strlen(path) + 2);

	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);

/*
	filelist[i] = mallocstrcpy(filelist[i], next->d_name);
*/
	i++;
    }

    longest -= strlen(path);

    if (*longest > COLS - 1)
	*longest = COLS - 1;

    return filelist;
}

/* Free our malloced memory */
void free_charptrarray(char **array, int len)
{
    int i;

    for (i = 0; i < len - 1; i++)
	free(array[i]);
    free(array);
}

char *tail(char *foo)
{
    char *tmp = NULL;

    tmp = foo + strlen(foo);
    while (*tmp != '/' && tmp != foo)
	tmp--;

    tmp++;

    return tmp;
}

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;
}

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;
    char **filelist = (char **) NULL;

    if (path != NULL && strcmp(path, inpath)) {
	free(path);
	path = NULL;
    }

    if (path == NULL)
	path = mallocstrcpy(path, inpath);

    filelist = browser_init(path, &longest, &numents);
    foo = nmalloc(longest + 8);
    qsort(filelist, numents, sizeof(char *), diralphasort);

    titlebar(path);
    bottombars(browser_list, BROWSER_LIST_LEN);
1208
    keypad_on(TRUE);
Chris Allegretta's avatar
Chris Allegretta committed
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
    curs_set(0);
    wmove(edit, 0, 0);
    i = 0;
    width = 0;
    filecols = 0;
    do {
	blank_edit();
	blank_statusbar();
 	editline = 0;
	col = 0;

	switch (kbinput) {
	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:
	case KEY_PPAGE:
	    if ((selected / width) % editwinrows == 0) {
1244
1245
		if (selected - (editwinrows * width) >= 0)
		    selected -= editwinrows * width; 
Chris Allegretta's avatar
Chris Allegretta committed
1246
1247
1248
1249
		else
		    selected = 0;
	    }
	    else if (selected - (editwinrows + 
1250
1251
1252
			(selected / width) % editwinrows) * width  >= 0)
		selected -= (editwinrows + (selected / width) % 
				editwinrows) * width; 
Chris Allegretta's avatar
Chris Allegretta committed
1253
1254
1255
1256
1257
1258
	    else
		selected = 0;
	    break;
	case NANO_NEXTPAGE_KEY:
	case KEY_NPAGE:	
	    if ((selected / width) % editwinrows == 0) {
1259
1260
		if (selected + (editwinrows * width) <= numents - 1)
		    selected += editwinrows * width; 
Chris Allegretta's avatar
Chris Allegretta committed
1261
1262
1263
1264
		else
		    selected = numents - 1;
	    }
	    else if (selected + (editwinrows - 
1265
1266
			(selected / width) %  editwinrows) * width <= numents - 1)
 		selected += (editwinrows - (selected / width) % editwinrows) * width; 
Chris Allegretta's avatar
Chris Allegretta committed
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
 	    else
		selected = numents - 1;
	    break;
	case KEY_ENTER:
	case NANO_CONTROL_M:
	    if (!strcmp(filelist[selected], "/..") && !strcmp(path, "/"))
		statusbar(_("Can't move up a directory"));
	    else
		path = mallocstrcpy(path, filelist[selected]);

	    st = filestat(path);
	    if (S_ISDIR(st.st_mode)) {
		if (opendir(path) == NULL) {
		    statusbar(_("Can't open \"%s\": %s"), path, strerror(errno));
		    striponedir(path);		    
		    align(&path);
		    break;
		}
	    }

	    st = filestat(path);
	    if (S_ISDIR(st.st_mode)) {
		if (!strcmp("..", tail(path))) {
		    striponedir(path);
		    striponedir(path);
		    align(&path);
		}
		return do_browser(path);
	    } else {
		ungetch(NANO_EXIT_KEY);
		retval = path;
		abort = 1;
	    }
	    break;
	/* Stuff we want to abort the browser */
	case 'q':
	case 'Q':
	case 'e':	/* Pico compatibility, yeech */
	case 'E':
		abort = 1;
		break;
	}
	if (abort)
	    break;

	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 */
	    st = filestat(filelist[j]);
	    if (S_ISDIR(st.st_mode))
		strcpy(foo + longest - 5, "(dir)");
	    else {
		if (st.st_size < 1024) /* less than 1 K */
		    sprintf(foo + longest - 7, "%4d  B", (int) st.st_size);
		else if (st.st_size > 1073741824) /* at least 1 gig */
		    sprintf(foo + longest - 7, "%4d GB", (int) st.st_size / 1073741824);
		else if (st.st_size > 1048576) /* at least 1 meg */
		    sprintf(foo + longest - 7, "%4d MB", (int) st.st_size / 1048576);
		else /* Its more than 1 k and less than a meg */
		    sprintf(foo + longest - 7, "%4d KB", (int) st.st_size / 1024);
	    }

	    if (j == selected)
		wattron(edit, A_REVERSE);
	    waddnstr(edit, foo, strlen(foo));
	    if (j == selected)
		wattroff(edit, A_REVERSE);

	    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();

    free_charptrarray(filelist, numents);
    free(foo);
    return retval;
}
#endif