5 /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars.
6 * Comments from the orignal:
7 * This is a faster version of getcwd. It's also more dangerous
8 * because you might chdir out of a directory that you can't chdir
13 /* XXX Should we just use getcwd(3) if available? */
15 int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
16 int i = 0, j = 0, k = 0, ndirs = 16, pathlen = 0, namelen;
21 Newz(0, names, ndirs, char*);
23 if (PerlLIO_lstat(".", &statbuf) < 0) {
27 orig_cdev = statbuf.st_dev;
28 orig_cino = statbuf.st_ino;
35 if (PerlDir_chdir("..") < 0) {
39 if (PerlLIO_stat(".", &statbuf) < 0) {
43 cdev = statbuf.st_dev;
44 cino = statbuf.st_ino;
45 if (odev == cdev && oino == cino)
48 if (!(dir = PerlDir_open("."))) {
53 while ((dp = PerlDir_read(dir)) != NULL) {
54 if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) {
58 if (strEQ(dp->d_name, "."))
60 if (strEQ(dp->d_name, ".."))
62 tdev = statbuf.st_dev;
63 tino = statbuf.st_ino;
64 if (tino == oino && tdev == odev)
75 Renew(names, ndirs, char*);
78 namelen = dp->d_namlen;
80 namelen = strlen(dp->d_name);
82 Newz(0, *(names + i), namelen + 1, char);
83 Copy(dp->d_name, *(names + i), namelen, char);
84 *(names[i] + namelen) = '\0';
85 pathlen += (namelen + 1);
88 if (PerlDir_close(dir) < 0) {
94 Newz(0, path, pathlen + 1, char);
95 for (j = i - 1; j >= 0; j--) {
97 Copy(names[j], path + k + 1, strlen(names[j]) + 1, char);
98 k = k + strlen(names[j]) + 1;
102 if (PerlDir_chdir(path) < 0) {
107 if (PerlLIO_stat(".", &statbuf) < 0) {
112 cdev = statbuf.st_dev;
113 cino = statbuf.st_ino;
114 if (cdev != orig_cdev || cino != orig_cino)
115 Perl_croak(aTHX_ "Unstable directory path, current directory changed unexpectedly");
122 MODULE = Cwd PACKAGE = Cwd
128 buf = _cwdxs_fastcwd();
130 PUSHs(sv_2mortal(newSVpv(buf, 0)));