shithub: riscv

Download patch

ref: d17a049c5b083751f66d7089d8950aaefd047bc6
parent: 1a55b8d1f0253b4d39b1972978b469a352f0f94b
author: cinap_lenrek <[email protected]>
date: Fri Oct 26 06:25:23 EDT 2018

rc: use searchpath() logic to handle $cdpath

--- a/sys/src/cmd/rc/fns.h
+++ b/sys/src/cmd/rc/fns.h
@@ -52,7 +52,7 @@
 void	pushredir(int, int, int);
 word*	pushword(char*);
 void	readhere(void);
-word*	searchpath(char*);
+word*	searchpath(char*, char*);
 void	setstatus(char*);
 void	setvar(char*, word*);
 void	shuffleredir(void);
--- a/sys/src/cmd/rc/simple.c
+++ b/sys/src/cmd/rc/simple.c
@@ -70,7 +70,6 @@
 		}
 	}
 }
-struct word nullpath = { "", 0};
 
 void
 doredir(redir *rp)
@@ -95,7 +94,7 @@
 }
 
 word*
-searchpath(char *w)
+searchpath(char *w, char *v)
 {
 	word *path;
 	if(strncmp(w, "/", 1)==0
@@ -102,8 +101,10 @@
 	|| strncmp(w, "#", 1)==0
 	|| strncmp(w, "./", 2)==0
 	|| strncmp(w, "../", 3)==0
-	|| (path = vlook("path")->val)==0)
+	|| (path = vlook(v)->val)==0){
+		static struct word nullpath = { "", 0};
 		path=&nullpath;
+	}
 	return path;
 }
 
@@ -116,7 +117,7 @@
 		return;
 	}
 	doredir(runq->redir);
-	Execute(runq->argv->words, searchpath(runq->argv->words->word));
+	Execute(runq->argv->words, searchpath(runq->argv->words->word, "path"));
 	poplist();
 	Xexit();
 }
@@ -153,15 +154,12 @@
 	char *dir;
 
 	setstatus("can't cd");
-	cdpath = vlook("cdpath")->val;
 	switch(count(a)){
 	default:
 		pfmt(err, "Usage: cd [directory]\n");
 		break;
 	case 2:
-		if(a->next->word[0]=='/' || a->next->word[0]=='#' || cdpath==0)
-			cdpath = &nullpath;
-		for(; cdpath; cdpath = cdpath->next){
+		for(cdpath = searchpath(a->next->word, "cdpath"); cdpath; cdpath = cdpath->next){
 			if(cdpath->word[0] != '\0')
 				dir = smprint("%s/%s", cdpath->word,
 					a->next->word);
@@ -336,7 +334,7 @@
 	zero = estrdup(p->argv->words->word);
 	popword();
 	fd = -1;
-	for(path = searchpath(zero); path; path = path->next){
+	for(path = searchpath(zero, "path"); path; path = path->next){
 		if(path->word[0] != '\0')
 			file = smprint("%s/%s", path->word, zero);
 		else
@@ -452,7 +450,7 @@
 					break;
 				}
 			if(!bp->name){
-				for(path = searchpath(a->word); path;
+				for(path = searchpath(a->word, "path"); path;
 				    path = path->next){
 					if(path->word[0] != '\0')
 						file = smprint("%s/%s",