rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* Rc globbing bug, with fix
@ 1991-06-30 12:21 David Hogan
  0 siblings, 0 replies; only message in thread
From: David Hogan @ 1991-06-30 12:21 UTC (permalink / raw)
  To: The Rc Mailing List

I run rc under MIPS RISC/os, compiled using -systype bsd43.  I ran into
the following problem: if you type echo */* in a directory which contains
any regular files, rc dumps core.  After much painstaking debugging,
I discovered that rc was calling opendir() on every file in the directory,
and it was succeeding, even for regular files, as it will on many systems.
(It is neccessary for opendir() to succeed on regular files so that the -f
option to the bsd version of ls will work).

The problem, then, is in rc: it must not assume that opendir() will fail if
the pathname it is given is not that of a directory.  The fix is quite
simple: add a call to stat() inside dmatch, as the following patch
to glob.c does:

*** glob.c.old	Sun Jun 30 22:14:31 1991
--- glob.c	Sun Jun 30 22:14:41 1991
***************
*** 1,6 ****
--- 1,7 ----
  /* glob.c: rc's (ugly) globber. This code is not elegant, but it works */
  
  #include <sys/types.h>
+ #include <sys/stat.h>
  #include "rc.h"
  #include "glob.h"
  #include "glom.h"
***************
*** 78,88 ****
--- 79,94 ----
  	List *top, *r;
  	DIR *dirp;
  	struct dirent *dp;
+ 	struct stat stbuf;
  	/* prototypes for XXXdir functions. comment out if necessary */
  	extern DIR *opendir(const char *);
  	extern struct dirent *readdir(DIR *);
  	extern int closedir(DIR *);
  
+ 	/* First check that d is a directory -- on many systems, opendir
+ 	   will succeed even if it isn't -- to our great peril! */
+ 	if (stat(d, &stbuf) < 0 || (stbuf.st_mode & S_IFMT) != S_IFDIR)
+ 		return NULL;
  	if ((dirp = opendir(d)) == NULL)
  		return NULL;
  


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1991-06-30 14:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-06-30 12:21 Rc globbing bug, with fix David Hogan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).