Use open(2)/fstat(2) and don't bother with links at all

This commit is contained in:
lostd 2014-10-08 18:30:39 +03:00
parent 3459f6a5e7
commit 6b51ec4585

99
noice.c
View file

@ -192,20 +192,6 @@ nextsel(int *cur, int max)
return 0; return 0;
} }
int
testopen(char *path)
{
int fd;
fd = open(path, O_RDONLY);
if (fd == -1) {
return 0;
} else {
close(fd);
return 1;
}
}
int int
testopendir(char *path) testopendir(char *path)
{ {
@ -340,11 +326,11 @@ nochange:
} }
} }
if (ret == 3) { if (ret == 3) {
char *pathnew, *pathtmp; char *pathnew;
char *name; char *name;
u_int8_t type;
char *bin; char *bin;
pid_t pid; pid_t pid;
int fd;
struct stat sb; struct stat sb;
/* Cannot descend in empty directories */ /* Cannot descend in empty directories */
@ -352,62 +338,35 @@ nochange:
goto nochange; goto nochange;
name = dents[cur].d_name; name = dents[cur].d_name;
type = dents[cur].d_type;
asprintf(&pathnew, "%s/%s", path, name); asprintf(&pathnew, "%s/%s", path, name);
DPRINTF_S(name); DPRINTF_S(name);
DPRINTF_U(type);
DPRINTF_S(pathnew); DPRINTF_S(pathnew);
again: /* Get path info */
switch (type) { fd = open(pathnew, O_RDONLY | O_NONBLOCK);
case DT_LNK: if (fd == -1) {
/* Resolve link */ printwarn();
pathtmp = realpath(pathnew, NULL); free(pathnew);
if (pathtmp == NULL) { goto nochange;
printwarn(); }
free(pathnew); r = fstat(fd, &sb);
goto nochange; close(fd);
} else { DPRINTF_U(sb.st_mode);
r = stat(pathtmp, &sb); if (r == -1) {
free(pathtmp); printwarn();
if (r == -1) { free(pathnew);
printwarn(); goto nochange;
free(pathnew); }
goto nochange; /* Directory */
} if (S_ISDIR(sb.st_mode)) {
/* Directory or file */ free(path);
if (S_ISDIR(sb.st_mode)) { path = pathnew;
type = DT_DIR; goto out;
goto again; }
} /* Regular file */
if (S_ISREG(sb.st_mode)) { if (S_ISREG(sb.st_mode)) {
type = DT_REG;
goto again;
}
/* All the rest */
printmsg("Unsupported file");
free(pathnew);
goto nochange;
}
case DT_DIR:
/* Change to new path */
if (testopen(pathnew)) {
free(path);
path = pathnew;
goto out;
} else {
printwarn();
free(pathnew);
goto nochange;
}
case DT_REG:
if (!testopen(pathnew)) {
printwarn();
free(pathnew);
goto nochange;
}
/* Open with */ /* Open with */
bin = openwith(name); bin = openwith(name);
if (bin == NULL) { if (bin == NULL) {
@ -429,11 +388,11 @@ again:
free(pathnew); free(pathnew);
goto redraw; goto redraw;
default:
printmsg("Unsupported file");
free(pathnew);
goto nochange;
} }
/* All the rest */
printmsg("Unsupported file");
free(pathnew);
goto nochange;
} }
} }