Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: zgetcwd patch?



> I never heard a response to the patch I sent to compat.c.
> Does it appear correct?
> 
> I'm also curious under what circumstances these two operations differ (aside
> from the obvious chdir side effect):
> 
> 1. opendir( ".." );
> 
> 2. chdir( ".." );
> 	opendir( "." );

Sorry for my silence about this.  Zsh-3.1.1 already had a fix for this
promlem, here is the patch for zsh-3.0.4.  It avoids the double scan you
noticed.

Zoltan


*** compat.c.3.0.4	Tue Dec 17 15:14:11 1996
--- compat.c	Mon Aug 25 00:58:18 1997
***************
*** 115,122 ****
      struct stat sbuf;
      struct dirent *de;
      DIR *dir;
!     ino_t ino, rootino = (ino_t) ~ 0;
!     dev_t dev, rootdev = (dev_t) ~ 0;
  
      holdintr();
      buf2[0] = '\0';
--- 115,122 ----
      struct stat sbuf;
      struct dirent *de;
      DIR *dir;
!     ino_t ino, pino, rootino = (ino_t) ~ 0;
!     dev_t dev, pdev, rootdev = (dev_t) ~ 0;
  
      holdintr();
      buf2[0] = '\0';
***************
*** 127,146 ****
  	rootdev = sbuf.st_dev;
      }
  
      for (;;) {
- 	if (stat(".", &sbuf) < 0) {
- 	    chdir(buf0);
- 	    noholdintr();
- 	    return ztrdup(".");
- 	}
- 	ino = sbuf.st_ino;
- 	dev = sbuf.st_dev;
  	if (stat("..", &sbuf) < 0) {
  	    chdir(buf0);
  	    noholdintr();
  	    return ztrdup(".");
  	}
! 	if ((sbuf.st_ino == ino && sbuf.st_dev == dev) ||
  	    (ino == rootino && dev == rootdev)) {
  	    chdir(buf0);
  	    noholdintr();
--- 127,153 ----
  	rootdev = sbuf.st_dev;
      }
  
+     if (stat(".", &sbuf) < 0) {
+ 	noholdintr();
+ 	return ztrdup(".");
+     }
+ 
+     pino = sbuf.st_ino;
+     pdev = sbuf.st_dev;
+ 
      for (;;) {
  	if (stat("..", &sbuf) < 0) {
  	    chdir(buf0);
  	    noholdintr();
  	    return ztrdup(".");
  	}
! 
! 	ino = pino;
! 	dev = pdev;
! 	pino = sbuf.st_ino;
! 	pdev = sbuf.st_dev;
! 
! 	if ((ino == pino && dev == pdev) ||
  	    (ino == rootino && dev == rootdev)) {
  	    chdir(buf0);
  	    noholdintr();
***************
*** 160,194 ****
  		(fn[1] == '\0' ||
  		 (fn[1] == '.' && fn[2] == '\0')))
  		continue;
! 	    if ((ino_t) de->d_ino == ino) {
  		lstat(fn, &sbuf);
! 		if (sbuf.st_dev == dev)
! 		    goto match;
  	    }
  	}
  	closedir(dir);
! 	dir = opendir(".");
! 	while ((de = readdir(dir))) {
! 	    char *fn = de->d_name;
! 	    /* Ignore `.' and `..'. */
! 	    if (fn[0] == '.' &&
! 		(fn[1] == '\0' ||
! 		 (fn[1] == '.' && fn[2] == '\0')))
! 		continue;
! 	    lstat(fn, &sbuf);
! 	    if (sbuf.st_dev == dev)
! 		goto match;
  	}
- 	noholdintr();
- 	closedir(dir);
- 	return ztrdup(".");
-       match:
- 	strcpy(buf3, de->d_name);
  	if (*buf2)
  	    strcat(buf3, "/");
  	strcat(buf3, buf2);
  	strcpy(buf2, buf3);
- 	closedir(dir);
      }
  }
  
--- 167,189 ----
  		(fn[1] == '\0' ||
  		 (fn[1] == '.' && fn[2] == '\0')))
  		continue;
! 	    if (dev != pdev || (ino_t) de->d_ino == ino) {
  		lstat(fn, &sbuf);
! 		if (sbuf.st_dev == dev && sbuf.st_ino == ino) {
! 		    strcpy(buf3, de->d_name);
! 		    break;
! 		}
  	    }
  	}
  	closedir(dir);
! 	if (!de) {
! 	    noholdintr();
! 	    return ztrdup(".");
  	}
  	if (*buf2)
  	    strcat(buf3, "/");
  	strcat(buf3, buf2);
  	strcpy(buf2, buf3);
      }
  }
  



Messages sorted by: Reverse Date, Date, Thread, Author