mirror of
https://github.com/jarun/nnn.git
synced 2024-11-28 05:41:31 +00:00
Only use chdir(3) just before spawning a shell
This commit is contained in:
parent
a606073296
commit
8d4019f3aa
48
noice.c
48
noice.c
|
@ -141,13 +141,15 @@ xdirname(const char *path)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
spawn(const char *file, const char *arg)
|
spawn(const char *file, const char *arg, const char *dir)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
|
if (dir != NULL)
|
||||||
|
chdir(dir);
|
||||||
execlp(file, file, arg, NULL);
|
execlp(file, file, arg, NULL);
|
||||||
_exit(1);
|
_exit(1);
|
||||||
} else {
|
} else {
|
||||||
|
@ -570,10 +572,10 @@ browse(const char *ipath, const char *ifilter)
|
||||||
struct entry *dents;
|
struct entry *dents;
|
||||||
int i, n, cur;
|
int i, n, cur;
|
||||||
int r, ret, fd;
|
int r, ret, fd;
|
||||||
char *path = xrealpath(ipath);
|
char *path = xstrdup(ipath);
|
||||||
char *filter = xstrdup(ifilter);
|
char *filter = xstrdup(ifilter);
|
||||||
regex_t filter_re;
|
regex_t filter_re;
|
||||||
char *cwd;
|
char *cwd, *newpath;
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
char *hpath;
|
char *hpath;
|
||||||
|
|
||||||
|
@ -588,9 +590,6 @@ begin:
|
||||||
if (dirp == NULL) {
|
if (dirp == NULL) {
|
||||||
printwarn();
|
printwarn();
|
||||||
goto nochange;
|
goto nochange;
|
||||||
} else {
|
|
||||||
if (chdir(path) == -1)
|
|
||||||
printwarn();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Search filter */
|
/* Search filter */
|
||||||
|
@ -680,30 +679,35 @@ nochange:
|
||||||
goto nochange;
|
goto nochange;
|
||||||
|
|
||||||
name = dents[cur].name;
|
name = dents[cur].name;
|
||||||
DPRINTF_S(name);
|
newpath = makepath(path, name);
|
||||||
|
DPRINTF_S(newpath);
|
||||||
|
|
||||||
/* Get path info */
|
/* Get path info */
|
||||||
fd = openat(dirfd(dirp), name, O_RDONLY | O_NONBLOCK);
|
fd = open(newpath, O_RDONLY | O_NONBLOCK);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
printwarn();
|
printwarn();
|
||||||
|
free(newpath);
|
||||||
|
goto nochange;
|
||||||
|
}
|
||||||
|
r = fstat(fd, &sb);
|
||||||
|
if (r == -1) {
|
||||||
|
printwarn();
|
||||||
|
close(fd);
|
||||||
|
free(newpath);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
r = fstatat(dirfd(dirp), name, &sb, 0);
|
|
||||||
if (r == -1) {
|
|
||||||
printwarn();
|
|
||||||
goto nochange;
|
|
||||||
}
|
|
||||||
DPRINTF_U(sb.st_mode);
|
DPRINTF_U(sb.st_mode);
|
||||||
|
|
||||||
switch (sb.st_mode & S_IFMT) {
|
switch (sb.st_mode & S_IFMT) {
|
||||||
case S_IFDIR:
|
case S_IFDIR:
|
||||||
if (canopendir(path) == 0) {
|
if (canopendir(newpath) == 0) {
|
||||||
printwarn();
|
printwarn();
|
||||||
|
free(newpath);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
path = xrealpath(name);
|
path = newpath;
|
||||||
/* Reset filter */
|
/* Reset filter */
|
||||||
free(filter);
|
free(filter);
|
||||||
filter = xstrdup(ifilter);
|
filter = xstrdup(ifilter);
|
||||||
|
@ -715,11 +719,13 @@ nochange:
|
||||||
bin = openwith(name);
|
bin = openwith(name);
|
||||||
if (bin == NULL) {
|
if (bin == NULL) {
|
||||||
printmsg("No association");
|
printmsg("No association");
|
||||||
|
free(newpath);
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
exitcurses();
|
exitcurses();
|
||||||
spawn(bin, name);
|
spawn(bin, newpath, NULL);
|
||||||
initcurses();
|
initcurses();
|
||||||
|
free(newpath);
|
||||||
goto redraw;
|
goto redraw;
|
||||||
default:
|
default:
|
||||||
printmsg("Unsupported file");
|
printmsg("Unsupported file");
|
||||||
|
@ -746,7 +752,7 @@ nochange:
|
||||||
goto out;
|
goto out;
|
||||||
case SEL_SH:
|
case SEL_SH:
|
||||||
exitcurses();
|
exitcurses();
|
||||||
spawn("/bin/sh", NULL);
|
spawn("/bin/sh", NULL, path);
|
||||||
initcurses();
|
initcurses();
|
||||||
break;
|
break;
|
||||||
case SEL_CD:
|
case SEL_CD:
|
||||||
|
@ -757,13 +763,15 @@ nochange:
|
||||||
clearprompt();
|
clearprompt();
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
if (canopendir(tmp) == 0) {
|
newpath = makepath(path, tmp);
|
||||||
|
free(tmp);
|
||||||
|
if (canopendir(newpath) == 0) {
|
||||||
|
free(newpath);
|
||||||
printwarn();
|
printwarn();
|
||||||
goto nochange;
|
goto nochange;
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
path = xrealpath(tmp);
|
path = newpath;
|
||||||
free(tmp);
|
|
||||||
free(filter);
|
free(filter);
|
||||||
filter = xstrdup(ifilter); /* Reset filter */
|
filter = xstrdup(ifilter); /* Reset filter */
|
||||||
forgethist();
|
forgethist();
|
||||||
|
|
Loading…
Reference in a new issue