rc: use searchpath() logic to handle $cdpath

This commit is contained in:
cinap_lenrek 2018-10-26 10:25:23 +02:00
parent 1a55b8d1f0
commit d17a049c5b
2 changed files with 9 additions and 11 deletions

View file

@ -52,7 +52,7 @@ void pushlist(void);
void pushredir(int, int, int); void pushredir(int, int, int);
word* pushword(char*); word* pushword(char*);
void readhere(void); void readhere(void);
word* searchpath(char*); word* searchpath(char*, char*);
void setstatus(char*); void setstatus(char*);
void setvar(char*, word*); void setvar(char*, word*);
void shuffleredir(void); void shuffleredir(void);

View file

@ -70,7 +70,6 @@ Xsimple(void)
} }
} }
} }
struct word nullpath = { "", 0};
void void
doredir(redir *rp) doredir(redir *rp)
@ -95,15 +94,17 @@ doredir(redir *rp)
} }
word* word*
searchpath(char *w) searchpath(char *w, char *v)
{ {
word *path; word *path;
if(strncmp(w, "/", 1)==0 if(strncmp(w, "/", 1)==0
|| strncmp(w, "#", 1)==0 || strncmp(w, "#", 1)==0
|| strncmp(w, "./", 2)==0 || strncmp(w, "./", 2)==0
|| strncmp(w, "../", 3)==0 || strncmp(w, "../", 3)==0
|| (path = vlook("path")->val)==0) || (path = vlook(v)->val)==0){
static struct word nullpath = { "", 0};
path=&nullpath; path=&nullpath;
}
return path; return path;
} }
@ -116,7 +117,7 @@ execexec(void)
return; return;
} }
doredir(runq->redir); doredir(runq->redir);
Execute(runq->argv->words, searchpath(runq->argv->words->word)); Execute(runq->argv->words, searchpath(runq->argv->words->word, "path"));
poplist(); poplist();
Xexit(); Xexit();
} }
@ -153,15 +154,12 @@ execcd(void)
char *dir; char *dir;
setstatus("can't cd"); setstatus("can't cd");
cdpath = vlook("cdpath")->val;
switch(count(a)){ switch(count(a)){
default: default:
pfmt(err, "Usage: cd [directory]\n"); pfmt(err, "Usage: cd [directory]\n");
break; break;
case 2: case 2:
if(a->next->word[0]=='/' || a->next->word[0]=='#' || cdpath==0) for(cdpath = searchpath(a->next->word, "cdpath"); cdpath; cdpath = cdpath->next){
cdpath = &nullpath;
for(; cdpath; cdpath = cdpath->next){
if(cdpath->word[0] != '\0') if(cdpath->word[0] != '\0')
dir = smprint("%s/%s", cdpath->word, dir = smprint("%s/%s", cdpath->word,
a->next->word); a->next->word);
@ -336,7 +334,7 @@ execdot(void)
zero = estrdup(p->argv->words->word); zero = estrdup(p->argv->words->word);
popword(); popword();
fd = -1; fd = -1;
for(path = searchpath(zero); path; path = path->next){ for(path = searchpath(zero, "path"); path; path = path->next){
if(path->word[0] != '\0') if(path->word[0] != '\0')
file = smprint("%s/%s", path->word, zero); file = smprint("%s/%s", path->word, zero);
else else
@ -452,7 +450,7 @@ execwhatis(void){ /* mildly wrong -- should fork before writing */
break; break;
} }
if(!bp->name){ if(!bp->name){
for(path = searchpath(a->word); path; for(path = searchpath(a->word, "path"); path;
path = path->next){ path = path->next){
if(path->word[0] != '\0') if(path->word[0] != '\0')
file = smprint("%s/%s", file = smprint("%s/%s",