Subject: Re: kern/1781: 'magic' symbolic link expansion
To: Chris G Demetriou <Chris_G_Demetriou@BALVENIE.PDL.CS.CMU.EDU>
From: Greg Hudson <>
List: netbsd-bugs
Date: 11/24/1995 19:01:49
> by "follow symlinks" do you mean "take them apart, parse them, and
> use the components"?  it's not clear to me that portable code can
> safely do that, anyway...

No parsing required.  It's hard to argue that the following code is
"portable" since POSIX.1 doesn't yet specify symlinks (I don't have
access to the POSIX.1A drafts), but here's a bit of code that does
something vaguely useful and ought to work (modulo the filename and
linktarget buffers not necessarily being big enough):

	char filename[1024], linktarget[1024];
	struct stat statbuf;
	int fd;

	strcpy(filename, "/my/pathname");
	while ((count = readlink(filename, linktarget,
				 sizeof(linktarget) - 1)) >= 0) {
		linktarget[count] = 0;
		printf("%s -> %s\n", filename, linktarget);
		strcpy(filename, linktarget);
	if (stat(filename, &statbuf) >= 0)
		printf("%s is a file.\n", filename);
		printf("%s cannot be found.\n", filename);

Because you chose to implement your magic expansions at symlink
resolution time, this code won't work any more.  Why was it cleaner to
specify the meaning of these magic expansions at symlink resolution
time rather than at the namei level, as AFS did?