Checking change of directory

Martin Weitzel martin at mwtech.UUCP
Wed May 29 20:39:42 AEST 1991


In article <1991May28.124051.23538 at dircon.co.uk> uaa1006 at dircon.co.uk (Peter Miles) writes:
>
>Is there any function available which, when given a string such 
>as "../../etc" or whatever, can resolve this into the 
>absolute path which I can then compare with the HOME dir and 
>accept or reject?

If I don't miss anything important, you should be able to obtain the
desired information as follows:

	----------------------------------------------------------------------
	FILE *fp = popen("IFS='\t ';PATH='/bin:/usr/bin'; cd xxx && pwd", "r");
	/*                                                   ^^^
	 * Make the target directory appear here ------------^^^;
	 * check for embedded funny characters that may change the shell's
	 * parsing of this line. (To play safe, only allow alpha-numeric
	 * characters, underscore, and dot.) If the given directory doesn't
	 * exist, you may further want to suppress error messages from the
	 * shell by redirecting 2>/dev/null in the above command line.
	*/
	char buffer[200];
	/*          ^^^
	 * Set this ^^^ to whatever you think is appropriate.
	*/
	int err = 0;

	if (fp == (FILE *)0) err++;
	else {
		if (fgets(buffer, sizeof buffer, fp) == (char *)0) err++;
		else {
			int n = strlen(buffer);
			if (n == 0 || buffer[n-1] != '\n') err++;
			else buffer[n-1] = '\0';
		}
		if (pclose(fp) != 0) err++;
	}
	if (err) {
		/*
		 * Destination directory doesn't exist or user tries to trick
		 * you out with non-readable intermediate path components.
		*/
	}
	else {
		/*
		 * Absolute path to destination directory is now in buffer.
		*/
	}
	----------------------------------------------------------------------

I've three times checked the above and hope there are no silly typos.
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.unix.programmer mailing list