Csh question: Doing cd .. from a symbolically linked directory

Bob Peirce #305 rbp at investor.pgh.pa.us
Sat Nov 3 11:25:45 AEST 1990


Yesterday I posted a note on the problem facing this user of csh who
has sym links but lacks eval and a setting mechanism for cwd.  After
thinking about it more today I decided the best solution was to build
the path with a small C program and to use a short alias to drive it.
The results follow.  The C program could be made smaller and, possibly
faster by not using stdio, but this works.  Setting the sticky bit
also might help.

============================== csh alias ==============================
#  Handle typical .. moves from a symbolically linked directory
set cwd = `/bin/pwd`
alias	pwd	'echo $cwd'
alias	rpwd	/bin/pwd
alias	rcd	chdir
alias	cd	'\\
	if (\!* == "") set tmp = $home;\\
	if (\!* != "") set tmp = `glob \!*`;\\
	set cwd = `/usr/local/lib/chd $cwd $tmp`;\\
	chdir $cwd'
============================== C program ==============================
/*  @(#) chd.c
	C program for cd
*/

#include <stdio.h>

main(argc,argv)
char **argv;
int argc;
{
	int	i, j;
	char	path[256];

	if (argc != 3) exit();		/*  silent failure  */

	/*  Top dir in Altos Worknet may be @node */
	if (argv[2][0] == '/' || argv[2][0] == '@')
		printf ("%s\n", argv[2]);
	else {
		strcpy(path, argv[1]);
		strcat(path, "/");
		strcat(path, argv[2]);
		i = j = 0;
		while (path[i] != '\0') {
			if ( j != i) path[j] = path[i];
			if (path[i] == '.' && path[i+1] == '.') {
				++i;
				if (j >= 2) {
					j -= 2;
					while (path[j--] != '/')
						;
				}
				else j = -1;
			}
			++i;
			++j;
		}
		if (j == 0)
			strcpy (path, "/");
		else
			path[j] = '\0';
		printf ("%s\n", path);
	}
}
============================== END ==============================
-- 
Bob Peirce, Pittsburgh, PA				  412-471-5320
...!uunet!pitt!investor!rbp			rbp at investor.pgh.pa.us



More information about the Comp.unix.shell mailing list