home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* routine to normalise a path name, replacing '.' and '..' entries */
- /* J Segrave 00:39:59 Sat Nov 24 1990 originate */
- /* */
- /****************************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dir.h>
-
- /****************************************************************************/
- /* */
- /* routine to remove any self references from a path name - used since */
- /* Novel Netware does not have '.' or '..' entries, so stat() on these */
- /* fails. We will take the oldpath name and replace all the '.' and '..' */
- /* bits with the appropriate directory names */
- /* return TRUE (non-zero) on error */
- /* */
- /****************************************************************************/
-
- int norm_path (char *fullpath, char *oldpath)
- {
- char buf[256], /* working copy of source */
- cwd[256], /* current working dir of drive */
- *bp,
- *cp,
- *firstp, *lastp, /* ptrs to first and last '/' in target */
- *nextp; /* ptr to next '/' in source */
- int drive = 0; /* set for default drive */
-
- /* we want to work on a copy of the source */
-
- strcpy (buf, oldpath);
-
- /* normalise the name to lowercase with forward slashes */
-
- strlwr (buf);
- for (bp = buf; *bp != '\0'; ++bp)
- if (*bp == '\\')
- *bp = '/';
-
- /* if there's a drive letter, extract it */
-
- bp = buf;
- if ((strlen (buf) >= 2) && (buf[1] == ':'))
- {
- strncpy (fullpath, buf, 2);
- fullpath[2] = '\0';
- bp += 2; /* skip past drive letters */
- drive = (fullpath[0] - 'a' + 1);
- }
- else
- fullpath[0] = '\0';
-
- if (strlen (bp) == 0)
- strcat (bp, ".");
-
- /* get a copy of the current working directory for the drive */
-
- if (getcurdir (drive, cwd) != 0)
- return (1);
-
- /* normalise current working directory */
-
- strlwr (cwd);
- for (cp = cwd; *cp != '\0'; ++cp)
- if (*cp == '\\')
- *cp = '/';
-
- /* ensure source & current working dir end in a '/' */
- /* Turbo C's getcurdir never has leading or trailing */
- /* separators. */
-
- strcat (cwd, "/");
- if (*(bp + strlen (bp) - 1) != '/')
- strcat (buf, "/");
-
- /* if the input path name is not from the root, copy */
- /* the current working directory to the target */
-
- strcat (fullpath, "/");
- if (*bp != '/')
- {
- strcat (fullpath, cwd);
- }
- else
- ++bp; /* discard leading slash */
-
- /* now begin copying the source path to the target, */
- /* coping with '.' and '..' as they occur */
-
- firstp = strchr (fullpath, '/'); /* track first and */
- lastp = strrchr (fullpath, '/'); /* last '/'s */
-
- while (*bp != '\0')
- {
- if (strncmp (bp, "../", 3) == 0)
- {
- /* remove one element from the target (ignore */
- /* any attempt to go back past the root) */
-
- if (firstp != lastp)
- {
- *lastp = '\0';
- lastp = strrchr (fullpath, '/');
- lastp[1] = '\0';
- }
-
- bp += 3;
- continue;
- }
-
- /* discard any refernces to self */
-
- if (strncmp (bp, "./", 2) == 0)
- {
- bp += 2;
- continue;
- }
-
- /* copy one directory element to the target */
-
- nextp = strchr (bp, '/'); /* terminate source at '/' */
- *nextp = '\0';
- strcat (fullpath, bp); /* and add element to target */
- strcat (fullpath, "/"); /* with a separator... */
- lastp = strrchr (fullpath, '/');
- bp = nextp + 1; /* to next source element */
-
- }
-
- /* remove trailing '/', unless it's the root */
-
- if (lastp != firstp)
- *lastp = 0;
-
- return (0);
- }
-
-