52 |
#include "error.h" |
#include "error.h" |
53 |
#include "exec.h" |
#include "exec.h" |
54 |
#include "redir.h" |
#include "redir.h" |
55 |
|
#include "main.h" |
56 |
#include "mystring.h" |
#include "mystring.h" |
57 |
#include "show.h" |
#include "show.h" |
58 |
#include "cd.h" |
#include "cd.h" |
106 |
if (!dest) |
if (!dest) |
107 |
dest = nullstr; |
dest = nullstr; |
108 |
if (*dest == '/') |
if (*dest == '/') |
109 |
goto step7; |
goto step6; |
110 |
if (*dest == '.') { |
if (*dest == '.') { |
111 |
c = dest[1]; |
c = dest[1]; |
112 |
dotdot: |
dotdot: |
122 |
} |
} |
123 |
if (!*dest) |
if (!*dest) |
124 |
dest = "."; |
dest = "."; |
125 |
if (!(path = bltinlookup("CDPATH"))) { |
path = bltinlookup("CDPATH"); |
126 |
step6: |
while (path) { |
|
step7: |
|
|
p = dest; |
|
|
goto docd; |
|
|
} |
|
|
do { |
|
127 |
c = *path; |
c = *path; |
128 |
p = padvance(&path, dest); |
p = padvance(&path, dest); |
129 |
if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
132 |
docd: |
docd: |
133 |
if (!docd(p, flags)) |
if (!docd(p, flags)) |
134 |
goto out; |
goto out; |
135 |
break; |
goto err; |
136 |
} |
} |
137 |
} while (path); |
} |
138 |
|
|
139 |
|
step6: |
140 |
|
p = dest; |
141 |
|
goto docd; |
142 |
|
|
143 |
|
err: |
144 |
sh_error("can't cd to %s", dest); |
sh_error("can't cd to %s", dest); |
145 |
/* NOTREACHED */ |
/* NOTREACHED */ |
146 |
out: |
out: |
243 |
} |
} |
244 |
|
|
245 |
|
|
|
#define MAXPWD 256 |
|
|
|
|
246 |
/* |
/* |
247 |
* Find out what the current directory is. If we already know the current |
* Find out what the current directory is. If we already know the current |
248 |
* directory, this routine returns immediately. |
* directory, this routine returns immediately. |
251 |
STATIC char * |
STATIC char * |
252 |
getpwd() |
getpwd() |
253 |
{ |
{ |
254 |
|
#ifdef __GLIBC__ |
255 |
|
char *dir = getcwd(0, 0); |
256 |
|
if (dir) |
257 |
|
return dir; |
258 |
|
#else |
259 |
char buf[PATH_MAX]; |
char buf[PATH_MAX]; |
260 |
char *dir = getcwd(buf, sizeof(buf)); |
if(getcwd(buf, sizeof(buf))) |
261 |
return dir ? savestr(dir) : nullstr; |
return savestr(buf); |
262 |
|
#endif |
263 |
|
sh_warnx("getcwd() failed: %s", strerror(errno)); |
264 |
|
return nullstr; |
265 |
} |
} |
266 |
|
|
267 |
int |
int |