Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.unix.programmer Subject: Re: Checking change of directory Message-ID: <1156@mwtech.UUCP> Date: 29 May 91 10:39:42 GMT References: <1991May28.124051.23538@dircon.co.uk> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 52 In article <1991May28.124051.23538@dircon.co.uk> uaa1006@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@mwtech.UUCP, voice: 49-(0)6151-6 56 83