Support symbolic links and fix message reporting

This commit is contained in:
lostd 2014-10-08 10:52:44 +03:00
parent c5e5a19d0f
commit 0deba427a9
1 changed files with 55 additions and 17 deletions

72
noice.c
View File

@ -1,3 +1,4 @@
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <errno.h> #include <errno.h>
@ -337,37 +338,73 @@ nochange:
} }
} }
if (ret == 3) { if (ret == 3) {
char *name, *file = NULL; char *pathnew, *pathtmp;
char *newpath; char *name;
u_int8_t type;
char *bin; char *bin;
pid_t pid; pid_t pid;
struct stat sb;
/* Cannot descend in empty directories */ /* Cannot descend in empty directories */
if (n == 0) if (n == 0)
goto nochange; goto nochange;
name = dents[cur]->d_name; name = dents[cur]->d_name;
type = dents[cur]->d_type;
switch (dents[cur]->d_type) { pathnew = malloc(strlen(path) + 1
+ strlen(name) + 1);
sprintf(pathnew, "%s/%s", path, name);
DPRINTF_S(pathnew);
again:
switch (type) {
case DT_LNK:
/* Resolve link */
pathtmp = realpath(pathnew, NULL);
if (pathtmp == NULL) {
printwarn();
free(pathnew);
goto nochange;
} else {
r = stat(pathtmp, &sb);
free(pathtmp);
if (r == -1) {
printwarn();
free(pathnew);
goto nochange;
}
/* Directory or file */
if (S_ISDIR(sb.st_mode)) {
type = DT_DIR;
goto again;
}
if (S_ISREG(sb.st_mode)) {
type = DT_REG;
goto again;
}
/* All the rest */
printmsg("Unsupported file");
free(pathnew);
goto nochange;
}
case DT_DIR: case DT_DIR:
newpath = malloc(strlen(path) + 1 /* Change to new path */
+ strlen(name) + 1); if (testopen(pathnew)) {
sprintf(newpath, "%s/%s", path, name);
if (testopen(newpath)) {
free(path); free(path);
path = newpath; path = pathnew;
goto out; goto out;
} else { } else {
printwarn(); printwarn();
free(newpath); free(pathnew);
goto nochange; goto nochange;
} }
case DT_REG: case DT_REG:
file = malloc(strlen(path) + 1 if (!testopen(pathnew)) {
+ strlen(name) + 1); printwarn();
sprintf(file, "%s/%s", path, name); free(pathnew);
DPRINTF_S(file); goto nochange;
}
/* Open with */ /* Open with */
bin = openwith(name); bin = openwith(name);
if (bin == NULL) { if (bin == NULL) {
@ -380,17 +417,18 @@ nochange:
/* Run program */ /* Run program */
pid = fork(); pid = fork();
if (pid == 0) if (pid == 0)
execlp(bin, bin, file, NULL); execlp(bin, bin, pathnew, NULL);
else else
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
initcurses(); initcurses();
free(file); free(pathnew);
goto redraw; goto redraw;
default: default:
DPRINTF_D(dents[cur]->d_type); DPRINTF_D(dents[cur]->d_type);
printmsg("Unsupported file");
goto nochange;
} }
} }
} }