shithub: vdir

Download patch

ref: c7312d75a4ecb460c498ebd0540c3c92d4f20fb0
parent: 2bab9ca96b1aef62b5a83b2cda600517d40b5758
author: telephil9 <[email protected]>
date: Sat Apr 4 13:23:02 EDT 2020

Fix bug in 'cd'

No checks where done when calling cd() which would break if user
enters a non-existing directory.

--- a/vdir.c
+++ b/vdir.c
@@ -81,11 +81,13 @@
 {
 	int fd;
 
+	fd = open(path, OREAD);
+	if(fd<0){
+		showerrstr();
+		return;
+	}
 	if(dirs!=nil)
 		free(dirs);
-	fd = open(path, OREAD);
-	if(fd<0)
-		sysfatal("open: %r");
 	ndirs = dirreadall(fd, &dirs);
 	qsort(dirs, ndirs, sizeof *dirs, (int(*)(void*,void*))dircmp);
 	offset = 0;
@@ -114,16 +116,23 @@
 void
 cd(char *dir)
 {
+	char newpath[256];
 	char *sep;
+	int n;
 
 	if(dir == nil)
-		sprint(path, home);
+		n = snprint(newpath, sizeof path, home);
 	else if(dir[0] == '/')
-		snprint(path, sizeof path, dir);
+		n = snprint(newpath, sizeof newpath, dir);
 	else{
 		sep = strlen(path)==1 ? "" : "/";
-		snprint(path, sizeof path, "%s%s%s", path, sep, dir);
+		n = snprint(newpath, sizeof newpath, "%s%s%s", path, sep, dir);
 	}
+	if(access(newpath, 0)<0){
+		alert("Error", "Directory does not exist");
+		return;
+	}
+	strncpy(path, newpath, n);
 	loaddirs();
 }
 
@@ -384,7 +393,7 @@
 			up();
 			redraw();
 		}else if(ptinrect(m.xy, cdr)){
-			if(eenter("Directory", buf, sizeof buf, &m)>0){
+			if(eenter("Go to directory", buf, sizeof buf, &m)>0){
 				cd(buf);
 				redraw();
 			}